This repository has been archived on 2024-10-29. You can view files and clone it, but cannot push or open issues or pull requests.
scoresaber-reloadedv3/src/hooks/use-toast.ts

192 lines
3.9 KiB
TypeScript
Raw Normal View History

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