import { useEffect, useState } from "react"; import { checkLsfgVkInstalled, getFlatpakSupportStatus, getLosslessScalingBranchStatus, installLsfgVk, repairFlatpakSupport, uninstallLsfgVk, type FlatpakExtensionStatus, type SteamBranchStatus, } from "../api/lsfgApi"; import { showInstallErrorToast, showInstallSuccessToast, showUninstallErrorToast, showUninstallSuccessToast, showErrorToast, } from "../utils/toastUtils"; export function useInstallation(reloadConfig?: () => Promise) { const [isInstalled, setIsInstalled] = useState(false); const [installationStatus, setInstallationStatus] = useState(""); const [losslessScalingInstalled, setLosslessScalingInstalled] = useState(false); const [losslessScalingStatus, setLosslessScalingStatus] = useState(""); const [flatpakStatus, setFlatpakStatus] = useState(null); const [steamBranchStatus, setSteamBranchStatus] = useState(null); const [isInstalling, setIsInstalling] = useState(false); const [isUninstalling, setIsUninstalling] = useState(false); const [isRepairingFlatpak, setIsRepairingFlatpak] = useState(false); const checkInstallation = async () => { try { setFlatpakStatus(await getFlatpakSupportStatus()); } catch (error) { console.error("Error checking Flatpak support:", error); setFlatpakStatus(null); } try { setSteamBranchStatus(await getLosslessScalingBranchStatus()); } catch (error) { console.error("Error checking Lossless Scaling Steam branch:", error); setSteamBranchStatus(null); } try { const status = await checkLsfgVkInstalled(); setIsInstalled(status.installed); setLosslessScalingInstalled(status.lossless_scaling_installed); setLosslessScalingStatus(status.lossless_scaling_status || "Lossless Scaling Not Installed"); setInstallationStatus(status.installed ? "lsfg-vk Installed" : "lsfg-vk Not Installed"); return status.installed; } catch { setSteamBranchStatus(null); setLosslessScalingInstalled(false); setLosslessScalingStatus("Lossless Scaling Not Installed"); setInstallationStatus("lsfg-vk Not Installed"); return false; } }; useEffect(() => { void checkInstallation(); }, []); const install = async () => { setIsInstalling(true); setInstallationStatus("Installing lsfg-vk..."); try { const result = await installLsfgVk(); if (!result.success) { setInstallationStatus(`Installation failed: ${result.error}`); showInstallErrorToast(result.error ?? undefined); return; } setIsInstalled(true); setInstallationStatus("lsfg-vk installed"); showInstallSuccessToast(); await reloadConfig?.(); await checkInstallation(); } catch (error) { setInstallationStatus(`Installation failed: ${error}`); showInstallErrorToast(String(error)); } finally { setIsInstalling(false); } }; const uninstall = async () => { setIsUninstalling(true); setInstallationStatus("Uninstalling lsfg-vk..."); try { const result = await uninstallLsfgVk(); if (!result.success) { setInstallationStatus(`Uninstallation failed: ${result.error}`); showUninstallErrorToast(result.error ?? undefined); return; } setIsInstalled(false); setInstallationStatus("lsfg-vk and Flatpak support uninstalled successfully!"); await checkInstallation(); showUninstallSuccessToast(); } catch (error) { setInstallationStatus(`Uninstallation failed: ${error}`); showUninstallErrorToast(String(error)); } finally { setIsUninstalling(false); } }; const repairFlatpak = async () => { if (isRepairingFlatpak) return false; setIsRepairingFlatpak(true); try { const result = await repairFlatpakSupport(); setFlatpakStatus(result); if (!result.success) { showErrorToast("Flatpak support repair failed", result.error || result.message || "Unknown error"); } return result.success; } catch (error) { showErrorToast("Flatpak support repair failed", String(error)); return false; } finally { setIsRepairingFlatpak(false); } }; return { isInstalled, installationStatus, losslessScalingInstalled, losslessScalingStatus, flatpakStatus, steamBranchStatus, isInstalling, isUninstalling, isRepairingFlatpak, install, uninstall, repairFlatpak, checkInstallation, }; }