mirror of
https://github.com/BradNut/boredgame
synced 2025-09-08 17:40:22 +00:00
34 lines
876 B
TypeScript
34 lines
876 B
TypeScript
import { writable } from 'svelte/store';
|
|
import type { ToastData } from '$lib/types';
|
|
import { ToastType } from '$lib/types';
|
|
|
|
// Custom store
|
|
const newToast = () => {
|
|
const { subscribe, update } = writable<ToastData[]>([]);
|
|
|
|
function send(message: string, { duration = 2000, type = ToastType.INFO, autoDismiss = true, dismissible = false, showButton = false } = {}) {
|
|
const id = Math.floor(Math.random() * 1000);
|
|
|
|
const newMessage: ToastData = {
|
|
id,
|
|
duration,
|
|
autoDismiss,
|
|
dismissible,
|
|
showButton,
|
|
type,
|
|
message
|
|
};
|
|
update((store) => [...store, newMessage]);
|
|
}
|
|
|
|
function remove(id: number) {
|
|
update((store) => {
|
|
const newStore = store.filter((item: ToastData) => item.id !== id);
|
|
return [...newStore];
|
|
});
|
|
}
|
|
|
|
return { subscribe, send, remove };
|
|
};
|
|
|
|
export const toast = newToast();
|