Implements the activity-stats change (specs/activity-stats): - gpx: `movingTime(tracks)` — moving seconds from trackpoint timestamps, excluding stationary spans + long gaps; null when no timestamps. - stats.ts: pure formatters (distance/elevation/duration/speed/pace), sport-aware `deriveRate` (pace for foot sports, speed otherwise, speed default), and `activityStatItems` encoding the canonical order (distance · time · [moving] · pace/speed · ascent · descent; compact subset). - StatRow: one shared presentational component (size sm/lg), adopted on the activity detail (full set; loader derives moving time), route detail (distance/ascent/descent), feed card + profile list (compact). - i18n: journal.stats.* in en + de. Tests: movingTime (stationary/gap exclusion, moving ≤ elapsed), deriveRate, formatters, activityStatItems ordering + compact, StatRow render (jsdom). typecheck + lint + unit (gpx 63, journal 311) green. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
119 lines
4.5 KiB
TypeScript
119 lines
4.5 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 { requireOwnedActivity, requireOwnedRoute } from "~/lib/ownership.server";
|
|
import { parseGpxAsync, movingTime } from "@trails-cool/gpx";
|
|
import { logger } from "~/lib/logger.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 fetched = await getActivity(id ?? "");
|
|
if (!fetched) throw data({ error: "Activity not found" }, { status: 404 });
|
|
// Remote-ingested activities have no local detail page — their
|
|
// canonical page lives on the origin instance; the feed links there.
|
|
if (fetched.ownerId === null) throw data({ error: "Activity not found" }, { status: 404 });
|
|
const activity = { ...fetched, ownerId: fetched.ownerId };
|
|
|
|
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) : [];
|
|
|
|
// Moving time is derived from trackpoint timestamps (null when the GPX has
|
|
// none). Detail-page only — too costly to parse per row in list views.
|
|
let movingTimeSec: number | null = null;
|
|
if (activity.gpx) {
|
|
try {
|
|
const parsed = await parseGpxAsync(activity.gpx);
|
|
movingTimeSec = movingTime(parsed.tracks);
|
|
} catch (err) {
|
|
logger.warn({ activityId: activity.id, err }, "moving-time: failed to parse gpx");
|
|
}
|
|
}
|
|
|
|
return {
|
|
activity: {
|
|
id: activity.id,
|
|
name: activity.name,
|
|
description: activity.description,
|
|
sportType: activity.sportType,
|
|
distance: activity.distance,
|
|
elevationGain: activity.elevationGain,
|
|
elevationLoss: activity.elevationLoss,
|
|
duration: activity.duration,
|
|
movingTimeSec,
|
|
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 activity = await requireOwnedActivity(activityId, user.id);
|
|
const routeIdRaw = formData.get("routeId") as string;
|
|
if (routeIdRaw) {
|
|
const route = await requireOwnedRoute(routeIdRaw, user.id);
|
|
await linkActivityToRoute(activity, route);
|
|
}
|
|
return redirect(`/activities/${activityId}`);
|
|
}
|
|
|
|
if (intent === "create-route") {
|
|
const activity = await requireOwnedActivity(activityId, user.id);
|
|
const routeId = await createRouteFromActivity(activity);
|
|
if (routeId) return redirect(`/routes/${routeId}`);
|
|
return data({ error: "No GPX data to create route from" }, { status: 400 });
|
|
}
|
|
|
|
if (intent === "delete") {
|
|
const activity = await requireOwnedActivity(activityId, user.id);
|
|
await deleteImportByActivity(activityId);
|
|
await deleteActivity(activity);
|
|
return redirect("/activities");
|
|
}
|
|
|
|
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 activity = await requireOwnedActivity(activityId, user.id);
|
|
await updateActivityVisibility(activity, raw as Visibility);
|
|
return redirect(`/activities/${activityId}`);
|
|
}
|
|
|
|
return data({ error: "Unknown action" }, { status: 400 });
|
|
}
|