1
0
Fork 0
polaris-task-force/src/app/(frontend)/logistics/resources/[id]/page.tsx

281 lines
9.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import config from "@payload-config";
import { getPayload } from "payload";
import { notFound } from "next/navigation";
import Link from "next/link";
import type { Asset, Structure } from "@/payload-types";
import { RichText } from "@payloadcms/richtext-lexical/react";
import { convertLexicalToPlaintext } from "@payloadcms/richtext-lexical/plaintext";
import {
ArrowLeftIcon,
BoxIcon,
Building2Icon,
CoinsIcon,
Grid3x3Icon,
LayersIcon,
RotateCcwIcon,
ScaleIcon,
TagIcon,
WeightIcon,
} from "lucide-react";
import {
Item,
ItemContent,
ItemDescription,
ItemHeader,
ItemMedia,
ItemTitle,
} from "@/components/ui/item";
interface ResourceDetailPageProps {
params: Promise<{ id: string }>;
}
const rarityColors: Record<string, string> = {
common: "bg-muted text-muted-foreground",
uncommon: "bg-green-500/20 text-green-400",
rare: "bg-blue-500/20 text-blue-400",
epic: "bg-purple-500/20 text-purple-400",
legendary: "bg-amber-500/20 text-amber-400",
exotic: "bg-pink-500/20 text-pink-400",
unique: "bg-red-500/20 text-red-400",
};
const typeColors: Record<string, string> = {
physical: "bg-muted text-muted-foreground",
fluid: "bg-blue-500/20 text-blue-400",
energy: "bg-amber-500/20 text-amber-400",
abstract: "bg-purple-500/20 text-purple-400",
};
export default async function ResourceDetailPage({ params }: ResourceDetailPageProps) {
const { id } = await params;
const payload = await getPayload({ config });
const resource = await payload
.findByID({
collection: "resources",
id: Number(id),
depth: 2,
})
.catch(() => null);
if (!resource) {
notFound();
}
const [producersRes, autoGenRes, craftingRes] = await Promise.all([
payload
.find({
collection: "structures",
where: {
"canProduce.value": { equals: resource.id },
},
limit: 50,
depth: 1,
})
.catch(() => ({ docs: [] })),
payload
.find({
collection: "structures",
where: {
"autoGeneratedResources.resource": { equals: resource.id },
},
limit: 50,
depth: 1,
})
.catch(() => ({ docs: [] })),
payload
.find({
collection: "assets",
where: {
"crafting.craftingData.inputs.resource": { equals: resource.id },
},
limit: 50,
depth: 1,
})
.catch(() => ({ docs: [] })),
]);
const producers = producersRes.docs as unknown as Structure[];
const autoGenStructures = autoGenRes.docs as unknown as Structure[];
const craftingAssets = craftingRes.docs as unknown as Asset[];
const allRelatedStructures = [
...producers,
...autoGenStructures.filter((s) => !producers.some((p) => p.id === s.id)),
];
return (
<div className="p-5 flex flex-col gap-6 max-w-7xl">
<Link
href="/logistics/resources"
className="text-sm text-muted-foreground hover:text-primary transition-colors flex items-center gap-1 w-fit"
>
<ArrowLeftIcon className="size-3" />
Back to Resources
</Link>
<div className="flex items-start justify-between gap-4">
<div className="flex flex-col gap-1">
<div className="flex items-center gap-2">
{resource.color ? (
<div className="size-5 rounded-sm" style={{ backgroundColor: resource.color }} />
) : (
<BoxIcon className="size-5 text-muted-foreground" />
)}
<h1 className="text-lg font-semibold">{resource.name}</h1>
</div>
<div className="flex items-center gap-2 text-sm text-muted-foreground">
<span
className={`text-[10px] font-semibold uppercase tracking-wider px-1.5 py-0.5 rounded ${typeColors[resource.type] ?? "bg-muted text-muted-foreground"}`}
>
{resource.type}
</span>
<span
className={`text-[10px] font-semibold uppercase tracking-wider px-1.5 py-0.5 rounded ${rarityColors[resource.rarity] ?? "bg-muted text-muted-foreground"}`}
>
{resource.rarity}
</span>
</div>
</div>
</div>
{resource.description && (
<Item variant="outline">
<ItemHeader>
<ItemTitle>Description</ItemTitle>
</ItemHeader>
<ItemContent className="prose-invert prose-sm max-w-none [&_p]:mb-2 [&_ul]:list-disc [&_ul]:ml-4 [&_ol]:list-decimal [&_ol]:ml-4 [&_strong]:text-foreground [&_em]:italic">
<RichText data={resource.description} />
</ItemContent>
</Item>
)}
<div className="grid grid-cols-2 md:grid-cols-4 gap-3">
<Item variant="muted" size="sm">
<ItemMedia variant="icon">
<CoinsIcon />
</ItemMedia>
<ItemContent>
<ItemTitle>Base Value</ItemTitle>
<ItemDescription>{resource.baseValue.toLocaleString()}</ItemDescription>
</ItemContent>
</Item>
<Item variant="muted" size="sm">
<ItemMedia variant="icon">
<TagIcon />
</ItemMedia>
<ItemContent>
<ItemTitle>Unit of Measure</ItemTitle>
<ItemDescription>{resource.unitOfMeasure}</ItemDescription>
</ItemContent>
</Item>
<Item variant="muted" size="sm">
<ItemMedia variant="icon">
<WeightIcon />
</ItemMedia>
<ItemContent>
<ItemTitle>Mass Per Unit</ItemTitle>
<ItemDescription>{resource.massPerUnit.toLocaleString()} kg</ItemDescription>
</ItemContent>
</Item>
<Item variant="muted" size="sm">
<ItemMedia variant="icon">
<LayersIcon />
</ItemMedia>
<ItemContent>
<ItemTitle>Stack Size</ItemTitle>
<ItemDescription>{resource.stackSize ?? 1}</ItemDescription>
</ItemContent>
</Item>
<Item variant="muted" size="sm">
<ItemMedia variant="icon">
<Grid3x3Icon />
</ItemMedia>
<ItemContent>
<ItemTitle>Grid</ItemTitle>
<ItemDescription>
{resource.gridWidth}×{resource.gridHeight}
</ItemDescription>
</ItemContent>
</Item>
<Item variant="muted" size="sm">
<ItemMedia variant="icon">
<LayersIcon />
</ItemMedia>
<ItemContent>
<ItemTitle>Stackable</ItemTitle>
<ItemDescription>{resource.stackable ? "Yes" : "No"}</ItemDescription>
</ItemContent>
</Item>
<Item variant="muted" size="sm">
<ItemMedia variant="icon">
<RotateCcwIcon />
</ItemMedia>
<ItemContent>
<ItemTitle>Rotatable</ItemTitle>
<ItemDescription>{resource.rotatable ? "Yes" : "No"}</ItemDescription>
</ItemContent>
</Item>
<Item variant="muted" size="sm">
<ItemMedia variant="icon">
<ScaleIcon />
</ItemMedia>
<ItemContent>
<ItemTitle>Tradeable</ItemTitle>
<ItemDescription>{resource.tradeable ? "Yes" : "No"}</ItemDescription>
</ItemContent>
</Item>
</div>
{allRelatedStructures.length > 0 && (
<section className="flex flex-col gap-3">
<h2 className="text-sm font-semibold uppercase tracking-wider text-muted-foreground">
Related Structures
</h2>
<div className="grid grid-cols-1 md:grid-cols-2 gap-2">
{allRelatedStructures.map((structure) => (
<Link key={structure.id} href={`/logistics/structures/${structure.id}`}>
<Item
variant="outline"
className="cursor-pointer group transition-all duration-200 hover:scale-[1.01]"
>
<ItemHeader>
<div className="flex items-center gap-2">
<ItemMedia variant="icon">
<Building2Icon />
</ItemMedia>
<ItemTitle>{structure.name}</ItemTitle>
</div>
</ItemHeader>
<ItemContent>
<ItemDescription className="line-clamp-1">
{convertLexicalToPlaintext({ data: structure.description }).slice(0, 120)}
</ItemDescription>
</ItemContent>
</Item>
</Link>
))}
</div>
</section>
)}
{craftingAssets.length > 0 && (
<section className="flex flex-col gap-3">
<h2 className="text-sm font-semibold uppercase tracking-wider text-muted-foreground">
Used in Crafting
</h2>
<div className="flex flex-wrap gap-2">
{craftingAssets.map((asset) => (
<Link key={asset.id} href={`/logistics/assets/${asset.id}`}>
<span className="text-xs font-medium bg-muted border border-border px-2.5 py-1 rounded-md hover:bg-accent/50 transition-colors">
{asset.name}
</span>
</Link>
))}
</div>
</section>
)}
</div>
);
}