refactor(settings): share the general settings save between desktop and phone

Saving one preference with the error toast on failure lives in one useSettingSaver hook; the desktop tab drops its eleven inline copies of it.
This commit is contained in:
Maurice
2026-10-09 10:22:29 +02:00
parent 3b4f17643c
commit a7bd26722a
4 changed files with 81 additions and 42 deletions
@@ -0,0 +1,25 @@
import { useTranslation } from '../../i18n';
import { useSettingsStore } from '../../store/settingsStore';
import type { Settings } from '../../types';
import { useToast } from '../shared/Toast';
/**
* One user preference saved straight away, with the error as a toast when it fails.
* Behind both general settings shells (the desktop tab and the phone section), which
* read the current values from the `settings` this returns.
*/
export function useSettingSaver() {
const { settings, updateSetting } = useSettingsStore();
const { t } = useTranslation();
const toast = useToast();
const save = async (key: keyof Settings, value: Settings[keyof Settings]) => {
try {
await updateSetting(key, value);
} catch (e: unknown) {
toast.error(e instanceof Error ? e.message : t('common.error'));
}
};
return { settings, save };
}