import { useCallback, useMemo, useState } from "preact/hooks"; import { twMerge } from "tailwind-merge"; import RouteIcon from "../RouteIcon"; import ShadowOverflow from "../ShadowOverflow"; import { LAYERS_NAMES, LNP_LINE_ID_PROP } from "../utils/constants"; import useLayerConfig from "../utils/hooks/useLayerConfig"; import { useLnpLinesInfos, useLnpStopsInfos } from "../utils/hooks/useLnp"; import useMapContext from "../utils/hooks/useMapContext"; import type { RealtimeLine } from "mobility-toolbox-js/types"; import type { Feature } from "ol"; import type { PreactDOMAttributes } from "preact"; import type { LnpLineInfo } from "../utils/hooks/useLnp"; const RUNS_PROP = "runs"; function LinesNetworkPlanDetails({ className, features, ...props }: { className?: string; features: Feature[] } & PreactDOMAttributes) { const { linesIds } = useMapContext(); const lineInfos = useLnpLinesInfos(); const stopInfos = useLnpStopsInfos(); const layerConfig = useLayerConfig(LAYERS_NAMES.linesnetworkplan); // eslint-disable-next-line @typescript-eslint/no-unused-vars const [stopInfosOpenId, setStopInfosOpenId] = useState(null); const isRunsDisplay = useMemo(() => { return ( new URLSearchParams(window.location.search).get(RUNS_PROP) === "true" ); }, []); const getLink = useCallback( (id: string) => { const href = layerConfig?.link?.href; if (href) { return href.replace(`{{id}}`, id); } return null; }, [layerConfig], ); const lineInfosByOperator: Record = useMemo(() => { const byOperators = {}; [ ...new Set([ ...(linesIds || []), ...features.map((f) => { return f.get(LNP_LINE_ID_PROP); }), ]), ] .filter((id) => { return !!id && !!lineInfos?.[id]; }) .forEach((id) => { const { operator_name: operatorName } = lineInfos[id]; if (!byOperators[operatorName]) { byOperators[operatorName] = []; byOperators[operatorName].runs = 0; } lineInfos[id].id = id; const runs = features ?.filter((f) => { return f.get(LNP_LINE_ID_PROP) === id; }) .reduce((acc, featuree) => { return acc + featuree.get(RUNS_PROP); }, 0); lineInfos[id].id = id; lineInfos[id].runs = runs; byOperators[operatorName].runs += runs; byOperators[operatorName].push(lineInfos[id]); }); return byOperators; }, [features, lineInfos, linesIds]); // eslint-disable-next-line @typescript-eslint/no-unused-vars const stopInfoIdsByLineId: Record = useMemo(() => { const byLineId = {}; features.forEach((f) => { const lineId = f.get(LNP_LINE_ID_PROP); if (lineId && !byLineId[lineId] && f.get("stop_ids")) { try { byLineId[lineId] = JSON.parse(f.get("stop_ids")); } catch (e) { // eslint-disable-next-line no-console console.warn("Impossible to parse stop_ids", e); } } }); return byLineId; }, [features]); if (!features?.length && !linesIds?.length) { return null; } return (
{Object.entries(lineInfosByOperator) .sort(([operatorNameA], [operatorNameB]) => { return lineInfosByOperator[operatorNameA][RUNS_PROP] < lineInfosByOperator[operatorNameB][RUNS_PROP] ? 1 : -1; }) .map(([operatorName, linesInfos]) => { return (
{operatorName}
{linesInfos .sort((a, b) => { return a.runs < b.runs ? 1 : -1; }) .map((lineInfo) => { const { color: backgroundColor, // color, // external_id, long_name, mot, runs, short_name: shortName, text_color: textColor, } = lineInfo; let longName = long_name || shortName; let stops = null; //stopInfoIdsByLineId?.[id] || null; if (!stops?.length) { stops = null; } if (!longName && stops) { const names = stops.map((stopId) => { return stopInfos[stopId].short_name; }); longName = [ ...new Set([names[0], names[names.length - 1]]), ].join(" - "); } // Build a line object const line: { type: string } & RealtimeLine = { color: null, id: null, name: shortName, stroke: null, text_color: null, type: mot, }; if (textColor) { line.text_color = textColor.startsWith("#") ? textColor : `#${textColor}`; } if (backgroundColor) { line.color = backgroundColor.startsWith("#") ? backgroundColor : `#${backgroundColor}`; } return (
{ // setStopInfosOpenId(stopInfosOpenId === id ? null : id); // }} >
{getLink(lineInfo.id) ? ( ) : ( )}
{!!longName && (
{longName.split("-").map((name) => { return
{name}
; })}
)} {isRunsDisplay && (
{runs}
)} {/* We deactivate the list of stopsfor now */} {/* {!!stops && ( )} */}
{/* {!!stops && (
{stops?.map((stopId, index, arr) => { const stop = stopInfos[stopId]; return (
{stop.short_name}
); })}
)} */}
); })}
); })}
); } export default LinesNetworkPlanDetails;