From 77494457e2a4f5c80c3a2f7acb054b12d918d8ad Mon Sep 17 00:00:00 2001 From: xXJSONDeruloXx Date: Sun, 13 Jul 2025 00:04:54 -0400 Subject: restructure for maintainability --- src/hooks/useInstallationActions.ts | 89 ++++++++++++++++++++ src/hooks/useLsfgHooks.ts | 156 ++++++++++++++++++++++++++++++++++++ 2 files changed, 245 insertions(+) create mode 100644 src/hooks/useInstallationActions.ts create mode 100644 src/hooks/useLsfgHooks.ts (limited to 'src/hooks') diff --git a/src/hooks/useInstallationActions.ts b/src/hooks/useInstallationActions.ts new file mode 100644 index 0000000..8dcf831 --- /dev/null +++ b/src/hooks/useInstallationActions.ts @@ -0,0 +1,89 @@ +import { useState } from "react"; +import { toaster } from "@decky/api"; +import { installLsfgVk, uninstallLsfgVk } from "../api/lsfgApi"; + +export function useInstallationActions() { + const [isInstalling, setIsInstalling] = useState(false); + const [isUninstalling, setIsUninstalling] = useState(false); + + const handleInstall = async ( + setIsInstalled: (value: boolean) => void, + setInstallationStatus: (value: string) => void, + reloadConfig?: () => Promise + ) => { + setIsInstalling(true); + setInstallationStatus("Installing lsfg-vk..."); + + try { + const result = await installLsfgVk(); + if (result.success) { + setIsInstalled(true); + setInstallationStatus("lsfg-vk installed successfully!"); + toaster.toast({ + title: "Installation Complete", + body: "lsfg-vk has been installed successfully" + }); + + // Reload lsfg config after installation + if (reloadConfig) { + await reloadConfig(); + } + } else { + setInstallationStatus(`Installation failed: ${result.error}`); + toaster.toast({ + title: "Installation Failed", + body: result.error || "Unknown error occurred" + }); + } + } catch (error) { + setInstallationStatus(`Installation failed: ${error}`); + toaster.toast({ + title: "Installation Failed", + body: `Error: ${error}` + }); + } finally { + setIsInstalling(false); + } + }; + + const handleUninstall = async ( + setIsInstalled: (value: boolean) => void, + setInstallationStatus: (value: string) => void + ) => { + setIsUninstalling(true); + setInstallationStatus("Uninstalling lsfg-vk..."); + + try { + const result = await uninstallLsfgVk(); + if (result.success) { + setIsInstalled(false); + setInstallationStatus("lsfg-vk uninstalled successfully!"); + toaster.toast({ + title: "Uninstallation Complete", + body: result.message || "lsfg-vk has been uninstalled successfully" + }); + } else { + setInstallationStatus(`Uninstallation failed: ${result.error}`); + toaster.toast({ + title: "Uninstallation Failed", + body: result.error || "Unknown error occurred" + }); + } + } catch (error) { + setInstallationStatus(`Uninstallation failed: ${error}`); + toaster.toast({ + title: "Uninstallation Failed", + body: `Error: ${error}` + }); + } finally { + setIsUninstalling(false); + } + }; + + return { + isInstalling, + isUninstalling, + handleInstall, + handleUninstall + }; +} diff --git a/src/hooks/useLsfgHooks.ts b/src/hooks/useLsfgHooks.ts new file mode 100644 index 0000000..76acd33 --- /dev/null +++ b/src/hooks/useLsfgHooks.ts @@ -0,0 +1,156 @@ +import { useState, useEffect } from "react"; +import { toaster } from "@decky/api"; +import { + checkLsfgVkInstalled, + checkLosslessScalingDll, + getLsfgConfig, + updateLsfgConfig, + type ConfigUpdateResult +} from "../api/lsfgApi"; + +export function useInstallationStatus() { + const [isInstalled, setIsInstalled] = useState(false); + const [installationStatus, setInstallationStatus] = useState(""); + + const checkInstallation = async () => { + try { + const status = await checkLsfgVkInstalled(); + setIsInstalled(status.installed); + if (status.installed) { + setInstallationStatus("lsfg-vk is installed"); + } else { + setInstallationStatus("lsfg-vk is not installed"); + } + return status.installed; + } catch (error) { + setInstallationStatus("Error checking installation status"); + return false; + } + }; + + useEffect(() => { + checkInstallation(); + }, []); + + return { + isInstalled, + installationStatus, + setIsInstalled, + setInstallationStatus, + checkInstallation + }; +} + +export function useDllDetection() { + const [dllDetected, setDllDetected] = useState(false); + const [dllDetectionStatus, setDllDetectionStatus] = useState(""); + + const checkDllDetection = async () => { + try { + const result = await checkLosslessScalingDll(); + setDllDetected(result.detected); + if (result.detected) { + setDllDetectionStatus(`Lossless Scaling App detected (${result.source})`); + } else { + setDllDetectionStatus(result.message || "Lossless Scaling App not detected"); + } + } catch (error) { + setDllDetectionStatus("Error checking Lossless Scaling App"); + } + }; + + useEffect(() => { + checkDllDetection(); + }, []); + + return { + dllDetected, + dllDetectionStatus + }; +} + +export function useLsfgConfig() { + const [enableLsfg, setEnableLsfg] = useState(true); + const [multiplier, setMultiplier] = useState(2); + const [flowScale, setFlowScale] = useState(1.0); + const [hdr, setHdr] = useState(false); + const [perfMode, setPerfMode] = useState(false); + const [immediateMode, setImmediateMode] = useState(false); + + const loadLsfgConfig = async () => { + try { + const result = await getLsfgConfig(); + if (result.success && result.config) { + setEnableLsfg(result.config.enable_lsfg); + setMultiplier(result.config.multiplier); + setFlowScale(result.config.flow_scale); + setHdr(result.config.hdr); + setPerfMode(result.config.perf_mode); + setImmediateMode(result.config.immediate_mode); + console.log("Loaded lsfg config:", result.config); + } else { + console.log("lsfg config not available, using defaults:", result.error); + } + } catch (error) { + console.error("Error loading lsfg config:", error); + } + }; + + const updateConfig = async ( + newEnableLsfg: boolean, + newMultiplier: number, + newFlowScale: number, + newHdr: boolean, + newPerfMode: boolean, + newImmediateMode: boolean + ): Promise => { + try { + const result = await updateLsfgConfig( + newEnableLsfg, + newMultiplier, + newFlowScale, + newHdr, + newPerfMode, + newImmediateMode + ); + if (!result.success) { + toaster.toast({ + title: "Update Failed", + body: result.error || "Failed to update configuration" + }); + } + return result; + } catch (error) { + toaster.toast({ + title: "Update Failed", + body: `Error: ${error}` + }); + return { success: false, error: String(error) }; + } + }; + + useEffect(() => { + loadLsfgConfig(); + }, []); + + return { + config: { + enableLsfg, + multiplier, + flowScale, + hdr, + perfMode, + immediateMode + }, + setters: { + setEnableLsfg, + setMultiplier, + setFlowScale, + setHdr, + setPerfMode, + setImmediateMode + }, + loadLsfgConfig, + updateConfig + }; +} -- cgit v1.2.3