makearmy-app/app/rigs/page.tsx
2025-09-29 19:39:37 -04:00

52 lines
1.5 KiB
TypeScript

// app/portal/rigs/page.tsx
import { cookies } from "next/headers";
import { redirect } from "next/navigation";
import RigsSwitcher from "@/components/portal/RigsSwitcher";
type Opt = { id: string | number; label: string };
export default async function Page() {
const jar = await cookies(); // Next 15: async
const ma_at = jar.get("ma_at")?.value;
if (!ma_at) {
redirect("/auth/sign-in?next=/portal/rigs");
}
const DIRECTUS = (process.env.DIRECTUS_URL || "").replace(/\/$/, "");
let rigTypes: Opt[] = [];
try {
const res = await fetch(
`${DIRECTUS}/items/user_rig_type?fields=id,name&sort=sort`,
{
headers: {
Authorization: `Bearer ${ma_at}`,
Accept: "application/json",
"Cache-Control": "no-store",
},
cache: "no-store",
}
);
if (res.ok) {
const json = await res.json();
rigTypes = (json?.data ?? []).map((r: any) => ({
id: r.id,
label: r.name ?? String(r.id),
}));
} else {
// Optional: log server-side for debugging
// console.error("Failed to fetch user_rig_type", res.status);
rigTypes = [];
}
} catch {
rigTypes = [];
}
return (
<div className="rounded-lg border p-6">
<h2 className="mb-4 text-xl font-semibold">Rigs</h2>
<RigsSwitcher rigTypes={rigTypes} />
</div>
);
}