import { getContext, setContext, type Snippet } from 'svelte'; import { derived, get, type Readable, type Writable, writable, } from 'svelte/store'; import { BannerVariant, type BannerVariantType } from '../banner'; import { uniqueId } from '../unique-id'; import { pausableProgress } from './pausable-progress'; /** Internal notification context. */ export interface NotifyContext { state: NotifyState; notify: Notify; } export interface NotifyOptions { persist?: boolean; onDismiss?: VoidFunction; content?: Snippet; } export type NotifyFunction = ( title: string, message?: string, options?: NotifyOptions ) => { dismiss: VoidFunction }; /** Notification sender. */ export interface Notify { /** Push an info notification. */ info: NotifyFunction; /** Push a warning notification. */ warn: NotifyFunction; /** Push a danger notification. */ danger: NotifyFunction; /** Push a success notification. */ success: NotifyFunction; } /** Internal notification state. */ export interface NotifyState { notifications: Readable; pageIsVisible: Writable; } /** Internal notification item state and actions. */ export interface Notification { id: string; title: string; message: string | undefined; variant: BannerVariantType; progress: Readable; content?: Snippet; pause: () => void; resume: () => void; dismiss: () => void; } const NotifyContextKey = Symbol('notify-context'); const NotificationDuration = 4000; /** Create the internal notification state and context object. */ export const createNotifyContext = (): NotifyContext => { const notifications = writable([]); const pageIsVisible = writable(true); const pausedID = writable(); const unsubscribeByID = new Map void>(); const add = (variant: BannerVariantType) => ( title: string, message?: string, options?: NotifyOptions ): { dismiss: VoidFunction } => { const id = uniqueId('notification'); const isPaused = derived( [pageIsVisible, pausedID], ([$pageIsVisible, $pausedID]) => $pausedID === id || !$pageIsVisible ); const pause = () => setPaused(id, true); const resume = () => setPaused(id, false); const dismiss = () => { options?.onDismiss?.(); remove(id); }; const { progress, unsubscribe } = pausableProgress({ isPaused, totalDuration: NotificationDuration, onComplete: options?.persist ? undefined : dismiss, }); const notification = { id, variant, title, message, content: options?.content, progress, pause, resume, dismiss, }; unsubscribeByID.set(id, unsubscribe); notifications.set([...get(notifications), notification]); return { dismiss }; }; const remove = (id: string) => { unsubscribeByID.get(id)?.(); unsubscribeByID.delete(id); notifications.set(get(notifications).filter((item) => item.id !== id)); }; const setPaused = (id: string, isPaused: boolean) => { if (isPaused) { pausedID.set(id); } else if (get(pausedID) === id) { pausedID.set(undefined); } }; const context: NotifyContext = { state: { notifications, pageIsVisible }, notify: { info: add(BannerVariant.Info), warn: add(BannerVariant.Warning), danger: add(BannerVariant.Danger), success: add(BannerVariant.Success), }, }; return context; }; /** Provide notification state to a component tree. */ export const provideNotify = (context = createNotifyContext()): void => { setContext(NotifyContextKey, context); }; const useNotifyContext = (): NotifyContext => { const context = getContext(NotifyContextKey); if (!context) { throw new Error('Usage: No context from `provideNotify` available'); } return context; }; /** Get access to the notification sender in a component. */ export const useNotify = (): Notify => { return useNotifyContext().notify; }; /** Get access to the internal notification state. */ export const useNotifyState = (): NotifyState => { return useNotifyContext().state; };