summaryrefslogtreecommitdiff
path: root/src/components/ConfigFileTab.tsx
blob: 07408d795ed69f62a30dc5df937f6525ffee9cc5 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { ButtonItem, Field, PanelSection, PanelSectionRow, Spinner } from "@decky/ui";
import { useEffect, useState } from "react";
import { RiArrowDownSFill, RiArrowUpSFill } from "react-icons/ri";
import { getDebugFileContents, type DebugFileContent, type DebugFileContentsResult } from "../api/lsfgApi";
import t from "../i18n/i18n";

function usePersistentCollapsed(key: string) {
  const [collapsed, setCollapsed] = useState(() => {
    try {
      return localStorage.getItem(key) !== "false";
    } catch {
      return true;
    }
  });

  useEffect(() => {
    try {
      localStorage.setItem(key, String(collapsed));
    } catch {
      // Persisting the view preference is optional.
    }
  }, [collapsed, key]);

  return [collapsed, () => setCollapsed((value) => !value)] as const;
}

function DebugFileSection({ file }: { file: DebugFileContent }) {
  const [collapsed, toggleCollapsed] = usePersistentCollapsed(`lsfg-debug-file-${file.id}-collapsed-v1`);
  const status = file.exists ? "Present" : "Not present";

  return (
    <>
      <PanelSectionRow>
        <Field
          label={file.label}
          description={`${file.path} · ${status}`}
          bottomSeparator="none"
        />
      </PanelSectionRow>
      <PanelSectionRow>
        <div
          className="LSFG_DebugFileCollapseButton_Container"
          style={{ marginTop: "-2px", marginBottom: "4px" }}
        >
          <ButtonItem
            layout="below"
            bottomSeparator={collapsed ? "standard" : "none"}
            onClick={toggleCollapsed}
          >
            {collapsed ? <RiArrowDownSFill /> : <RiArrowUpSFill />}
          </ButtonItem>
        </div>
      </PanelSectionRow>
      {!collapsed && (
        <PanelSectionRow>
          {file.exists && file.content !== null && file.content !== undefined ? (
            <pre style={{ whiteSpace: "pre-wrap", overflowWrap: "anywhere" }}>
              {file.content}
            </pre>
          ) : (
            <Field
              label="File unavailable"
              description={file.error || "The file has not been created yet."}
            />
          )}
        </PanelSectionRow>
      )}
    </>
  );
}

export function ConfigFileTab() {
  const [result, setResult] = useState<DebugFileContentsResult | null>(null);

  useEffect(() => {
    getDebugFileContents().then(setResult).catch((error) => {
      setResult({ success: false, error: String(error) });
    });
  }, []);

  if (!result) {
    return (
      <PanelSection title={t("NERD_CONFIG_FILE", "Configuration File")}>
        <PanelSectionRow>
          <Spinner />
        </PanelSectionRow>
      </PanelSection>
    );
  }

  return (
    <>
      <style>
        {`
          .LSFG_DebugFileCollapseButton_Container > div > div > div > button,
          .LSFG_DebugFileCollapseButton_Container > div > div > div > div > button {
            height: 24px !important;
            min-height: 24px !important;
            padding: 0 !important;
            display: flex !important;
            align-items: center !important;
            justify-content: center !important;
          }

          .LSFG_DebugFileCollapseButton_Container svg {
            display: block;
            margin: 0;
          }
        `}
      </style>
      <PanelSection title={t("NERD_CONFIG_FILE", "Configuration File")}>
        {result.error && (
          <PanelSectionRow>
            <Field label="Error" description={result.error} />
          </PanelSectionRow>
        )}
        {result.success && result.files?.map((file) => (
          <DebugFileSection key={file.id} file={file} />
        ))}
      </PanelSection>
    </>
  );
}