import { useStore } from '@nanostores/react'; import { $toastMessage } from '../stores/toast'; import { useEffect } from 'react'; import { CheckIcon } from './ReactIcons/CheckIcon'; import { ErrorIcon } from './ReactIcons/ErrorIcon'; import { WarningIcon } from './ReactIcons/WarningIcon'; import { InfoIcon } from './ReactIcons/InfoIcon'; import { Spinner } from './ReactIcons/Spinner'; import { deleteUrlParam, getUrlParams, setUrlParams } from '../lib/browser'; export interface Props {} const messageCodes: Record = { tl: 'Successfully left the team', fs: 'Friend request sent', fa: 'Friend request accepted', }; export function Toaster(props: Props) { const toastMessage = useStore($toastMessage); const { c } = getUrlParams(); if (c) { deleteUrlParam('c'); if (messageCodes[c]) { $toastMessage.set({ type: 'success', message: messageCodes[c] }); } } useEffect(() => { if (toastMessage === undefined) { return; } const removeMessage = setTimeout(() => { if (toastMessage?.type !== 'loading') { $toastMessage.set(undefined); } }, 2500); return () => { clearTimeout(removeMessage); }; }, [toastMessage]); if (!toastMessage) { return null; } return (
{ $toastMessage.set(undefined); }} className={`fixed bottom-5 left-1/2 z-[9999] min-w-[375px] max-w-[375px] animate-fade-slide-up sm:min-w-[auto]`} >
{toastMessage.type === 'success' && ( )} {toastMessage.type === 'error' && ( )} {toastMessage.type === 'warning' && ( )} {toastMessage.type === 'info' && ( )} {toastMessage.type === 'loading' && } {toastMessage.message}
); }