import { useEffect, useState } from "react"; import type { FilterNode } from "../../customer-grid/types"; import type { MapViewProps, MapViewSurface } from "../../customer-grid/MapView"; import { formatDisplay } from "../../customer-grid/cells"; import { readRoutingFilters, routingDetailFields } from "../../customer-grid/routePrefs"; import type { RoutingCustomerDetail } from "../../customer-grid/RouteNav"; import { RoutingStopLabelErrors } from "../../customer-grid/routeZoneApi"; import { useRoutingCustomers, useRoutingSelected } from "./useRoutingCustomers"; /** Routing's map view. The host passes its map drawing surface in as `Surface`, so this module imports MapView only as types. */ export function RouteApplicationMapView({ props, titleKey, applicationId, Surface }: { props: MapViewProps & { field: NonNullable }; titleKey: string; applicationId: string; Surface: typeof MapViewSurface; }) { const [customerPid, setCustomerPid] = useState(null); const data = useRoutingCustomers(props.tableKey, { filters: props.routingFilterNodes ?? props.activeView?.config.filters ?? [], filterConj: props.routingFilterConj ?? props.activeView?.config.filterConj ?? "and", sorts: props.activeView?.config.sorts ?? [], }, [titleKey]); const [search, setSearch] = useState(""); const selected = useRoutingSelected(props.tableKey, Array.from(props.selectedPids, String), true); const selectedPending = selected.loading && props.selectedPids.size > 0; const allRows = selected.rows.length ? [...new Map([...data.rows, ...selected.rows].map((row) => [row.pid, row])).values()] : data.rows; const [savedFilters, setSavedFilters] = useState([]); useEffect(() => { const controller = new AbortController(); void readRoutingFilters(applicationId, controller.signal).then((saved) => { if (!controller.signal.aborted) setSavedFilters(saved?.filters ?? []); }); return () => controller.abort(); }, [applicationId]); useEffect(() => { if (!data.loading && !data.error) props.onCatalogData?.(data.fields, data.rows); }, [data.loading, data.error, data.fields, data.rows, props.onCatalogData]); const row = customerPid == null ? undefined : allRows.find((candidate) => candidate.pid === customerPid); const customerDetail: RoutingCustomerDetail | null = row ? { pid: row.pid, name: String(row[titleKey] ?? "").trim() || `Customer ${row.pid}`, fields: routingDetailFields(props.routingFilterNodes ?? props.activeView?.config.filters ?? savedFilters, data.fields, titleKey) .map((field) => { const value = formatDisplay(field, row[field.key]).trim(); return { key: field.key, label: field.label, value: !value || /^[–—]$/.test(value) ? "Empty" : value }; }), } : null; const openCustomer = (pid: number) => { if (allRows.some((candidate) => candidate.pid === pid)) setCustomerPid(pid); }; return setCustomerPid(null)} routingFilters={<> {data.loading && } {props.routingFilters}
{ event.preventDefault(); setCustomerPid(null); data.setSearch(search.trim()); }}> setSearch(event.target.value)} /> {data.loading ? "Loading customers" : selectedPending ? "Loading stops" : data.error ? "" : `${data.rows.length.toLocaleString()} ${data.rows.length === 1 ? "customer" : "customers"}`}
{data.error &&
{data.error}
} {(selected.error || Object.keys(selected.errors).length > 0) &&
{selected.error || "Some stops could not be loaded."}
} } />
; }