135 lines
4.3 KiB
TypeScript
135 lines
4.3 KiB
TypeScript
"use client";
|
||
|
||
import { useMemo, useState } from "react";
|
||
import type { Asset } from "@/payload-types";
|
||
import { Button } from "@/components/ui/button";
|
||
import {
|
||
Dialog,
|
||
DialogContent,
|
||
DialogDescription,
|
||
DialogFooter,
|
||
DialogHeader,
|
||
DialogTitle,
|
||
} from "@/components/ui/dialog";
|
||
import { getAssetTypeLabel, type LoadoutSlotConfig, type LockerGridItem } from "@/lib/locker";
|
||
import { getAssetIcon } from "./LockerGrid";
|
||
import { cn } from "@/lib/utils";
|
||
|
||
interface WardrobeSlotPickerProps {
|
||
open: boolean;
|
||
onOpenChange: (open: boolean) => void;
|
||
slot: LoadoutSlotConfig;
|
||
items: LockerGridItem[];
|
||
currentValue: number | Asset | null;
|
||
busy: boolean;
|
||
onPick: (assetId: number | null) => Promise<{ success: boolean; error?: string }>;
|
||
}
|
||
|
||
function resolveCurrentId(value: number | Asset | null): number | null {
|
||
if (value == null) return null;
|
||
if (typeof value === "object" && "id" in value) return value.id;
|
||
return value;
|
||
}
|
||
|
||
export function WardrobeSlotPicker({
|
||
open,
|
||
onOpenChange,
|
||
slot,
|
||
items,
|
||
currentValue,
|
||
busy,
|
||
onPick,
|
||
}: WardrobeSlotPickerProps) {
|
||
const [error, setError] = useState<string | null>(null);
|
||
const [pending, setPending] = useState(false);
|
||
|
||
const options = useMemo(() => {
|
||
const types = new Set(slot.assetTypes);
|
||
const seen = new Set<number>();
|
||
return items.filter((entry) => {
|
||
if (seen.has(entry.assetId)) return false;
|
||
if (!types.has(entry.asset.assetType)) return false;
|
||
seen.add(entry.assetId);
|
||
return true;
|
||
});
|
||
}, [items, slot.assetTypes]);
|
||
|
||
const currentId = resolveCurrentId(currentValue);
|
||
|
||
async function handlePick(assetId: number | null) {
|
||
setError(null);
|
||
setPending(true);
|
||
try {
|
||
const res = await onPick(assetId);
|
||
if (!res.success) {
|
||
setError(res.error ?? "Failed to equip item.");
|
||
}
|
||
} catch (e) {
|
||
setError(e instanceof Error ? e.message : "An unexpected error occurred.");
|
||
} finally {
|
||
setPending(false);
|
||
}
|
||
}
|
||
|
||
return (
|
||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||
<DialogContent className="max-h-[85vh] overflow-y-auto">
|
||
<DialogHeader>
|
||
<DialogTitle>{slot.label} Slot</DialogTitle>
|
||
<DialogDescription>
|
||
Choose an item from your locker to equip in this slot, or clear it.
|
||
</DialogDescription>
|
||
</DialogHeader>
|
||
|
||
{error && (
|
||
<div className="rounded-md border border-red-500/50 bg-red-500/10 px-3 py-2 text-sm text-red-400">
|
||
{error}
|
||
</div>
|
||
)}
|
||
|
||
<div className="flex flex-col gap-1.5">
|
||
{options.length === 0 && (
|
||
<p className="text-sm text-muted-foreground py-2">
|
||
No matching items in your locker for this slot.
|
||
</p>
|
||
)}
|
||
{options.map((entry) => {
|
||
const selected = entry.assetId === currentId;
|
||
return (
|
||
<button
|
||
key={entry.assetId}
|
||
type="button"
|
||
disabled={busy || pending}
|
||
onClick={() => handlePick(entry.assetId)}
|
||
className={cn(
|
||
"flex items-center gap-3 rounded-md border px-3 py-2 text-left text-sm transition-colors",
|
||
selected ? "border-primary bg-primary/10" : "border-border hover:bg-muted",
|
||
)}
|
||
>
|
||
<span className="text-muted-foreground">{getAssetIcon(entry.asset.assetType)}</span>
|
||
<span className="flex-1 min-w-0">
|
||
<span className="block font-medium truncate">{entry.asset.name}</span>
|
||
<span className="block text-xs text-muted-foreground">
|
||
{getAssetTypeLabel(entry.asset.assetType)} · {entry.quantity.toLocaleString()}×
|
||
</span>
|
||
</span>
|
||
{selected && <span className="text-xs font-semibold text-primary">Equipped</span>}
|
||
</button>
|
||
);
|
||
})}
|
||
</div>
|
||
|
||
<DialogFooter>
|
||
<Button variant="outline" disabled={busy || pending} onClick={() => onOpenChange(false)}>
|
||
Cancel
|
||
</Button>
|
||
{currentId != null && (
|
||
<Button variant="ghost" disabled={busy || pending} onClick={() => handlePick(null)}>
|
||
Clear Slot
|
||
</Button>
|
||
)}
|
||
</DialogFooter>
|
||
</DialogContent>
|
||
</Dialog>
|
||
);
|
||
}
|