"use client"; import { useEffect, useRef } from "react"; import { RefreshCw } from "lucide-react"; import { Button } from "@/components/ui/button"; import { cn } from "@/lib/utils"; import { useI18n } from "@/i18n/i18n-context"; interface RefreshButtonProps { /** Whether an async fetch is currently in-flight. */ loading: boolean; /** Click handler, usually calls `refetch()`. */ onClick: () => void; /** Optional tooltip / accessible label. */ title?: string; /** Extra class applied to the underlying button. */ className?: string; /** Size variant forwarded to Button. */ size?: "sm" | "default" | "icon"; /** Text label rendered beside the icon when size is not "icon". */ label?: string; /** Visual variant forwarded to Button. */ variant?: "outline" | "ghost"; } /** Duration of one full revolution. 800ms is the GitHub / Vercel / Slack * band — fast enough to feel responsive, slow enough to read as motion * rather than a blur. */ const SPIN_PERIOD_MS = 800; /** * Refresh button with **atomic chained rotations**. * * Industry pattern (GitHub, Linear, Raycast, ChatGPT regenerate): a refresh * spin is not a single "infinite" CSS animation that gets stopped mid-flight * — it's a chain of complete 360° cycles. Each cycle is one Animation with * `iterations: 1, easing: 'linear'`. On `finish`, if loading is still in * flight we start another cycle; otherwise we stop. This way: * * - The icon always lands cleanly at 0° (== 360°), no partial-angle settle. * - Speed is constant the entire time — no "slow down to a stop" that looks * like the spinner hit a wall, which is the most common cause of refresh * animations feeling unnatural. * - The transition between "loading" and "idle" never produces a snap-back, * even if the request finishes inside a single millisecond. * * A minimum of one full revolution plays after a click, so sub-100ms refetches * still register as "I did something" instead of flickering. * * Drive `loading` from TanStack Query's `isRefetching` (not `isFetching`) so * background auto-refetches on mount do not animate without user intent. */ export function RefreshButton({ loading, onClick, title, className, size = "icon", label, variant = "ghost", }: RefreshButtonProps) { const { t } = useI18n(); const resolvedTitle = title ?? t("common.refresh"); const iconRef = useRef(null); /** Currently-running cycle; null when idle. */ const animRef = useRef(null); /** Mirror of the `loading` prop accessible from inside the `finish` * listener, which closes over a stale value if we read the prop directly. */ const loadingRef = useRef(loading); const startCycle = () => { const el = iconRef.current; if (!el) return; if (animRef.current) return; const anim = el.animate( [{ transform: "rotate(0deg)" }, { transform: "rotate(360deg)" }], { duration: SPIN_PERIOD_MS, easing: "linear", iterations: 1, fill: "none", }, ); animRef.current = anim; anim.addEventListener("finish", () => { animRef.current = null; // Chain the next cycle if the request is still in flight. The chain // is implicit, not explicit: each cycle only knows about itself. if (loadingRef.current) { startCycle(); } }); // `cancel` shouldn't normally fire — we never call .cancel() ourselves — // but defensively clear the ref so a fresh click can start a new cycle. anim.addEventListener("cancel", () => { animRef.current = null; }); }; // Keep the ref in sync so the `finish` listener sees the latest value. useEffect(() => { loadingRef.current = loading; }, [loading]); // Start a cycle when loading begins. We don't have to do anything when // loading ends — the in-flight cycle finishes on its own and the chain // doesn't continue because `loadingRef.current` will be false. useEffect(() => { if (loading) { startCycle(); } // startCycle is a stable closure over refs; including it would force a // re-run on every render with no behaviour change. // eslint-disable-next-line react-hooks/exhaustive-deps }, [loading]); useEffect( () => () => { animRef.current?.cancel(); animRef.current = null; }, [], ); const handleClick = () => { if (loading) return; // Start a cycle eagerly so the user gets visual confirmation even when // the request resolves in <100ms (cache hit). Cycles are atomic: once // started, they always play to completion regardless of `loading` — // that's what makes the "minimum one full revolution" guarantee // structural rather than a separate timer. startCycle(); onClick(); }; const showLabel = size !== "icon" && Boolean(label); const iconSize = size === "sm" ? "h-3.5 w-3.5" : "h-4 w-4"; return ( ); }