The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.
TypeScript budget-trackercollaborativedockeritinerarymcp-serveroffline-firstopen-sourceopenstreetmappacking-listpwareal-timeself-hostedselfhostedtraveltravel-apptravel-journaltravel-plannertripitwanderlog
/**
* Turning a day's reordered timeline back into the writes that store it, shared by
* the desktop day plan and the phone's plan timeline. Places get sequential
* integer positions (0, 1, 2, ...); the notes and transports between place N-1 and
* place N get fractional positions between the two.
*/
/** A timeline row as the reorder reads it: a place, a note or a transport (one leg of it, for a multi-leg booking). */
export interface MergedOrderItem {
type: string;
data: { id: number; __leg?: { index: number } | null };
}
export interface MergedOrderPlan {
/** The day's assignments in their new order. */
assignmentIds: number[];
noteUpdates: { id: number; sort_order: number }[];
transportUpdates: { id: number; day_plan_position: number }[];
/**
* Multi-leg flight legs share a reservation id, so their positions cannot live in
* the single per-booking slot: reservation id, then leg index, then position.
*/
legPosUpdates: Record<number, Record<number, number>>;
}
/**
* The day order with the row at `from` taken out and put back in front of the first
* row `isTarget` picks, or behind it with `after`; at the end when no row is picked.
* `index` is where the row landed.
*/
export function moveRow<T>(
order: readonly T[],
from: number,
isTarget: (row: T) => boolean,
after = false
): { order: T[]; index: number } {
const moved = [...order];
const [row] = moved.splice(from, 1);
const target = moved.findIndex(isTarget);
const index = (target === -1 ? moved.length : target) + (after ? 1 : 0);
moved.splice(index, 0, row);
return { order: moved, index };
}
/**
* The day's full stored order once the rows on screen take the order given: a row the
* list does not show keeps its slot, and the shown rows fill the remaining slots in
* their new order.
*/
export function withHiddenRows(storedIds: readonly number[], visibleIds: readonly number[]): number[] {
const visible = new Set(visibleIds);
let next = 0;
return storedIds.map((id) => (visible.has(id) ? visibleIds[next++] : id));
}
/**
* `storedIds` is the day's full stored order when the timeline leaves rows out (the
* desktop list hides the stop a lodging booking puts on the day). The places are stored
* across that full day, so a booking or note has to be positioned in the same count, or
* every hidden row ahead of a place pushed that place one slot behind the booking it was
* dropped above (#3100). Without it the timeline is taken to show the whole day.
*/
export function planMergedOrder(newOrder: readonly MergedOrderItem[], storedIds?: readonly number[]): MergedOrderPlan {
const plan: MergedOrderPlan = { assignmentIds: [], noteUpdates: [], transportUpdates: [], legPosUpdates: {} };
for (const item of newOrder) if (item.type === 'place') plan.assignmentIds.push(item.data.id);
const full = storedIds ? withHiddenRows(storedIds, plan.assignmentIds) : plan.assignmentIds;
const slotOf = (id: number): number => {
const at = full.indexOf(id);
return at === -1 ? plan.assignmentIds.indexOf(id) : at;
};
let lastPlaceId: number | null = null;
let i = 0;
while (i < newOrder.length) {
if (newOrder[i].type === 'place') {
lastPlaceId = newOrder[i].data.id;
i++;
continue;
}
// Consecutive non-place items share the gap after the place before them, or the
// gap ahead of the first place when they open the day.
const group: MergedOrderItem[] = [];
while (i < newOrder.length && newOrder[i].type !== 'place') {
group.push(newOrder[i]);
i++;
}
const nextPlace = newOrder.slice(i).find((it) => it.type === 'place');
const base = lastPlaceId !== null ? slotOf(lastPlaceId) : nextPlace ? slotOf(nextPlace.data.id) - 1 : -1;
group.forEach((g, idx) => {
const pos = base + (idx + 1) / (group.length + 1);
if (g.type === 'note') plan.noteUpdates.push({ id: g.data.id, sort_order: pos });
else if (g.type === 'transport') {
if (g.data.__leg) (plan.legPosUpdates[g.data.id] ??= {})[g.data.__leg.index] = pos;
else plan.transportUpdates.push({ id: g.data.id, day_plan_position: pos });
}
});
}
return plan;
}
interface PositionedBooking {
id: number;
day_plan_position?: number | null;
day_positions?: Record<string, number> | null;
}
/** The bookings with the new positions written in, for the day they were moved on. */
export function withTransportPositions<R extends PositionedBooking>(
reservations: R[],
updates: MergedOrderPlan['transportUpdates'],
dayId: number
): R[] {
return reservations.map((r) => {
const tu = updates.find((u) => u.id === r.id);
if (!tu) return r;
return {
...r,
day_plan_position: tu.day_plan_position,
day_positions: { ...(r.day_positions || {}), [dayId]: tu.day_plan_position },
};
});
}
/**
* A booking as an edit or a collaborator's event hands it back, with the day slots of
* the copy it replaces when it came without any. A server that did not send them yet
* put a moved booking back by its time on every edit until the next reload (#2631).
*/
export function keepDayPositions<R extends PositionedBooking>(next: R, prev: R | undefined): R {
if (!prev || next.day_positions !== undefined) return next;
return { ...next, day_positions: prev.day_positions };
}
/**
* A booking's metadata with each moved leg's position for the day written into
* metadata.legs[i].day_positions. Null when the metadata holds no legs to move.
* Metadata that cannot be parsed counts as empty.
*/
export function metadataWithLegPositions(
metadata: unknown,
positions: Record<number, number>,
dayId: number
): Record<string, unknown> | null {
let parsed: Record<string, unknown>;
try {
parsed = typeof metadata === 'string' ? JSON.parse(metadata || '{}') : (metadata as Record<string, unknown>) || {};
} catch {
parsed = {};
}
if (!Array.isArray(parsed.legs)) return null;
const legs = (parsed.legs as Record<string, unknown>[]).map((leg, li) => {
const pos = positions[li];
return pos == null
? leg
: { ...leg, day_positions: { ...((leg.day_positions as Record<string, number>) || {}), [dayId]: pos } };
});
return { ...parsed, legs };
}