Completes the .server.ts split started in #418. Every route that mixes a default-export component with a server-only loader/action now has a sibling <route>.server.ts holding the data-fetching helpers; the route .tsx is a thin delegator. Routes converted (21): activities._index, activities.$id, activities.new, auth.accept-terms, auth.verify, explore, feed, notifications, routes._index, routes.$id, routes.$id.edit, routes.new, settings, settings.account, settings.connections.komoot, settings.profile, settings.security, sync.import.$provider, sync.import.komoot, users.$username.followers, users.$username.following Pattern (same as home.tsx / users.$username.tsx / settings.connections.tsx): - loader → `return data(await loadX(request, params?))` - action → `return await xAction(request, params?)` - All `getDb` / Drizzle schema / `~/lib/*.server` imports move to the .server.ts sibling. - `throw redirect(...)` and `throw data(...)` propagate through the delegator unchanged. No behavior changes — pure module-graph cleanup. Component modules no longer transitively import the DB client; Vite's tree-shake of server-only code is now backed by an explicit, file-local contract. Verified: - pnpm typecheck — green - pnpm lint — green - pnpm test — 181 passed, 31 integration-gated skipped - pnpm --filter @trails-cool/journal build — succeeds Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
95 lines
3.4 KiB
TypeScript
95 lines
3.4 KiB
TypeScript
// Server-only loader/action for /activities/:id. See `home.server.ts`.
|
|
|
|
import { data, redirect } from "react-router";
|
|
import { canView } from "~/lib/auth.server";
|
|
import { getSessionUser, requireSessionUser } from "~/lib/auth/session.server";
|
|
import {
|
|
getActivity,
|
|
deleteActivity,
|
|
linkActivityToRoute,
|
|
createRouteFromActivity,
|
|
updateActivityVisibility,
|
|
} from "~/lib/activities.server";
|
|
import { deleteImportByActivity } from "~/lib/sync/imports.server";
|
|
import { listRoutes } from "~/lib/routes.server";
|
|
import type { Visibility } from "@trails-cool/db/schema/journal";
|
|
|
|
const VISIBILITY_VALUES = new Set<Visibility>(["private", "unlisted", "public"]);
|
|
|
|
export async function loadActivityDetail(request: Request, id: string | undefined) {
|
|
const activity = await getActivity(id ?? "");
|
|
if (!activity) throw data({ error: "Activity not found" }, { status: 404 });
|
|
|
|
const user = await getSessionUser(request);
|
|
const isOwner = user?.id === activity.ownerId;
|
|
|
|
// Visibility gate — public always, unlisted on direct link, private owner-only.
|
|
// 404 (not 403) to avoid leaking existence.
|
|
if (!canView(activity, user, { asDirectLink: true })) {
|
|
throw data({ error: "Activity not found" }, { status: 404 });
|
|
}
|
|
|
|
const userRoutes = isOwner && user ? await listRoutes(user.id) : [];
|
|
|
|
return {
|
|
activity: {
|
|
id: activity.id,
|
|
name: activity.name,
|
|
description: activity.description,
|
|
distance: activity.distance,
|
|
elevationGain: activity.elevationGain,
|
|
elevationLoss: activity.elevationLoss,
|
|
duration: activity.duration,
|
|
routeId: activity.routeId,
|
|
hasGpx: !!activity.gpx,
|
|
geojson: activity.geojson ?? null,
|
|
startedAt: activity.startedAt?.toISOString() ?? null,
|
|
visibility: activity.visibility,
|
|
createdAt: activity.createdAt.toISOString(),
|
|
importSource: activity.importSource,
|
|
},
|
|
isOwner,
|
|
routes: userRoutes.map((r) => ({ id: r.id, name: r.name })),
|
|
};
|
|
}
|
|
|
|
export async function activityDetailAction(request: Request, id: string | undefined) {
|
|
const user = await requireSessionUser(request);
|
|
const activityId = id ?? "";
|
|
|
|
const formData = await request.formData();
|
|
const intent = formData.get("intent");
|
|
|
|
if (intent === "link-route") {
|
|
const routeId = formData.get("routeId") as string;
|
|
if (routeId) {
|
|
await linkActivityToRoute(activityId, routeId, user.id);
|
|
}
|
|
return redirect(`/activities/${activityId}`);
|
|
}
|
|
|
|
if (intent === "create-route") {
|
|
const routeId = await createRouteFromActivity(activityId, user.id);
|
|
if (routeId) return redirect(`/routes/${routeId}`);
|
|
return data({ error: "No GPX data to create route from" }, { status: 400 });
|
|
}
|
|
|
|
if (intent === "delete") {
|
|
await deleteImportByActivity(activityId);
|
|
const deleted = await deleteActivity(activityId, user.id);
|
|
if (deleted) return redirect("/activities");
|
|
return data({ error: "Activity not found" }, { status: 404 });
|
|
}
|
|
|
|
if (intent === "set-visibility") {
|
|
const raw = formData.get("visibility") as string | null;
|
|
if (!raw || !VISIBILITY_VALUES.has(raw as Visibility)) {
|
|
return data({ error: "Invalid visibility" }, { status: 400 });
|
|
}
|
|
const ok = await updateActivityVisibility(activityId, user.id, raw as Visibility);
|
|
if (!ok) return data({ error: "Activity not found" }, { status: 404 });
|
|
return redirect(`/activities/${activityId}`);
|
|
}
|
|
|
|
return data({ error: "Unknown action" }, { status: 400 });
|
|
}
|