import React, { useState, useMemo } from 'react'; import { NavigationScreen, TruckProfile, DriverProfile, LogisticsHub, } from './types/fleet'; import { INITIAL_TRUCK, INITIAL_DRIVER, LOGISTICS_HUBS, calculateDynamicRoutes, } from './data/fleetData'; import { Sidebar } from './components/Sidebar'; import { Header } from './components/Header'; import { LoginScreen } from './components/screens/LoginScreen'; import { BerandaScreen } from './components/screens/BerandaScreen'; import { BandingkanRuteScreen } from './components/screens/BandingkanRuteScreen'; import { DetailRisikoScreen } from './components/screens/DetailRisikoScreen'; import { ProfilTrukScreen } from './components/screens/ProfilTrukScreen'; import { PetaOfflineScreen } from './components/screens/PetaOfflineScreen'; import { PengaturanScreen } from './components/screens/PengaturanScreen'; import { NavigasiAktifScreen } from './components/screens/NavigasiAktifScreen'; import { SuratJalanModal } from './components/modals/SuratJalanModal'; import { OtpModal } from './components/modals/OtpModal'; import { DriverProfileModal } from './components/modals/DriverProfileModal'; import { OnboardingGuideModal } from './components/modals/OnboardingGuideModal'; import { CustomLocationModal } from './components/modals/CustomLocationModal'; import { createCustomHub } from './data/indonesiaLocations'; export default function App() { const [isAuthenticated, setIsAuthenticated] = useState(true); const [currentScreen, setCurrentScreen] = useState('beranda'); const [driver, setDriver] = useState(INITIAL_DRIVER); const [truck, setTruck] = useState(INITIAL_TRUCK); // Dynamic origin & destination hubs const [originHub, setOriginHub] = useState( () => LOGISTICS_HUBS.find((h) => h.id === 'hub-bandung-gedebage') || LOGISTICS_HUBS[1] ); const [destinationHub, setDestinationHub] = useState( () => LOGISTICS_HUBS.find((h) => h.id === 'hub-cirebon-dc04') || LOGISTICS_HUBS[0] ); const [selectedRouteId, setSelectedRouteId] = useState('rute-tol-utama'); const [searchQuery, setSearchQuery] = useState(''); // Modals state const [isMobileSidebarOpen, setIsMobileSidebarOpen] = useState(false); const [isSuratJalanOpen, setIsSuratJalanOpen] = useState(false); const [isOtpModalOpen, setIsOtpModalOpen] = useState(false); const [isHelpModalOpen, setIsHelpModalOpen] = useState(false); const [isDriverProfileModalOpen, setIsDriverProfileModalOpen] = useState(false); const [isCustomLocationModalOpen, setIsCustomLocationModalOpen] = useState(false); const [customLocationTarget, setCustomLocationTarget] = useState<'origin' | 'destination'>('destination'); const [isMapPickMode, setIsMapPickMode] = useState(false); // Initial Onboarding Tutorial modal (shown on first load unless dismissed) const [isOnboardingOpen, setIsOnboardingOpen] = useState(() => { try { return localStorage.getItem('troute_onboarding_dismissed') !== 'true'; } catch { return true; } }); // Calculate dynamic routes whenever origin, destination, or truck specs change const dynamicRoutes = useMemo(() => { return calculateDynamicRoutes(originHub, destinationHub, truck); }, [originHub, destinationHub, truck]); const safeRoute = dynamicRoutes[0]; const altRoute = dynamicRoutes[1]; // Hub switching handlers const handleSelectOriginHub = (hub: LogisticsHub) => { setOriginHub(hub); setTruck((prev) => ({ ...prev, originHubId: hub.id, origin: `${hub.name} (${hub.city})`, })); }; const handleSelectDestinationHub = (hub: LogisticsHub) => { setDestinationHub(hub); setTruck((prev) => ({ ...prev, destinationHubId: hub.id, destination: `${hub.name} (${hub.city})`, })); }; const handleSwapHubs = () => { const tempOrigin = originHub; const tempDest = destinationHub; setOriginHub(tempDest); setDestinationHub(tempOrigin); setTruck((prev) => ({ ...prev, originHubId: tempDest.id, origin: `${tempDest.name} (${tempDest.city})`, destinationHubId: tempOrigin.id, destination: `${tempOrigin.name} (${tempOrigin.city})`, })); }; // Search input change handler const handleSearchChange = (query: string) => { setSearchQuery(query); if (!query) return; // Check if query matches any hub city or name const match = LOGISTICS_HUBS.find( (h) => h.name.toLowerCase().includes(query.toLowerCase()) || h.city.toLowerCase().includes(query.toLowerCase()) ); if (match && match.id !== originHub.id) { handleSelectDestinationHub(match); } }; // Handlers for custom location modal and interactive map picking const handleSelectCustomHub = (hub: LogisticsHub, targetType: 'origin' | 'destination') => { if (targetType === 'origin') { handleSelectOriginHub(hub); } else { handleSelectDestinationHub(hub); } }; const handleActivateMapPickMode = (targetType: 'origin' | 'destination') => { setCustomLocationTarget(targetType); setIsMapPickMode(true); setIsCustomLocationModalOpen(false); setCurrentScreen('beranda'); }; const handleMapPickLocation = (coords: [number, number], target: 'origin' | 'destination') => { const latStr = coords[0].toFixed(3); const lngStr = coords[1].toFixed(3); const newHub = createCustomHub( `Titik Peta (${latStr}, ${lngStr})`, 'Koordinat Pilihan Pengemudi', coords ); if (target === 'origin') { handleSelectOriginHub(newHub); } else { handleSelectDestinationHub(newHub); } setIsMapPickMode(false); }; // Handle logout / exit cabin const handleLogout = () => { setIsAuthenticated(false); setCurrentScreen('login'); }; // Login Screen render if (!isAuthenticated || currentScreen === 'login') { return (
{ setDriver(updatedDriver); setIsAuthenticated(true); setCurrentScreen('beranda'); }} onSkipLogin={() => { setIsAuthenticated(true); setCurrentScreen('beranda'); }} onOpenTutorial={() => setIsOnboardingOpen(true)} onOpenOtpModal={() => setIsOtpModalOpen(true)} onOpenHelpModal={() => setIsHelpModalOpen(true)} /> setIsOtpModalOpen(false)} onVerifySuccess={() => { setIsOtpModalOpen(false); setIsAuthenticated(true); setCurrentScreen('beranda'); }} /> {/* Dispatch Hub Help Modal */} {isHelpModalOpen && (
headset_mic

Bantuan Dispatch Hub

Pusat Komando Operasional Logistik Jalur Jawa - Bali - Sumatra 24 Jam.

Hotline Darurat Tol & Kerusakan: 0800-1-TROUTE (876883) Radio Komunikasi Rig Kabin: CH-14 (142.850 MHz)
)}
); } return (
{/* Desktop Sidebar Dock */} setCurrentScreen(screen)} truck={truck} isOpenMobile={isMobileSidebarOpen} onCloseMobile={() => setIsMobileSidebarOpen(false)} onLogout={handleLogout} onOpenTutorial={() => setIsOnboardingOpen(true)} /> {/* Main App Layout */}
{/* Cockpit Top Header */}
setIsMobileSidebarOpen(true)} onOpenSuratJalan={() => setIsSuratJalanOpen(true)} onOpenDriverProfileModal={() => setIsDriverProfileModalOpen(true)} onLogout={handleLogout} /> {/* Content Viewport */}
{currentScreen === 'beranda' && ( setIsDriverProfileModalOpen(true)} onNavigateToCompare={() => setCurrentScreen('bandingkan-rute')} onNavigateToRisk={() => setCurrentScreen('detail-risiko')} onNavigateToTruckProfile={() => setCurrentScreen('profil-truk')} onNavigateToOfflineMaps={() => setCurrentScreen('peta-offline')} onOpenSuratJalan={() => setIsSuratJalanOpen(true)} onStartNavigation={() => setCurrentScreen('navigasi-aktif')} onOpenCustomLocationModal={(targetType) => { setCustomLocationTarget(targetType); setIsCustomLocationModalOpen(true); }} onOpenTutorial={() => setIsOnboardingOpen(true)} isMapPickMode={isMapPickMode} mapPickTarget={customLocationTarget} onMapPickLocation={handleMapPickLocation} onCancelMapPickMode={() => setIsMapPickMode(false)} onActivateMapPickMode={handleActivateMapPickMode} /> )} {currentScreen === 'bandingkan-rute' && ( setCurrentScreen('detail-risiko')} onStartNavigation={() => setCurrentScreen('navigasi-aktif')} onBackToHome={() => setCurrentScreen('beranda')} /> )} {currentScreen === 'detail-risiko' && ( setCurrentScreen('bandingkan-rute')} onStartNavigation={() => setCurrentScreen('navigasi-aktif')} onOpenSuratJalan={() => setIsSuratJalanOpen(true)} /> )} {currentScreen === 'profil-truk' && ( setTruck(updated)} onBackToHome={() => setCurrentScreen('beranda')} /> )} {currentScreen === 'peta-offline' && ( setCurrentScreen('beranda')} /> )} {currentScreen === 'pengaturan' && ( setCurrentScreen('beranda')} /> )} {currentScreen === 'navigasi-aktif' && ( setCurrentScreen('bandingkan-rute')} onOpenSuratJalan={() => setIsSuratJalanOpen(true)} /> )}
{/* Global Modals */} setIsSuratJalanOpen(false)} truck={truck} driver={driver} originHub={originHub} destinationHub={destinationHub} /> setIsDriverProfileModalOpen(false)} driver={driver} onSaveDriver={(updatedDriver) => setDriver(updatedDriver)} /> {/* Onboarding / Tutorial Guide Modal */} { setIsOnboardingOpen(false); if (dontShowAgain) { try { localStorage.setItem('troute_onboarding_dismissed', 'true'); } catch { // ignore } } }} onStartNavigationSample={() => { setIsOnboardingOpen(false); setCurrentScreen('bandingkan-rute'); }} /> {/* Custom Location / Lainnya Search & Map Pick Modal */} setIsCustomLocationModalOpen(false)} targetType={customLocationTarget} currentHub={customLocationTarget === 'origin' ? originHub : destinationHub} onSelectCustomHub={handleSelectCustomHub} onActivateMapPickMode={handleActivateMapPickMode} />
);