import { useState } from "react"; import { Download, Loader2 } from "lucide-react"; import { Button } from "@/components/ui/button"; export function agentAvatarFilename(name: string) { return `${name.replace(/[^a-zA-Z0-9_-]+/g, "-") || "agent"}-avatar.png`; } /** Downloads the instance's generated PNG; provider upload remains explicit. */ export function AgentAvatarDownload({ avatarUrl, name, iconOnly = false, }: { avatarUrl: string; name: string; iconOnly?: boolean; }) { const [downloading, setDownloading] = useState(false); const [failed, setFailed] = useState(false); const download = async () => { if (downloading) return; setDownloading(true); setFailed(false); try { const response = await fetch(avatarUrl); if ( !response.ok || !response.headers.get("content-type")?.startsWith("image/png") ) throw new Error("Avatar unavailable"); const url = URL.createObjectURL(await response.blob()); const link = document.createElement("a"); link.href = url; link.download = agentAvatarFilename(name); document.body.append(link); link.click(); link.remove(); setTimeout(() => URL.revokeObjectURL(url), 1_000); } catch { setFailed(true); } finally { setDownloading(false); } }; return (
{failed && (

Couldn’t download the avatar. Try downloading it again.

)}
); }