Satisfies the Next 16 rule that page files only export page fields: getSupplyBoxDetail/getSupplyBoxList and getReservationList/getReservationDetail move to reservations/surface.ts and pages import from it.
252 lines
9 KiB
TypeScript
252 lines
9 KiB
TypeScript
import config from "@payload-config";
|
|
import { getPayload } from "payload";
|
|
import { headers as nextHeaders } from "next/headers";
|
|
import Link from "next/link";
|
|
import { isPayloadUser } from "@/utils/access-control/isPayloadUser";
|
|
import { hasPermission } from "@/utils/access-control/hasPermission";
|
|
import { LedgerRow, NoticeState, PageShell } from "@/components/frontend/operations";
|
|
import type { SurfaceUser } from "@/lib/operations/surface";
|
|
import { CancelButton } from "@/components/frontend/operations/reservations/ReservationCancelButton";
|
|
import type { OperationReservation } from "@/payload-types";
|
|
import { getReservationDetail } from "../surface";
|
|
|
|
type PayloadType = Awaited<ReturnType<typeof import("payload").getPayload>>;
|
|
|
|
function relationId(value: unknown): number | null {
|
|
if (typeof value === "number") return value;
|
|
if (value !== null && typeof value === "object" && "id" in (value as { id: unknown })) {
|
|
const id = (value as { id: unknown }).id;
|
|
return typeof id === "number" ? id : null;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function relationLabel(value: unknown): string {
|
|
if (
|
|
typeof value === "object" &&
|
|
value !== null &&
|
|
"codeName" in (value as { codeName?: unknown })
|
|
) {
|
|
const codeName = (value as { codeName?: unknown }).codeName;
|
|
if (typeof codeName === "string" && codeName) return codeName;
|
|
}
|
|
return String(relationId(value) ?? "n/a");
|
|
}
|
|
|
|
export interface ReservationDetail {
|
|
reservation: OperationReservation | null;
|
|
canCancel: boolean;
|
|
}
|
|
|
|
/**
|
|
* Full detail for a single reservation, fetched through collection access
|
|
* control (default access + the explicit session user, never overrideAccess).
|
|
* Returns null for an unknown id so the page can render a not-found notice.
|
|
* canCancel is true only when the row is still reserved AND the viewer holds
|
|
* operation-reservations:update.
|
|
*/
|
|
|
|
function Section({ title, children }: { title: string; children: React.ReactNode }) {
|
|
return (
|
|
<section data-slot="reservation-section" className="flex flex-col gap-2">
|
|
<h2 className="font-mono text-[9px] uppercase tracking-widest text-white/60">{title}</h2>
|
|
{children}
|
|
</section>
|
|
);
|
|
}
|
|
|
|
function kvRow(label: string, value: React.ReactNode) {
|
|
return (
|
|
<div className="flex items-start justify-between gap-4 border border-white/10 px-2 py-1.5">
|
|
<span className="shrink-0 font-mono text-[9px] uppercase tracking-widest text-white/50">
|
|
{label}
|
|
</span>
|
|
<span className="min-w-0 truncate text-right font-mono text-xs text-white/80">{value}</span>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export const metadata = {
|
|
title: "Reservation Detail: Polaris Task Force",
|
|
};
|
|
|
|
export default async function ReservationDetailPage({
|
|
params,
|
|
}: {
|
|
params: Promise<{ id: string }>;
|
|
}) {
|
|
const { id } = await params;
|
|
const payload = await getPayload({ config });
|
|
const { user: authUser } = await payload.auth({
|
|
headers: await nextHeaders(),
|
|
canSetHeaders: false,
|
|
});
|
|
const user = isPayloadUser(authUser) ? authUser : null;
|
|
const surfaceUser: SurfaceUser = user ? { id: user.id } : null;
|
|
|
|
const { reservation, canCancel } = await getReservationDetail(payload, surfaceUser, id);
|
|
|
|
if (reservation === null) {
|
|
return (
|
|
<div className="mx-auto flex w-full max-w-7xl flex-col gap-6 px-4 py-6">
|
|
<PageShell title="Reservation" meta="ALLOCATION RESERVATIONS // COMMAND VIEW">
|
|
<NoticeState
|
|
variant="error"
|
|
message="Reservation not found."
|
|
hint="The reservation id is unknown or you lack permission to read it."
|
|
action={
|
|
<Link
|
|
href="/operations/reservations"
|
|
className="mt-2 border border-white/15 px-3 py-1 font-mono text-[10px] uppercase tracking-widest text-white/80 hover:border-white/30 hover:text-white"
|
|
>
|
|
Back to reservations
|
|
</Link>
|
|
}
|
|
/>
|
|
</PageShell>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
const personnel = reservation.personnel ?? [];
|
|
const vehicles = reservation.vehicles ?? [];
|
|
const cargo = reservation.cargo ?? [];
|
|
const budget = reservation.budget;
|
|
|
|
const readoutCells = [
|
|
{ label: "Key", value: reservation.reservationKey },
|
|
{ label: "Operation", value: reservation.operationId },
|
|
{ label: "Status", value: reservation.status },
|
|
{ label: "Expires", value: reservation.expiresAt ?? "n/a" },
|
|
];
|
|
|
|
return (
|
|
<div className="mx-auto flex w-full max-w-7xl flex-col gap-6 px-4 py-6">
|
|
<PageShell title={reservation.reservationKey} meta="RESERVATION // DETAIL">
|
|
<Link
|
|
href="/operations/reservations"
|
|
className="font-mono text-[10px] uppercase tracking-widest text-white/60 underline-offset-4 hover:underline"
|
|
>
|
|
Back to reservations
|
|
</Link>
|
|
|
|
<LedgerRow cells={readoutCells} />
|
|
|
|
<Section title="Personnel">
|
|
{personnel.length === 0 ? (
|
|
<NoticeState variant="empty" message="No personnel reserved." />
|
|
) : (
|
|
<ul className="flex flex-col gap-1">
|
|
{personnel.map((member) => (
|
|
<li
|
|
key={member.id}
|
|
className="flex items-center justify-between gap-2 border border-white/10 px-2 py-1.5"
|
|
>
|
|
<span className="min-w-0 truncate font-mono text-xs text-white/80">
|
|
{relationLabel(member.user)} · id {relationId(member.user)}
|
|
</span>
|
|
<span className="font-mono text-[10px] uppercase tracking-widest text-white/50">
|
|
{member.slot ?? "slot"}
|
|
</span>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
)}
|
|
</Section>
|
|
|
|
<Section title="Vehicles">
|
|
{vehicles.length === 0 ? (
|
|
<NoticeState variant="empty" message="No vehicles reserved." />
|
|
) : (
|
|
<ul className="flex flex-col gap-1">
|
|
{vehicles.map((vehicle) => (
|
|
<li
|
|
key={vehicle.id}
|
|
className="flex items-center justify-between gap-2 border border-white/10 px-2 py-1.5"
|
|
>
|
|
<span className="min-w-0 truncate font-mono text-xs text-white/80">
|
|
{relationLabel(vehicle.vehicle)} · id {relationId(vehicle.vehicle)}
|
|
</span>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
)}
|
|
</Section>
|
|
|
|
<Section title="Cargo">
|
|
{cargo.length === 0 ? (
|
|
<NoticeState variant="empty" message="No cargo reserved." />
|
|
) : (
|
|
<ul className="flex flex-col gap-1">
|
|
{cargo.map((line) => (
|
|
<li
|
|
key={line.id}
|
|
className="flex items-center justify-between gap-2 border border-white/10 px-2 py-1.5"
|
|
>
|
|
<span className="min-w-0 truncate font-mono text-xs text-white/80">
|
|
{relationLabel(line.resource)} · id {relationId(line.resource)}
|
|
</span>
|
|
<span className="font-mono text-xs tabular-nums text-white/70">
|
|
{line.amount}
|
|
</span>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
)}
|
|
</Section>
|
|
|
|
<Section title="Budget">
|
|
{budget?.amount != null || budget?.account != null ? (
|
|
<div className="flex flex-col gap-1">
|
|
{kvRow(
|
|
"Account",
|
|
budget.account != null
|
|
? `${relationLabel(budget.account)} · id ${relationId(budget.account)}`
|
|
: "n/a",
|
|
)}
|
|
{kvRow("Amount", budget.amount ?? "n/a")}
|
|
</div>
|
|
) : (
|
|
<NoticeState variant="empty" message="No budget earmarked." />
|
|
)}
|
|
</Section>
|
|
|
|
<Section title="Routing">
|
|
<div className="flex flex-col gap-1">
|
|
{kvRow(
|
|
"Origin",
|
|
reservation.origin != null ? relationLabel(reservation.origin) : "n/a",
|
|
)}
|
|
{kvRow(
|
|
"Destination",
|
|
reservation.destination != null ? relationLabel(reservation.destination) : "n/a",
|
|
)}
|
|
</div>
|
|
</Section>
|
|
|
|
<Section title="Metadata">
|
|
<div className="flex flex-col gap-1">
|
|
{kvRow(
|
|
"Created by",
|
|
reservation.createdBy != null ? relationLabel(reservation.createdBy) : "n/a",
|
|
)}
|
|
{kvRow("Created at", reservation.createdAt)}
|
|
{kvRow("Expires", reservation.expiresAt ?? "n/a")}
|
|
</div>
|
|
</Section>
|
|
|
|
{reservation.status !== "reserved" && (
|
|
<Section title="Settlement">
|
|
<pre className="overflow-x rounded border border-white/10 bg-black/40 p-2 font-mono text-[11px] text-white/70">
|
|
{JSON.stringify(reservation.settlement, null, 2)}
|
|
</pre>
|
|
</Section>
|
|
)}
|
|
|
|
{canCancel && reservation.status === "reserved" ? (
|
|
<CancelButton reservationId={reservation.id} />
|
|
) : null}
|
|
</PageShell>
|
|
</div>
|
|
);
|
|
}
|