From ec995c801ceee1041e8a93c49034adef5bec4402 Mon Sep 17 00:00:00 2001 From: Sam Becker Date: Sat, 26 Apr 2025 18:02:11 -0500 Subject: [PATCH] Refine key command/toast interaction --- src/components/primitives/KeyCommand.tsx | 4 ++- ...ts => useNavigateOrRunActionWithToast.tsx} | 32 +++++++++++-------- 2 files changed, 21 insertions(+), 15 deletions(-) rename src/components/{useNavigateOrRunActionWithToast.ts => useNavigateOrRunActionWithToast.tsx} (67%) diff --git a/src/components/primitives/KeyCommand.tsx b/src/components/primitives/KeyCommand.tsx index a4260503..2bf32ad8 100644 --- a/src/components/primitives/KeyCommand.tsx +++ b/src/components/primitives/KeyCommand.tsx @@ -34,7 +34,9 @@ export default function KeyCommand({ {key === '⌘' ? : key === '⌫' - ? + ? : key} ))} diff --git a/src/components/useNavigateOrRunActionWithToast.ts b/src/components/useNavigateOrRunActionWithToast.tsx similarity index 67% rename from src/components/useNavigateOrRunActionWithToast.ts rename to src/components/useNavigateOrRunActionWithToast.tsx index d8bbb113..9a727630 100644 --- a/src/components/useNavigateOrRunActionWithToast.ts +++ b/src/components/useNavigateOrRunActionWithToast.tsx @@ -1,12 +1,13 @@ import { toastWaiting } from '@/toast'; import { useRouter } from 'next/navigation'; import { useCallback, useEffect, useRef, useTransition } from 'react'; +import { FiCheckSquare } from 'react-icons/fi'; import { toast } from 'sonner'; export default function useNavigateOrRunActionWithToast({ pathOrAction, toastMessage = 'Loading...', - dismissDelay = 500, + dismissDelay = 1500, }: { pathOrAction?: string | (() => Promise | undefined) toastMessage?: string @@ -17,15 +18,20 @@ export default function useNavigateOrRunActionWithToast({ const toastId = useRef(undefined); const [isPending, startTransition] = useTransition(); + + const dismissToast = useCallback(() => { + if (toastId.current) { + toast(toastMessage, { + id: toastId.current, + icon: , + }); + return setTimeout(() => { + toast.dismiss(toastId.current); + }, dismissDelay); + } + }, [dismissDelay, toastMessage]); useEffect(() => { - const dismissToast = () => { - if (toastId.current) { - return setTimeout(() => { - toast.dismiss(toastId.current); - }, dismissDelay); - } - }; if (!isPending) { const timeout = dismissToast(); return () => clearTimeout(timeout); @@ -33,7 +39,7 @@ export default function useNavigateOrRunActionWithToast({ return () => { dismissToast(); }; - }, [isPending, dismissDelay]); + }, [isPending, dismissDelay, dismissToast]); const navigateOrRunAction = useCallback(() => { if (typeof pathOrAction === 'string') { @@ -44,15 +50,13 @@ export default function useNavigateOrRunActionWithToast({ } else if (typeof pathOrAction === 'function') { const result = pathOrAction(); if (result instanceof Promise) { - const toastId = toastWaiting(toastMessage); + toastId.current = toastWaiting(toastMessage); result.finally(() => { - setTimeout(() => { - toast.dismiss(toastId); - }, 1000); + dismissToast(); }); } } - }, [pathOrAction, router, toastMessage]); + }, [dismissToast, pathOrAction, router, toastMessage]); return navigateOrRunAction; }