diff options
Diffstat (limited to 'src/hooks')
| -rw-r--r-- | src/hooks/useGameConfiguration.ts | 61 | ||||
| -rw-r--r-- | src/hooks/useLsfgHooks.ts | 61 |
2 files changed, 42 insertions, 80 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 }; } diff --git a/src/hooks/useLsfgHooks.ts b/src/hooks/useLsfgHooks.ts index ea8b3d0..0b71ee9 100644 --- a/src/hooks/useLsfgHooks.ts +++ b/src/hooks/useLsfgHooks.ts @@ -1,14 +1,9 @@ -import { useState, useEffect, useCallback } from "react"; +import { useState, useEffect } from "react"; import { checkLsfgVkInstalled, - getLsfgConfig, getLosslessScalingBranchStatus, - updateLsfgConfigFromObject, - type ConfigUpdateResult, type SteamBranchStatus } from "../api/lsfgApi"; -import { ConfigurationData, getDefaults } from "../config/configSchema"; -import { showErrorToast, ToastMessages } from "../utils/toastUtils"; export function useInstallationStatus() { const [isInstalled, setIsInstalled] = useState<boolean>(false); @@ -60,57 +55,3 @@ export function useInstallationStatus() { checkInstallation }; } - -export function useLsfgConfig() { - const [config, setConfig] = useState<ConfigurationData>(() => getDefaults()); - - const loadLsfgConfig = useCallback(async () => { - try { - const result = await getLsfgConfig(); - if (result.success && result.config) { - setConfig(result.config); - } else { - console.log("lsfg config not available, using defaults:", result.error); - setConfig(getDefaults()); - } - } catch (error) { - console.error("Error loading lsfg config:", error); - setConfig(getDefaults()); - } - }, []); - - const updateConfig = useCallback(async (newConfig: ConfigurationData): Promise<ConfigUpdateResult> => { - try { - const result = await updateLsfgConfigFromObject(newConfig); - if (result.success) { - setConfig(newConfig); - } else { - showErrorToast( - ToastMessages.CONFIG_UPDATE_ERROR.title, - result.error || ToastMessages.CONFIG_UPDATE_ERROR.body - ); - } - return result; - } catch (error) { - showErrorToast(ToastMessages.CONFIG_UPDATE_ERROR.title, String(error)); - return { success: false, error: String(error) }; - } - }, []); - - const updateField = useCallback(async (fieldName: keyof ConfigurationData, value: boolean | number | string): Promise<ConfigUpdateResult> => { - const newConfig = { ...config, [fieldName]: value }; - return updateConfig(newConfig); - }, [config, updateConfig]); - - useEffect(() => { - loadLsfgConfig(); - }, []); - - return { - config, - setConfig, - loadLsfgConfig, - updateConfig, - updateField - }; -} |
