/** * @license * SPDX-License-Identifier: Apache-2.0 */ import React, { useState, useMemo } from 'react'; import { Route, ShoppingBag, Truck, MapPin, Calendar, ChevronLeft, ChevronRight, Store, Users } from 'lucide-react'; import DispatchView from './DispatchView'; import OrdersView from './OrdersView'; import DeliveriesView from './DeliveriesView'; import { useFiestaDeliveries, useFiestaTenantLocations } from '../services/fiestaQueries'; import { FIESTA_TENANT_ID, ymd, num as fnum, str as fstr } from '../services/fiestaApi'; import type { Row } from '../services/fiestaApi'; import './DispatchView.css'; // For #hdr and date-chip styles interface DispatchHubViewProps { locationid?: number; tenantId?: number; } const WEEKDAYS = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; export default function DispatchHubView({ locationid, tenantId = FIESTA_TENANT_ID }: DispatchHubViewProps) { const [activeTab, setActiveTab] = useState<'map' | 'orders' | 'deliveries'>('map'); const [mapViewMode, setMapViewMode] = useState<'stores' | 'customers'>('stores'); const today = new Date(); const [date, setDate] = useState(ymd(today)); // Fetch today's deliveries just for the global Hub header live stats const deliveriesQ = useFiestaDeliveries({ tenantid: tenantId, fromdate: date, todate: date, locationid }); const allRows = deliveriesQ.data ?? []; const inScope = (r: Row) => !locationid || fnum(r.locationid) === locationid; const rows = useMemo(() => allRows.filter(inScope), [allRows, locationid]); const totalOrders = rows.length; // Operating area for the header pill. This was the hardcoded string // "Coimbatore", shown to every tenant on the platform regardless of where they // actually trade. The locations query is already cached by DispatchView (same // key), so reading it here costs no extra request. const locationsQ = useFiestaTenantLocations(tenantId); const scopeLabel = useMemo(() => { const locs = locationsQ.data ?? []; if (locs.length === 0) return ''; if (locationid) { // Store user — name the outlet's own city. const mine = locs.find((l) => fnum(l.locationid) === locationid); return fstr(mine?.city).trim() || fstr(mine?.state).trim(); } // Admin — one city if the whole tenant sits in one, otherwise a count. const cities = [...new Set(locs.map((l) => fstr(l.city).trim()).filter(Boolean))]; if (cities.length === 1) return cities[0]; if (cities.length > 1) return `${cities.length} cities`; return ''; }, [locationsQ.data, locationid]); const isToday = date === ymd(today); const dateObj = new Date(`${date}T00:00:00`); const prettyDate = `${WEEKDAYS[dateObj.getDay()]}, ${dateObj.getDate()} ${MONTHS[dateObj.getMonth()]}`; const shiftDate = (delta: number) => { const d = new Date(`${date}T00:00:00`); d.setDate(d.getDate() + delta); if (d > today) return; setDate(ymd(d)); }; return (
{/* ── Unified Hub Header ── */}
C
Console
{scopeLabel && (
{scopeLabel}
)}
{deliveriesQ.isLoading ? ( Syncing ) : deliveriesQ.isError ? ( Offline ) : totalOrders === 0 ? ( No deliveries today ) : ( Live · {totalOrders} {totalOrders === 1 ? 'delivery' : 'deliveries'} )}
Date {isToday && TODAY} {prettyDate}
setDate(e.target.value)} className="absolute inset-0 opacity-0 cursor-pointer w-full h-full" aria-label="Pick date" />
{/* ── Unified Tab Row ── */}
{/* Sub-tabs for Map View — only meaningful on the Map tab; they used to stay visible (and inert) on Orders and Deliveries. */} {activeTab === 'map' && (
)}
{/* Content Area */}
{activeTab === 'map' && } {activeTab === 'orders' && } {activeTab === 'deliveries' && }
); }