feat: add setting toggle
This commit is contained in:
parent
6e4c4f2c9d
commit
14ddb9e860
31
app/components/settings/setting-toggle.tsx
Normal file
31
app/components/settings/setting-toggle.tsx
Normal 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 })}
|
||||
/>
|
||||
);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user