| 1 | import * as React from "react"; |
| 2 | |
| 3 | import type { ToastActionElement, ToastProps } from "@/components/ui/toast"; |
| 4 | |
| 5 | const TOAST_LIMIT = 1; |
| 6 | const TOAST_REMOVE_DELAY = 1000000; |
| 7 | |
| 8 | type ToasterToast = ToastProps & { |
| 9 | id: string; |
| 10 | title?: React.ReactNode; |
| 11 | description?: React.ReactNode; |
| 12 | action?: ToastActionElement; |
| 13 | }; |
| 14 | |
| 15 | const actionTypes = { |
| 16 | ADD_TOAST: "ADD_TOAST", |
| 17 | UPDATE_TOAST: "UPDATE_TOAST", |
| 18 | DISMISS_TOAST: "DISMISS_TOAST", |
| 19 | REMOVE_TOAST: "REMOVE_TOAST", |
| 20 | } as const; |
| 21 | |
| 22 | let count = 0; |
| 23 | |
| 24 | function genId() { |
| 25 | count = (count + 1) % Number.MAX_SAFE_INTEGER; |
| 26 | return count.toString(); |
| 27 | } |
| 28 | |
| 29 | type ActionType = typeof actionTypes; |
| 30 | |
| 31 | type Action = |
| 32 | | { |
| 33 | type: ActionType["ADD_TOAST"]; |
| 34 | toast: ToasterToast; |
| 35 | } |
| 36 | | { |
| 37 | type: ActionType["UPDATE_TOAST"]; |
| 38 | toast: Partial<ToasterToast>; |
| 39 | } |
| 40 | | { |
| 41 | type: ActionType["DISMISS_TOAST"]; |
| 42 | toastId?: ToasterToast["id"]; |
| 43 | } |
| 44 | | { |
| 45 | type: ActionType["REMOVE_TOAST"]; |
| 46 | toastId?: ToasterToast["id"]; |
| 47 | }; |
| 48 | |
| 49 | interface State { |
| 50 | toasts: ToasterToast[]; |
| 51 | } |
| 52 | |
| 53 | const toastTimeouts = new Map<string, ReturnType<typeof setTimeout>>(); |
| 54 | |
| 55 | const addToRemoveQueue = (toastId: string) => { |
| 56 | if (toastTimeouts.has(toastId)) { |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | const timeout = setTimeout(() => { |
| 61 | toastTimeouts.delete(toastId); |
| 62 | dispatch({ |
| 63 | type: "REMOVE_TOAST", |
| 64 | toastId: toastId, |
| 65 | }); |
| 66 | }, TOAST_REMOVE_DELAY); |
| 67 | |
| 68 | toastTimeouts.set(toastId, timeout); |
| 69 | }; |
| 70 | |
| 71 | export const reducer = (state: State, action: Action): State => { |
| 72 | switch (action.type) { |
| 73 | case "ADD_TOAST": |
| 74 | return { |
| 75 | ...state, |
| 76 | toasts: [action.toast, ...state.toasts].slice(0, TOAST_LIMIT), |
| 77 | }; |
| 78 | |
| 79 | case "UPDATE_TOAST": |
| 80 | return { |
| 81 | ...state, |
| 82 | toasts: state.toasts.map((t) => (t.id === action.toast.id ? { ...t, ...action.toast } : t)), |
| 83 | }; |
| 84 | |
| 85 | case "DISMISS_TOAST": { |
| 86 | const { toastId } = action; |
| 87 | |
| 88 | // ! Side effects ! - This could be extracted into a dismissToast() action, |
| 89 | // but I'll keep it here for simplicity |
| 90 | if (toastId) { |
| 91 | addToRemoveQueue(toastId); |
| 92 | } else { |
| 93 | state.toasts.forEach((toast) => { |
| 94 | addToRemoveQueue(toast.id); |
| 95 | }); |
| 96 | } |
| 97 | |
| 98 | return { |
| 99 | ...state, |
| 100 | toasts: state.toasts.map((t) => |
| 101 | t.id === toastId || toastId === undefined |
| 102 | ? { |
| 103 | ...t, |
| 104 | open: false, |
| 105 | } |
| 106 | : t, |
| 107 | ), |
| 108 | }; |
| 109 | } |
| 110 | case "REMOVE_TOAST": |
| 111 | if (action.toastId === undefined) { |
| 112 | return { |
| 113 | ...state, |
| 114 | toasts: [], |
| 115 | }; |
| 116 | } |
| 117 | return { |
| 118 | ...state, |
| 119 | toasts: state.toasts.filter((t) => t.id !== action.toastId), |
| 120 | }; |
| 121 | } |
| 122 | }; |
| 123 | |
| 124 | const listeners: Array<(state: State) => void> = []; |
| 125 | |
| 126 | let memoryState: State = { toasts: [] }; |
| 127 | |
| 128 | function dispatch(action: Action) { |
| 129 | memoryState = reducer(memoryState, action); |
| 130 | listeners.forEach((listener) => { |
| 131 | listener(memoryState); |
| 132 | }); |
| 133 | } |
| 134 | |
| 135 | type Toast = Omit<ToasterToast, "id">; |
| 136 | |
| 137 | function toast({ ...props }: Toast) { |
| 138 | const id = genId(); |
| 139 | |
| 140 | const update = (props: ToasterToast) => |
| 141 | dispatch({ |
| 142 | type: "UPDATE_TOAST", |
| 143 | toast: { ...props, id }, |
| 144 | }); |
| 145 | const dismiss = () => dispatch({ type: "DISMISS_TOAST", toastId: id }); |
| 146 | |
| 147 | dispatch({ |
| 148 | type: "ADD_TOAST", |
| 149 | toast: { |
| 150 | ...props, |
| 151 | id, |
| 152 | open: true, |
| 153 | onOpenChange: (open) => { |
| 154 | if (!open) dismiss(); |
| 155 | }, |
| 156 | }, |
| 157 | }); |
| 158 | |
| 159 | return { |
| 160 | id: id, |
| 161 | dismiss, |
| 162 | update, |
| 163 | }; |
| 164 | } |
| 165 | |
| 166 | function useToast() { |
| 167 | const [state, setState] = React.useState<State>(memoryState); |
| 168 | |
| 169 | React.useEffect(() => { |
| 170 | listeners.push(setState); |
| 171 | return () => { |
| 172 | const index = listeners.indexOf(setState); |
| 173 | if (index > -1) { |
| 174 | listeners.splice(index, 1); |
| 175 | } |
| 176 | }; |
| 177 | }, [state]); |
| 178 | |
| 179 | return { |
| 180 | ...state, |
| 181 | toast, |
| 182 | dismiss: (toastId?: string) => dispatch({ type: "DISMISS_TOAST", toastId }), |
| 183 | }; |
| 184 | } |
| 185 | |
| 186 | export { useToast, toast }; |