'use client'; import { Text, Toast, ToastProvider, ToastViewport, XStack, YStack } from '@hanzo/gui'; import * as React from 'react'; // Toasts, on the gui engine. // // The published surface is imperative — `toaster.error({ title })` from // anywhere, including code that is not a component — while gui's toast is a // hook (`useToastController`) that only a descendant of a provider can call. // The bridge is a module-level store: callers push onto it, `` // subscribes with `useSyncExternalStore` and renders one gui `Toast` per entry. // That is the whole adaptation, and it is why `sonner` is gone rather than // wrapped — a second toast implementation would have been a second way to do // the same thing. const SECOND = 1_000; const DURATION = 10 * SECOND; type ToastType = 'info' | 'warning' | 'success' | 'error' | 'loading'; interface ToastOptions { readonly id?: string | number; readonly title?: string; readonly description?: string; readonly type?: ToastType; readonly duration?: number; readonly action?: { readonly label: string; readonly onClick: () => void }; readonly onStatusChange?: (details: { readonly status: string }) => void; } interface Entry extends ToastOptions { readonly id: string | number; } // ── the store ────────────────────────────────────────────────────────────── let entries: ReadonlyArray = []; const listeners = new Set<() => void>(); let counter = 0; const emit = () => listeners.forEach((l) => l()); const subscribe = (l: () => void) => { listeners.add(l); return () => { listeners.delete(l); }; }; /** `useSyncExternalStore` compares by identity, so the array is replaced, never mutated. */ const snapshot = () => entries; const remove = (id: string | number): void => { const found = entries.find((e) => e.id === id); entries = entries.filter((e) => e.id !== id); emit(); found?.onStatusChange?.({ status: 'unmounted' }); }; const create = (opts: ToastOptions): string | number => { const id = opts.id ?? `toast-${ ++counter }`; const entry: Entry = { ...opts, id }; entries = [ ...entries.filter((e) => e.id !== id), entry ]; emit(); return id; }; const update = (id: string | number, opts: ToastOptions): void => { entries = entries.map((e) => (e.id === id ? { ...e, ...opts, id } : e)); emit(); }; export const toaster = { create, success: (opts: ToastOptions) => create({ ...opts, type: 'success' }), error: (opts: ToastOptions) => create({ ...opts, type: 'error' }), loading: (opts: ToastOptions) => create({ ...opts, type: 'loading' }), update, remove, } as const; // ── presentation ─────────────────────────────────────────────────────────── /** Border colour carries the severity; the surface stays the popover surface. */ const EDGE: Record = { success: 'var(--color-status-good)', error: 'var(--color-status-bad)', warning: 'var(--color-status-warn)', loading: 'var(--color-border-divider)', info: 'var(--color-border-divider)', }; const One = ({ entry }: { entry: Entry }) => ( { if (!open) remove(entry.id); } } unstyled flexDirection="column" gap={ 4 } padding={ 12 } borderRadius={ 8 } borderWidth={ 1 } borderColor={ EDGE[entry.type ?? 'info'] as never } backgroundColor={ 'var(--color-popover-bg)' as never } transition="quick" enterStyle={{ opacity: 0, y: -8 }} exitStyle={{ opacity: 0, y: -8 }} > { entry.title ? ( { entry.title } ) : null } { entry.description ? ( { entry.description } ) : null } { /* Toast.Action is the accessible affordance and takes no styling of its own; the look belongs to the Text it wraps. */ } { entry.action ? ( { entry.action.label } ) : null } ); export const Toaster: React.FC = () => { const list = React.useSyncExternalStore(subscribe, snapshot, snapshot); return ( { list.map((entry) => ) } ); };