2024-09-07 09:12:48 +02:00
|
|
|
"use client";
|
2024-07-03 19:29:49 +02:00
|
|
|
|
|
|
|
// Inspired by react-hot-toast library
|
2024-09-07 09:12:48 +02:00
|
|
|
import * as React from "react";
|
2024-07-03 19:29:49 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
import type { ToastActionElement, ToastProps } from "@/components/ui/toast";
|
2024-07-03 19:29:49 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
const TOAST_LIMIT = 1;
|
|
|
|
const TOAST_REMOVE_DELAY = 1000000;
|
2024-07-03 19:29:49 +02:00
|
|
|
|
|
|
|
type ToasterToast = ToastProps & {
|
2024-09-07 09:12:48 +02:00
|
|
|
id: string;
|
|
|
|
title?: React.ReactNode;
|
|
|
|
description?: React.ReactNode;
|
|
|
|
action?: ToastActionElement;
|
|
|
|
};
|
2024-07-03 19:29:49 +02:00
|
|
|
|
|
|
|
const actionTypes = {
|
|
|
|
ADD_TOAST: "ADD_TOAST",
|
|
|
|
UPDATE_TOAST: "UPDATE_TOAST",
|
|
|
|
DISMISS_TOAST: "DISMISS_TOAST",
|
2024-09-07 09:12:48 +02:00
|
|
|
REMOVE_TOAST: "REMOVE_TOAST"
|
|
|
|
} as const;
|
2024-07-03 19:29:49 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
let count = 0;
|
2024-07-03 19:29:49 +02:00
|
|
|
|
|
|
|
function genId() {
|
2024-09-07 09:12:48 +02:00
|
|
|
count = (count + 1) % Number.MAX_SAFE_INTEGER;
|
|
|
|
return count.toString();
|
2024-07-03 19:29:49 +02:00
|
|
|
}
|
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
type ActionType = typeof actionTypes;
|
2024-07-03 19:29:49 +02:00
|
|
|
|
|
|
|
type Action =
|
|
|
|
| {
|
2024-09-07 09:12:48 +02:00
|
|
|
type: ActionType["ADD_TOAST"];
|
|
|
|
toast: ToasterToast;
|
2024-07-03 19:29:49 +02:00
|
|
|
}
|
|
|
|
| {
|
2024-09-07 09:12:48 +02:00
|
|
|
type: ActionType["UPDATE_TOAST"];
|
|
|
|
toast: Partial<ToasterToast>;
|
2024-07-03 19:29:49 +02:00
|
|
|
}
|
|
|
|
| {
|
2024-09-07 09:12:48 +02:00
|
|
|
type: ActionType["DISMISS_TOAST"];
|
|
|
|
toastId?: ToasterToast["id"];
|
2024-07-03 19:29:49 +02:00
|
|
|
}
|
|
|
|
| {
|
2024-09-07 09:12:48 +02:00
|
|
|
type: ActionType["REMOVE_TOAST"];
|
|
|
|
toastId?: ToasterToast["id"];
|
|
|
|
};
|
2024-07-03 19:29:49 +02:00
|
|
|
|
|
|
|
interface State {
|
2024-09-07 09:12:48 +02:00
|
|
|
toasts: ToasterToast[];
|
2024-07-03 19:29:49 +02:00
|
|
|
}
|
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
const toastTimeouts = new Map<string, ReturnType<typeof setTimeout>>();
|
2024-07-03 19:29:49 +02:00
|
|
|
|
|
|
|
const addToRemoveQueue = (toastId: string) => {
|
|
|
|
if (toastTimeouts.has(toastId)) {
|
2024-09-07 09:12:48 +02:00
|
|
|
return;
|
2024-07-03 19:29:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const timeout = setTimeout(() => {
|
2024-09-07 09:12:48 +02:00
|
|
|
toastTimeouts.delete(toastId);
|
2024-07-03 19:29:49 +02:00
|
|
|
dispatch({
|
|
|
|
type: "REMOVE_TOAST",
|
2024-09-07 09:12:48 +02:00
|
|
|
toastId: toastId
|
|
|
|
});
|
|
|
|
}, TOAST_REMOVE_DELAY);
|
2024-07-03 19:29:49 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
toastTimeouts.set(toastId, timeout);
|
|
|
|
};
|
2024-07-03 19:29:49 +02:00
|
|
|
|
|
|
|
export const reducer = (state: State, action: Action): State => {
|
|
|
|
switch (action.type) {
|
|
|
|
case "ADD_TOAST":
|
|
|
|
return {
|
|
|
|
...state,
|
2024-09-07 09:12:48 +02:00
|
|
|
toasts: [action.toast, ...state.toasts].slice(0, TOAST_LIMIT)
|
|
|
|
};
|
2024-07-03 19:29:49 +02:00
|
|
|
|
|
|
|
case "UPDATE_TOAST":
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
toasts: state.toasts.map((t) =>
|
|
|
|
t.id === action.toast.id ? { ...t, ...action.toast } : t
|
2024-09-07 09:12:48 +02:00
|
|
|
)
|
|
|
|
};
|
2024-07-03 19:29:49 +02:00
|
|
|
|
|
|
|
case "DISMISS_TOAST": {
|
2024-09-07 09:12:48 +02:00
|
|
|
const { toastId } = action;
|
2024-07-03 19:29:49 +02:00
|
|
|
|
|
|
|
// ! Side effects ! - This could be extracted into a dismissToast() action,
|
|
|
|
// but I'll keep it here for simplicity
|
|
|
|
if (toastId) {
|
2024-09-07 09:12:48 +02:00
|
|
|
addToRemoveQueue(toastId);
|
2024-07-03 19:29:49 +02:00
|
|
|
} else {
|
|
|
|
state.toasts.forEach((toast) => {
|
2024-09-07 09:12:48 +02:00
|
|
|
addToRemoveQueue(toast.id);
|
|
|
|
});
|
2024-07-03 19:29:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
toasts: state.toasts.map((t) =>
|
|
|
|
t.id === toastId || toastId === undefined
|
|
|
|
? {
|
|
|
|
...t,
|
2024-09-07 09:12:48 +02:00
|
|
|
open: false
|
2024-07-03 19:29:49 +02:00
|
|
|
}
|
|
|
|
: t
|
2024-09-07 09:12:48 +02:00
|
|
|
)
|
|
|
|
};
|
2024-07-03 19:29:49 +02:00
|
|
|
}
|
|
|
|
case "REMOVE_TOAST":
|
|
|
|
if (action.toastId === undefined) {
|
|
|
|
return {
|
|
|
|
...state,
|
2024-09-07 09:12:48 +02:00
|
|
|
toasts: []
|
|
|
|
};
|
2024-07-03 19:29:49 +02:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
...state,
|
2024-09-07 09:12:48 +02:00
|
|
|
toasts: state.toasts.filter((t) => t.id !== action.toastId)
|
|
|
|
};
|
2024-07-03 19:29:49 +02:00
|
|
|
}
|
2024-09-07 09:12:48 +02:00
|
|
|
};
|
2024-07-03 19:29:49 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
const listeners: Array<(state: State) => void> = [];
|
2024-07-03 19:29:49 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
let memoryState: State = { toasts: [] };
|
2024-07-03 19:29:49 +02:00
|
|
|
|
|
|
|
function dispatch(action: Action) {
|
2024-09-07 09:12:48 +02:00
|
|
|
memoryState = reducer(memoryState, action);
|
2024-07-03 19:29:49 +02:00
|
|
|
listeners.forEach((listener) => {
|
2024-09-07 09:12:48 +02:00
|
|
|
listener(memoryState);
|
|
|
|
});
|
2024-07-03 19:29:49 +02:00
|
|
|
}
|
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
type Toast = Omit<ToasterToast, "id">;
|
2024-07-03 19:29:49 +02:00
|
|
|
|
|
|
|
function toast({ ...props }: Toast) {
|
2024-09-07 09:12:48 +02:00
|
|
|
const id = genId();
|
2024-07-03 19:29:49 +02:00
|
|
|
|
|
|
|
const update = (props: ToasterToast) =>
|
|
|
|
dispatch({
|
|
|
|
type: "UPDATE_TOAST",
|
2024-09-07 09:12:48 +02:00
|
|
|
toast: { ...props, id }
|
|
|
|
});
|
|
|
|
const dismiss = () => dispatch({ type: "DISMISS_TOAST", toastId: id });
|
2024-07-03 19:29:49 +02:00
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: "ADD_TOAST",
|
|
|
|
toast: {
|
|
|
|
...props,
|
|
|
|
id,
|
|
|
|
open: true,
|
|
|
|
onOpenChange: (open) => {
|
2024-09-07 09:12:48 +02:00
|
|
|
if (!open) dismiss();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2024-07-03 19:29:49 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
id: id,
|
|
|
|
dismiss,
|
2024-09-07 09:12:48 +02:00
|
|
|
update
|
|
|
|
};
|
2024-07-03 19:29:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function useToast() {
|
2024-09-07 09:12:48 +02:00
|
|
|
const [state, setState] = React.useState<State>(memoryState);
|
2024-07-03 19:29:49 +02:00
|
|
|
|
|
|
|
React.useEffect(() => {
|
2024-09-07 09:12:48 +02:00
|
|
|
listeners.push(setState);
|
2024-07-03 19:29:49 +02:00
|
|
|
return () => {
|
2024-09-07 09:12:48 +02:00
|
|
|
const index = listeners.indexOf(setState);
|
2024-07-03 19:29:49 +02:00
|
|
|
if (index > -1) {
|
2024-09-07 09:12:48 +02:00
|
|
|
listeners.splice(index, 1);
|
2024-07-03 19:29:49 +02:00
|
|
|
}
|
2024-09-07 09:12:48 +02:00
|
|
|
};
|
|
|
|
}, [state]);
|
2024-07-03 19:29:49 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
toast,
|
2024-09-07 09:12:48 +02:00
|
|
|
dismiss: (toastId?: string) => dispatch({ type: "DISMISS_TOAST", toastId })
|
|
|
|
};
|
2024-07-03 19:29:49 +02:00
|
|
|
}
|
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
export { useToast, toast };
|