import { useQuery, useMutation } from "@tanstack/react-query"; import { CAMBASE } from "../../../utils/config"; import type { SystemSettings } from "../../../types/types"; const camBase = import.meta.env.MODE !== "development" ? CAMBASE : ""; const fetchSystemSettings = async () => { const response = await fetch(`${camBase}/api/fetch-config?id=GLOBAL--Device`); if (!response.ok) { throw new Error("Failed to fetch system settings"); } return response.json(); }; const postSystemSettings = async (settings: SystemSettings) => { const systemSettingConfig = { id: "GLOBAL--Device", fields: [ { property: "propDeviceName", value: settings.deviceName }, { property: "propSNTPServer", value: settings.SNTPServer }, { property: "propSNTPIntervalMinutes", value: Number(settings.SNTPIntervalMinutes), }, { property: "propLocalTimeZone", value: settings.localTimeZone }, { property: "propTimeSource", value: settings.timeSource }, ], }; const response = await fetch(`${camBase}/api/update-config`, { method: "POST", body: JSON.stringify(systemSettingConfig), headers: { "Content-Type": "application/json", }, }); if (!response.ok) { throw new Error("Failed to update system settings"); } return response.json(); }; export const useSystemSettings = () => { const systemSettingsQuery = useQuery({ queryKey: ["systemSettings"], queryFn: fetchSystemSettings, }); const systemSettingsMutation = useMutation({ mutationKey: ["updateSystemSettings"], mutationFn: postSystemSettings, }); return { systemSettingsQuery, systemSettingsMutation }; };