import React, { useState, useEffect } from 'react'; import { RefreshCw } from 'lucide-react'; import Spinner from '../Spinner'; import Modal from '../Modal'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Switch } from '@/components/ui/switch'; const SCHEDULE_PRESETS = [ { label: 'Daily at 3 AM', value: '0 3 * * *' }, { label: 'Weekly Sunday 3 AM', value: '0 3 * * 0' }, { label: 'Every 6 hours', value: '0 */6 * * *' }, { label: 'Every 12 hours', value: '0 */12 * * *' }, { label: 'Custom', value: 'custom' }, ]; const AutoSyncScheduleModal = ({ environment, prodId, onClose, api }) => { const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); const [enabled, setEnabled] = useState(false); const [schedule, setSchedule] = useState('0 3 * * *'); const [selectedPreset, setSelectedPreset] = useState('0 3 * * *'); const [customCron, setCustomCron] = useState(''); const [nextRuns, setNextRuns] = useState([]); useEffect(() => { loadSchedule(); }, []); async function loadSchedule() { setLoading(true); try { const data = await api.getAutoSyncSchedule(prodId, environment.id); setEnabled(data.enabled || false); if (data.schedule) { const matchingPreset = SCHEDULE_PRESETS.find(p => p.value === data.schedule); if (matchingPreset) { setSelectedPreset(data.schedule); } else { setSelectedPreset('custom'); setCustomCron(data.schedule); } setSchedule(data.schedule); } setNextRuns(data.next_runs || []); } catch { // Default values already set } finally { setLoading(false); } } function handlePresetChange(value) { setSelectedPreset(value); if (value !== 'custom') { setSchedule(value); } } function handleCustomCronChange(value) { setCustomCron(value); setSchedule(value); } async function handleSave() { setSaving(true); try { const finalSchedule = selectedPreset === 'custom' ? customCron : selectedPreset; await api.updateAutoSyncSchedule(prodId, environment.id, { enabled, schedule: finalSchedule, }); onClose(); } catch (err) { console.error('Failed to save auto-sync schedule:', err); } finally { setSaving(false); } } function formatDateTime(iso) { if (!iso) return ''; const d = new Date(iso); return d.toLocaleString(); } const envName = environment?.name || 'Environment'; return ( {loading ? (
Loading schedule...
) : (
{envName}

Automatically sync this environment from production on a schedule.

{enabled && ( <>
{SCHEDULE_PRESETS.map(preset => ( ))}
{selectedPreset === 'custom' && (
handleCustomCronChange(e.target.value)} placeholder="0 3 * * *" className="auto-sync-cron-input" /> Format: minute hour day-of-month month day-of-week
)} {nextRuns.length > 0 && (
Next Scheduled Runs
    {nextRuns.map((run, i) => (
  • {formatDateTime(run)}
  • ))}
)} )}
)}
); }; export default AutoSyncScheduleModal;