import { useEffect, useRef } from "react"; import { toast } from "../utils/toast"; function Notice({ message, kind, className = "" }: { message: string | null | undefined; kind: "danger" | "success"; className?: string }) { const ref = useRef(null); // A message that appears where you are looking needs nothing more. One that appears out of view — above after scrolling, or // far down a long page — would otherwise go unseen, so it is also raised as a toast. useEffect(() => { if (!message) return; const rect = ref.current?.getBoundingClientRect(); const inView = !!rect && rect.height > 0 && rect.bottom > 0 && rect.top < window.innerHeight; if (!inView) (kind === "danger" ? toast.error : toast.success)(message); }, [message, kind]); if (!message) return null; return (
{message}
); } /** The usual red message under a page title. Also shown as a toast when it appears out of view. */ export function ErrorNotice({ error, className }: { error: string | null | undefined; className?: string }) { return ; } /** The usual green message after something worked. Also shown as a toast when it appears out of view. */ export function SuccessNotice({ notice, className }: { notice: string | null | undefined; className?: string }) { return ; }