import { useEffect } from 'react' import { Toaster, ToastBar, toast, ToastPosition } from 'react-hot-toast' import { ToastType } from 'react-hot-toast/src/core/types' import { useSearchParams } from 'react-router-dom' import IconCancel from '~/icons/compiled/Cancel' export { toast as notify } interface Notifications { [key: string]: NotificationHandler } interface NotificationReturnValue { kind?: ToastType body: string } type NotificationHandler = (value?: string) => NotificationReturnValue /** * List notifications here that need to be triggered remotely, e.g. via callbacks or redirects. */ const notifications: Notifications = { 'nc-email-confirmed': () => ({ kind: 'success', body: 'Your email address was confirmed.', }), 'nc-new-organization': name => ({ kind: 'success', body: `New organization ${name} created.`, }), 'nc-save-success': () => ({ kind: 'success', body: `Your changes were saved.`, }), 'nc-subscribe-success': () => ({ kind: 'success', body: 'Your subscription was successful. It may take a few moments before the change is reflected in the dashboard.', }), } export default function NotificationCenter(props: { className?: string }) { const [searchParams, setSearchParams] = useSearchParams() const POSITION: ToastPosition = 'top-center' const { className = 'fixed top-0 left-0 z-50 w-full mx-auto sm:w-96 sm:left-1/2 sm:-ml-48', } = props useEffect(() => { const entries = searchParams.entries() for (const [key, value] of entries) { if (notifications[key]) { const n = notifications[key](decodeURIComponent(value)) const toaster = n.kind && n.kind in toast ? toast[n.kind] : toast toaster(n.body) const filtered = Array.from(entries).filter(([k]) => k !== key) setSearchParams(filtered, { replace: true }) } } }, [searchParams, setSearchParams]) return (