summaryrefslogtreecommitdiff
path: root/src/hooks/useGameConfiguration.ts
diff options
context:
space:
mode:
authorxXJSONDeruloXx <danielhimebauch@gmail.com>2026-09-06 22:04:54 -0400
committerxXJSONDeruloXx <danielhimebauch@gmail.com>2026-09-06 22:04:54 -0400
commit7bc5f685186b1ff03ce0f7c99e7bec411beabaeb (patch)
treecf94a5752af7f45f6d4a87e89546705f32096766 /src/hooks/useGameConfiguration.ts
parent170d183fdafc1ec1a686c006fd6a2431c1f30c71 (diff)
downloaddecky-lsfg-vk-7bc5f685186b1ff03ce0f7c99e7bec411beabaeb.tar.gz
decky-lsfg-vk-7bc5f685186b1ff03ce0f7c99e7bec411beabaeb.zip
feat: make ui suck less, frfr
Diffstat (limited to 'src/hooks/useGameConfiguration.ts')
-rw-r--r--src/hooks/useGameConfiguration.ts61
1 files changed, 41 insertions, 20 deletions
diff --git a/src/hooks/useGameConfiguration.ts b/src/hooks/useGameConfiguration.ts
index b177551..d279a8c 100644
--- a/src/hooks/useGameConfiguration.ts
+++ b/src/hooks/useGameConfiguration.ts
@@ -1,37 +1,40 @@
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { Router } from "@decky/ui";
-import { getGameConfigs, getInstalledGames, updateGameConfig, updateLsfgConfig, resetGameConfig, resetAllGameConfigs, type GameConfigEntry, type InstalledGame } from "../api/lsfgApi";
+import { getGameConfigs, getInstalledGames, updateGameConfig, resetGameConfig, resetAllGameConfigs, type GameConfigEntry, type GlobalConfig, type InstalledGame } from "../api/lsfgApi";
import { ConfigurationData, getDefaults } from "../config/configSchema";
export interface GameTarget extends InstalledGame { configured: boolean; }
export function useGameConfiguration() {
- const [defaultConfig, setDefaultConfig] = useState<ConfigurationData>(getDefaults());
const [games, setGames] = useState<GameConfigEntry[]>([]);
+ const [globalConfig, setGlobalConfig] = useState<GlobalConfig>({ dll: "", no_fp16: false });
const [installedGames, setInstalledGames] = useState<InstalledGame[]>([]);
+ const [configsLoaded, setConfigsLoaded] = useState(false);
const [selectedAppId, setSelectedAppId] = useState("");
const [runningGame, setRunningGame] = useState<GameTarget | null>(null);
- const autoSelected = useRef(false);
+ const previousRunningAppId = useRef<string | null>(null);
const load = useCallback(async () => {
const [result, installed] = await Promise.all([getGameConfigs(), getInstalledGames()]);
if (result.success) {
- setDefaultConfig(result.default || getDefaults());
+ setGlobalConfig(result.global_config || { dll: "", no_fp16: false });
setGames(result.games || []);
}
if (installed.success) setInstalledGames(installed.games || []);
+ setConfigsLoaded(true);
}, []);
useEffect(() => { load(); }, [load]);
useEffect(() => {
const poll = () => {
+ if (!configsLoaded) return;
const app = Router.MainRunningApp as any;
if (!app?.appid) return setRunningGame(null);
const appid = String(app.appid);
const installed = installedGames.find((game) => game.appid === appid);
const name = app.display_name || installed?.name;
if (!name) return setRunningGame(null);
- setRunningGame({
+ setRunningGame((current) => current?.appid === appid ? current : {
...(installed || { appid, name, nonSteam: false }),
name,
configured: games.some((game) => game.appid === appid),
@@ -40,13 +43,14 @@ export function useGameConfiguration() {
poll();
const interval = window.setInterval(poll, 2000);
return () => window.clearInterval(interval);
- }, [games, installedGames]);
+ }, [configsLoaded, games, installedGames]);
useEffect(() => {
- if (!autoSelected.current && runningGame) {
- autoSelected.current = true;
- setSelectedAppId(runningGame.appid);
+ const appid = runningGame?.appid || null;
+ if (appid !== previousRunningAppId.current) {
+ previousRunningAppId.current = appid;
+ setSelectedAppId(appid || "");
}
- }, [runningGame]);
+ }, [runningGame?.appid]);
const targets = useMemo<GameTarget[]>(() => {
const configured = installedGames.map((game) => ({ ...game, configured: games.some((item) => item.appid === game.appid) }));
@@ -54,25 +58,42 @@ export function useGameConfiguration() {
if (runningGame && !configured.some((game) => game.appid === runningGame.appid)) configured.unshift(runningGame);
return configured;
}, [games, installedGames, runningGame]);
- const selected = selectedAppId ? games.find((game) => game.appid === selectedAppId)?.config : defaultConfig;
- const config = selected || defaultConfig;
+ const template = useMemo(() => ({ ...getDefaults(), ...globalConfig }), [globalConfig]);
+ const config = games.find((game) => game.appid === selectedAppId)?.config || template;
const save = useCallback(async (next: ConfigurationData) => {
- if (!selectedAppId) {
- const result = await updateLsfgConfig(next);
- if (result.success) setDefaultConfig(next);
- return;
- }
const selectedTarget = targets.find((target) => target.appid === selectedAppId);
if (!selectedTarget?.name) return;
const result = await updateGameConfig(selectedAppId, selectedTarget.name, next);
if (result.success) await load();
}, [load, selectedAppId, targets]);
+ const enable = useCallback(async (appid: string) => {
+ const target = targets.find((item) => item.appid === appid);
+ if (!target?.name) return false;
+ const result = await updateGameConfig(appid, target.name, template);
+ if (result.success) await load();
+ return result.success;
+ }, [load, targets, template]);
+
const resetSelected = useCallback(async () => {
- if (selectedAppId) { await resetGameConfig(selectedAppId); setSelectedAppId(""); await load(); }
+ if (selectedAppId) {
+ const result = await resetGameConfig(selectedAppId);
+ if (result.success) {
+ setRunningGame((current) => current?.appid === selectedAppId ? { ...current, configured: false } : current);
+ setSelectedAppId("");
+ await load();
+ }
+ }
}, [load, selectedAppId]);
- const resetAll = useCallback(async () => { await resetAllGameConfigs(); setSelectedAppId(""); await load(); }, [load]);
+ const resetAll = useCallback(async () => {
+ const result = await resetAllGameConfigs();
+ if (result.success) {
+ setRunningGame((current) => current ? { ...current, configured: false } : current);
+ setSelectedAppId("");
+ await load();
+ }
+ }, [load]);
- return { config, defaultConfig, games, targets, runningGame, selectedAppId, setSelectedAppId, save, resetSelected, resetAll, reload: load };
+ return { config, games, targets, runningGame, selectedAppId, setSelectedAppId, save, enable, resetSelected, resetAll, reload: load };
}