Skip to content

Commit bb3725f

Browse files
toast reversion
1 parent 1a9299d commit bb3725f

2 files changed

Lines changed: 27 additions & 21 deletions

File tree

src/frontend/src/components/Toast/ToastProvider.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
// Inspired by https://github.com/ryohey/use-toast-mui
77

8-
import { createContext, FC, ReactNode, useCallback, useMemo, useState } from 'react';
8+
import { createContext, FC, ReactNode, useState } from 'react';
99
import Toast from './Toast';
1010
import { AlertColor } from '@mui/material';
1111

@@ -22,11 +22,15 @@ export const ToastContext = createContext<{
2222

2323
export const ToastProvider: FC<{ children: ReactNode }> = ({ children, ...props }) => {
2424
const [toasts, setToasts] = useState<ToastInputs[]>([]);
25-
const removeToast = useCallback((key: number) => setToasts((toastList) => toastList.filter((t) => t.key !== key)), []);
26-
const addToast = useCallback((toast: ToastInputs) => setToasts((toastList) => [...toastList, toast]), []);
27-
const contextValue = useMemo(() => ({ addToast }), [addToast]);
25+
const removeToast = (key: number) => setToasts((toastList) => toastList.filter((t) => t.key !== key));
2826
return (
29-
<ToastContext.Provider value={contextValue}>
27+
<ToastContext.Provider
28+
value={{
29+
addToast(toast) {
30+
setToasts((toastList) => [...toastList, toast]);
31+
}
32+
}}
33+
>
3034
{children}
3135
{toasts.map((t) => (
3236
<Toast toast={t} key={t.key} onExited={() => removeToast(t.key)} {...props} />

src/frontend/src/hooks/toasts.hooks.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
// Inspired by https://github.com/ryohey/use-toast-mui
77

8-
import { useCallback, useContext, useMemo } from 'react';
8+
import { useContext } from 'react';
99
import { AlertColor } from '@mui/material';
1010
import { ToastContext } from '../components/Toast/ToastProvider';
1111

@@ -19,20 +19,22 @@ import { ToastContext } from '../components/Toast/ToastProvider';
1919
*/
2020
export const useToast = () => {
2121
const { addToast } = useContext(ToastContext);
22-
const fire = useCallback(
23-
(message: string, options: { type: AlertColor; autoHideDuration?: number }) => {
24-
addToast({ message, ...options, key: new Date().getTime() });
22+
const fire = (message: string, options: { type: AlertColor; autoHideDuration?: number }) => {
23+
addToast({ message, ...options, key: new Date().getTime() });
24+
};
25+
return {
26+
fire,
27+
info(message: string, autoHideDuration?: number) {
28+
fire(message, { type: 'info', autoHideDuration });
2529
},
26-
[addToast]
27-
);
28-
return useMemo(
29-
() => ({
30-
fire,
31-
info: (message: string, autoHideDuration?: number) => fire(message, { type: 'info', autoHideDuration }),
32-
success: (message: string, autoHideDuration?: number) => fire(message, { type: 'success', autoHideDuration }),
33-
warning: (message: string, autoHideDuration?: number) => fire(message, { type: 'warning', autoHideDuration }),
34-
error: (message: string, autoHideDuration?: number) => fire(message, { type: 'error', autoHideDuration })
35-
}),
36-
[fire]
37-
);
30+
success(message: string, autoHideDuration?: number) {
31+
fire(message, { type: 'success', autoHideDuration });
32+
},
33+
warning(message: string, autoHideDuration?: number) {
34+
fire(message, { type: 'warning', autoHideDuration });
35+
},
36+
error(message: string, autoHideDuration?: number) {
37+
fire(message, { type: 'error', autoHideDuration });
38+
}
39+
};
3840
};

0 commit comments

Comments
 (0)