import { useState, useEffect, useMemo } from 'react'; import { Clock, AlertTriangle } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Progress } from '@/components/ui/progress'; import { Badge } from '@/components/ui/badge'; import { cn } from '@/lib/utils'; interface LockState { submissionId: string; expiresAt: Date; } interface QueueStats { pendingCount: number; assignedToMe: number; avgWaitHours: number; } interface EnhancedLockStatusDisplayProps { currentLock: LockState | null; queueStats: QueueStats | null; loading: boolean; onExtendLock: () => void; onReleaseLock: () => void; getCurrentTime: () => Date; } const LOCK_DURATION_MS = 15 * 60 * 1000; // 15 minutes const WARNING_THRESHOLD_MS = 5 * 60 * 1000; // 5 minutes const CRITICAL_THRESHOLD_MS = 2 * 60 * 1000; // 2 minutes export const EnhancedLockStatusDisplay = ({ currentLock, queueStats, loading, onExtendLock, onReleaseLock, getCurrentTime, }: EnhancedLockStatusDisplayProps) => { const [timeLeft, setTimeLeft] = useState(0); useEffect(() => { if (!currentLock) return; const updateTimer = () => { const now = getCurrentTime(); const remaining = currentLock.expiresAt.getTime() - now.getTime(); setTimeLeft(Math.max(0, remaining)); }; updateTimer(); const interval = setInterval(updateTimer, 1000); return () => clearInterval(interval); }, [currentLock, getCurrentTime]); const { urgency, progressPercent } = useMemo(() => { if (timeLeft <= 0) return { urgency: 'expired', progressPercent: 0 }; if (timeLeft <= CRITICAL_THRESHOLD_MS) return { urgency: 'critical', progressPercent: (timeLeft / LOCK_DURATION_MS) * 100 }; if (timeLeft <= WARNING_THRESHOLD_MS) return { urgency: 'warning', progressPercent: (timeLeft / LOCK_DURATION_MS) * 100 }; return { urgency: 'safe', progressPercent: (timeLeft / LOCK_DURATION_MS) * 100 }; }, [timeLeft]); const formatTime = (ms: number): string => { const totalSeconds = Math.floor(ms / 1000); const minutes = Math.floor(totalSeconds / 60); const seconds = totalSeconds % 60; return `${minutes}:${seconds.toString().padStart(2, '0')}`; }; const showExtendButton = timeLeft > 0 && timeLeft <= WARNING_THRESHOLD_MS; if (!currentLock) { return (
No submission claimed
{queueStats && (
{queueStats.pendingCount} pending
)}
); } return (
Lock expires in
{formatTime(timeLeft)}
div]:bg-destructive', urgency === 'warning' && '[&>div]:bg-yellow-500', urgency === 'safe' && '[&>div]:bg-primary' )} /> {urgency === 'critical' && (
Lock expiring soon! Extend or release.
)}
{showExtendButton && ( )}
); }; EnhancedLockStatusDisplay.displayName = 'EnhancedLockStatusDisplay';