- Debounce project config saves: use local state + save-on-blur instead of firing IPC requests on every keystroke in text inputs - Add Zustand selectors to all store consumers to prevent full-store re-renders on any state change - Fix initialization race: chain checkImage after checkDocker resolves - Fix DockerSettings setTimeout race: await checkImage after save - Add console.error logging to all 11 empty catch blocks in ProjectCard - Add keyboard support to AddProjectDialog: Escape to close, click-outside-to-close, form submit on Enter, auto-focus Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
71 lines
1.6 KiB
TypeScript
71 lines
1.6 KiB
TypeScript
import { useCallback } from "react";
|
|
import { useShallow } from "zustand/react/shallow";
|
|
import { useAppState } from "../store/appState";
|
|
import * as commands from "../lib/tauri-commands";
|
|
import type { AppSettings } from "../lib/types";
|
|
|
|
export function useSettings() {
|
|
const { hasKey, setHasKey, appSettings, setAppSettings } = useAppState(
|
|
useShallow(s => ({
|
|
hasKey: s.hasKey,
|
|
setHasKey: s.setHasKey,
|
|
appSettings: s.appSettings,
|
|
setAppSettings: s.setAppSettings,
|
|
}))
|
|
);
|
|
|
|
const checkApiKey = useCallback(async () => {
|
|
try {
|
|
const has = await commands.hasApiKey();
|
|
setHasKey(has);
|
|
return has;
|
|
} catch {
|
|
setHasKey(false);
|
|
return false;
|
|
}
|
|
}, [setHasKey]);
|
|
|
|
const saveApiKey = useCallback(
|
|
async (key: string) => {
|
|
await commands.setApiKey(key);
|
|
setHasKey(true);
|
|
},
|
|
[setHasKey],
|
|
);
|
|
|
|
const removeApiKey = useCallback(async () => {
|
|
await commands.deleteApiKey();
|
|
setHasKey(false);
|
|
}, [setHasKey]);
|
|
|
|
const loadSettings = useCallback(async () => {
|
|
try {
|
|
const settings = await commands.getSettings();
|
|
setAppSettings(settings);
|
|
return settings;
|
|
} catch (e) {
|
|
console.error("Failed to load settings:", e);
|
|
return null;
|
|
}
|
|
}, [setAppSettings]);
|
|
|
|
const saveSettings = useCallback(
|
|
async (settings: AppSettings) => {
|
|
const updated = await commands.updateSettings(settings);
|
|
setAppSettings(updated);
|
|
return updated;
|
|
},
|
|
[setAppSettings],
|
|
);
|
|
|
|
return {
|
|
hasKey,
|
|
checkApiKey,
|
|
saveApiKey,
|
|
removeApiKey,
|
|
appSettings,
|
|
loadSettings,
|
|
saveSettings,
|
|
};
|
|
}
|