import { useState, useEffect } from "react"; import { PanelSectionRow, ButtonItem } from "@decky/ui"; import { FaClipboard, FaCheck } from "react-icons/fa"; import { getLaunchOption } from "../api/lsfgApi"; import { showClipboardErrorToast } from "../utils/toastUtils"; import { copyWithVerification } from "../utils/clipboardUtils"; export function SmartClipboardButton() { const [isLoading, setIsLoading] = useState(false); const [showSuccess, setShowSuccess] = useState(false); useEffect(() => { if (showSuccess) { const timer = setTimeout(() => { setShowSuccess(false); }, 3000); return () => clearTimeout(timer); } return undefined; }, [showSuccess]); const getLaunchOptionText = async (): Promise => { try { const result = await getLaunchOption(); return result.launch_option || "~/lsfg %command%"; } catch (error) { return "~/lsfg %command%"; } }; const copyToClipboard = async () => { if (isLoading || showSuccess) return; setIsLoading(true); try { const text = await getLaunchOptionText(); const { success, verified } = await copyWithVerification(text); if (success) { setShowSuccess(true); if (!verified) { console.log('Copy verification failed but copy likely worked'); } } else { showClipboardErrorToast(); } } catch (error) { showClipboardErrorToast(); } finally { setIsLoading(false); } }; return (
{showSuccess ? ( ) : isLoading ? ( ) : ( )}
{showSuccess ? "Copied to clipboard" : isLoading ? "Copying..." : "Copy Launch Option"}
); }