diff --git a/app/components/settings/setting-toggle.tsx b/app/components/settings/setting-toggle.tsx new file mode 100644 index 0000000..be6d2cb --- /dev/null +++ b/app/components/settings/setting-toggle.tsx @@ -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(); + const pendingValue = fetcher.formData?.get('value'); + const checked = pendingValue === undefined || pendingValue === null ? value : pendingValue === 'true'; + + useActionToast(fetcher.data); + + return ( + fetcher.submit({ key: setting.key, value: String(next) }, { method: 'post', action: ROUTES.settings })} + /> + ); +}