summaryrefslogtreecommitdiff
path: root/src/components/GameConfigurationSelector.tsx
blob: 15f24658542f2026b33b448b914afac460b32a4a (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { ButtonItem, ConfirmModal, Field, PanelSectionRow, showModal } from "@decky/ui";
import { useEffect, useState } from "react";
import { RiArrowDownSFill, RiArrowUpSFill } from "react-icons/ri";
import { GameTarget } from "../hooks/useGameConfiguration";

interface Props {
  targets: GameTarget[];
  runningGame: GameTarget | null;
  onSelect: (appid: string) => void;
  onEnableAll: () => Promise<void>;
  onResetAll: () => Promise<void>;
}

const CONFIGURED_COLLAPSED_KEY = "lsfg-configured-games-collapsed-v2";
const AVAILABLE_COLLAPSED_KEY = "lsfg-available-games-collapsed";

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

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

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

function GameGroup({
  title,
  games,
  collapsed,
  onToggle,
  onSelect,
}: {
  title: string;
  games: GameTarget[];
  collapsed: boolean;
  onToggle: () => void;
  onSelect: (appid: string) => void;
}) {
  if (games.length === 0) return null;

  return (
    <>
      <PanelSectionRow>
        <Field label={`${title} (${games.length})`} bottomSeparator="none" />
      </PanelSectionRow>
      <PanelSectionRow>
        <div
          className="LSFG_GameGroupCollapseButton_Container"
          style={{ marginTop: "-2px", marginBottom: "4px" }}
        >
          <ButtonItem
            layout="below"
            bottomSeparator={collapsed ? "standard" : "none"}
            onClick={onToggle}
          >
            {collapsed ? (
              <RiArrowDownSFill style={{ transform: "translate(0, -13px)", fontSize: "1.5em" }} />
            ) : (
              <RiArrowUpSFill style={{ transform: "translate(0, -12px)", fontSize: "1.5em" }} />
            )}
          </ButtonItem>
        </div>
      </PanelSectionRow>
      {!collapsed && games.map((game) => (
        <PanelSectionRow key={game.appid}>
          <Field
            label={game.name}
            description={game.nonSteam ? "Non-Steam" : "Steam"}
            onActivate={() => onSelect(game.appid)}
            highlightOnFocus
          />
        </PanelSectionRow>
      ))}
    </>
  );
}

export function GameConfigurationSelector({ targets, runningGame, onSelect, onEnableAll, onResetAll }: Props) {
  const sortGames = (games: GameTarget[]) => [...games].sort((a, b) => {
    if (a.appid === runningGame?.appid) return -1;
    if (b.appid === runningGame?.appid) return 1;
    return a.name.localeCompare(b.name);
  });
  const configuredGames = sortGames(targets.filter((game) => game.configured));
  const availableGames = sortGames(targets.filter((game) => !game.configured));
  const configuredSteamGames = configuredGames.filter((game) => !game.nonSteam);
  const configuredNonSteamGames = configuredGames.filter((game) => game.nonSteam);
  const availableSteamGames = availableGames.filter((game) => !game.nonSteam);
  const availableNonSteamGames = availableGames.filter((game) => game.nonSteam);
  const [configuredCollapsed, toggleConfigured] = usePersistentCollapsed(CONFIGURED_COLLAPSED_KEY);
  const [configuredNonSteamCollapsed, toggleConfiguredNonSteam] = usePersistentCollapsed(`${CONFIGURED_COLLAPSED_KEY}-non-steam`);
  const [availableCollapsed, toggleAvailable] = usePersistentCollapsed(AVAILABLE_COLLAPSED_KEY);
  const [availableNonSteamCollapsed, toggleAvailableNonSteam] = usePersistentCollapsed(`${AVAILABLE_COLLAPSED_KEY}-non-steam`);
  const confirmResetAll = () => {
    showModal(
      <ConfirmModal
        strTitle="Remove all profiles?"
        strOKButtonText="Remove all"
        strCancelButtonText="Cancel"
        onOK={() => void onResetAll()}
        onCancel={() => {}}
      />,
    );
  };
  const confirmEnableAll = () => {
    showModal(
      <ConfirmModal
        strTitle="Enable all available games?"
        strDescription="Create individual LSFG-VK profiles for every available game using the plugin defaults."
        strOKButtonText="Enable all"
        strCancelButtonText="Cancel"
        onOK={() => void onEnableAll()}
        onCancel={() => {}}
      />,
    );
  };

  return (
    <>
      <style>
        {`
          .LSFG_GameGroupCollapseButton_Container > div > div > div > button,
          .LSFG_GameGroupCollapseButton_Container > div > div > div > div > button {
            height: 10px !important;
          }
        `}
      </style>
      {targets.length === 0 && (
        <PanelSectionRow>
          <Field label="No installed games" description="Steam has not reported any eligible games" />
        </PanelSectionRow>
      )}
      {availableGames.length > 0 && (
        <PanelSectionRow>
          <ButtonItem layout="below" onClick={confirmEnableAll}>
            Enable all available games
          </ButtonItem>
        </PanelSectionRow>
      )}
      <GameGroup
        title="LSFG-VK Enabled"
        games={configuredSteamGames}
        collapsed={configuredCollapsed}
        onToggle={toggleConfigured}
        onSelect={onSelect}
      />
      <GameGroup
        title="LSFG-VK Enabled (Non-Steam)"
        games={configuredNonSteamGames}
        collapsed={configuredNonSteamCollapsed}
        onToggle={toggleConfiguredNonSteam}
        onSelect={onSelect}
      />
      <GameGroup
        title="Available games"
        games={availableSteamGames}
        collapsed={availableCollapsed}
        onToggle={toggleAvailable}
        onSelect={onSelect}
      />
      <GameGroup
        title="Available games (Non-Steam)"
        games={availableNonSteamGames}
        collapsed={availableNonSteamCollapsed}
        onToggle={toggleAvailableNonSteam}
        onSelect={onSelect}
      />
      <PanelSectionRow>
        <ButtonItem
          layout="below"
          onClick={confirmResetAll}
          disabled={!targets.some((target) => target.configured)}
        >
          Remove all profiles
        </ButtonItem>
      </PanelSectionRow>
    </>
  );
}