/** * Sync Dashboard Component * Displays sync status and last sync information */ 'use client'; import { useEffect, useState } from 'react'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Badge } from '@/components/ui/badge'; import { formatDistanceToNow } from 'date-fns'; import { getEntityDisplayName } from '@/lib/utils/sync-helpers'; import { EntityType } from '@/lib/types/sync'; interface LastSyncInfo { [key: string]: { completed_at: string; status: string; records_added: number; records_updated: number; records_deleted: number; }; } interface SyncDashboardProps { refreshKey: number; } export default function SyncDashboard({ refreshKey }: SyncDashboardProps) { const [lastSyncInfo, setLastSyncInfo] = useState({}); const [loading, setLoading] = useState(true); useEffect(() => { fetchLastSyncInfo(); }, [refreshKey]); const fetchLastSyncInfo = async () => { try { const response = await fetch('/api/sync/last-sync'); if (response.ok) { const data = await response.json(); setLastSyncInfo(data.lastSync || {}); } } catch (error) { console.error('Failed to fetch last sync info:', error); } finally { setLoading(false); } }; if (loading) { return ( Sync Status Last sync information for each entity
{[1, 2, 3, 4, 5, 6].map((i) => (
))}
); } const entityKeys = Object.keys(lastSyncInfo); return ( Sync Status Last sync information for each entity {entityKeys.length === 0 ? (

No sync history available

) : (
{entityKeys.map((entityKey) => { const info = lastSyncInfo[entityKey]; const completedAt = new Date(info.completed_at); return (

{getEntityDisplayName(entityKey as EntityType)}

{info.status}

{formatDistanceToNow(completedAt, { addSuffix: true })}

Added: +{info.records_added.toLocaleString()}
Updated: ~{info.records_updated.toLocaleString()}
Deleted: -{info.records_deleted.toLocaleString()}
{/* Subtle hover indicator */}
); })}
)} ); }