Files
co-lite/solvway-admin-portal/app/components/warehouses/WarehouseTree.tsx
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session,
api-gateway, mobile-api, services-auth, services-system,
services-counter, services-member, services-log, services-email.
solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal
from tastefully 8bd93d15. Copied as tracked files only.

Literal secrets replaced with environment placeholders:
- services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY,
  MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default)
- services-system: JUMIO_TOKEN, JUMIO_SECRET (optional)
- services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed
- core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default
  removed, ipgeolocation key read from IPGEOLOCATION_API_KEY
- services-system EkycAppConfig: commented Jumio credentials removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 15:55:45 +08:00

100 lines
3.4 KiB
TypeScript

"use client";
import { useMemo } from "react";
import { cn } from "@/app/lib/utils";
import { Badge } from "@/app/components/ui/badge";
import { STATUS_TONE, warehouseStatusLabel, warehouseTypeLabel } from "./warehouseLabels";
import type { AdminWarehouse } from "@/app/lib/types";
import { useT } from "@/app/lib/i18n/MessagesProvider";
const INDENT = 24;
interface WarehouseTreeNode {
node: AdminWarehouse;
children: WarehouseTreeNode[];
}
/**
* Builds a forest from a flat AdminWarehouse[] by grouping on `parentId` (mirrors
* LocationTree's `buildTree`, which groups on `parent.id` — here there is no nested
* `parent` object, just a `parentId`). Roots are nodes whose `parentId` is null/absent,
* or whose parent isn't present in the list (orphan safety).
*/
function buildForest(warehouses: AdminWarehouse[]): WarehouseTreeNode[] {
const byId = new Map(warehouses.map((w) => [w.id, { node: w, children: [] as WarehouseTreeNode[] }]));
const roots: WarehouseTreeNode[] = [];
for (const wrapper of byId.values()) {
const parentId = wrapper.node.parentId;
const parent = parentId != null ? byId.get(parentId) : undefined;
if (parent) {
parent.children.push(wrapper);
} else {
roots.push(wrapper);
}
}
return roots;
}
function flatten(entry: WarehouseTreeNode, depth = 0): { entry: WarehouseTreeNode; depth: number }[] {
return [{ entry, depth }, ...entry.children.flatMap((c) => flatten(c, depth + 1))];
}
export function WarehouseTree({
warehouses,
selectedId,
onSelect,
}: {
warehouses: AdminWarehouse[];
selectedId: number | null;
onSelect: (id: number) => void;
}) {
const t = useT();
const roots = useMemo(() => buildForest(warehouses), [warehouses]);
if (roots.length === 0) {
return (
<div role="status" className="p-4 text-sm text-muted-foreground">
{t("WhEmpty")}
</div>
);
}
const rows = roots.flatMap((r) => flatten(r));
return (
<div role="tree" aria-label={t("WhTreeLabel")} className="overflow-y-auto rounded-xl border bg-card">
{rows.map(({ entry, depth }) => {
const { node } = entry;
const isSelected = selectedId === node.id;
return (
<div
key={node.id}
role="treeitem"
aria-level={depth + 1}
aria-selected={isSelected}
aria-label={`${node.name} (${node.code})`}
tabIndex={0}
onClick={() => onSelect(node.id)}
onKeyDown={(e) => {
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
onSelect(node.id);
}
}}
className={cn(
"flex h-9 cursor-pointer items-center gap-2 border-b px-3 text-sm last:border-b-0 hover:bg-muted focus-visible:ring-2 focus-visible:ring-ring focus-visible:outline-none",
isSelected && "bg-accent",
)}
style={{ paddingLeft: depth * INDENT + 12 }}
>
<span className="truncate font-medium">{node.name}</span>
<span className="shrink-0 text-xs text-muted-foreground">{node.code}</span>
<span className="shrink-0 text-xs text-muted-foreground">{warehouseTypeLabel(node.type, t)}</span>
<Badge variant={STATUS_TONE[node.status ?? ""] ?? "neutral"} className="ml-auto">{warehouseStatusLabel(node.status, t)}</Badge>
</div>
);
})}
</div>
);
}