// Venue settings for the v2 settings sections (plan step 2.6f): GET /api/settings/ (all // declared keys with their values) and PUT /api/settings/{key} {value} (core.settings.manage; // the backend checks types, enums, time zones and languages). Shares the react-query key // with the older settings tabs, so both stay in sync. import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' import toast from 'react-hot-toast' import { errorMessage } from '@xenia/api' import { useAccess } from '@xenia/access' import { useTranslation } from '@xenia/i18n' import client from '../../../api/client' /** Keys that change what bootstrap says (language, time zone, sign-in mode): reload it. */ const BOOTSTRAP_KEYS = new Set(['venue.language', 'venue.timezone', 'venue.theme', 'auth.manager_login_mode', 'security.autofill_username']) export function useSettings() { const { t } = useTranslation('manager') const qc = useQueryClient() const access = useAccess() const query = useQuery({ queryKey: ['pos-settings'], queryFn: () => client.get('/api/settings/').then(r => r.data), staleTime: 30_000, }) const mutation = useMutation({ mutationFn: ({ key, value }) => client.put(`/api/settings/${key}`, { value }), onSuccess: (_r, { key }) => { toast.success(t('settings.saved')) qc.invalidateQueries({ queryKey: ['pos-settings'] }) if (BOOTSTRAP_KEYS.has(key)) access.refresh() }, onError: err => toast.error(errorMessage(err, t)), }) return { loading: query.isLoading, error: query.error ? errorMessage(query.error, t) : null, /** The stored value (strings, as the backend keeps them), or `fallback`. */ get: (key, fallback = '') => query.data?.[key]?.value ?? fallback, save: (key, value) => mutation.mutate({ key, value: String(value) }), saving: mutation.isPending, } }