import { useEffect, useRef, useState } from "react" import { Check, Download, LoaderCircle } from "lucide-react" import { backgroundFor, EXPORT_IGNORE, savePng } from "@/lib/export" /** * Round icon button in the top right of a chart: saves `target` as a PNG named after `name`. * `tone` matches what it sits on (a card on the page, or the dark stage). */ export function DownloadButton({ target, name, tone = "card", pad = 0, big = false, className = "" }: { target: React.RefObject; name: string; tone?: "card" | "stage"; pad?: number; big?: boolean; className?: string }) { const [state, setState] = useState<"idle" | "busy" | "done">("idle") const timer = useRef(0) useEffect(() => () => window.clearTimeout(timer.current), []) const run = async () => { const node = target.current if (!node || state === "busy") return setState("busy") try { await savePng(node, name, { background: backgroundFor(node), pad }) setState("done") timer.current = window.setTimeout(() => setState("idle"), 1400) } catch { setState("idle") } } const look = tone === "stage" ? "border-white/15 bg-white/[0.06] text-white/70 backdrop-blur hover:border-white/35 hover:bg-white/12 hover:text-white focus-visible:outline-white/60" : "border-border bg-card text-muted-foreground hover:bg-muted hover:text-foreground focus-visible:outline-ring" const Icon = state === "busy" ? LoaderCircle : state === "done" ? Check : Download return ( ) }