perf(planner): tabs that open ready instead of building in front of the user

Two seconds after a trip opens, the code of every tab panel loads in the background, and the packing list's bags with it, which are kept per trip so Lists opens with its bag sidebar in place. The plan and tour planner maps stay mounted (hidden) once opened, so switching between them no longer starts a map again and flickers. Not on a narrow screen, where the plan tab portals its own controls over the page.
This commit is contained in:
Maurice
2026-11-29 10:04:15 +01:00
parent 64697fdb7f
commit 467287f138
6 changed files with 94 additions and 10 deletions
@@ -13,6 +13,20 @@ interface Toaster {
error: (message: string) => void;
}
/**
* The last bags read per trip in this tab. A list opened again, or opened after
* the trip read them ahead (warmPackingBags), starts with them instead of
* drawing the list first and the bag sidebar a moment later; the read that
* follows only refreshes them.
*/
const keptBags = new Map<number, { bags: PackingBag[]; unassigned: number | null }>();
/** Reads a trip's bags ahead, while it sits open, for the list to start with. */
export async function warmPackingBags(tripId: number): Promise<void> {
const r = await packingApi.listBags(tripId);
keptBags.set(tripId, { bags: r.bags || [], unassigned: r.unassigned_weight_grams ?? null });
}
/**
* The trip's bags behind both packing lists: the bags with their server-summed
* weights, kept fresh by the room's totals ping, and the bag writes. The desktop
@@ -29,9 +43,16 @@ export function usePackingBags({
t: Translate;
toast: Toaster;
}) {
const [bags, setBags] = useState<PackingBag[]>([]);
const [bags, setBags] = useState<PackingBag[]>(() => keptBags.get(tripId)?.bags ?? []);
/** Server-summed weight of everything in no bag (#2191); null until the first load. */
const [unassignedWeightGrams, setUnassignedWeightGrams] = useState<number | null>(null);
const [unassignedWeightGrams, setUnassignedWeightGrams] = useState<number | null>(
() => keptBags.get(tripId)?.unassigned ?? null
);
// What the list shows now is what it starts with next time.
useEffect(() => {
keptBags.set(tripId, { bags, unassigned: unassignedWeightGrams });
}, [tripId, bags, unassignedWeightGrams]);
const reloadBags = useCallback(async () => {
if (!bagTrackingEnabled) return;