'use client'; import { useState, useEffect, useCallback } from 'react'; import { useTranslations } from 'next-intl'; import { SettingsSection, SettingItem, ToggleSwitch } from './settings-section'; import { Button } from '@/components/ui/button'; import { useVacationStore } from '@/stores/vacation-store'; import { useAuthStore } from '@/stores/auth-store'; import { Loader2, AlertTriangle, Eye, EyeOff } from 'lucide-react'; import { toast } from '@/stores/toast-store'; function utcToLocalDatetime(utcIso: string): string { const d = new Date(utcIso); const pad = (n: number) => String(n).padStart(2, '0'); return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}T${pad(d.getHours())}:${pad(d.getMinutes())}`; } export function VacationSettings() { const t = useTranslations('settings.vacation'); const tNotifications = useTranslations('notifications'); const { client } = useAuthStore(); const { isEnabled, fromDate, toDate, subject, textBody, isLoading, isSaving, error, isSupported, fetchVacationResponse, updateVacationResponse, } = useVacationStore(); const [localEnabled, setLocalEnabled] = useState(isEnabled); const [localFromDate, setLocalFromDate] = useState(fromDate || ''); const [localToDate, setLocalToDate] = useState(toDate || ''); const [localSubject, setLocalSubject] = useState(subject); const [localTextBody, setLocalTextBody] = useState(textBody); const [showPreview, setShowPreview] = useState(false); const [validationWarnings, setValidationWarnings] = useState([]); useEffect(() => { if (client && isSupported) { void fetchVacationResponse(client); } }, [client, isSupported, fetchVacationResponse]); useEffect(() => { setLocalEnabled(isEnabled); setLocalFromDate(fromDate || ''); setLocalToDate(toDate || ''); setLocalSubject(subject); setLocalTextBody(textBody); }, [isEnabled, fromDate, toDate, subject, textBody]); const validate = useCallback(() => { const warnings: string[] = []; if (localFromDate && localToDate && new Date(localToDate) <= new Date(localFromDate)) { warnings.push(t('warnings.end_before_start')); } if (localFromDate && new Date(localFromDate) < new Date()) { warnings.push(t('warnings.start_in_past')); } if (localEnabled && !localTextBody.trim()) { warnings.push(t('warnings.empty_body')); } setValidationWarnings(warnings); return warnings; }, [localFromDate, localToDate, localEnabled, localTextBody, t]); useEffect(() => { validate(); }, [validate]); const hasChanges = localEnabled !== isEnabled || (localFromDate || null) !== (fromDate || null) || (localToDate || null) !== (toDate || null) || localSubject !== subject || localTextBody !== textBody; const hasBlockingError = !!(localFromDate && localToDate && new Date(localToDate) <= new Date(localFromDate)); const handleSave = async () => { if (!client) return; validate(); if (hasBlockingError) return; try { await updateVacationResponse(client, { isEnabled: localEnabled, fromDate: localFromDate || null, toDate: localToDate || null, subject: localSubject, textBody: localTextBody, }); toast.success(tNotifications('vacation_saved')); } catch (error) { console.error('Failed to save vacation response:', error); toast.error(tNotifications('vacation_save_failed')); } }; if (!isSupported) { return (
{t('not_supported')}
); } if (isLoading) { return (
{t('loading')}
); } if (error) { return (
{t('fetch_error')}
); } return (
{localEnabled ? t('status.active') : t('status.inactive')}
setLocalFromDate(e.target.value ? new Date(e.target.value).toISOString() : '')} className="px-3 py-1.5 text-sm rounded bg-muted border border-border text-foreground focus:outline-none focus:ring-2 focus:ring-primary" /> setLocalToDate(e.target.value ? new Date(e.target.value).toISOString() : '')} className="px-3 py-1.5 text-sm rounded bg-muted border border-border text-foreground focus:outline-none focus:ring-2 focus:ring-primary" /> setLocalSubject(e.target.value)} placeholder={t('message.subject_placeholder')} className="w-64 px-3 py-1.5 text-sm rounded bg-muted border border-border text-foreground placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-primary" />

{t('message.body_description')}