feat: add setting toggle

This commit is contained in:
rajun 2026-10-27 16:28:58 +05:30
parent 6e4c4f2c9d
commit 14ddb9e860

View File

@ -0,0 +1,31 @@
import { useFetcher } from 'react-router';
import { SwitchRow } from '~/components/ui/switch-row';
import { ROUTES } from '~/data/routes';
import { useActionToast } from '~/hooks/use-action-toast';
import type { ActionResult } from '~/types/action';
import type { SettingKey } from '~/types/settings';
interface SettingToggleProps {
setting: { key: SettingKey; label: string; hint: string };
value: boolean;
}
// Saves as soon as it is flipped; shows the new value while saving
export function SettingToggle({ setting, value }: SettingToggleProps) {
const fetcher = useFetcher<ActionResult>();
const pendingValue = fetcher.formData?.get('value');
const checked = pendingValue === undefined || pendingValue === null ? value : pendingValue === 'true';
useActionToast(fetcher.data);
return (
<SwitchRow
id={`setting-${setting.key}`}
label={setting.label}
hint={setting.hint}
checked={checked}
disabled={fetcher.state !== 'idle'}
onCheckedChange={(next) => fetcher.submit({ key: setting.key, value: String(next) }, { method: 'post', action: ROUTES.settings })}
/>
);
}