"use client"; import { useState } from "react"; import { IconRefresh } from "@tabler/icons-react"; import { Tooltip, TooltipContent, TooltipTrigger } from "@kandev/ui/tooltip"; type BranchRefreshButtonProps = { onRefresh: () => void; refreshing?: boolean; fetchedAt?: string; fetchError?: string; }; export function BranchRefreshButton({ onRefresh, refreshing, fetchedAt, fetchError, }: BranchRefreshButtonProps) { // Controlled open so the tooltip only reacts to hover, not focus. // Radix Popover auto-focuses the first focusable child when it opens, which // would otherwise trigger this tooltip the moment the dropdown is opened. const [open, setOpen] = useState(false); const hasError = Boolean(fetchError); const tooltip = formatRefreshTooltip(fetchedAt, refreshing, fetchError); return ( {tooltip} ); } function formatRefreshTooltip( fetchedAt: string | undefined, refreshing: boolean | undefined, fetchError: string | undefined, ) { if (refreshing) return "Refreshing branches..."; if (fetchError) return `Last refresh failed: ${fetchError}`; if (!fetchedAt) return "Refresh branches (git fetch)"; const date = new Date(fetchedAt); if (Number.isNaN(date.getTime())) return "Refresh branches (git fetch)"; return `Refresh branches (last fetched ${date.toLocaleTimeString()})`; }