loopable / web /src /apps /routing /RouteApplicationMapView.tsx
fsanyoto's picture
Deploy AIOS web (React glide grid + FastAPI slice)
82f0f29 verified
Raw History Blame
5.31 kB
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<MapViewProps["field"]> }; titleKey: string; applicationId: string;
Surface: typeof MapViewSurface;
}) {
const [customerPid, setCustomerPid] = useState<number | null>(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<FilterNode[]>([]);
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 <RoutingStopLabelErrors.Provider value={selected.errors}>
<Surface {...props} rows={data.rows} allRows={allRows}
recordsPending={data.loading || selected.loading || !!selected.error}
applicationId={applicationId} onOpen={openCustomer} customerDetail={customerDetail}
onCustomerBack={() => setCustomerPid(null)} routingFilters={<>
{data.loading && <style>{`
.cg-mapview.cg-app-route-surface .cg-routing-customer-loading,
.cg-mapview.cg-app-route-surface .cg-toolbar > .cg-page-count {
font-family: Inter, system-ui, sans-serif;
font-size: var(--lp-fs-2xs);
font-weight: normal;
line-height: var(--lp-lh);
letter-spacing: normal;
color: var(--lp-muted);
}
`}</style>}
{props.routingFilters}
<form className="cg-catalog-record-head" aria-label="Customer search" style={{ flexWrap: "wrap", margin: 0 }} onSubmit={(event) => {
event.preventDefault(); setCustomerPid(null); data.setSearch(search.trim());
}}>
<input className="cg-input" type="search" aria-label="Search customers" placeholder="Search customers"
value={search} onChange={(event) => setSearch(event.target.value)} />
<button className="cg-btn" type="submit" disabled={data.loading && search.trim() === data.search}>Search</button>
<span className={data.loading ? "cg-routing-customer-loading" : undefined}
role="status" aria-live="polite" aria-label={selectedPending ? "Loading selected customers" : undefined}>
{data.loading ? "Loading customers" : selectedPending ? "Loading stops" : data.error ? ""
: `${data.rows.length.toLocaleString()} ${data.rows.length === 1 ? "customer" : "customers"}`}
</span>
</form>
{data.error && <div className="cg-catalog-library-error" role="alert"><span>{data.error}</span>
<button className="cg-btn" type="button" disabled={data.loading} onClick={data.retry}>Retry</button></div>}
{(selected.error || Object.keys(selected.errors).length > 0) && <div className="cg-catalog-library-error" role="alert">
<span>{selected.error || "Some stops could not be loaded."}</span>
<button className="cg-btn" type="button" disabled={selected.loading} onClick={selected.retry}>Retry selected</button></div>}
</>} /></RoutingStopLabelErrors.Provider>;
}