From b16bfa74b96f8ee26ef008071b25e0333dee6067 Mon Sep 17 00:00:00 2001 From: rajun Date: Tue, 27 Oct 2026 16:28:48 +0530 Subject: [PATCH] feat: add hook to toast action results --- app/hooks/use-action-toast.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 app/hooks/use-action-toast.ts diff --git a/app/hooks/use-action-toast.ts b/app/hooks/use-action-toast.ts new file mode 100644 index 0000000..74f89f7 --- /dev/null +++ b/app/hooks/use-action-toast.ts @@ -0,0 +1,22 @@ +import { useEffect, useRef } from 'react'; +import { toast } from 'sonner'; +import type { ActionResult } from '~/types/action'; + +// Toasts every new action result once; `onSuccess` runs for successful ones +export function useActionToast( + result: ActionResult | undefined, + onSuccess?: (result: Extract, { ok: true }>) => void, +) { + const onSuccessRef = useRef(onSuccess); + onSuccessRef.current = onSuccess; + + useEffect(() => { + if (!result) return; + if (result.ok) { + toast.success(result.message); + onSuccessRef.current?.(result); + } else { + toast.error(result.error); + } + }, [result]); +}