svrjs-nextjs-website/components/ui/use-toast.ts

192 lines
3.9 KiB
TypeScript
Raw Normal View History

"use client";
2024-07-03 19:29:49 +02:00
// Inspired by react-hot-toast library
import * as React from "react";
2024-07-03 19:29:49 +02:00
import type { ToastActionElement, ToastProps } from "@/components/ui/toast";
2024-07-03 19:29:49 +02:00
const TOAST_LIMIT = 1;
const TOAST_REMOVE_DELAY = 1000000;
2024-07-03 19:29:49 +02:00
type ToasterToast = ToastProps & {
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",
REMOVE_TOAST: "REMOVE_TOAST"
} as const;
2024-07-03 19:29:49 +02:00
let count = 0;
2024-07-03 19:29:49 +02:00
function genId() {
count = (count + 1) % Number.MAX_SAFE_INTEGER;
return count.toString();
2024-07-03 19:29:49 +02:00
}
type ActionType = typeof actionTypes;
2024-07-03 19:29:49 +02:00
type Action =
| {
type: ActionType["ADD_TOAST"];
toast: ToasterToast;
2024-07-03 19:29:49 +02:00
}
| {
type: ActionType["UPDATE_TOAST"];
toast: Partial<ToasterToast>;
2024-07-03 19:29:49 +02:00
}
| {
type: ActionType["DISMISS_TOAST"];
toastId?: ToasterToast["id"];
2024-07-03 19:29:49 +02:00
}
| {
type: ActionType["REMOVE_TOAST"];
toastId?: ToasterToast["id"];
};
2024-07-03 19:29:49 +02:00
interface State {
toasts: ToasterToast[];
2024-07-03 19:29:49 +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)) {
return;
2024-07-03 19:29:49 +02:00
}
const timeout = setTimeout(() => {
toastTimeouts.delete(toastId);
2024-07-03 19:29:49 +02:00
dispatch({
type: "REMOVE_TOAST",
toastId: toastId
});
}, TOAST_REMOVE_DELAY);
2024-07-03 19:29:49 +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,
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-07-03 19:29:49 +02:00
case "DISMISS_TOAST": {
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) {
addToRemoveQueue(toastId);
2024-07-03 19:29:49 +02:00
} else {
state.toasts.forEach((toast) => {
addToRemoveQueue(toast.id);
});
2024-07-03 19:29:49 +02:00
}
return {
...state,
toasts: state.toasts.map((t) =>
t.id === toastId || toastId === undefined
? {
...t,
open: false
2024-07-03 19:29:49 +02:00
}
: t
)
};
2024-07-03 19:29:49 +02:00
}
case "REMOVE_TOAST":
if (action.toastId === undefined) {
return {
...state,
toasts: []
};
2024-07-03 19:29:49 +02:00
}
return {
...state,
toasts: state.toasts.filter((t) => t.id !== action.toastId)
};
2024-07-03 19:29:49 +02:00
}
};
2024-07-03 19:29:49 +02:00
const listeners: Array<(state: State) => void> = [];
2024-07-03 19:29:49 +02:00
let memoryState: State = { toasts: [] };
2024-07-03 19:29:49 +02:00
function dispatch(action: Action) {
memoryState = reducer(memoryState, action);
2024-07-03 19:29:49 +02:00
listeners.forEach((listener) => {
listener(memoryState);
});
2024-07-03 19:29:49 +02:00
}
type Toast = Omit<ToasterToast, "id">;
2024-07-03 19:29:49 +02:00
function toast({ ...props }: Toast) {
const id = genId();
2024-07-03 19:29:49 +02:00
const update = (props: ToasterToast) =>
dispatch({
type: "UPDATE_TOAST",
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) => {
if (!open) dismiss();
}
}
});
2024-07-03 19:29:49 +02:00
return {
id: id,
dismiss,
update
};
2024-07-03 19:29:49 +02:00
}
function useToast() {
const [state, setState] = React.useState<State>(memoryState);
2024-07-03 19:29:49 +02:00
React.useEffect(() => {
listeners.push(setState);
2024-07-03 19:29:49 +02:00
return () => {
const index = listeners.indexOf(setState);
2024-07-03 19:29:49 +02:00
if (index > -1) {
listeners.splice(index, 1);
2024-07-03 19:29:49 +02:00
}
};
}, [state]);
2024-07-03 19:29:49 +02:00
return {
...state,
toast,
dismiss: (toastId?: string) => dispatch({ type: "DISMISS_TOAST", toastId })
};
2024-07-03 19:29:49 +02:00
}
export { useToast, toast };