Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 248x 248x 93x 93x 93x 93x 1x 1x 15x 15x 15x 15x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 1x 1x 14x 14x 14x 14x 14x 14x 14x 1x 1x 13x 13x 13x 13x 13x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 1x 1x 1x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 9x 9x 9x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x | import { NfpaDiamond } from '@/components/NfpaDiamond';
import { useChemicalInfo, type ChemicalInfo } from '@/hooks/useChemicalInfo';
import { i18n } from '@/helpers/i18n';
import CloseIcon from '@mui/icons-material/Close';
import OpenInNewIcon from '@mui/icons-material/OpenInNew';
import Box from '@mui/material/Box';
import Button from '@mui/material/Button';
import CircularProgress from '@mui/material/CircularProgress';
import Dialog from '@mui/material/Dialog';
import DialogActions from '@mui/material/DialogActions';
import DialogContent from '@mui/material/DialogContent';
import DialogTitle from '@mui/material/DialogTitle';
import IconButton from '@mui/material/IconButton';
import Stack from '@mui/material/Stack';
import Tooltip from '@mui/material/Tooltip';
import Typography from '@mui/material/Typography';
import { useEffect, useState, type ReactNode } from 'react';
/** Props for {@link ChemicalInfoDialog}. */
interface ChemicalInfoDialogProps {
/** The reagent name or CAS to show, or undefined when closed. */
term?: string;
/** Called when the dialog is dismissed. */
onClose: () => void;
}
/**
* A labelled value row; renders nothing for empty values.
* @param props - The row label and value.
* @returns The row, or null.
* @source
*/
function Row({ label, value }: { label: string; value?: ReactNode }) {
if (value === undefined || value === null || value === '') return null;
return (
<Typography variant="body2" sx={{ wordBreak: 'break-word' }}>
<strong>{label}:</strong> {value}
</Typography>
);
}
/**
* Structure image that walks its candidate URLs, moving on when one fails to load.
* @param props - Candidate URLs and alt text.
* @returns The image, or null when none loads.
* @source
*/
function StructureImage({ urls, alt }: { urls: string[]; alt: string }) {
const [index, setIndex] = useState(0);
useEffect(() => setIndex(0), [urls]);
if (index >= urls.length) return null;
return (
<Box
component="img"
src={urls[index]}
alt={alt}
onError={() => setIndex((current) => current + 1)}
sx={{ maxWidth: 160, maxHeight: 160, bgcolor: '#fff', borderRadius: 1, p: 0.5 }}
/>
);
}
/**
* A site favicon, falling back to a generic "open link" glyph if it fails to load.
* @param props - The icon URL.
* @returns The icon.
* @source
*/
function LinkIcon({ src }: { src: string }) {
const [failed, setFailed] = useState(false);
if (failed) return <OpenInNewIcon fontSize="small" />;
return (
<Box
component="img"
src={src}
alt=""
onError={() => setFailed(true)}
sx={{ width: 20, height: 20 }}
/>
);
}
/**
* Body of the dialog once data has loaded.
* @param props - The merged chemical info.
* @returns The sections.
* @source
*/
function ChemicalInfoBody({ info }: { info: ChemicalInfo }) {
const { osha, properties, ghs } = info;
const hasNfpa = [osha?.nfpaHealth, osha?.nfpaFire, osha?.nfpaReactivity].some((v) => v != null);
const formula = properties?.molecularFormula ?? osha?.formula;
const mass = properties?.molecularWeight ?? osha?.molecularWeight;
const synonyms = (info.synonyms.length > 0 ? info.synonyms : (osha?.synonyms ?? [])).slice(0, 12);
if (osha === undefined && info.cid === undefined) {
return <Typography>{i18n('chem_info_not_found')}</Typography>;
}
return (
<Stack spacing={2}>
<Stack direction="row" spacing={2} alignItems="flex-start">
<StructureImage urls={info.imageUrls} alt={info.term} />
<Stack spacing={0.5}>
<Row label={i18n('chem_info_iupac')} value={properties?.iupacName} />
<Row label={i18n('chem_info_cas')} value={info.cas} />
<Row label={i18n('chem_info_formula')} value={formula} />
<Row label={i18n('chem_info_mass')} value={mass ? `${mass} g/mol` : undefined} />
<Row label={i18n('chem_info_smiles')} value={properties?.smiles} />
<Row label={i18n('chem_info_inchikey')} value={properties?.inchiKey} />
<Row label={i18n('chem_info_synonyms')} value={synonyms.join('; ')} />
</Stack>
</Stack>
{info.description?.description && (
<Typography variant="body2">{info.description.description}</Typography>
)}
<Box>
<Typography variant="subtitle2">{i18n('chem_info_properties')}</Typography>
<Row label={i18n('chem_info_appearance')} value={osha?.physicalDescription} />
<Row label={i18n('chem_info_boiling')} value={osha?.boilingPoint} />
<Row label={i18n('chem_info_melting')} value={osha?.freezingPoint} />
<Row label={i18n('chem_info_flash')} value={osha?.flashPoint} />
<Row label={i18n('chem_info_vapor')} value={osha?.vaporPressure} />
<Row label={i18n('chem_info_density')} value={osha?.specificGravity} />
<Row label={i18n('chem_info_solubility')} value={info.solubility?.join('; ')} />
<Row label="XLogP" value={properties?.xLogP} />
<Row
label="TPSA"
value={properties?.tpsa === undefined ? undefined : `${properties.tpsa} Ų`}
/>
</Box>
{(hasNfpa || ghs !== undefined) && (
<Box>
<Typography variant="subtitle2">{i18n('chem_info_hazards')}</Typography>
<Stack direction="row" spacing={2} alignItems="center" useFlexGap flexWrap="wrap">
{hasNfpa && (
<NfpaDiamond
health={osha?.nfpaHealth}
fire={osha?.nfpaFire}
reactivity={osha?.nfpaReactivity}
special={osha?.nfpaSpecial}
/>
)}
{ghs?.pictograms.map((pictogram) => (
<Box
key={pictogram.code}
component="img"
src={pictogram.url}
alt={pictogram.label}
title={pictogram.label}
sx={{ width: 64, height: 64 }}
/>
))}
</Stack>
<Row label={i18n('chem_info_signal')} value={ghs?.signal} />
{ghs?.hazardStatements.map((statement) => (
<Typography key={statement} variant="body2">
{statement}
</Typography>
))}
</Box>
)}
{osha !== undefined && (
<Box>
<Typography variant="subtitle2">{i18n('chem_info_exposure')}</Typography>
<Row label="OSHA PEL" value={osha.pelTwa} />
<Row label="NIOSH REL" value={osha.relTwa} />
<Row label="ACGIH TLV" value={osha.tlvTwa} />
<Row label="IDLH" value={osha.idlh} />
<Row label={i18n('chem_info_carcinogen')} value={osha.carcinogen} />
</Box>
)}
<Typography variant="caption" color="text.secondary">
{i18n('chem_info_sources')}
</Typography>
</Stack>
);
}
/**
* Dialog showing merged OSHA OBIS + PubChem details for a searched chemical: identifiers,
* structure, properties, NFPA diamond, GHS pictograms and exposure limits.
* @component
* @category Components
* @group Chemical Info
* @param props - See {@link ChemicalInfoDialogProps}.
* @returns The dialog.
* @example
* ```tsx
* <ChemicalInfoDialog term="acetone" onClose={() => setTerm(undefined)} />
* ```
* @source
*/
export function ChemicalInfoDialog({ term, onClose }: ChemicalInfoDialogProps) {
const { info, loading } = useChemicalInfo(term);
const links = [
{
href: info?.pubchemUrl,
label: i18n('chem_info_pubchem_link'),
icon: 'https://pubchem.ncbi.nlm.nih.gov/favicon.ico',
},
{
href: info?.wikipediaUrl,
label: i18n('chem_info_wikipedia_link'),
icon: 'https://en.wikipedia.org/static/favicon/wikipedia.ico',
},
{
href: info?.nistUrl,
label: i18n('chem_info_nist_link'),
icon: 'https://webbook.nist.gov/favicon.ico',
},
{
href: info?.osha?.cameoUrl,
label: i18n('chem_info_cameo_link'),
icon: 'https://cameochemicals.noaa.gov/favicon.ico',
},
{
href: info?.osha?.nioshPocketGuideUrl,
label: i18n('chem_info_niosh_link'),
icon: 'https://www.cdc.gov/favicon.ico',
},
].flatMap(({ href, label, icon }) => (href === undefined ? [] : [{ href, label, icon }]));
return (
<Dialog
open={term !== undefined}
onClose={onClose}
maxWidth="sm"
fullWidth
aria-labelledby="chem-info-title"
>
<DialogTitle id="chem-info-title" sx={{ pr: 6 }}>
{info?.properties?.title ?? info?.osha?.name ?? term}
<IconButton
aria-label={i18n('chem_info_close')}
onClick={onClose}
sx={{ position: 'absolute', right: 8, top: 8 }}
>
<CloseIcon />
</IconButton>
</DialogTitle>
<DialogContent dividers>
{loading || info === undefined ? (
<Stack alignItems="center" py={4} spacing={1}>
<CircularProgress size={28} />
<Typography variant="body2">{i18n('chem_info_loading')}</Typography>
</Stack>
) : (
<ChemicalInfoBody info={info} />
)}
</DialogContent>
<DialogActions>
{links.length > 0 && (
<Stack direction="row" spacing={0.5} sx={{ mr: 'auto', ml: 1 }}>
{links.map((link) => (
<Tooltip key={link.href} title={link.label}>
<IconButton
component="a"
href={link.href}
target="_blank"
rel="noopener noreferrer"
aria-label={link.label}
size="small"
>
<LinkIcon src={link.icon} />
</IconButton>
</Tooltip>
))}
</Stack>
)}
<Button onClick={onClose}>{i18n('chem_info_close')}</Button>
</DialogActions>
</Dialog>
);
}
|