'use client'; import { useState, useEffect } from 'react'; import { Button } from '@/components/ui/Button'; import { Card, CardHeader, CardContent } from '@/components/ui/Card'; import { Badge } from '@/components/ui/Badge'; import { parseDate, getToday } from '@/lib/date-utils'; interface TfsSchedulerStatus { isRunning: boolean; isEnabled: boolean; interval: 'hourly' | 'daily' | 'weekly'; nextSync: Date | null; tfsConfigured: boolean; } interface TfsSchedulerConfigProps { className?: string; } export function TfsSchedulerConfig({ className = "" }: TfsSchedulerConfigProps) { const [schedulerStatus, setSchedulerStatus] = useState(null); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); // Charger le statut initial useEffect(() => { loadSchedulerStatus(); }, []); const loadSchedulerStatus = async () => { try { const response = await fetch('/api/tfs/scheduler-status'); if (response.ok) { const status = await response.json(); setSchedulerStatus(status.data); } } catch (err) { console.error('Erreur lors du chargement du statut scheduler:', err); } }; const toggleScheduler = async () => { if (!schedulerStatus) return; setIsLoading(true); setError(null); try { const response = await fetch('/api/tfs/scheduler-config', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ tfsAutoSync: !schedulerStatus.isRunning, tfsSyncInterval: schedulerStatus.interval }) }); if (response.ok) { const result = await response.json(); setSchedulerStatus(result.data); } else { const error = await response.json(); setError(error.error || 'Erreur lors du toggle scheduler'); } } catch (err) { setError(err instanceof Error ? err.message : 'Erreur lors du toggle scheduler'); } finally { setIsLoading(false); } }; const updateInterval = async (interval: 'hourly' | 'daily' | 'weekly') => { if (!schedulerStatus) return; setIsLoading(true); setError(null); try { const response = await fetch('/api/tfs/scheduler-config', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ tfsAutoSync: true, tfsSyncInterval: interval }) }); if (response.ok) { const result = await response.json(); setSchedulerStatus(result.data); } else { const error = await response.json(); setError(error.error || 'Erreur lors de la mise à jour'); } } catch (err) { setError(err instanceof Error ? err.message : 'Erreur lors de la mise à jour'); } finally { setIsLoading(false); } }; const getStatusBadge = () => { if (!schedulerStatus) return null; if (!schedulerStatus.tfsConfigured) { return ⚠️ TFS non configuré; } if (!schedulerStatus.isEnabled) { return ⏸️ Désactivé; } return schedulerStatus.isRunning ? ( ✅ Actif ) : ( ❌ Arrêté ); }; const getNextSyncText = () => { if (!schedulerStatus?.nextSync) return 'Aucune synchronisation planifiée'; const nextSync = typeof schedulerStatus.nextSync === 'string' ? parseDate(schedulerStatus.nextSync) : schedulerStatus.nextSync; const now = getToday(); const diffMs = nextSync.getTime() - now.getTime(); if (diffMs <= 0) return 'Synchronisation en cours...'; const diffHours = Math.floor(diffMs / (1000 * 60 * 60)); const diffMinutes = Math.floor((diffMs % (1000 * 60 * 60)) / (1000 * 60)); if (diffHours > 0) { return `Dans ${diffHours}h ${diffMinutes}min`; } else { return `Dans ${diffMinutes}min`; } }; const getIntervalText = (interval: string) => { switch (interval) { case 'hourly': return 'Horaire'; case 'daily': return 'Quotidien'; case 'weekly': return 'Hebdomadaire'; default: return interval; } }; if (!schedulerStatus) { return (

⏰ Synchronisation automatique

Chargement...

); } return (

⏰ Synchronisation automatique

{getStatusBadge()}
{error && (

{error}

)} {/* Statut actuel */}
Statut:

{schedulerStatus.isEnabled && schedulerStatus.isRunning ? '🟢 Actif' : '🔴 Arrêté'}

Fréquence:

{getIntervalText(schedulerStatus.interval)}

Prochaine synchronisation:

{getNextSyncText()}

{/* Contrôles */}
{/* Toggle scheduler */}
Synchronisation automatique
{/* Sélecteur d'intervalle */} {schedulerStatus.isEnabled && (
Fréquence de synchronisation
{(['hourly', 'daily', 'weekly'] as const).map((interval) => ( ))}
)}
{/* Avertissement si TFS non configuré */} {!schedulerStatus.tfsConfigured && (

⚠️ Configurez d'abord votre connexion TFS pour activer la synchronisation automatique.

)}
); }