| 55 | >; |
| 56 | |
| 57 | export const useToastNotification = (): UseToastNotification => { |
| 58 | const client = useQueryClient(); |
| 59 | const { data: toast } = useQuery<ToastNotification | null>({ |
| 60 | queryKey: TOAST_NOTIF_KEY, |
| 61 | queryFn: () => |
| 62 | client.getQueryData<ToastNotification | null>(TOAST_NOTIF_KEY) ?? null, |
| 63 | initialData: () => |
| 64 | client.getQueryData<ToastNotification | null>(TOAST_NOTIF_KEY) ?? null, |
| 65 | }); |
| 66 | const setToastNotification = (data: ToastNotification) => |
| 67 | client.setQueryData(TOAST_NOTIF_KEY, data); |
| 68 | |
| 69 | const displayToast = ( |
| 70 | message: ReactNode, |
| 71 | { timer = 5000, ...props }: NotifyOptionalProps = {}, |
| 72 | ) => setToastNotification({ message, timer, ...props }); |
| 73 | |
| 74 | return useMemo( |
| 75 | () => ({ |
| 76 | displayToast, |
| 77 | subject: toast?.subject, |
| 78 | dismissToast: () => { |
| 79 | if (!toast) { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | setToastNotification({ ...toast, timer: 0 }); |
| 84 | }, |
| 85 | }), |
| 86 | // @NOTE see https://dailydotdev.atlassian.net/l/cp/dK9h1zoM |
| 87 | // eslint-disable-next-line react-hooks/exhaustive-deps |
| 88 | [toast], |
| 89 | ); |
| 90 | }; |