// Toasts, the confirmation modal every state-changing action goes through, and generic modals. import { createContext, useCallback, useContext, useEffect, useRef, useState, type ReactNode } from 'react' import { action as postAction, type ActionResult } from '../lib/api' import { Icon, Tip } from './ui' type ToastKind = 'info' | 'success' | 'error' | 'warning' | 'live' type Toast = { id: number; kind: ToastKind; text: ReactNode; ttl: number } type Confirm = { title: string; body: ReactNode; confirmLabel?: string; danger?: boolean; resolve: (ok: boolean) => void } type Feedback = { toast: (text: ReactNode, kind?: ToastKind, ttl?: number) => void confirm: (o: Omit) => Promise } const Ctx = createContext({ toast: () => {}, confirm: async () => false }) export const useFeedback = () => useContext(Ctx) export function FeedbackProvider({ children }: { children: ReactNode }) { const [toasts, setToasts] = useState([]) const [confirm, setConfirm] = useState(null) const seq = useRef(0) const toast = useCallback((text: ReactNode, kind: ToastKind = 'info', ttl = 6000) => { const id = ++seq.current setToasts((t) => [...t.slice(-5), { id, kind, text, ttl }]) if (ttl > 0) setTimeout(() => setToasts((t) => t.filter((x) => x.id !== id)), ttl) }, []) const ask = useCallback((o: Omit) => new Promise((resolve) => setConfirm({ ...o, resolve })), []) const close = (ok: boolean) => { confirm?.resolve(ok) setConfirm(null) } return ( {children}
{toasts.map((t) => (
{t.text}
))}
{confirm && ( close(false)} foot={<> }> {confirm.body} )}
) } export function Modal({ title, icon, onClose, children, foot, wide }: { title: ReactNode; icon?: string; onClose: () => void; children: ReactNode; foot?: ReactNode; wide?: boolean }) { useEffect(() => { const k = (e: KeyboardEvent) => e.key === 'Escape' && onClose() window.addEventListener('keydown', k) return () => window.removeEventListener('keydown', k) }, [onClose]) return (
e.target === e.currentTarget && onClose()}>
{title}
{children}
{foot &&
{foot}
}
) } /** * A button for one state-changing action: it asks for confirmation (naming the effect), POSTs the action, and toasts the * result or the server's refusal. Every write in the console goes through this. */ export function ActionButton({ label, icon, name, body, confirm, variant = 'outline-light', disabled, help, size = 'sm', onDone, className = '' }: { label: ReactNode; icon?: string; name: string; body?: Record confirm: { title: string; body: ReactNode; confirmLabel?: string; danger?: boolean } variant?: string; disabled?: boolean; help?: ReactNode; size?: 'sm' | 'md'; onDone?: (r: ActionResult) => void; className?: string }) { const fb = useFeedback() const [busy, setBusy] = useState(false) const run = async () => { if (!(await fb.confirm(confirm))) return setBusy(true) try { const r = await postAction(name, body ?? {}) fb.toast(r.message || 'Done', 'success') onDone?.(r) } catch (e) { fb.toast((e as Error).message, 'error', 10000) } finally { setBusy(false) } } const btn = ( ) return help ? {btn} : btn }