diff --git a/client/src/components/home/PublicGames/RefreshButton.tsx b/client/src/components/home/PublicGames/RefreshButton.tsx index 33a1671..88389a1 100644 --- a/client/src/components/home/PublicGames/RefreshButton.tsx +++ b/client/src/components/home/PublicGames/RefreshButton.tsx @@ -2,26 +2,38 @@ import { IconRefresh } from "@tabler/icons-react"; import { useRouter } from "next/navigation"; -import { useTransition } from "react"; +import { useState, useTransition } from "react"; -export default function RefreshButton({ onRefresh }: { onRefresh?: () => void }) { +export default function RefreshButton({ onRefresh }: { onRefresh?: () => Promise | void }) { const router = useRouter(); - const [isLoading, startTransition] = useTransition(); + const [isPending, startTransition] = useTransition(); + const [isLoading, setIsLoading] = useState(false); - function handleRefresh() { - startTransition(() => { - if (onRefresh) onRefresh(); - else router.refresh(); - }); + async function handleRefresh() { + if (onRefresh) { + setIsLoading(true); + try { + await onRefresh(); + } finally { + setIsLoading(false); + } + } else { + startTransition(() => { + router.refresh(); + }); + } } + const loading = isLoading || isPending; + return ( ); }