2026-02-27 04:29:51 +00:00
|
|
|
import { useCallback } from "react";
|
2026-02-28 20:42:40 +00:00
|
|
|
import { useShallow } from "zustand/react/shallow";
|
2026-02-27 04:29:51 +00:00
|
|
|
import { useAppState } from "../store/appState";
|
|
|
|
|
import * as commands from "../lib/tauri-commands";
|
2026-02-27 15:22:49 +00:00
|
|
|
import type { AppSettings } from "../lib/types";
|
2026-02-27 04:29:51 +00:00
|
|
|
|
|
|
|
|
export function useSettings() {
|
2026-03-01 03:59:58 +00:00
|
|
|
const { appSettings, setAppSettings } = useAppState(
|
2026-02-28 20:42:40 +00:00
|
|
|
useShallow(s => ({
|
|
|
|
|
appSettings: s.appSettings,
|
|
|
|
|
setAppSettings: s.setAppSettings,
|
|
|
|
|
}))
|
|
|
|
|
);
|
2026-02-27 04:29:51 +00:00
|
|
|
|
2026-02-27 15:22:49 +00:00
|
|
|
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],
|
|
|
|
|
);
|
|
|
|
|
|
2026-02-27 04:29:51 +00:00
|
|
|
return {
|
2026-02-27 15:22:49 +00:00
|
|
|
appSettings,
|
|
|
|
|
loadSettings,
|
|
|
|
|
saveSettings,
|
2026-02-27 04:29:51 +00:00
|
|
|
};
|
|
|
|
|
}
|