58 lines
2 KiB
TypeScript
58 lines
2 KiB
TypeScript
"use client";
|
|
|
|
import { useState } from "react";
|
|
import { Faction, GameStructure, Structure } from "@/payload-types";
|
|
import { StructureCard } from "@/components/frontend/storage/StructureCard";
|
|
import { StorageFilters } from "@/components/frontend/storage/StorageFilters";
|
|
|
|
interface StorageGridProps {
|
|
structures: (GameStructure & { type: Structure; faction?: Faction | null })[];
|
|
categories: Structure["category"][];
|
|
factions: Faction[];
|
|
}
|
|
|
|
export function StorageGrid({ structures, categories, factions }: StorageGridProps) {
|
|
const [filters, setFilters] = useState<{
|
|
category: Structure["category"] | null;
|
|
factionId: number | null;
|
|
}>({ category: null, factionId: null });
|
|
|
|
const filtered = structures.filter((s) => {
|
|
if (filters.category && s.type.category !== filters.category) return false;
|
|
if (filters.factionId) {
|
|
const factionId =
|
|
typeof s.faction === "object" && s.faction
|
|
? s.faction.id
|
|
: typeof s.faction === "number"
|
|
? s.faction
|
|
: null;
|
|
if (factionId !== filters.factionId) return false;
|
|
}
|
|
return true;
|
|
});
|
|
|
|
return (
|
|
<div className="flex flex-col gap-4">
|
|
<StorageFilters categories={categories} factions={factions} onFilterChange={setFilters} />
|
|
{filtered.length === 0 ? (
|
|
<div className="flex flex-col items-center justify-center py-12 text-muted-foreground">
|
|
<p className="text-sm">No structures found.</p>
|
|
{filters.category || filters.factionId ? (
|
|
<button
|
|
onClick={() => setFilters({ category: null, factionId: null })}
|
|
className="text-sm text-primary hover:underline mt-1"
|
|
>
|
|
Clear filters
|
|
</button>
|
|
) : null}
|
|
</div>
|
|
) : (
|
|
<div className="flex flex-col gap-2">
|
|
{filtered.map((structure) => (
|
|
<StructureCard key={structure.id} structure={structure} />
|
|
))}
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|