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