|
| 1 | +# Implementation Plan: Trip Reconstruction (retrospective journeys) |
| 2 | + |
| 3 | +**Branch**: `019-trip-reconstruction` (developed on `claude/journal-redesign-spec-kit-ku0l3d`) | **Date**: 2026-07-21 | **Spec**: [spec.md](./spec.md) |
| 4 | + |
| 5 | +**Input**: Feature specification from `/specs/019-trip-reconstruction/spec.md` |
| 6 | + |
| 7 | +## Summary |
| 8 | + |
| 9 | +Evolve the Travel screen's trip log so a user can **reconstruct a past journey as an ordered |
| 10 | +chain of stops** (airports + cities), see its **total great-circle distance** live, and have it |
| 11 | +count toward the Travel totals — a fast, approximate "here's what I did," never a planner. The |
| 12 | +existing single-leg `Trip` gains an **additive, optional `stops` array** (first/last stop mirror |
| 13 | +the existing required `from`/`to`, so old app builds still read new trips as a simple leg), plus a |
| 14 | +looser **approximate date** (month or year). A dedicated **composer page layer** hosts the builder |
| 15 | +(add/remove/reorder stops, name, rough date, running km). Separately, fix the **Travel back bug**: |
| 16 | +tapping an airport in the roll-up jumps to the map and opens a popup card, so the first Back only |
| 17 | +dismisses the card and strands the user on the map — re-route the row to the airport's **detail |
| 18 | +page layer** (row → details, per the app's own rule) so a single Back returns to the Travel list. |
| 19 | +Reuses the existing `haversineKm`/`coordsOf` helpers, `searchPlaces`/`PlacePicker`, `useTrips` |
| 20 | +store + `tripsDb`, and the `cityPageId`/`countryPageId` page-layer + `history`/`goBack` model. |
| 21 | + |
| 22 | +## Technical Context |
| 23 | + |
| 24 | +**Language/Version**: TypeScript 5 + React 19 (Vite), PWA + Capacitor (iOS/Android). |
| 25 | + |
| 26 | +**Primary Dependencies**: Zustand (stores), Zod (schema → JSON Schema), MapLibre GL (map only — |
| 27 | +not used for distance), existing `features/travel/distance.ts` (`haversineKm`, `coordsOf`, |
| 28 | +`travelTotals`, `tripArcs`). No new runtime dependency. |
| 29 | + |
| 30 | +**Storage**: IndexedDB working store (`lib/db/tripsDb`, via `useTrips`); canonical portable |
| 31 | +**JSON** file validated by `lib/schema/models.ts` (`PostcardsFileSchema`, current |
| 32 | +`SCHEMA_VERSION = 10`). Trips already persist in the `trips` array (additive since schema v2). |
| 33 | + |
| 34 | +**Testing**: Vitest (pure logic: distance, date parsing, stops validation, schema round-trip) + |
| 35 | +Playwright/axe (composer flow, offline zero-egress, back-navigation, WCAG 2.1 AA). Existing gate: |
| 36 | +`pnpm typecheck && pnpm test && pnpm test:e2e`. |
| 37 | + |
| 38 | +**Target Platform**: Web (self-hostable PWA) + native iOS/Android via Capacitor. Offline-first. |
| 39 | + |
| 40 | +**Project Type**: Single web app (`apps/postcards/`), feature-foldered. |
| 41 | + |
| 42 | +**Performance Goals**: Composer interactions feel instantaneous; distance recompute on |
| 43 | +add/remove/reorder is O(stops) haversine calls — trivially <16ms for realistic trips. |
| 44 | + |
| 45 | +**Constraints**: Fully offline; zero network egress; no maps/routing/geocoding service (distance |
| 46 | +is pure haversine on-device); aggregator-only (no invented reference data); additive schema |
| 47 | +(older files + existing single-leg trips stay valid); WCAG 2.1 AA + keyboard-first; i18n parity |
| 48 | +en/fr/ko enforced at compile time. |
| 49 | + |
| 50 | +**Scale/Scope**: One new page-layer composer + a store/schema evolution + a small nav fix. Trips |
| 51 | +per file bounded by the existing `.max(200_000)`; stops per trip bounded (e.g. ≤200). |
| 52 | + |
| 53 | +## Constitution Check |
| 54 | + |
| 55 | +*GATE: must pass before Phase 0 and re-checked after Phase 1 design.* |
| 56 | + |
| 57 | +| Principle | Assessment | |
| 58 | +|-----------|------------| |
| 59 | +| **I. Aggregator, never an author** | ✅ Stops reference existing reference places (airports/cities) resolved from named datasets; the app invents no coordinates or place facts. Railway stations are **deferred** precisely because no openly-licensed dataset ships — they will arrive as a separate shareable dataset, never hard-wired. | |
| 60 | +| **II. Local-first & decentralized** | ✅ No server, account, or network. Trips live on-device (IndexedDB) + the portable file. | |
| 61 | +| **III. Privacy by default** | ✅ No telemetry, no egress. Trips leave the device only via the existing explicit export. Distance is computed locally. | |
| 62 | +| **IV. One portable, human-readable file** | ✅ Trips stay in the single JSON file; the multi-stop shape is **additive + versioned** (schema bump), export/re-import lossless, Markdown export unaffected. | |
| 63 | +| **V. Zero lock-in** | ✅ No new proprietary/cloud dependency; no new runtime dep at all. Great-circle math is local. | |
| 64 | +| **VI. Security — data is inert** | ✅ Stops are inert `PlaceRef`s validated by Zod on import; no executable content; sanitized name/date; strict schema rejects unknown fields. | |
| 65 | +| **VII. Efficient, accessible, keyboard-first** | ✅ Composer is keyboard-operable (add/remove/**reorder via buttons**, not drag-only), WCAG 2.1 AA, tooltips/aria on controls, reduced-motion respected; the nav fix removes a dead-end. | |
| 66 | +| **VIII. Interoperable & AI-friendly** | ✅ Additive versioned schema; new `stops` documented; old readers still parse new trips as a `from→to` leg (graceful degradation). | |
| 67 | +| **Scope discipline (NON-NEGOTIABLE)** | ✅ **This is retrospective reconstruction — remembering a journey already taken — NOT trip planning.** No routing, no future itineraries, no booking, no suggestions of where to go. It records "what I did + how far," squarely inside "store data and display it well." Framed and named to avoid any planner drift. | |
| 68 | + |
| 69 | +**Gate result: PASS.** No violations; Complexity Tracking not required. |
| 70 | + |
| 71 | +## Project Structure |
| 72 | + |
| 73 | +### Documentation (this feature) |
| 74 | + |
| 75 | +```text |
| 76 | +specs/019-trip-reconstruction/ |
| 77 | +├── plan.md # This file |
| 78 | +├── research.md # Phase 0 — decisions (schema shape, date encoding, nav fix, stations) |
| 79 | +├── data-model.md # Phase 1 — Trip.stops, approximate date, derived distance, versioning |
| 80 | +├── contracts/ |
| 81 | +│ └── trip-reconstruction.md # Phase 1 — pure fns + store + nav contract |
| 82 | +├── quickstart.md # Phase 1 — how to validate end-to-end |
| 83 | +├── checklists/ |
| 84 | +│ └── requirements.md # (from /speckit-specify) |
| 85 | +└── tasks.md # Phase 2 — /speckit-tasks (NOT created here) |
| 86 | +``` |
| 87 | + |
| 88 | +### Source Code (repository root) |
| 89 | + |
| 90 | +```text |
| 91 | +apps/postcards/ |
| 92 | +├── src/ |
| 93 | +│ ├── features/ |
| 94 | +│ │ └── travel/ |
| 95 | +│ │ ├── TravelScreen.tsx # + entry point to the composer; roll-up row → details (nav fix) |
| 96 | +│ │ ├── TripComposer.tsx # NEW — full-page multi-stop builder (page layer) |
| 97 | +│ │ ├── TripForm.tsx # existing single-leg add/edit (kept; may share PlacePicker) |
| 98 | +│ │ ├── PlacePicker.tsx # reused for each stop (aggregator-only search) |
| 99 | +│ │ ├── distance.ts # + tripPathKm(stops, ref); travelTotals honors stops |
| 100 | +│ │ ├── tripDate.ts # NEW — parse/format/compare approximate dates (YYYY | YYYY-MM | YYYY-MM-DD) |
| 101 | +│ │ └── tripStops.ts # NEW — pure helpers: reorder/add/remove, endpoints, validation |
| 102 | +│ │ └── map/ |
| 103 | +│ │ └── MapScreen.tsx / MapView.tsx # optional: tripArcs draws each leg (nice-to-have) |
| 104 | +│ ├── lib/ |
| 105 | +│ │ ├── schema/models.ts # TripSchema += optional stops[]; date regex widened; SCHEMA_VERSION → 11 |
| 106 | +│ │ ├── schema/helpers.ts # SCHEMA_VERSION bump + changelog entry |
| 107 | +│ │ ├── store/useTrips.ts # unchanged shape (stores richer Trip); no API break |
| 108 | +│ │ ├── store/useUi.ts # + tripEditId page-layer state (open/close, pushes history) |
| 109 | +│ │ └── i18n/{en,fr,ko}.ts # new composer/date/nav strings, parity |
| 110 | +│ └── styles.css # composer + stop-row styles (≥44px targets) |
| 111 | +└── tests/ |
| 112 | + ├── unit/ |
| 113 | + │ ├── tripPathKm.spec.ts # NEW — multi-stop distance, unresolved-stop skip |
| 114 | + │ ├── tripDate.spec.ts # NEW — approximate date parse/format/sort |
| 115 | + │ ├── tripStops.spec.ts # NEW — reorder/add/remove/endpoints |
| 116 | + │ └── schema round-trip # extend: new trip validates; old file still valid |
| 117 | + └── e2e/ |
| 118 | + └── trip-reconstruction.spec.ts # NEW — build multi-stop trip, live km, back-from-airport, offline, a11y |
| 119 | +``` |
| 120 | + |
| 121 | +**Structure Decision**: Single web app, feature-foldered under `apps/postcards/src/features/travel/`. |
| 122 | +The composer is a **page layer** (new `useUi.tripEditId`) rendered over the Trips tab in |
| 123 | +`App.tsx`, mirroring `cityPageId`/`countryPageId` so Back/Escape/`goBack` behavior is inherited and |
| 124 | +consistent — this both delivers the "dedicated page" and underpins the US3 navigation fix. |
| 125 | + |
| 126 | +## Phase notes |
| 127 | + |
| 128 | +- **Phase 0 (research.md)** resolves: (a) multi-stop schema shape — additive `stops[]` with |
| 129 | + `from`/`to` mirrored to first/last for backward-compat vs. a new entity (chosen: additive |
| 130 | + `stops[]`); (b) approximate-date encoding (widen the existing `date` string to accept |
| 131 | + `YYYY`/`YYYY-MM`/`YYYY-MM-DD`); (c) distance model (great-circle sum, unresolved-stop skip); |
| 132 | + (d) the US3 nav-fix approach (row → airport **detail page layer**, not the map popup) and how |
| 133 | + the airport detail page is reached (`openCity` with the IATA id / the intra-city-places path |
| 134 | + from feature 014) — verified during implementation; (e) confirming **no station dataset** exists |
| 135 | + and the deferral is documented; (f) no new dependencies. |
| 136 | +- **Phase 1** emits `data-model.md` (the `Trip.stops` shape, approximate date, derived distance, |
| 137 | + `SCHEMA_VERSION` bump + backward-compat rules), `contracts/trip-reconstruction.md` (the pure |
| 138 | + functions `tripPathKm`/`tripStops`/`tripDate`, the `useUi.tripEditId` nav contract, and the |
| 139 | + composer↔store contract), and `quickstart.md` (end-to-end validation incl. offline + back-nav). |
| 140 | + |
| 141 | +## Complexity Tracking |
| 142 | + |
| 143 | +No constitutional violations; no deviations from ratified tech defaults. Section intentionally empty. |
0 commit comments