"use client"; import { useRouter } from "next/navigation"; import { useTranslations } from "next-intl"; import { useState } from "react"; import { updateItemsBase } from "@/actions/items-base"; import Link from "@/components/link"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { useServerAction } from "@/hooks/use-server-action"; export type ItemsBaseEditValues = { id: number; spriteId: number; publicName: string; itemName: string; type: string; width: number; length: number; stackHeight: string; allowStack: number; allowSit: number; allowLay: number; allowWalk: number; allowGift: number; allowTrade: number; allowRecycle: number; allowMarketplaceSell: number; allowInventoryStack: number; interactionType: string; interactionModesCount: number; vendingIds: string; multiheight: string; customparams: string; }; const ALLOW_FLAGS: Array<{ key: keyof ItemsBaseEditValues; label: string }> = [ { key: "allowStack", label: "Stack" }, { key: "allowSit", label: "Sit" }, { key: "allowLay", label: "Lay" }, { key: "allowWalk", label: "Walk" }, { key: "allowGift", label: "Gift" }, { key: "allowTrade", label: "Trade" }, { key: "allowRecycle", label: "Recycle" }, { key: "allowMarketplaceSell", label: "Marketplace" }, { key: "allowInventoryStack", label: "Inv. stack" }, ]; export function ItemsBaseEditForm({ initial, canEdit, }: { initial: ItemsBaseEditValues; canEdit: boolean; }) { const t = useTranslations("pages.admin.items"); const router = useRouter(); const { run, isPending } = useServerAction(); const [form, setForm] = useState(initial); function setField( key: K, value: ItemsBaseEditValues[K], ) { setForm((prev) => ({ ...prev, [key]: value })); } function handleSave() { if (!canEdit || isPending) return; const { id, ...fields } = form; run(() => updateItemsBase({ id, fields }), { successMessage: t("saved"), onSuccess: () => router.refresh(), }); } return (

#{form.id}

{form.publicName || form.itemName || t("untitled")}

{canEdit ? ( ) : null}
{t("sectionIdentity")}
setField("publicName", e.target.value)} />
setField("itemName", e.target.value)} />
setField("type", e.target.value)} />
setField("spriteId", Number(e.target.value))} />
{t("sectionDims")}
setField("width", Number(e.target.value))} />
setField("length", Number(e.target.value))} />
setField("stackHeight", e.target.value)} />
setField("interactionType", e.target.value)} />
setField("interactionModesCount", Number(e.target.value)) } />
{t("sectionFlags")} {ALLOW_FLAGS.map(({ key, label }) => ( ))} {t("sectionExtra")}
setField("vendingIds", e.target.value)} />
setField("multiheight", e.target.value)} />
setField("customparams", e.target.value)} />
); }