1
0
Fork 0
polaris-task-force/src/app/(frontend)/personnel/contacts/[id]/page.tsx
Z8MB1E 755bd04875 feat(personnel): add NPC portraits and rich bio, redesign contact cards
Game NPCs gain an image upload and a Lexical rich-text bio (with data
migration that wraps existing plain text in a paragraph). NPC profile, card
and directory surface the portrait, a status-chip row and initials fallback;
contacts list and detail pages adopt the redesigned cards.
2026-10-05 03:22:11 -04:00

173 lines
5.6 KiB
TypeScript

import config from "@payload-config";
import { getPayload } from "payload";
import { notFound } from "next/navigation";
import { headers as nextHeaders } from "next/headers";
import type { GameNpc, MarketListing, NpcStaffing } from "@/payload-types";
import { NpcProfile } from "@/components/frontend/personnel/NpcProfile";
import { EmploymentCard } from "@/components/frontend/baseManagement/EmploymentCard";
import { hasLogisticsQualification } from "@/utils/access-control/hasLogisticsQualification";
import type { LaborClassificationOption, StructureStaffingOption } from "@/lib/base";
import { currencyConfigFromGameRules } from "@/lib/banking";
interface NpcContactPageProps {
params: Promise<{ id: string }>;
}
export default async function NpcContactPage({ params }: NpcContactPageProps) {
const { id: idParam } = await params;
const id = Number(idParam);
if (!Number.isInteger(id) || id <= 0) notFound();
const payload = await getPayload({ config });
const headers = await nextHeaders();
const { user } = await payload.auth({ headers, canSetHeaders: false });
let npc: GameNpc;
try {
npc = (await payload.findByID({
collection: "game-npcs",
id,
depth: 2,
})) as unknown as GameNpc;
} catch {
notFound();
}
if (!npc) notFound();
const staffingRes = await payload.find({
collection: "npc-staffing",
where: {
and: [{ npc: { equals: id } }, { status: { equals: "active" } }],
},
limit: 1,
depth: 2,
});
const staffing = staffingRes.docs[0] as NpcStaffing | undefined;
const staffingStructure = staffing?.structure;
const structureName =
typeof staffingStructure === "object" && staffingStructure !== null
? staffingStructure.name
: typeof staffingStructure === "number"
? `Structure #${staffingStructure}`
: null;
const employment =
staffing && structureName
? {
positionTitle: staffing.positionTitle,
positionType: staffing.positionType,
structureName,
salary: staffing.salary ?? 0,
}
: null;
const structuresRes = await payload.find({
collection: "game-structures",
limit: 200,
depth: 0,
});
const activeStaffRes = await payload.find({
collection: "npc-staffing",
where: { status: { equals: "active" } },
limit: 1000,
depth: 0,
overrideAccess: true,
});
const structures: StructureStaffingOption[] = structuresRes.docs.map((structure) => ({
id: structure.id,
name: structure.name,
maintenanceCost: structure.maintenanceCost ?? 0,
laborRequirements: (structure.laborRequirements ?? []).map((requirement) => {
const classificationId =
typeof requirement.classification === "object"
? requirement.classification.id
: requirement.classification;
return {
classificationId,
requiredHeadcount: requirement.requiredHeadcount,
maxHeadcount: requirement.maxHeadcount ?? 0,
enforcedSalary: requirement.enforcedSalary ?? null,
currentHeadcount: activeStaffRes.docs
.filter((entry) => {
const structureId =
typeof entry.structure === "object" ? entry.structure.id : entry.structure;
const laborClassificationId =
typeof entry.laborClassification === "object" && entry.laborClassification !== null
? entry.laborClassification.id
: entry.laborClassification;
return structureId === structure.id && laborClassificationId === classificationId;
})
.reduce((total, entry) => total + (entry.headcount ?? 1), 0),
};
}),
staffPositions: (structure.staffPositions ?? []).map((position) => ({
positionType: position.positionType,
max: position.max ?? null,
enforcedSalary: position.enforcedSalary ?? null,
currentHeadcount: activeStaffRes.docs.filter((entry) => {
const structureId =
typeof entry.structure === "object" ? entry.structure.id : entry.structure;
return (
structureId === structure.id &&
entry.npc !== null &&
entry.positionType === position.positionType
);
}).length,
})),
}));
const classificationsRes = await payload.find({
collection: "labor-classifications",
limit: 100,
depth: 0,
});
const classifications: LaborClassificationOption[] = classificationsRes.docs.map((doc) => ({
id: doc.id,
name: doc.name,
key: doc.key,
defaultUnitSalary: doc.defaultUnitSalary ?? null,
availableHeadcount: doc.availableHeadcount ?? 0,
}));
const rules = await payload.findGlobal({
slug: "game-rules",
depth: 1,
overrideAccess: true,
});
const currencyConfig = currencyConfigFromGameRules(rules);
const canManage = await hasLogisticsQualification(payload, user);
const listingsRes = await payload.find({
collection: "market-listings",
where: {
and: [{ npc: { equals: id } }, { status: { equals: "active" } }],
},
limit: 100,
depth: 1,
});
const listings = listingsRes.docs as unknown as MarketListing[];
return (
<div className="p-5">
<NpcProfile
npc={npc}
listings={listings}
employmentSlot={
<EmploymentCard
employment={employment}
structures={structures}
canManage={canManage}
npcId={npc.id}
npcName={npc.name}
classifications={classifications}
hiringEnabled={Boolean(rules.staffHiringEnabled)}
hiringDisabledMessage={rules.staffHiringDisabledMessage ?? undefined}
currencyConfig={currencyConfig}
/>
}
/>
</div>
);
}