'use client'; import { useState, useEffect } from 'react'; import { useTranslations, useLocale } from 'next-intl'; import { Card, CardHeader, CardTitle, CardDescription, CardContent } from '@/components/ui/card'; import { Input } from '@/components/ui/input'; import { Button } from '@/components/ui/button'; import { Search, Package, FileText, ClipboardList, Lightbulb, History, X, Clock, Ship, ExternalLink, Maximize2, Minimize2, Globe, Anchor, } from 'lucide-react'; interface SearchHistoryItem { id: string; trackingNumber: string; carrierId: string; carrierName: string; timestamp: Date; } type CarrierDescKey = 'containerOrBl' | 'containerBlOrBooking' | 'containerOnly'; const carriers = [ { id: 'maersk', name: 'Maersk', color: '#00243D', textColor: 'text-white', trackingUrl: 'https://www.maersk.com/tracking/', placeholder: 'Ex: MSKU1234567', descKey: 'containerOrBl' as CarrierDescKey }, { id: 'msc', name: 'MSC', color: '#002B5C', textColor: 'text-white', trackingUrl: 'https://www.msc.com/track-a-shipment?query=', placeholder: 'Ex: MSCU1234567', descKey: 'containerBlOrBooking' as CarrierDescKey }, { id: 'cma-cgm', name: 'CMA CGM', color: '#E30613', textColor: 'text-white', trackingUrl: 'https://www.cma-cgm.com/ebusiness/tracking/search?SearchBy=Container&Reference=', placeholder: 'Ex: CMAU1234567', descKey: 'containerOrBl' as CarrierDescKey }, { id: 'hapag-lloyd', name: 'Hapag-Lloyd', color: '#FF6600', textColor: 'text-white', trackingUrl: 'https://www.hapag-lloyd.com/en/online-business/track/track-by-container-solution.html?container=', placeholder: 'Ex: HLCU1234567', descKey: 'containerOnly' as CarrierDescKey }, { id: 'cosco', name: 'COSCO', color: '#003A70', textColor: 'text-white', trackingUrl: 'https://elines.coscoshipping.com/ebusiness/cargoTracking?trackingNumber=', placeholder: 'Ex: COSU1234567', descKey: 'containerOrBl' as CarrierDescKey }, { id: 'one', name: 'ONE', color: '#FF00FF', textColor: 'text-white', trackingUrl: 'https://ecomm.one-line.com/one-ecom/manage-shipment/cargo-tracking?trkNoParam=', placeholder: 'Ex: ONEU1234567', descKey: 'containerOrBl' as CarrierDescKey }, { id: 'evergreen', name: 'Evergreen', color: '#006633', textColor: 'text-white', trackingUrl: 'https://www.shipmentlink.com/servlet/TDB1_CargoTracking.do?BL=', placeholder: 'Ex: EGHU1234567', descKey: 'containerOrBl' as CarrierDescKey }, { id: 'yangming', name: 'Yang Ming', color: '#FFD700', textColor: 'text-gray-900', trackingUrl: 'https://www.yangming.com/e-service/Track_Trace/track_trace_cargo_tracking.aspx?rdolType=CT&str=', placeholder: 'Ex: YMLU1234567', descKey: 'containerOnly' as CarrierDescKey }, { id: 'zim', name: 'ZIM', color: '#1E3A8A', textColor: 'text-white', trackingUrl: 'https://www.zim.com/tools/track-a-shipment?consnumber=', placeholder: 'Ex: ZIMU1234567', descKey: 'containerOrBl' as CarrierDescKey }, { id: 'hmm', name: 'HMM', color: '#E65100', textColor: 'text-white', trackingUrl: 'https://www.hmm21.com/cms/business/ebiz/trackTrace/trackTrace/index.jsp?type=1&number=', placeholder: 'Ex: HDMU1234567', descKey: 'containerOrBl' as CarrierDescKey }, ]; const HISTORY_KEY = 'xpeditis_track_history'; export default function TrackTracePage() { const t = useTranslations('dashboard.trackTrace'); const locale = useLocale(); const [trackingNumber, setTrackingNumber] = useState(''); const [selectedCarrier, setSelectedCarrier] = useState(''); const [error, setError] = useState(''); const [searchHistory, setSearchHistory] = useState([]); const [showMap, setShowMap] = useState(false); const [isMapFullscreen, setIsMapFullscreen] = useState(false); const [isMapLoading, setIsMapLoading] = useState(true); useEffect(() => { const savedHistory = localStorage.getItem(HISTORY_KEY); if (savedHistory) { try { const parsed = JSON.parse(savedHistory); setSearchHistory(parsed.map((item: any) => ({ ...item, timestamp: new Date(item.timestamp) }))); } catch (e) { console.error('Failed to parse search history:', e); } } }, []); const saveHistory = (history: SearchHistoryItem[]) => { localStorage.setItem(HISTORY_KEY, JSON.stringify(history)); setSearchHistory(history); }; const handleTrack = () => { if (!trackingNumber.trim()) { setError(t('errors.noTrackingNumber')); return; } if (!selectedCarrier) { setError(t('errors.noCarrier')); return; } setError(''); const carrier = carriers.find(c => c.id === selectedCarrier); if (carrier) { const newHistoryItem: SearchHistoryItem = { id: Date.now().toString(), trackingNumber: trackingNumber.trim(), carrierId: carrier.id, carrierName: carrier.name, timestamp: new Date(), }; const updatedHistory = [newHistoryItem, ...searchHistory.filter( h => !(h.trackingNumber === newHistoryItem.trackingNumber && h.carrierId === newHistoryItem.carrierId) )].slice(0, 10); saveHistory(updatedHistory); const trackingUrl = carrier.trackingUrl + encodeURIComponent(trackingNumber.trim()); window.open(trackingUrl, '_blank', 'noopener,noreferrer'); } }; const handleHistoryClick = (item: SearchHistoryItem) => { setTrackingNumber(item.trackingNumber); setSelectedCarrier(item.carrierId); }; const handleDeleteHistory = (id: string) => { const updatedHistory = searchHistory.filter(h => h.id !== id); saveHistory(updatedHistory); }; const handleClearHistory = () => { saveHistory([]); }; const handleKeyPress = (e: React.KeyboardEvent) => { if (e.key === 'Enter') { handleTrack(); } }; const selectedCarrierData = carriers.find(c => c.id === selectedCarrier); const formatTimeAgo = (date: Date) => { const now = new Date(); const diffMs = now.getTime() - date.getTime(); const diffMins = Math.floor(diffMs / 60000); const diffHours = Math.floor(diffMs / 3600000); const diffDays = Math.floor(diffMs / 86400000); if (diffMins < 1) return t('timeAgo.justNow'); if (diffMins < 60) return t('timeAgo.minutesAgo', { count: diffMins }); if (diffHours < 24) return t('timeAgo.hoursAgo', { count: diffHours }); if (diffDays < 7) return t('timeAgo.daysAgo', { count: diffDays }); return date.toLocaleDateString(locale); }; return (
{/* Header */}

{t('title')}

{t('description')}

{/* Search Form */} {t('searchCard.title')} {t('searchCard.description')} {/* Carrier Selection */}
{carriers.map(carrier => ( ))}
{/* Tracking Number Input */}
{ setTrackingNumber(e.target.value.toUpperCase()); setError(''); }} onKeyPress={handleKeyPress} placeholder={selectedCarrierData?.placeholder || 'Ex: MSKU1234567'} className="text-lg font-mono border-gray-300 focus:border-blue-500 h-12" /> {selectedCarrierData && (

{t(`carriers.${selectedCarrierData.descKey}` as any)}

)}
{/* Map Toggle */}
{/* Error Message */} {error && (

{error}

)}
{/* Vessel Position Map */} {showMap && (
{/* Map Header */}

{t('map.title')}

{t('map.subtitle')}

{/* Map Container */}
{isMapLoading && (

{t('map.loading')}

{t('map.connecting')}

)}