1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
"use client";
import { useCallback } from "react";
import { cn } from "@/lib/utils";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import {
ChevronRight,
ChevronDown,
ChevronUp,
Folder,
FolderOpen,
File,
Pencil,
Plus,
ArrowUpDown,
EyeOff,
} from "lucide-react";
import type { MenuTreeNode } from "../types";
interface MenuTreeProps {
nodes: MenuTreeNode[];
onEdit: (node: MenuTreeNode) => void;
onMoveUp: (nodeId: number) => void;
onMoveDown: (nodeId: number) => void;
onMoveTo: (node: MenuTreeNode) => void;
onAddGroup: (parentId: number) => void;
expandedIds: Set<number>;
onExpandedIdsChange: (ids: Set<number>) => void;
isPending?: boolean;
}
interface TreeItemProps {
node: MenuTreeNode;
depth: number;
isFirst: boolean;
isLast: boolean;
onEdit: (node: MenuTreeNode) => void;
onMoveUp: (nodeId: number) => void;
onMoveDown: (nodeId: number) => void;
onMoveTo: (node: MenuTreeNode) => void;
onAddGroup: (parentId: number) => void;
isExpanded: boolean;
onToggleExpand: () => void;
isPending?: boolean;
}
function TreeItem({
node,
depth,
isFirst,
isLast,
onEdit,
onMoveUp,
onMoveDown,
onMoveTo,
onAddGroup,
isExpanded,
onToggleExpand,
isPending,
}: TreeItemProps) {
const isMenuGroup = node.nodeType === "menu_group";
const isGroup = node.nodeType === "group";
const isMenu = node.nodeType === "menu";
const isTopLevel = node.parentId === null;
const hasChildren = node.children && node.children.length > 0;
const isExpandable = isMenuGroup || isGroup;
// Move To is disabled for:
// - menu_group (always at top level, cannot be moved)
// - top-level menu (parentId === null, can only reorder with up/down)
const canMoveTo = !isMenuGroup && !isTopLevel;
const getIcon = () => {
if (isMenuGroup || isGroup) {
return isExpanded ? (
<FolderOpen className="h-4 w-4 text-amber-500" />
) : (
<Folder className="h-4 w-4 text-amber-500" />
);
}
return <File className="h-4 w-4 text-slate-500" />;
};
const getTypeLabel = () => {
switch (node.nodeType) {
case "menu_group": return "Menu Group";
case "group": return "Group";
case "menu": return "Menu";
default: return "";
}
};
return (
<div
className={cn(
"flex items-center gap-2 px-2 py-1.5 rounded-md border bg-background hover:bg-accent/50 transition-colors",
!node.isActive && "opacity-50 bg-muted/30 border-dashed"
)}
style={{ marginLeft: depth * 24 }}
>
{/* Expand/Collapse */}
{isExpandable ? (
<button
onClick={onToggleExpand}
className="p-0.5 hover:bg-accent rounded shrink-0"
>
{isExpanded ? (
<ChevronDown className="h-4 w-4" />
) : (
<ChevronRight className="h-4 w-4" />
)}
</button>
) : (
<div className="w-5 shrink-0" />
)}
{/* Icon */}
{getIcon()}
{/* Title */}
<span className={cn(
"flex-1 text-sm font-medium truncate min-w-0",
!node.isActive && "line-through text-muted-foreground"
)}>
{node.titleKo}
{node.titleEn && (
<span className="text-muted-foreground font-normal"> [{node.titleEn}]</span>
)}
</span>
{/* Hidden indicator */}
{!node.isActive && (
<EyeOff className="h-3.5 w-3.5 text-muted-foreground shrink-0" title="Hidden" />
)}
{/* Path (for menus) */}
{isMenu && node.menuPath && (
<span className="text-xs text-muted-foreground truncate max-w-[150px] shrink-0">
{node.menuPath}
</span>
)}
{/* Type Badge */}
<Badge variant={node.isActive ? "default" : "secondary"} className="text-xs shrink-0">
{getTypeLabel()}
</Badge>
{/* Active indicator */}
<div
className={cn(
"w-2 h-2 rounded-full shrink-0",
node.isActive ? "bg-green-500" : "bg-gray-400"
)}
title={node.isActive ? "Visible" : "Hidden"}
/>
{/* Actions */}
<div className="flex items-center gap-1 shrink-0">
{/* Move Up */}
<Button
variant="ghost"
size="icon"
className="h-7 w-7"
onClick={() => onMoveUp(node.id)}
disabled={isFirst || isPending}
title="Move Up"
>
<ChevronUp className="h-4 w-4" />
</Button>
{/* Move Down */}
<Button
variant="ghost"
size="icon"
className="h-7 w-7"
onClick={() => onMoveDown(node.id)}
disabled={isLast || isPending}
title="Move Down"
>
<ChevronDown className="h-4 w-4" />
</Button>
{/* Move To (different parent) - disabled for top level nodes */}
<Button
variant="ghost"
size="icon"
className="h-7 w-7"
onClick={() => onMoveTo(node)}
disabled={!canMoveTo || isPending}
title={canMoveTo ? "Move To..." : "Cannot move top-level items"}
>
<ArrowUpDown className="h-4 w-4" />
</Button>
{/* Edit */}
<Button
variant="ghost"
size="icon"
className="h-7 w-7"
onClick={() => onEdit(node)}
disabled={isPending}
title="Edit"
>
<Pencil className="h-4 w-4" />
</Button>
{/* Add Sub-Group (for menu groups only) */}
{isMenuGroup && (
<Button
variant="ghost"
size="icon"
className="h-7 w-7"
onClick={() => onAddGroup(node.id)}
disabled={isPending}
title="Add Sub-Group"
>
<Plus className="h-4 w-4" />
</Button>
)}
</div>
</div>
);
}
export function MenuTree({
nodes,
onEdit,
onMoveUp,
onMoveDown,
onMoveTo,
onAddGroup,
expandedIds,
onExpandedIdsChange,
isPending,
}: MenuTreeProps) {
const toggleExpand = useCallback((nodeId: number) => {
const next = new Set(expandedIds);
if (next.has(nodeId)) {
next.delete(nodeId);
} else {
next.add(nodeId);
}
onExpandedIdsChange(next);
}, [expandedIds, onExpandedIdsChange]);
const renderTree = (nodeList: MenuTreeNode[], depth: number) => {
return nodeList.map((node, index) => {
const isExpanded = expandedIds.has(node.id);
const isExpandable = node.nodeType === "menu_group" || node.nodeType === "group";
const hasChildren = node.children && node.children.length > 0;
return (
<div key={node.id} className="space-y-1">
<TreeItem
node={node}
depth={depth}
isFirst={index === 0}
isLast={index === nodeList.length - 1}
onEdit={onEdit}
onMoveUp={onMoveUp}
onMoveDown={onMoveDown}
onMoveTo={onMoveTo}
onAddGroup={onAddGroup}
isExpanded={isExpanded}
onToggleExpand={() => toggleExpand(node.id)}
isPending={isPending}
/>
{isExpandable && isExpanded && hasChildren && (
<div className="space-y-1">
{renderTree(node.children!, depth + 1)}
</div>
)}
</div>
);
});
};
return <div className="space-y-1">{renderTree(nodes, 0)}</div>;
}
|