ptitlutins/app/utils/eventToNode.ts
2026-06-15 23:34:49 +02:00

26 lines
754 B
TypeScript

import type { Event, Node, Trip } from "~/types"
import { byStart } from "./byStart"
/**
* Turn one event into a tree node, recursing into its children.
*
* The node references the event (no field copy). `byParent` resolves children
* from the flat list; `tripByStart` folds in the transport to the next sibling.
*/
export const eventToNode = (
event: Event,
byParent: Map<number | null, Event[]>,
tripByStart: Map<number, Trip>,
): Node => {
const children = (byParent.get(event.id) ?? [])
.slice()
.sort(byStart)
.map((child) => eventToNode(child, byParent, tripByStart))
const trip = tripByStart.get(event.id)
return {
id: String(event.id),
event,
trip: trip ? { mode: trip.mode } : null,
children,
}
}