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 })} /> ); }