()
| 24 | * Hook providing profile CRUD operations. |
| 25 | */ |
| 26 | export function useProfiles() { |
| 27 | const [profilesData, setProfilesData] = useState<ProfilesData>(loadProfilesData); |
| 28 | |
| 29 | // Sync to localStorage whenever state changes |
| 30 | useEffect(() => { |
| 31 | saveProfilesData(profilesData); |
| 32 | }, [profilesData]); |
| 33 | |
| 34 | const profileNames = useMemo(() => { |
| 35 | const names = Object.keys(profilesData.profiles); |
| 36 | // Always include Default at the top |
| 37 | if (!names.includes(DEFAULT_PROFILE_NAME)) { |
| 38 | names.unshift(DEFAULT_PROFILE_NAME); |
| 39 | } |
| 40 | return names.sort((a, b) => { |
| 41 | // Default always first |
| 42 | if (a === DEFAULT_PROFILE_NAME) return -1; |
| 43 | if (b === DEFAULT_PROFILE_NAME) return 1; |
| 44 | return a.localeCompare(b); |
| 45 | }); |
| 46 | }, [profilesData.profiles]); |
| 47 | |
| 48 | const activeProfile = profilesData.activeProfile; |
| 49 | const isActiveProfileReadOnly = activeProfile === DEFAULT_PROFILE_NAME; |
| 50 | |
| 51 | /** |
| 52 | * Save current settings as a named profile. |
| 53 | * If name already exists, it will be overwritten. |
| 54 | */ |
| 55 | const saveProfile = useCallback((name: string) => { |
| 56 | const config = extractConfigurationFromStores(); |
| 57 | setProfilesData(prev => ({ |
| 58 | profiles: { ...prev.profiles, [name]: config }, |
| 59 | activeProfile: name, |
| 60 | })); |
| 61 | }, []); |
| 62 | |
| 63 | /** |
| 64 | * Load and apply a profile's settings. |
| 65 | * Default profile always uses fresh project defaults. |
| 66 | */ |
| 67 | const loadProfile = useCallback((name: string) => { |
| 68 | // Default profile always uses fresh project defaults |
| 69 | const config = name === DEFAULT_PROFILE_NAME |
| 70 | ? getDefaultConfiguration() |
| 71 | : profilesData.profiles[name]; |
| 72 | if (!config) return; |
| 73 | applyConfigurationToStores(config); |
| 74 | setProfilesData(prev => ({ ...prev, activeProfile: name })); |
| 75 | }, [profilesData.profiles]); |
| 76 | |
| 77 | /** |
| 78 | * Delete a profile by name. Cannot delete the Default profile. |
| 79 | */ |
| 80 | const deleteProfile = useCallback((name: string) => { |
| 81 | if (name === DEFAULT_PROFILE_NAME) return; // Protect default profile |
| 82 | setProfilesData(prev => { |
| 83 | const { [name]: _removed, ...remaining } = prev.profiles; |
no test coverage detected