trails/apps/journal/app/routes/api.routes.$id.edit-in-planner.ts
Ullrich Schäfer 14b179bb0c Fix waypoint loading in Edit in Planner flow
Three issues fixed:

1. GPX parser used browser DOMParser which doesn't exist in Node/Vite SSR.
   Added async parseGpxAsync() using linkedom for server-side parsing.

2. Server-side session initialization stored waypoints in a Yjs doc
   instance separate from the Vite WebSocket plugin's doc store.
   Moved waypoint initialization to client-side: API returns parsed
   waypoints, client adds them to Yjs after sync.

3. GPX was encoded in URL params causing HTTP 431. Now the Journal
   creates a Planner session via API (POST body), and only passes
   compact waypoint coordinates in URL params.

Verified: Journal route → Edit in Planner → 4 waypoints loaded,
route computed (79.3km), elevation profile, Save to Journal ready.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-24 20:22:58 +00:00

50 lines
1.9 KiB
TypeScript

import { data } from "react-router";
import type { Route } from "./+types/api.routes.$id.edit-in-planner";
import { getSessionUser } from "~/lib/auth.server";
import { getRouteWithVersions } from "~/lib/routes.server";
import { createRouteToken } from "~/lib/jwt.server";
export async function action({ params, request }: Route.ActionArgs) {
const user = await getSessionUser(request);
if (!user) return data({ error: "Not authenticated" }, { status: 401 });
const route = await getRouteWithVersions(params.id);
if (!route) return data({ error: "Route not found" }, { status: 404 });
if (route.ownerId !== user.id) return data({ error: "Not authorized" }, { status: 403 });
const token = await createRouteToken(params.id);
const origin = process.env.ORIGIN ?? "http://localhost:3000";
const callbackUrl = `${origin}/api/routes/${params.id}/callback`;
const plannerUrl = process.env.PLANNER_URL ?? "http://localhost:3001";
const returnUrl = `${origin}/routes/${params.id}`;
// Create Planner session via API (POST body, not URL params)
const sessionResp = await fetch(`${plannerUrl}/api/sessions`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
callbackUrl,
callbackToken: token,
gpx: route.gpx ?? undefined,
}),
});
if (!sessionResp.ok) {
return data({ error: "Failed to create Planner session" }, { status: 502 });
}
const session = (await sessionResp.json()) as {
url: string;
initialWaypoints?: Array<{ lat: number; lon: number; name?: string }>;
};
// Encode waypoints in URL params (small — just coordinates, not full GPX)
const urlParams = new URLSearchParams({ returnUrl });
if (session.initialWaypoints?.length) {
urlParams.set("waypoints", JSON.stringify(session.initialWaypoints));
}
return data({
url: `${plannerUrl}${session.url}?${urlParams}`,
});
}