summaryrefslogtreecommitdiff
path: root/src/hooks/useLsfgHooks.ts
blob: 9beb74933aad60f1ac066490586210cf9de53af5 (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
import { useEffect, useState } from "react";
import {
  checkLsfgVkInstalled,
  getLosslessScalingBranchStatus,
  installLsfgVk,
  uninstallLsfgVk,
  type SteamBranchStatus,
} from "../api/lsfgApi";
import {
  showInstallErrorToast,
  showInstallSuccessToast,
  showUninstallErrorToast,
  showUninstallSuccessToast,
} from "../utils/toastUtils";

export function useInstallation(reloadConfig?: () => Promise<void>) {
  const [isInstalled, setIsInstalled] = useState(false);
  const [installationStatus, setInstallationStatus] = useState("");
  const [losslessScalingInstalled, setLosslessScalingInstalled] = useState(false);
  const [losslessScalingStatus, setLosslessScalingStatus] = useState("");
  const [steamBranchStatus, setSteamBranchStatus] = useState<SteamBranchStatus | null>(null);
  const [isInstalling, setIsInstalling] = useState(false);
  const [isUninstalling, setIsUninstalling] = useState(false);

  const checkInstallation = async () => {
    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 uninstalled successfully!");
      await checkInstallation();
      showUninstallSuccessToast();
    } catch (error) {
      setInstallationStatus(`Uninstallation failed: ${error}`);
      showUninstallErrorToast(String(error));
    } finally {
      setIsUninstalling(false);
    }
  };

  return {
    isInstalled,
    installationStatus,
    losslessScalingInstalled,
    losslessScalingStatus,
    steamBranchStatus,
    isInstalling,
    isUninstalling,
    install,
    uninstall,
    checkInstallation,
  };
}