32 lines
1.2 KiB
TypeScript
32 lines
1.2 KiB
TypeScript
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 })}
|
|
/>
|
|
);
|
|
}
|