-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcodegate-status-refresh-button.tsx
41 lines (36 loc) · 1.25 KB
/
codegate-status-refresh-button.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { useQueryClient } from "@tanstack/react-query";
import { PollingInterval } from "./codegate-status-polling-control";
import { getQueryOptionsCodeGateStatus } from "../hooks/use-codegate-status";
import { useCallback, useEffect, useState } from "react";
import { Button } from "@stacklok/ui-kit";
import { RefreshCcw } from "lucide-react";
import { twMerge } from "tailwind-merge";
export function CodeGateStatusRefreshButton({
pollingInterval,
className,
}: {
pollingInterval: PollingInterval;
className?: string;
}) {
const queryClient = useQueryClient();
const { queryKey } = getQueryOptionsCodeGateStatus(pollingInterval);
const [refreshed, setRefreshed] = useState<boolean>(false);
useEffect(() => {
const id = setTimeout(() => setRefreshed(false), 500);
return () => clearTimeout(id);
}, [refreshed]);
const handleRefresh = useCallback(() => {
setRefreshed(true);
return queryClient.invalidateQueries({ queryKey, refetchType: "all" });
}, [queryClient, queryKey]);
return (
<Button
onPress={handleRefresh}
variant="tertiary"
className={twMerge("size-7", className)}
isDisabled={refreshed}
>
<RefreshCcw className={refreshed ? "animate-spin-once" : undefined} />
</Button>
);
}