summaryrefslogtreecommitdiff
path: root/src/components/SmartClipboardButton.tsx
blob: 098b53dec460f630e332ff94f5cba50c62f297b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { useState } from "react";
import { PanelSectionRow, ButtonItem } from "@decky/ui";
import { FaClipboard } from "react-icons/fa";
import { getLaunchOption } from "../api/lsfgApi";
import { showClipboardSuccessToast, showClipboardErrorToast, showSuccessToast } from "../utils/toastUtils";
import { copyWithVerification } from "../utils/clipboardUtils";

export function SmartClipboardButton() {
  const [isLoading, setIsLoading] = useState(false);

  const getLaunchOptionText = async (): Promise<string> => {
    try {
      const result = await getLaunchOption();
      return result.launch_option || "~/lsfg %command%";
    } catch (error) {
      return "~/lsfg %command%";
    }
  };

  const copyToClipboard = async () => {
    if (isLoading) return;
    
    setIsLoading(true);
    try {
      const text = await getLaunchOptionText();
      const { success, verified } = await copyWithVerification(text);
      
      if (success) {
        if (verified) {
          showClipboardSuccessToast();
        } else {
          showSuccessToast("Copied to Clipboard!", "Launch option copied (verification unavailable)");
        }
      } else {
        showClipboardErrorToast();
      }

    } catch (error) {
      showClipboardErrorToast();
    } finally {
      setIsLoading(false);
    }
  };

  return (
    <PanelSectionRow>
      <ButtonItem
        layout="below"
        onClick={copyToClipboard}
        disabled={isLoading}
      >
        <div style={{ display: "flex", alignItems: "center", gap: "8px" }}>
          {isLoading ? (
            <FaClipboard style={{ 
              animation: "pulse 1s ease-in-out infinite",
              opacity: 0.7 
            }} />
          ) : (
            <FaClipboard />
          )}
          <div>{isLoading ? "Copying..." : "Copy Launch Option"}</div>
        </div>
      </ButtonItem>
      <style>{`
        @keyframes pulse {
          0% { opacity: 0.7; }
          50% { opacity: 1; }
          100% { opacity: 0.7; }
        }
      `}</style>
    </PanelSectionRow>
  );
}