Files
co-lite/solvway-admin-portal/app/components/locations/LocationTree.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

154 lines
5.1 KiB
TypeScript

"use client";
import { useMemo, useState, useEffect } from "react";
import { Plus, Minus } from "lucide-react";
import { cn } from "@/app/lib/utils";
import type { AdminLocationNode } from "@/app/lib/types";
import { useT } from "@/app/lib/i18n/MessagesProvider";
const INDENT = 24;
interface TreeNode {
node: AdminLocationNode;
children: TreeNode[];
}
function buildTree(nodes: AdminLocationNode[]): TreeNode | null {
const byId = new Map(nodes.map((n) => [n.id, { node: n, children: [] as TreeNode[] }]));
let root: TreeNode | null = null;
for (const wrapper of byId.values()) {
if (wrapper.node.parent === null) {
root = wrapper;
} else {
byId.get(wrapper.node.parent.id)?.children.push(wrapper);
}
}
return root;
}
function ancestorIdsOfMatches(nodes: AdminLocationNode[], term: string): Set<number> {
const byId = new Map(nodes.map((n) => [n.id, n]));
const ids = new Set<number>();
const lower = term.toLowerCase();
for (const n of nodes) {
if (n.name.toLowerCase().includes(lower) || n.code.toLowerCase().includes(lower)) {
let cur: AdminLocationNode | undefined = n;
while (cur) {
ids.add(cur.id);
cur = cur.parent ? byId.get(cur.parent.id) : undefined;
}
}
}
return ids;
}
function flatten(
root: TreeNode,
expanded: Set<number>,
depth = 0,
): { entry: TreeNode; depth: number }[] {
const row = { entry: root, depth };
if (root.children.length > 0 && expanded.has(root.node.id)) {
return [row, ...root.children.flatMap((c) => flatten(c, expanded, depth + 1))];
}
return [row];
}
export function LocationTree({
nodes,
selectedId,
onSelect,
searchTerm = "",
}: {
nodes: AdminLocationNode[];
selectedId: number | null;
onSelect: (id: number) => void;
searchTerm?: string;
}) {
const t = useT();
const root = useMemo(() => buildTree(nodes), [nodes]);
// The business tree is small (single-digit nodes today) so it starts fully expanded.
const [expanded, setExpanded] = useState<Set<number>>(() => new Set(nodes.map((n) => n.id)));
useEffect(() => {
if (!searchTerm) return;
// eslint-disable-next-line react-hooks/set-state-in-effect -- auto-expands ancestors of search matches so the match is reachable; a search-triggered state synchronization, not a cascade.
setExpanded((prev) => new Set([...prev, ...ancestorIdsOfMatches(nodes, searchTerm)]));
}, [searchTerm, nodes]);
if (!root) {
return (
<div role="status" className="p-4 text-sm text-muted-foreground">
{t("LocTreeEmpty")}
</div>
);
}
const rows = flatten(root, expanded);
return (
<div
role="tree"
aria-label={t("LocPageTitle")}
className="overflow-y-auto rounded-xl border bg-card"
>
{rows.map(({ entry, depth }) => {
const { node, children } = entry;
const hasChildren = children.length > 0;
const isExpanded = expanded.has(node.id);
const isSelected = selectedId === node.id;
return (
<div
key={node.id}
role="treeitem"
aria-level={depth + 1}
aria-expanded={hasChildren ? isExpanded : undefined}
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 }}
>
{hasChildren ? (
<button
type="button"
aria-label={isExpanded ? t("LocTreeCollapse", { name: node.name }) : t("LocTreeExpand", { name: node.name })}
onClick={(e) => {
e.stopPropagation();
setExpanded((prev) => {
const next = new Set(prev);
if (next.has(node.id)) next.delete(node.id);
else next.add(node.id);
return next;
});
}}
className="flex size-4 shrink-0 items-center justify-center rounded-sm border border-input bg-background text-muted-foreground hover:bg-accent focus-visible:ring-2 focus-visible:ring-ring focus-visible:outline-none"
>
{isExpanded ? (
<Minus aria-hidden="true" strokeWidth={3} className="size-2.5" />
) : (
<Plus aria-hidden="true" strokeWidth={3} className="size-2.5" />
)}
</button>
) : (
<span aria-hidden="true" className="size-4 shrink-0" />
)}
<span className="truncate font-medium">{node.name}</span>
<span className="ml-auto shrink-0 text-xs text-muted-foreground">{node.code}</span>
</div>
);
})}
</div>
);
}