import { useState, useEffect } from 'react'; import { toast } from 'sonner'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Card, CardContent } from '@/components/ui/card'; import { Switch } from '@/components/ui/switch'; import TotpSetupSection from './TotpSetupSection'; import type { Settings } from '@/types'; interface SecurityTabProps { settings: Settings | undefined; updateSettings: (updates: Partial) => Promise; isUpdating: boolean; } export default function SecurityTab({ settings, updateSettings, isUpdating }: SecurityTabProps) { const [autoLockEnabled, setAutoLockEnabled] = useState(settings?.auto_lock_enabled ?? false); const [autoLockMinutes, setAutoLockMinutes] = useState(settings?.auto_lock_minutes ?? 5); useEffect(() => { if (settings) { setAutoLockEnabled(settings.auto_lock_enabled); setAutoLockMinutes(settings.auto_lock_minutes ?? 5); } }, [settings?.id]); const handleAutoLockToggle = async (checked: boolean) => { const previous = autoLockEnabled; setAutoLockEnabled(checked); try { await updateSettings({ auto_lock_enabled: checked }); toast.success(checked ? 'Auto-lock enabled' : 'Auto-lock disabled'); } catch { setAutoLockEnabled(previous); toast.error('Failed to update auto-lock setting'); } }; const handleAutoLockMinutesSave = async () => { const raw = typeof autoLockMinutes === 'string' ? parseInt(autoLockMinutes) : autoLockMinutes; const clamped = Math.max(1, Math.min(60, isNaN(raw) ? 5 : raw)); setAutoLockMinutes(clamped); if (clamped === settings?.auto_lock_minutes) return; try { await updateSettings({ auto_lock_minutes: clamped }); toast.success(`Auto-lock timeout set to ${clamped} minutes`); } catch { setAutoLockMinutes(settings?.auto_lock_minutes ?? 5); toast.error('Failed to update auto-lock timeout'); } }; return (
{/* Auto-lock */}

Automatically lock the screen after idle time

setAutoLockMinutes(e.target.value === '' ? '' : parseInt(e.target.value) || '')} onBlur={handleAutoLockMinutesSave} onKeyDown={(e) => { if (e.key === 'Enter') handleAutoLockMinutesSave(); }} className="w-20" disabled={!autoLockEnabled || isUpdating} /> minutes
{/* Password + TOTP */}
); }