import { Button } from '@/components/ui/button'; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from '@/components/ui/table'; import type { SyncStatus } from '@/contexts/sync-context'; import type { PendingChange } from '@/lib/dexie-db'; import { formatDistanceToNow } from 'date-fns'; import { RefreshCw } from 'lucide-react'; interface PendingOperationsDialogProps { open: boolean; onOpenChange: (open: boolean) => void; operations: PendingChange[]; onSyncNow: () => void; syncStatus: SyncStatus; isOnline: boolean; } function getOperationBadgeClass(operation: PendingChange['operation']): string { switch (operation) { case 'create': return 'bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200'; case 'update': return 'bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-200'; case 'delete': return 'bg-red-100 text-red-800 dark:bg-red-900 dark:text-red-200'; default: return 'bg-gray-100 text-gray-800 dark:bg-gray-900 dark:text-gray-200'; } } function formatDataPreview(data: Record): string { if (data.id) { const preview = `ID: ${String(data.id).slice(0, 8)}...`; if (data.name) { return `${preview}, Name: ${data.name}`; } if (data.description) { return `${preview}, ${String(data.description).slice(0, 20)}...`; } return preview; } return JSON.stringify(data).slice(0, 50) + '...'; } export function PendingOperationsDialog({ open, onOpenChange, operations, onSyncNow, syncStatus, isOnline, }: PendingOperationsDialogProps) { const isSyncing = syncStatus === 'syncing'; const canSync = isOnline && !isSyncing; return ( Pending Operations These operations are waiting to be synced with the server.
{operations.length === 0 ? (
No pending operations
) : ( Store Operation Data Time {operations.map((op) => ( {op.store.replace('_', ' ')} {op.operation} {formatDataPreview(op.data)} {formatDistanceToNow( new Date(op.timestamp), { addSuffix: true }, )} ))}
)}
); }