|
| 1 | +/* |
| 2 | + * Copyright © 2025, RTE (http://www.rte-france.com) |
| 3 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 | + */ |
| 7 | +import type { UUID } from 'crypto'; |
| 8 | +import { type PropsWithChildren, type ReactNode, useCallback, useEffect, useState } from 'react'; |
| 9 | +import { |
| 10 | + Alert, |
| 11 | + type AlertColor, |
| 12 | + type AlertProps, |
| 13 | + AlertTitle, |
| 14 | + Collapse, |
| 15 | + type SxProps, |
| 16 | + type Theme, |
| 17 | + Tooltip, |
| 18 | + useTheme, |
| 19 | +} from '@mui/material'; |
| 20 | +import { Campaign as CampaignIcon } from '@mui/icons-material'; |
| 21 | +import type { User } from 'oidc-client'; |
| 22 | +import { AnnouncementSeverity } from '../../utils/types'; |
| 23 | + |
| 24 | +// Pick the same color than Snackbar |
| 25 | +const snackbarInfo = '#2196f3'; |
| 26 | +const snackbarWarning = '#ff9800'; |
| 27 | +// const snackbarError = '#d32f2f'; |
| 28 | + |
| 29 | +const styles = { |
| 30 | + alert: (theme) => ({ |
| 31 | + '&.MuiAlert-colorWarning': { |
| 32 | + color: theme.palette.getContrastText(snackbarWarning), |
| 33 | + backgroundColor: snackbarWarning, |
| 34 | + }, |
| 35 | + '&.MuiAlert-colorInfo': { |
| 36 | + color: theme.palette.getContrastText(snackbarInfo), |
| 37 | + backgroundColor: snackbarInfo, |
| 38 | + }, |
| 39 | + }), |
| 40 | +} as const satisfies Record<string, SxProps<Theme>>; |
| 41 | + |
| 42 | +export type AnnouncementBannerProps = PropsWithChildren<{ |
| 43 | + // message only visible if user logged |
| 44 | + user?: User | {}; |
| 45 | + /** only field used to detect if msg change */ |
| 46 | + id?: UUID; |
| 47 | + duration?: number; |
| 48 | + severity?: AnnouncementSeverity; |
| 49 | + title?: ReactNode; |
| 50 | + sx?: AlertProps['sx']; |
| 51 | +}>; |
| 52 | + |
| 53 | +const iconMapping: AlertProps['iconMapping'] = { |
| 54 | + info: <CampaignIcon />, |
| 55 | +}; |
| 56 | + |
| 57 | +function convertSeverity(severity: AnnouncementSeverity): AlertColor | undefined { |
| 58 | + switch (severity) { |
| 59 | + case AnnouncementSeverity.INFO: |
| 60 | + return 'info'; |
| 61 | + case AnnouncementSeverity.WARN: |
| 62 | + return 'warning'; |
| 63 | + default: |
| 64 | + return undefined; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +export function AnnouncementBanner({ |
| 69 | + user, |
| 70 | + id, |
| 71 | + severity = AnnouncementSeverity.INFO, |
| 72 | + title, |
| 73 | + children, |
| 74 | + duration, |
| 75 | + sx, |
| 76 | +}: Readonly<AnnouncementBannerProps>) { |
| 77 | + const theme = useTheme(); |
| 78 | + const [visible, setVisible] = useState(false); |
| 79 | + |
| 80 | + useEffect( |
| 81 | + () => { |
| 82 | + // If the message to show changed |
| 83 | + if (id) { |
| 84 | + setVisible(true); |
| 85 | + if (duration) { |
| 86 | + // the previous timer is normally already cleared |
| 87 | + const bannerTimer = setTimeout(() => { |
| 88 | + setVisible(false); |
| 89 | + }, duration); |
| 90 | + return () => { |
| 91 | + clearTimeout(bannerTimer); // clear previous timer |
| 92 | + }; |
| 93 | + } |
| 94 | + } else { |
| 95 | + setVisible(false); |
| 96 | + } |
| 97 | + return () => {}; |
| 98 | + }, |
| 99 | + // eslint-disable-next-line react-hooks/exhaustive-deps -- we check msg id in cas of an announcement |
| 100 | + [id] |
| 101 | + ); |
| 102 | + |
| 103 | + const handleClose = useCallback(() => setVisible(false), []); |
| 104 | + |
| 105 | + return ( |
| 106 | + <Collapse in={user !== undefined && visible} unmountOnExit sx={sx} style={{ margin: theme.spacing(1) }}> |
| 107 | + <Alert |
| 108 | + variant="filled" |
| 109 | + elevation={0} |
| 110 | + severity={convertSeverity(severity)} |
| 111 | + onClose={handleClose} |
| 112 | + iconMapping={iconMapping} |
| 113 | + hidden={!visible} |
| 114 | + className={title ? undefined : 'no-title'} |
| 115 | + sx={styles.alert} |
| 116 | + > |
| 117 | + {title && <AlertTitle>{title}</AlertTitle>} |
| 118 | + <Tooltip title={children} placement="bottom"> |
| 119 | + <span>{children}</span> |
| 120 | + </Tooltip> |
| 121 | + </Alert> |
| 122 | + </Collapse> |
| 123 | + ); |
| 124 | +} |
0 commit comments