import type { CrosscodeEvent } from "@crosscode/contracts"; import { apiFetchListForPrerender } from "@/lib/api"; /** * Events from the church's other products (2.4.0, Settings → Linked products → Show events from): * read-only copies as `crosscode.event/1`, each with its own page here (/events/linked/…) that * points search engines and visitors to the product it comes from. */ export type LinkedEvent = { key: string; product: "website" | "app"; productName: string; event: CrosscodeEvent; }; /** Upcoming linked events for a public page. Never fails the page: no events if the API can't answer. */ export async function fetchLinkedEvents(campus?: string): Promise { const query = campus ? `?campus=${encodeURIComponent(campus)}` : ""; try { return await apiFetchListForPrerender(`/api/v2/linked-events${query}`, { nextOptions: { next: { revalidate: 60 } } }); } catch { return []; } } /** This site's page for a linked event. */ export const linkedEventPath = (e: LinkedEvent) => `/events/linked/${e.product}/${e.event.id}`; /** "From the Church Website". */ export const fromLabel = (e: LinkedEvent) => `From the ${e.productName}`; /** Our events and linked ones in one list by start time. `startOf` gives our events' start. */ export function mergeByStart(ours: T[], linked: LinkedEvent[], startOf: (e: T) => string): ({ kind: "own"; event: T } | { kind: "linked"; event: LinkedEvent })[] { const all = [ ...ours.map((event) => ({ kind: "own" as const, event, at: new Date(startOf(event)).getTime() })), ...linked.map((event) => ({ kind: "linked" as const, event, at: new Date(event.event.startsAt).getTime() })), ]; return all.sort((a, b) => a.at - b.at).map((x) => (x.kind === "own" ? { kind: "own" as const, event: x.event } : { kind: "linked" as const, event: x.event })); }