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