Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix snackbar changing color when spamming #317

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 11 additions & 23 deletions canopeum_frontend/src/components/context/SnackbarContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@
severity?: OverridableStringUnion<AlertColor, AlertPropsColorOverrides>,
}

const DEFAULT_SNACKBAR_ALERT_OPTIONS: SnackbarAlertOptions = {
const DEFAULT_SNACKBAR_ALERT_OPTIONS = {
autohideDuration: 5000,
severity: 'success',
}
} satisfies SnackbarAlertOptions

export const SnackbarContext = createContext<ISnackbarContext>({

Check warning on line 25 in canopeum_frontend/src/components/context/SnackbarContext.tsx

View workflow job for this annotation

GitHub Actions / Lint-Autofixes

Fast refresh only works when a file only exports components. Move your React context(s) to a separate file
openAlertSnackbar: (_message: string, _options?: SnackbarAlertOptions) => {/* empty */},
})
SnackbarContext.displayName = 'SnackbarContext'

const SnackbarContextProvider: FunctionComponent<{ readonly children?: ReactNode }> = memo(
props => {
const [snackbarAlertOptions, setSnackbarAlertOptions] = useState<
SnackbarAlertOptions | undefined
>()
const [snackbarAlertOptions, setSnackbarAlertOptions] = useState<SnackbarAlertOptions>(
DEFAULT_SNACKBAR_ALERT_OPTIONS,
)

const [snackPack, setSnackPack] = useState<readonly SnackbarMessage[]>([])
const [open, setOpen] = useState(false)
Expand All @@ -50,36 +50,25 @@
}, [snackPack, messageInfo, open])

const handleClose = (_event: Event | SyntheticEvent, reason?: string) => {
if (reason === 'clickaway') {
return
}
if (reason === 'clickaway') return
setOpen(false)
}

const handleExited = () => {
setSnackbarAlertOptions(undefined)
setMessageInfo(undefined)
}

const openAlertSnackbar = useCallback((message: string, options?: SnackbarAlertOptions) => {
setSnackbarAlertOptions(options)
setSnackbarAlertOptions(options ?? DEFAULT_SNACKBAR_ALERT_OPTIONS)
setSnackPack(previous => [...previous, { message, key: Date.now() }])
}, [setSnackPack, setSnackbarAlertOptions])

const context = useMemo<ISnackbarContext>(() => (
{
openAlertSnackbar,
}
), [openAlertSnackbar])
const context = useMemo<ISnackbarContext>(() => ({ openAlertSnackbar }), [openAlertSnackbar])

return (
<SnackbarContext.Provider value={context}>
{props.children}

<Snackbar
TransitionProps={{ onExited: handleExited }}
TransitionProps={{ onExited: () => setMessageInfo(undefined) }}
anchorOrigin={{ vertical: 'bottom', horizontal: 'right' }}
autoHideDuration={snackbarAlertOptions?.autohideDuration
autoHideDuration={snackbarAlertOptions.autohideDuration
?? DEFAULT_SNACKBAR_ALERT_OPTIONS.autohideDuration}
key={messageInfo
? messageInfo.key
Expand All @@ -89,7 +78,7 @@
>
<Alert
onClose={handleClose}
severity={snackbarAlertOptions?.severity ?? DEFAULT_SNACKBAR_ALERT_OPTIONS.severity}
severity={snackbarAlertOptions.severity ?? DEFAULT_SNACKBAR_ALERT_OPTIONS.severity}
sx={{ width: '100%', boxShadow: 3 }}
variant='filled'
>
Expand All @@ -100,6 +89,5 @@
)
},
)

SnackbarContextProvider.displayName = 'SnackbarContextProvider'
export default SnackbarContextProvider
Loading