1
0
Fork 0
polaris-task-force/src/components/frontend/locker/LockerView.tsx
Z8MB1E f1ff0436dd fix(locker): keep inventory workspace visible
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
2026-09-01 17:18:08 -04:00

353 lines
13 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.

"use client";
import { useCallback, useEffect, useMemo, useState } from "react";
import { useRouter } from "next/navigation";
import type { Asset, Loadout, LockerStorage } from "@/payload-types";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { Item, ItemContent, ItemHeader, ItemTitle } from "@/components/ui/item";
import { EventLedger } from "@/components/frontend/storage/EventLedger";
import { LockerGrid } from "./LockerGrid";
import { LockerGridToolbar } from "./LockerGridToolbar";
import { AddLockerItemDialog } from "./AddLockerItemDialog";
import { LoadoutsPanel } from "./LoadoutsPanel";
import { WardrobePanel } from "./WardrobePanel";
import { EquipmentEditorDialog } from "./EquipmentEditorDialog";
import { LockerSearch } from "./LockerSearch";
import { getAssetTypeLabel, type LockerGridItem } from "@/lib/locker";
import type { LockerSearchEntry } from "@/lib/locker/search";
import {
attachItemFromGrid,
autoArrangeLocker,
mergeLockerStacks,
moveLockerItem,
removeLockerItem,
rotateLockerItem,
splitLockerStack,
} from "@/app/(frontend)/locker/actions";
import { usePageCommands } from "@/components/command-palette/CommandPaletteContext";
import { LayoutGridIcon } from "lucide-react";
import { LockerCharacterPanel } from "./LockerCharacterPanel";
interface LockerViewProps {
locker: LockerStorage;
gridWidth: number;
gridHeight: number;
loadouts: Loadout[];
assetsCatalog: Asset[];
isManager: boolean;
userName: string;
}
export function LockerView({
locker,
gridWidth,
gridHeight,
loadouts,
assetsCatalog,
isManager,
userName,
}: LockerViewProps) {
const router = useRouter();
const [tab, setTab] = useState("inventory");
const [viewMode, setViewMode] = useState<"grid" | "list">("grid");
const [selectedId, setSelectedId] = useState<string | null>(null);
const [customizeId, setCustomizeId] = useState<string | null>(null);
const [highlightId, setHighlightId] = useState<string | null>(null);
const [eventLogKey, setEventLogKey] = useState(0);
const items: LockerGridItem[] = useMemo(
() =>
(locker.items ?? [])
.filter((entry) => entry.asset && typeof entry.asset === "object" && "id" in entry.asset)
.map((entry) => {
const asset = entry.asset as Asset;
return {
id: entry.id ?? `${asset.id}-${entry.gridX}-${entry.gridY}`,
assetId: asset.id,
quantity: entry.quantity,
gridX: entry.gridX ?? 0,
gridY: entry.gridY ?? 0,
rotated: entry.rotated ?? false,
asset,
equipment: entry.equipment ?? undefined,
};
}),
[locker.items],
);
const selectedItem = useMemo(
() => items.find((i) => i.id === selectedId) ?? null,
[items, selectedId],
);
const customizeItem = useMemo(
() => items.find((i) => i.id === customizeId) ?? null,
[items, customizeId],
);
useEffect(() => {
if (!highlightId) return;
const timer = setTimeout(() => setHighlightId(null), 3000);
return () => clearTimeout(timer);
}, [highlightId]);
const refresh = useCallback(() => {
router.refresh();
setEventLogKey((k) => k + 1);
}, [router]);
const handleMoveItem = useCallback(
async (entryId: string, gridX: number, gridY: number, rotated?: boolean) => {
const result = await moveLockerItem(entryId, gridX, gridY, rotated);
if (result.success) refresh();
},
[refresh],
);
const handleMergeStacks = useCallback(
async (sourceId: string, targetId: string) => {
const result = await mergeLockerStacks(sourceId, targetId);
if (result.success) refresh();
},
[refresh],
);
const handleRotateItem = useCallback(
async (entryId: string) => {
const result = await rotateLockerItem(entryId);
if (result.success) refresh();
},
[refresh],
);
const handleRemoveItem = useCallback(
async (entryId: string) => {
const result = await removeLockerItem(entryId);
if (result.success) {
setSelectedId(null);
refresh();
}
},
[refresh],
);
const handleSplitStack = useCallback(
async (entryId: string, amount: number) => {
const result = await splitLockerStack(entryId, amount);
if (result.success) refresh();
},
[refresh],
);
const handleAutoArrange = useCallback(async () => {
const result = await autoArrangeLocker();
if (result.success) refresh();
}, [refresh]);
usePageCommands([
{
id: "locker:auto-arrange",
group: "Locker",
label: "Auto-Arrange Locker",
icon: LayoutGridIcon,
keywords: ["arrange", "organize", "sort", "grid"],
onSelect: () => void handleAutoArrange(),
},
]);
const handleAttachItem = useCallback(
async (weaponEntryId: string, attachmentEntryId: string) => {
const result = await attachItemFromGrid(weaponEntryId, attachmentEntryId);
if (result.success) refresh();
return result;
},
[refresh],
);
const handleSearchSelect = useCallback(
(entry: LockerSearchEntry) => {
setViewMode("grid");
setTab("inventory");
const gridItem = items.find((i) => i.assetId === entry.assetId) ?? null;
if (gridItem && entry.asset.assetType === "weapon") {
setSelectedId(gridItem.id);
setCustomizeId(gridItem.id);
return;
}
const parentId = entry.locations.find(
(l) => (l.kind === "attached" || l.kind === "skin") && l.parentEntryId,
)?.parentEntryId;
if (parentId) {
const parent = items.find((i) => i.id === parentId);
if (parent && parent.asset.assetType === "weapon") {
setSelectedId(parent.id);
setCustomizeId(parent.id);
return;
}
}
if (gridItem) setHighlightId(gridItem.id);
},
[items],
);
return (
<Tabs
value={tab}
onValueChange={setTab}
className="flex min-w-0 w-full max-w-none flex-col gap-4"
>
<div className="flex min-w-0 flex-col gap-3 border-b border-border pb-3 sm:flex-row sm:items-center sm:justify-between">
<TabsList variant="line" aria-label="Locker sections" className="w-full sm:w-auto">
<TabsTrigger value="wardrobe">WARDROBE</TabsTrigger>
<TabsTrigger value="inventory">INVENTORY</TabsTrigger>
<TabsTrigger value="loadouts">LOADOUTS</TabsTrigger>
</TabsList>
<div className="flex shrink-0 items-center gap-3 self-start sm:self-auto">
<span className="hidden font-mono text-[10px] tabular-nums text-muted-foreground/60 lg:inline">
{items.length.toLocaleString()} ITEMS · {gridWidth}×{gridHeight} GRID
</span>
<LockerSearch items={items} loadouts={loadouts} onSelect={handleSearchSelect} />
</div>
</div>
<TabsContent value="wardrobe" className="mt-0">
<WardrobePanel
userName={userName}
loadouts={loadouts}
equippedLoadout={loadouts.find((l) => l.equipped) ?? null}
items={items}
/>
</TabsContent>
<TabsContent value="inventory" className="mt-0 flex min-w-0 w-full flex-col gap-3">
<div className="flex min-w-0 flex-col gap-2 border-y border-border/50 py-2 sm:flex-row sm:items-center sm:justify-between">
<div className="min-w-0">
<p className="font-mono text-xs font-medium uppercase tracking-[0.18em] text-foreground/90">
STASH // PERSONAL STORAGE
</p>
<p className="text-xs text-muted-foreground/70">
DRAG TO REPOSITION · SELECT TO INSPECT · R TO ROTATE
</p>
</div>
{isManager && (
<div className="shrink-0">
<AddLockerItemDialog assets={assetsCatalog} />
</div>
)}
</div>
<div className="grid min-w-0 items-start gap-4 xl:grid-cols-[minmax(24rem,30rem)_minmax(0,1fr)]">
<LockerCharacterPanel loadouts={loadouts} items={items} />
<div className="grid min-w-0 items-start gap-3 2xl:grid-cols-[minmax(0,1fr)_18rem] sticky top-5">
<div className="min-w-0">
{items.length === 0 ? (
<Item variant="outline" className="rounded-sm border border-border/30 bg-card/40">
<ItemHeader>
<ItemTitle className="text-muted-foreground/70">LOCKER EMPTY</ItemTitle>
</ItemHeader>
<ItemContent>
<p className="text-sm text-muted-foreground/60">
{isManager
? "Your locker is empty. Add items to get started."
: "Your locker is empty. Reach out to logistics to have equipment issued."}
</p>
</ItemContent>
</Item>
) : viewMode === "grid" ? (
<LockerGrid
gridWidth={gridWidth}
gridHeight={gridHeight}
items={items}
onMoveItem={handleMoveItem}
onMergeStacks={handleMergeStacks}
onRotateItem={handleRotateItem}
onRemoveItem={handleRemoveItem}
onSelectItem={(item) => setSelectedId(item?.id ?? null)}
selectedItem={selectedItem}
highlightId={highlightId}
onAttachItem={handleAttachItem}
/>
) : (
<Item
variant="outline"
className="rounded-sm border border-border/30 bg-card/40 p-0"
>
<ItemContent className="gap-0 p-1.5">
<div className="flex flex-col">
{items.map((entry, index) => (
<div key={entry.id}>
<button
type="button"
className="flex w-full items-center gap-3 rounded-sm px-2 py-2 text-left text-sm transition-colors hover:bg-muted/40 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-inset motion-reduce:transition-none"
onClick={() => setSelectedId(entry.id)}
aria-label={`Select ${entry.equipment?.customName?.trim() || entry.asset.name}`}
>
<span className="min-w-0 flex-1 truncate font-medium text-foreground/90">
{entry.equipment?.customName?.trim() || entry.asset.name}
</span>
<span className="shrink-0 font-mono text-[10px] text-muted-foreground/50 uppercase">
{getAssetTypeLabel(entry.asset.assetType)}
</span>
<span className="shrink-0 font-mono text-xs tabular-nums text-muted-foreground/50">
{entry.quantity.toLocaleString()}×
</span>
</button>
{index < items.length - 1 && (
<div className="border-t border-border/30" />
)}
</div>
))}
</div>
</ItemContent>
</Item>
)}
</div>
<aside
className="selection-rail flex min-w-0 flex-col gap-3 2xl:sticky 2xl:top-3"
aria-label="Selected item and locker activity"
>
<LockerGridToolbar
gridWidth={gridWidth}
gridHeight={gridHeight}
items={items}
selectedItem={selectedItem}
viewMode={viewMode}
onViewModeChange={setViewMode}
onAutoArrange={handleAutoArrange}
onRotate={handleRotateItem}
onRemove={handleRemoveItem}
onSplit={handleSplitStack}
onCustomize={() => setCustomizeId(selectedItem?.id ?? null)}
/>
<EventLedger
targetCollection="locker-storages"
targetId={locker.id}
refreshKey={eventLogKey}
/>
</aside>
</div>
</div>
</TabsContent>
<TabsContent value="loadouts" className="mt-0">
<LoadoutsPanel loadouts={loadouts} ownedItems={items} />
</TabsContent>
<EquipmentEditorDialog
item={customizeItem}
items={items}
open={!!customizeItem}
onOpenChange={(open) => {
if (!open) setCustomizeId(null);
}}
/>
</Tabs>
);
}