summaryrefslogtreecommitdiff
path: root/src/hooks/useProfileManagement.ts
blob: a5f2a0744563247d030bbd779d9a699f1ed94610 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { useState, useEffect, useCallback } from "react";
import {
  getProfiles,
  createProfile,
  deleteProfile,
  renameProfile,
  setCurrentProfile,
  updateProfileConfig,
  type ProfilesResult,
  type ProfileResult,
  type ConfigUpdateResult
} from "../api/lsfgApi";
import { ConfigurationData } from "../config/configSchema";
import { showSuccessToast, showErrorToast } from "../utils/toastUtils";

export function useProfileManagement() {
  const [profiles, setProfiles] = useState<string[]>([]);
  const [currentProfile, setCurrentProfileState] = useState<string>("decky-lsfg-vk");
  const [isLoading, setIsLoading] = useState(false);

  // Load profiles on hook initialization
  const loadProfiles = useCallback(async () => {
    try {
      const result: ProfilesResult = await getProfiles();
      if (result.success && result.profiles) {
        setProfiles(result.profiles);
        if (result.current_profile) {
          setCurrentProfileState(result.current_profile);
        }
        return result;
      } else {
        console.error("Failed to load profiles:", result.error);
        showErrorToast("Failed to load profiles", result.error || "Unknown error");
        return result;
      }
    } catch (error) {
      console.error("Error loading profiles:", error);
      showErrorToast("Error loading profiles", String(error));
      return { success: false, error: String(error) };
    }
  }, []);

  // Create a new profile
  const handleCreateProfile = useCallback(async (profileName: string, sourceProfile?: string) => {
    setIsLoading(true);
    try {
      const result: ProfileResult = await createProfile(profileName, sourceProfile || currentProfile);
      if (result.success) {
        // Use the normalized name returned from backend (spaces converted to dashes)
        const actualProfileName = result.profile_name || profileName;
        showSuccessToast("Profile created", `Created profile: ${actualProfileName}`);
        await loadProfiles();
        return result;
      } else {
        console.error("Failed to create profile:", result.error);
        showErrorToast("Failed to create profile", result.error || "Unknown error");
        return result;
      }
    } catch (error) {
      console.error("Error creating profile:", error);
      showErrorToast("Error creating profile", String(error));
      return { success: false, error: String(error) };
    } finally {
      setIsLoading(false);
    }
  }, [currentProfile, loadProfiles]);

  // Delete a profile
  const handleDeleteProfile = useCallback(async (profileName: string) => {
    if (profileName === "decky-lsfg-vk") {
      showErrorToast("Cannot delete default profile", "The default profile cannot be deleted");
      return { success: false, error: "Cannot delete default profile" };
    }

    setIsLoading(true);
    try {
      const result: ProfileResult = await deleteProfile(profileName);
      if (result.success) {
        showSuccessToast("Profile deleted", `Deleted profile: ${profileName}`);
        await loadProfiles();
        // If we deleted the current profile, it should have switched to default
        if (currentProfile === profileName) {
          setCurrentProfileState("decky-lsfg-vk");
        }
        return result;
      } else {
        console.error("Failed to delete profile:", result.error);
        showErrorToast("Failed to delete profile", result.error || "Unknown error");
        return result;
      }
    } catch (error) {
      console.error("Error deleting profile:", error);
      showErrorToast("Error deleting profile", String(error));
      return { success: false, error: String(error) };
    } finally {
      setIsLoading(false);
    }
  }, [currentProfile, loadProfiles]);

  // Rename a profile
  const handleRenameProfile = useCallback(async (oldName: string, newName: string) => {
    if (oldName === "decky-lsfg-vk") {
      showErrorToast("Cannot rename default profile", "The default profile cannot be renamed");
      return { success: false, error: "Cannot rename default profile" };
    }

    setIsLoading(true);
    try {
      const result: ProfileResult = await renameProfile(oldName, newName);
      if (result.success) {
        // Use the normalized name returned from backend (spaces converted to dashes)
        const actualNewName = result.profile_name || newName;
        showSuccessToast("Profile renamed", `Renamed profile to: ${actualNewName}`);
        await loadProfiles();
        // Update current profile if it was renamed
        if (currentProfile === oldName) {
          setCurrentProfileState(actualNewName);
        }
        return result;
      } else {
        console.error("Failed to rename profile:", result.error);
        showErrorToast("Failed to rename profile", result.error || "Unknown error");
        return result;
      }
    } catch (error) {
      console.error("Error renaming profile:", error);
      showErrorToast("Error renaming profile", String(error));
      return { success: false, error: String(error) };
    } finally {
      setIsLoading(false);
    }
  }, [currentProfile, loadProfiles]);

  // Set the current active profile
  const handleSetCurrentProfile = useCallback(async (profileName: string) => {
    setIsLoading(true);
    try {
      const result: ProfileResult = await setCurrentProfile(profileName);
      if (result.success) {
        setCurrentProfileState(profileName);
        showSuccessToast("Profile switched", `Switched to profile: ${profileName}`);
        return result;
      } else {
        console.error("Failed to switch profile:", result.error);
        showErrorToast("Failed to switch profile", result.error || "Unknown error");
        return result;
      }
    } catch (error) {
      console.error("Error switching profile:", error);
      showErrorToast("Error switching profile", String(error));
      return { success: false, error: String(error) };
    } finally {
      setIsLoading(false);
    }
  }, []);

  // Update configuration for a specific profile
  const handleUpdateProfileConfig = useCallback(async (profileName: string, config: ConfigurationData) => {
    setIsLoading(true);
    try {
      const result: ConfigUpdateResult = await updateProfileConfig(profileName, config);
      if (result.success) {
        return result;
      } else {
        console.error("Failed to update profile config:", result.error);
        showErrorToast("Failed to update profile config", result.error || "Unknown error");
        return result;
      }
    } catch (error) {
      console.error("Error updating profile config:", error);
      showErrorToast("Error updating profile config", String(error));
      return { success: false, error: String(error) };
    } finally {
      setIsLoading(false);
    }
  }, [currentProfile]);

  // Initialize profiles on mount
  useEffect(() => {
    loadProfiles();
  }, [loadProfiles]);

  return {
    profiles,
    currentProfile,
    isLoading,
    loadProfiles,
    createProfile: handleCreateProfile,
    deleteProfile: handleDeleteProfile,
    renameProfile: handleRenameProfile,
    setCurrentProfile: handleSetCurrentProfile,
    updateProfileConfig: handleUpdateProfileConfig
  };
}