Skip to content

Commit 898162b

Browse files
author
David
committed
Add Interrail as a pass choice
Interrail holders can now pick their pass in the header. Every train in the SNCF feed counts for them, since each takes a pass-holder reservation, and every result says that no open data tells whether a pass-holder seat is left. The data-refresh script now also writes public/data/interrail.json: the whole feed in a compact format that stores each service once with a bitmask of the dates it runs, so the full feed stays a small static file. Switching between a MAX pass and Interrail opens the page again on the other snapshot; the worker, the boot preload and the copy (11 locales) follow the pass. Refs #81
1 parent 3eb3509 commit 898162b

34 files changed

Lines changed: 675 additions & 40 deletions

‎.github/workflows/update-data.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
run: |
3838
git config user.name "github-actions[bot]"
3939
git config user.email "github-actions[bot]@users.noreply.github.com"
40-
git add public/data/tgvmax.json public/data/meta.json
40+
git add public/data/tgvmax.json public/data/meta.json public/data/interrail.json public/data/interrail-meta.json
4141
if git diff --cached --quiet; then
4242
echo "No changes to commit."
4343
else

‎docs/user-flows.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,17 @@ the destination is reachable, with its calendar open.
214214
from any screen. (The two overlap — a known cleanup item.) A saved trip opens its ticket
215215
modal, whose "See all dates" opens the route page; a favorite prefills the Trip tab with
216216
that route alone.
217+
- **Pass** — the header's pass picker: **MAX JEUNE** (default), **MAX SENIOR**, or
218+
**Interrail**. Both MAX passes search the MAX snapshot (trains with a free MAX seat).
219+
Interrail searches every train in the SNCF feed (`interrail.json`, see
220+
`src/data/compact.ts`): each one takes a pass-holder reservation, international stops
221+
included. No open data says whether an Interrail seat is left, so every Interrail result
222+
page opens with a notice saying so, and copy that would claim a free MAX seat reads its
223+
`<key>_ir` variant instead ("green = at least one train"). Switching between a MAX pass
224+
and Interrail opens the page again on the other snapshot, results shown straight away;
225+
switching between the two MAX passes re-runs in place. Booking still opens SNCF Connect,
226+
where pass-holder reservations are sold; "How does it work?" links Interrail's page on
227+
French reservations. A `card=interrail` link loads the Interrail snapshot at boot.
217228
- **Settings** — theme, MAX Jeune/Senior, comfortable/compact, and Low-end mode (map off +
218229
reduced motion + compact) with a one-time nudge on weak devices; language.
219230
- **Mobile** — the form is a sheet that collapses to a search bar (a long route wraps it to

‎index.html‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
<link rel="apple-touch-icon" href="icons/apple-touch-icon.png" />
3939
<link rel="alternate" type="text/plain" href="llms.txt" title="LLM instructions" />
4040
<link rel="alternate" type="application/json" href="api.json" title="MAX Finder machine API" />
41-
<link rel="preload" href="%BASE_URL%data/tgvmax.json" as="fetch" crossorigin />
4241
<script type="application/ld+json">
4342
{
4443
"@context": "https://schema.org",

‎public/api.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"to": "destination station id (in tour mode: optional finish city; = from for a loop)",
4949
"date": "YYYY-MM-DD",
5050
"conn": "0 | 1 | 2 | 3 | 6 (max changes; 6 = unlimited; default 1)",
51-
"card": "jeune | senior",
51+
"card": "jeune | senior | interrail",
5252
"after": "HH:MM (depart after)",
5353
"before": "HH:MM (depart before)",
5454
"maxdur": "integer minutes (max total journey duration)",

‎public/llms.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Query params:
4343
- `from`: origin station id ; `to`: destination station id (in `mode=tour`, the optional finish city; set `to`=`from` for a loop)
4444
- `date`: `YYYY-MM-DD`
4545
- `conn`: `0` | `1` | `2` | `3` | `6` (max changes; 6 = unlimited; default 1)
46-
- `card`: `jeune` | `senior`
46+
- `card`: `jeune` | `senior` | `interrail` (Interrail: every running train, seat unknown; data in `/data/interrail.json`, the compact format of `src/data/compact.ts`)
4747
- `after` / `before`: `HH:MM` departure window ; `arrbefore`: `HH:MM` arrive-before cutoff ; `maxdur`: minutes ; `type`: axis
4848
- `via`: station id to route through (`mode=od`)
4949
- `flex`: `1` | `2` | `3` | `7` — also search ± N days around `date` (`mode=od/from/to/tour`)

‎public/theme.js‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
*/
1313
(function () {
1414
var theme = "auto";
15+
var card = "";
1516
try {
1617
var raw = localStorage.getItem("mj.settings");
1718
if (raw) {
1819
var s = JSON.parse(raw);
1920
if (s && (s.theme === "light" || s.theme === "dark" || s.theme === "auto")) theme = s.theme;
21+
if (s && typeof s.card === "string") card = s.card;
2022
}
2123
} catch (e) {
2224
/* private mode / bad JSON — fall back to auto (follows the OS) */
@@ -33,4 +35,22 @@
3335
// Paint the canvas background immediately (matches --bg light/dark), so there is no
3436
// white/black flash before the CSS file loads. The stylesheet takes over once parsed.
3537
root.style.backgroundColor = dark ? "#181613" : "#efece3";
38+
39+
// Start the dataset download now, before the bundle boots. The pass picks the file
40+
// (see profileForCard in src/data/profile.ts): a link's card= wins, else the saved one.
41+
try {
42+
var linked = new URLSearchParams(location.search).get("card");
43+
if (linked) card = linked;
44+
} catch (e) {
45+
/* no URLSearchParams — keep the saved pass */
46+
}
47+
var script = document.currentScript;
48+
var base = script && script.src ? script.src.replace(/theme\.js(\?.*)?$/, "") : "";
49+
var link = document.createElement("link");
50+
link.rel = "preload";
51+
link.as = "fetch";
52+
link.crossOrigin = "anonymous";
53+
link.href = base + "data/" + (card === "interrail" ? "interrail.json" : "tgvmax.json");
54+
link.setAttribute("data-dataset-preload", ""); // the prerender strips it (scripts/prerender.mjs)
55+
document.head.appendChild(link);
3656
})();

‎scripts/fetch-data.ts‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
* scripts/fetch-data.ts
33
*
44
* Downloads the full SNCF tgvmax dataset via the Opendatasoft Explore API v2.1
5-
* export endpoint and writes:
6-
* data/tgvmax.json – full mapped record array (compact JSON)
7-
* data/meta.json – freshness metadata
5+
* export endpoint and writes, under public/data/:
6+
* tgvmax.json – the MAX pass: trains with a free MAX seat (record array)
7+
* meta.json – its freshness metadata
8+
* interrail.json – the Interrail pass: every train in the feed, in the
9+
* compact format of src/data/compact.ts
10+
* interrail-meta.json – its freshness metadata
811
*
912
* Run via: npm run fetch-data (tsx scripts/fetch-data.ts)
1013
*/
@@ -13,6 +16,7 @@
1316
// fs, path, and process so we can import them without @types/node.
1417
import * as fs from "fs";
1518
import * as path from "path";
19+
import { encodeCompact } from "../src/data/compact";
1620

1721
// ---------------------------------------------------------------------------
1822
// Types
@@ -47,6 +51,8 @@ const MAX_RETRIES = 3;
4751
const REPO_ROOT = process.cwd();
4852
const OUT_DATA = path.resolve(REPO_ROOT, "public", "data", "tgvmax.json");
4953
const OUT_META = path.resolve(REPO_ROOT, "public", "data", "meta.json");
54+
const OUT_IR_DATA = path.resolve(REPO_ROOT, "public", "data", "interrail.json");
55+
const OUT_IR_META = path.resolve(REPO_ROOT, "public", "data", "interrail-meta.json");
5056

5157
// ---------------------------------------------------------------------------
5258
// Helpers
@@ -186,6 +192,23 @@ async function main(): Promise<void> {
186192

187193
fs.writeFileSync(OUT_META, JSON.stringify(meta, null, 2), "utf-8");
188194
console.log(`[fetch-data] Wrote metadata → ${OUT_META}`);
195+
196+
// Interrail: every train that runs takes a pass-holder reservation, seat or not, so
197+
// keep them all. Each distinct service is stored once with the dates it runs on, which
198+
// keeps the whole feed a static file instead of ~65 MB of repeated rows.
199+
const compact = encodeCompact(mapped.filter((r) => r.origine && r.destination && r.date));
200+
const irMeta: Meta = {
201+
updatedAt: meta.updatedAt,
202+
source: "SNCF Open Data — tgvmax, all trains (Licence Ouverte)",
203+
recordCount: mapped.length,
204+
isSample: false,
205+
};
206+
fs.writeFileSync(OUT_IR_DATA, JSON.stringify(compact), "utf-8");
207+
console.log(
208+
`[fetch-data] Wrote ${compact.trains.length} services (${mapped.length} trains) → ${OUT_IR_DATA}`,
209+
);
210+
fs.writeFileSync(OUT_IR_META, JSON.stringify(irMeta, null, 2), "utf-8");
211+
console.log(`[fetch-data] Wrote metadata → ${OUT_IR_META}`);
189212
console.log(`[fetch-data] Done. updatedAt=${meta.updatedAt}`);
190213
}
191214

‎scripts/prerender.mjs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ server.close();
6767
// flags so the static paint shows the plain active segment (highlighted by its CSS
6868
// fallback background); hydration re-adds the pill, correctly placed for the real
6969
// viewport, with no initial animation.
70+
// theme.js adds the dataset preload at runtime for the visitor's pass; one baked in here
71+
// would point at the prerender server and fetch the MAX file for every pass.
7072
html = html
73+
.replace(/<link\b[^>]*\bdata-dataset-preload\b[^>]*>/g, "")
7174
.replace(/(<html\b[^>]*?)\s+data-map="off"/, "$1")
7275
.replace(/(<span class="mode-tab-thumb"[^>]*?)\s+style="[^"]*"/g, "$1")
7376
.replace(/(class="[^"]*?)\s+has-thumb\b/g, "$1")
@@ -79,6 +82,7 @@ if (!html.includes('<html lang="fr"')) failures.push("prerendered HTML is not in
7982
if (!html.includes('id="app"')) failures.push("prerendered HTML lost #app");
8083
if (!html.includes("application/ld+json")) failures.push("prerendered HTML lost JSON-LD");
8184
if (!html.includes("<noscript")) failures.push("prerendered HTML lost the noscript fallback");
85+
if (html.includes("data-dataset-preload")) failures.push("prerendered HTML carries a baked dataset preload");
8286
if (/leaflet|assets\/map-/.test(html)) failures.push("prerendered HTML carries the map (Leaflet DOM or map chunk)");
8387
if (failures.length) {
8488
console.error("PRERENDER FAILED:");

‎src/app.ts‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Dataset } from "./data/dataset";
2+
import { SNCF_PROFILE, parseCard, profileForCard, type DatasetProfile } from "./data/profile";
23
import { StationRegistry } from "./data/stations";
3-
import type { SearchQuery, SearchMode, MaxTrain, Journey, SortKey, CalendarDay, StayChoice } from "./types";
4+
import type { CardType, SearchQuery, SearchMode, MaxTrain, Journey, SortKey, CalendarDay, StayChoice } from "./types";
45
import { stayNights, stayFromNights } from "./core/roundtrip";
56
import {
67
reachableDestinations,
@@ -44,11 +45,12 @@ import {
4445
modalPopstate,
4546
} from "./ui/modals";
4647
import { generateBookingUrl } from "./util/booking";
47-
import { t, setLang, getLang, isLang } from "./i18n";
48+
import { t, setLang, getLang, isLang, setSeatUnknown } from "./i18n";
4849
import * as store from "./state/store";
4950
import {
5051
MAX_JEUNE_URL,
5152
MAX_SENIOR_URL,
53+
INTERRAIL_FRANCE_URL,
5254
GITHUB_URL,
5355
GITHUB_ISSUES_URL,
5456
OVERNIGHT_MAX_CONNECTION_MIN,
@@ -61,6 +63,8 @@ import { notify } from "./pwa/register";
6163
interface Deps {
6264
trains: MaxTrain[];
6365
meta: Dataset["meta"];
66+
/** The dataset's profile: which pass's trains are loaded. */
67+
profile: DatasetProfile;
6468
registry: StationRegistry;
6569
}
6670

@@ -609,7 +613,8 @@ async function promptInstall(): Promise<void> {
609613
}
610614

611615
export function initApp(root: HTMLElement, dataset: Dataset, registry: StationRegistry): void {
612-
deps = { trains: dataset.trains, meta: dataset.meta, registry };
616+
deps = { trains: dataset.trains, meta: dataset.meta, profile: dataset.profile ?? SNCF_PROFILE, registry };
617+
setSeatUnknown(!deps.profile.seatKnown);
613618
rootRef = root;
614619
settings = store.loadSettings();
615620
const urlLang = new URLSearchParams(location.search).get("lang");
@@ -664,6 +669,7 @@ export function initApp(root: HTMLElement, dataset: Dataset, registry: StationRe
664669
window.addEventListener("popstate", (ev) => {
665670
if (modalPopstate(ev.state)) return; // Back closed a dialog; the page under it stays
666671
const searched = queryFromUrl();
672+
if (reloadForPass(searched.card)) return;
667673
const state = entryState();
668674
// Restore the FORM and tab from this entry's snapshot (the bare landing's is landingForm
669675
// when set), then the RESULTS from the URL; an entry with no snapshot uses the URL.
@@ -681,6 +687,17 @@ export function initApp(root: HTMLElement, dataset: Dataset, registry: StationRe
681687
});
682688
}
683689

690+
/**
691+
* A pass whose trains aren't the ones loaded (MAX ↔ Interrail) needs the other snapshot:
692+
* open the page again so boot loads it for the URL's card. A navigation, not a reload,
693+
* so the results show straight away. Returns true when it is under way.
694+
*/
695+
function reloadForPass(card: CardType): boolean {
696+
if (profileForCard(card).id === deps.profile.id) return false;
697+
location.assign(location.href);
698+
return true;
699+
}
700+
684701
/**
685702
* Parse the query from the URL, resolve its station names, snap its dates back into the
686703
* bookable window, and correct the address bar to match. The date <input> is clamped
@@ -1070,7 +1087,7 @@ function readQueryFromForm(): SearchQuery {
10701087
.filter((l) => l.from && l.to)
10711088
: undefined,
10721089
date: refs.date.value || query.date,
1073-
card: refs.card.value === "senior" ? "senior" : "jeune",
1090+
card: parseCard(refs.card.value),
10741091
departAfter: refs.departAfter.value || undefined,
10751092
departBefore: refs.departBefore.value || undefined,
10761093
arriveBefore: refs.arriveBefore.value || undefined,
@@ -1874,6 +1891,10 @@ function renderSearch(): void {
18741891

18751892
// MAX SENIOR free tickets are weekday-only — flag a weekend outbound, and (round trip)
18761893
// a weekend RETURN on a later day, since either leg must be booked free.
1894+
// No open data says whether an Interrail seat is left: say so above every result.
1895+
if (!deps.profile.seatKnown) {
1896+
refs.results.append(el("p", { class: "notice", text: t("interrail_seat_notice") }));
1897+
}
18771898
if (query.card === "senior") {
18781899
if (isWeekend(query.date)) {
18791900
refs.results.append(el("p", { class: "notice", text: t("senior_weekend_warn") }));
@@ -3810,6 +3831,7 @@ function buildLayout(root: HTMLElement): void {
38103831
overnightMaxConnectionMin: OVERNIGHT_MAX_CONNECTION_MIN,
38113832
jeuneUrl: MAX_JEUNE_URL,
38123833
seniorUrl: MAX_SENIOR_URL,
3834+
interrailUrl: INTERRAIL_FRANCE_URL,
38133835
resolveStation,
38143836
stationLabel: (id) => deps.registry.label(id),
38153837
mode: () => query.mode,
@@ -3860,6 +3882,7 @@ function buildLayout(root: HTMLElement): void {
38603882
store.saveSettings(settings);
38613883
query = { ...query, card };
38623884
store.updateUrl(query);
3885+
if (reloadForPass(card)) return;
38633886
runSearch();
38643887
},
38653888
onShare: (onCopied) => void shareCurrentUrl(onCopied),

‎src/config.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ const BASE = (import.meta.env?.BASE_URL ?? "/") as string;
5151
/** Base-relative data URLs (work under the GitHub Pages sub-path). */
5252
export const DATA_URL = `${BASE}data/tgvmax.json`;
5353
export const META_URL = `${BASE}data/meta.json`;
54+
/** The Interrail snapshot: every running train in the feed, in the compact format
55+
* (`src/data/compact.ts`), with its own freshness metadata. */
56+
export const INTERRAIL_DATA_URL = `${BASE}data/interrail.json`;
57+
export const INTERRAIL_META_URL = `${BASE}data/interrail-meta.json`;
5458

5559
export const SNCF_CONNECT_URL = "https://www.sncf-connect.com/";
5660

@@ -63,6 +67,8 @@ export const GITHUB_ISSUES_URL = `${GITHUB_URL}/issues/new`;
6367
/** Official SNCF pages describing the MAX JEUNE / MAX SENIOR subscriptions. */
6468
export const MAX_JEUNE_URL = "https://www.sncf-connect.com/catalogue/description/max-jeune";
6569
export const MAX_SENIOR_URL = "https://www.sncf-connect.com/catalogue/description/max-senior";
70+
/** Interrail's page on pass-holder reservations for French trains. */
71+
export const INTERRAIL_FRANCE_URL = "https://www.interrail.com/en/plan-your-trip/reservations/reservations-france";
6672

6773
/** App version + build date, injected from package.json at build time (see vite.config).
6874
* Falls back to "dev" when the defines aren't present (e.g. a raw ts-node run). */

0 commit comments

Comments
 (0)