mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-17 14:29:46 +02:00
Remove useToast everywhere
This commit is contained in:
29
src-web/lib/toast.ts
Normal file
29
src-web/lib/toast.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { atom } from 'jotai/index';
|
||||
import type { PrivateToastEntry, ToastEntry } from '../components/Toasts';
|
||||
import { generateId } from './generateId';
|
||||
import { jotaiStore } from './jotai';
|
||||
|
||||
export const toastsAtom = atom<PrivateToastEntry[]>([]);
|
||||
|
||||
export function showToast({ id, timeout = 5000, ...props }: ToastEntry) {
|
||||
id = id ?? generateId();
|
||||
if (timeout != null) {
|
||||
setTimeout(() => hideToast(id), timeout);
|
||||
}
|
||||
jotaiStore.set(toastsAtom, (a) => {
|
||||
if (a.some((v) => v.id === id)) {
|
||||
// It's already visible with this id
|
||||
return a;
|
||||
}
|
||||
return [...a, { id, timeout, ...props }];
|
||||
});
|
||||
return id;
|
||||
}
|
||||
|
||||
export function hideToast(id: string) {
|
||||
jotaiStore.set(toastsAtom, (all) => {
|
||||
const t = all.find((t) => t.id === id);
|
||||
t?.onClose?.();
|
||||
return all.filter((t) => t.id !== id);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user