|
| 1 | +import React, { PropsWithChildren } from 'react'; |
| 2 | +import { |
| 3 | + BaseButton, |
| 4 | + Box, |
| 5 | + RouteBuilder, |
| 6 | + TemperatureBreachSortFieldInput, |
| 7 | + Typography, |
| 8 | + useMatch, |
| 9 | + useNavigate, |
| 10 | + useUrlQuery, |
| 11 | +} from '@openmsupply-client/common'; |
| 12 | +import { useFormatDateTime, useTranslation } from '@common/intl'; |
| 13 | +import { CircleAlertIcon } from '@common/icons'; |
| 14 | +import { alpha, useTheme } from '@common/styles'; |
| 15 | +import { AppRoute } from '@openmsupply-client/config'; |
| 16 | +import { |
| 17 | + TemperatureBreachFragment, |
| 18 | + useTemperatureBreach, |
| 19 | +} from './Monitoring/api/TemperatureBreach'; |
| 20 | + |
| 21 | +const Text: React.FC<PropsWithChildren> = ({ children }) => ( |
| 22 | + <Typography |
| 23 | + component="div" |
| 24 | + sx={{ |
| 25 | + fontSize: '14px', |
| 26 | + display: 'flex', |
| 27 | + alignContent: 'center', |
| 28 | + flexWrap: 'wrap', |
| 29 | + }} |
| 30 | + > |
| 31 | + {children} |
| 32 | + </Typography> |
| 33 | +); |
| 34 | + |
| 35 | +const Separator = () => ( |
| 36 | + <Text> |
| 37 | + <Typography paddingX={0.5}>|</Typography> |
| 38 | + </Text> |
| 39 | +); |
| 40 | + |
| 41 | +const DetailButton = ({ breach }: { breach: TemperatureBreachFragment }) => { |
| 42 | + const t = useTranslation('coldchain'); |
| 43 | + const navigate = useNavigate(); |
| 44 | + const { urlQuery } = useUrlQuery(); |
| 45 | + const currentTab = (urlQuery['tab'] as string) ?? ''; |
| 46 | + const isColdchain = useMatch( |
| 47 | + RouteBuilder.create(AppRoute.Coldchain).addWildCard().build() |
| 48 | + ); |
| 49 | + |
| 50 | + if (isColdchain && currentTab === t('label.breaches')) return null; |
| 51 | + |
| 52 | + return ( |
| 53 | + <BaseButton |
| 54 | + variant="contained" |
| 55 | + style={{ height: 32 }} |
| 56 | + onClick={() => |
| 57 | + navigate( |
| 58 | + RouteBuilder.create(AppRoute.Coldchain) |
| 59 | + .addPart(AppRoute.Monitoring) |
| 60 | + .addQuery({ tab: t('label.breaches') }) |
| 61 | + .addQuery({ |
| 62 | + sort: TemperatureBreachSortFieldInput.StartDatetime, |
| 63 | + }) |
| 64 | + .addQuery({ acknowledged: false }) |
| 65 | + .addQuery({ 'sensor.name': breach.sensor?.name ?? '' }) |
| 66 | + .build() |
| 67 | + ) |
| 68 | + } |
| 69 | + > |
| 70 | + {t('button.view-details')} |
| 71 | + </BaseButton> |
| 72 | + ); |
| 73 | +}; |
| 74 | +const Location = ({ breach }: { breach: TemperatureBreachFragment }) => { |
| 75 | + const t = useTranslation('coldchain'); |
| 76 | + |
| 77 | + if (!breach?.location?.name) return null; |
| 78 | + return ( |
| 79 | + <> |
| 80 | + <Separator /> |
| 81 | + {!!breach?.location?.name && ( |
| 82 | + <Text> |
| 83 | + {t('message.location')} |
| 84 | + <b style={{ paddingLeft: 4 }}>{breach.location.name}</b> |
| 85 | + </Text> |
| 86 | + )} |
| 87 | + </> |
| 88 | + ); |
| 89 | +}; |
| 90 | + |
| 91 | +export const ColdchainNotification = () => { |
| 92 | + const theme = useTheme(); |
| 93 | + const t = useTranslation('coldchain'); |
| 94 | + const { data: breaches } = useTemperatureBreach.document.notifications({ |
| 95 | + first: 1, |
| 96 | + offset: 0, |
| 97 | + sortBy: { key: 'startDatetime', direction: 'desc', isDesc: true }, |
| 98 | + filterBy: { acknowledged: false }, |
| 99 | + }); |
| 100 | + const { localisedDistanceToNow } = useFormatDateTime(); |
| 101 | + const breach = breaches?.nodes?.[0]; |
| 102 | + |
| 103 | + if (!breach) return null; |
| 104 | + |
| 105 | + return ( |
| 106 | + <Box |
| 107 | + sx={{ |
| 108 | + borderBottom: '1px solid', |
| 109 | + borderBottomColor: 'primary.main', |
| 110 | + backgroundColor: alpha(theme.palette.primary.main, 0.075), |
| 111 | + flex: '0 0 54px', |
| 112 | + display: 'flex', |
| 113 | + paddingLeft: 2, |
| 114 | + alignContent: 'center', |
| 115 | + flexWrap: 'wrap', |
| 116 | + }} |
| 117 | + > |
| 118 | + <Box |
| 119 | + sx={{ |
| 120 | + display: 'flex', |
| 121 | + alignContent: 'center', |
| 122 | + flexWrap: 'wrap', |
| 123 | + marginRight: 1, |
| 124 | + }} |
| 125 | + > |
| 126 | + <CircleAlertIcon |
| 127 | + fill={theme.palette.error.main} |
| 128 | + sx={{ |
| 129 | + color: 'background.white', |
| 130 | + }} |
| 131 | + width={27} |
| 132 | + height={27} |
| 133 | + /> |
| 134 | + </Box> |
| 135 | + <Text> |
| 136 | + <b> |
| 137 | + {t('message.notification-breach-detected', { |
| 138 | + time: localisedDistanceToNow(breach.startDatetime), |
| 139 | + })} |
| 140 | + </b> |
| 141 | + </Text> |
| 142 | + <Separator /> |
| 143 | + <Text> |
| 144 | + {t('message.last-temperature', { |
| 145 | + temperature: breach.maxOrMinTemperature, |
| 146 | + })} |
| 147 | + </Text> |
| 148 | + <Separator /> |
| 149 | + <Text> |
| 150 | + {t('message.device')} |
| 151 | + <b style={{ paddingLeft: 4 }}>{breach.sensor?.name}</b> |
| 152 | + </Text> |
| 153 | + <Location breach={breach} /> |
| 154 | + <Box |
| 155 | + sx={{ |
| 156 | + justifyContent: 'flex-end', |
| 157 | + display: 'flex', |
| 158 | + flex: 1, |
| 159 | + marginRight: 2, |
| 160 | + height: '32px', |
| 161 | + }} |
| 162 | + > |
| 163 | + <DetailButton breach={breach} /> |
| 164 | + </Box> |
| 165 | + </Box> |
| 166 | + ); |
| 167 | +}; |
0 commit comments