"use client"; import { useState } from "react"; import { useSnap } from "@/lib/client"; import { Empty, Field, LiveRegion } from "@/components/ui"; import { Panel } from "@/components/portal"; import { LOCATION_KINDS, csvOf, locMap, locPath, locTree } from "@/lib/compute"; import { downloadCsv } from "@/lib/print"; /* Where garments live: rooms hold shelves, shelves hold bays. Moved here from Settings. Editing is admin-only on the server (location.save / location.delete); issuers see the tree read-only. */ export default function Locations() { const { s, isAdmin, mutate } = useSnap(); const [nl, setNl] = useState({ name: "", kind: "Shelf", parentId: "" }); const [msg, setMsg] = useState<{ text: string; err: boolean }>({ text: "", err: false }); const say = (r: { ok: true } | { ok: false; error: string }, ok: string) => setMsg(r.ok ? { text: ok, err: false } : { text: r.error, err: true }); const tree = locTree(s, true); // How many sizes sit on each location, so an empty shelf is obvious before it is removed. const locCounts: Record = {}; for (const k in s.placed) locCounts[s.placed[k]] = (locCounts[s.placed[k]] || 0) + 1; function exportLocations() { const byId = locMap(s); // Each row carries its full path as well as its own name: "Bay B3" alone is on every shelf. const rows = tree.map(({ loc }) => [loc.name, loc.kind, loc.parentId ? byId[loc.parentId]?.name ?? "" : "", locPath(byId, loc.id).map((l) => l.name).join(" · "), locCounts[loc.id] || 0]); downloadCsv(`threadcount-locations-${s.today}.csv`, csvOf(["Location", "Kind", "Inside", "Full path", "Sizes"], rows)); } const parentOpts = (exclude?: string) => tree.filter(({ loc }) => loc.id !== exclude).map(({ loc, depth }) => ); return (
{s.locations.length} location{s.locations.length === 1 ? "" : "s"}} foot={isAdmin ? (
{(c) => setNl({ ...nl, name: e.target.value })} placeholder="e.g. Shelf B" />} {(c) => } {(c) => }
) : undefined} > {tree.length === 0 ?
No locations yet.
: (
{isAdmin && } {tree.map(({ loc, depth }) => ( {isAdmin && ( )} ))}
LocationKindInsideSizesRemove
{loc.name} {loc.kind} {locCounts[loc.id] || 0}
)}
); }