699ea6e8
杨鑫
完善打印逻辑
|
1
|
import React, { useMemo, useState } from 'react';
|
884054fb
“wangming”
项目初始化
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
import {
LayoutDashboard,
Tag,
MapPin,
Users,
Package,
FileText,
HelpCircle,
LogOut,
ChevronDown,
ChevronRight,
Layers,
Type,
FileBox,
Settings
} from 'lucide-react';
import { cn } from '../ui/utils';
import { ScrollArea } from '../ui/scroll-area';
import logo from "figma:asset/773f0c39e1986271e9144596caac519f934a6ae6.png";
|
699ea6e8
杨鑫
完善打印逻辑
|
21
|
import type { CurrentUserMenuNodeDto } from '../../types/authSession';
|
884054fb
“wangming”
项目初始化
|
22
23
24
25
|
interface SidebarProps {
currentView: string;
setCurrentView: (view: string) => void;
|
699ea6e8
杨鑫
完善打印逻辑
|
26
27
|
menus?: CurrentUserMenuNodeDto[];
onLogout?: () => void;
|
884054fb
“wangming”
项目初始化
|
28
29
|
}
|
699ea6e8
杨鑫
完善打印逻辑
|
30
31
32
33
34
35
36
|
const FALLBACK_ICONS = {
Dashboard: LayoutDashboard,
Labeling: Tag,
Labels: Tag,
"Label Categories": Layers,
"Label Types": Type,
"Label Templates": FileBox,
|
ef6b3255
杨鑫
修改BUG
|
37
|
"Multiple Option Sets": Settings,
|
699ea6e8
杨鑫
完善打印逻辑
|
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
"Multiple Options": Settings,
"Location Manager": MapPin,
"Account Management": Users,
"Menu Management": Package,
"System Menu": Settings,
Reports: FileText,
Support: HelpCircle,
"Log Out": LogOut,
} as const;
function normalizeLabel(name: string) {
return name.trim() || "N/A";
}
|
ef6b3255
杨鑫
修改BUG
|
52
53
54
55
56
57
|
function displayMenuLabel(name: string) {
const s = normalizeLabel(name);
if (s === "Multiple Options") return "Multiple Option Sets";
return s;
}
|
699ea6e8
杨鑫
完善打印逻辑
|
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
function pickIconKeyFromNode(node: CurrentUserMenuNodeDto, label: string): keyof typeof FALLBACK_ICONS | null {
const raw = String((node as any)?.menuIcon ?? (node as any)?.MenuIcon ?? "").trim();
if (raw && raw in FALLBACK_ICONS) return raw as keyof typeof FALLBACK_ICONS;
if (label in FALLBACK_ICONS) return label as keyof typeof FALLBACK_ICONS;
return null;
}
function MenuNode({
node,
level,
currentKey,
onSelect,
}: {
node: CurrentUserMenuNodeDto;
level: number;
currentKey: string;
onSelect: (key: string) => void;
}) {
const [open, setOpen] = React.useState(true);
const children = node.children ?? [];
const isDir = (node.menuType ?? 0) === 0 || children.length > 0;
const label = normalizeLabel(String(node.menuName ?? node.routerName ?? node.routeUrl ?? node.id ?? ""));
|
ef6b3255
杨鑫
修改BUG
|
80
81
82
|
const viewKey = label === "Multiple Options" ? "Multiple Option Sets" : label;
const active = currentKey === viewKey;
const iconKey = pickIconKeyFromNode(node, viewKey);
|
699ea6e8
杨鑫
完善打印逻辑
|
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
const Icon = iconKey ? FALLBACK_ICONS[iconKey] : null;
if (isDir) {
return (
<div className="space-y-1">
<button
onClick={() => setOpen((x) => !x)}
className={cn(
"w-full flex items-center justify-between px-4 py-2.5 text-sm font-medium rounded-lg transition-colors",
"hover:bg-blue-800/50 text-blue-100",
level > 0 && "ml-1",
)}
style={{ paddingLeft: 16 + level * 12 }}
>
<div className="flex items-center gap-3 min-w-0">
{level === 0 && Icon ? <Icon className="w-4 h-4 shrink-0" /> : <div className="w-1.5 h-1.5 rounded-full bg-blue-200 shrink-0" />}
|
ef6b3255
杨鑫
修改BUG
|
99
|
<span className="truncate">{displayMenuLabel(label)}</span>
|
699ea6e8
杨鑫
完善打印逻辑
|
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
</div>
{open ? <ChevronDown className="w-4 h-4" /> : <ChevronRight className="w-4 h-4" />}
</button>
{open && children.length > 0 && (
<div className="space-y-1">
{children.map((c) => (
<MenuNode key={String(c.id ?? Math.random())} node={c} level={level + 1} currentKey={currentKey} onSelect={onSelect} />
))}
</div>
)}
</div>
);
}
return (
<button
onClick={() => onSelect(label)}
className={cn(
"w-full flex items-center gap-3 px-4 py-2.5 text-sm font-medium rounded-lg transition-colors",
active ? "bg-blue-700 text-white shadow-md shadow-blue-900/20" : "text-blue-100 hover:bg-blue-800 hover:text-white",
)}
style={{ paddingLeft: 16 + level * 12 }}
>
{level === 0 && Icon ? <Icon className="w-4 h-4 shrink-0" /> : <div className="w-1 h-1 rounded-full bg-current" />}
|
ef6b3255
杨鑫
修改BUG
|
124
|
<span className="truncate">{displayMenuLabel(label)}</span>
|
699ea6e8
杨鑫
完善打印逻辑
|
125
126
127
128
129
|
</button>
);
}
export function Sidebar({ currentView, setCurrentView, menus, onLogout }: SidebarProps) {
|
884054fb
“wangming”
项目初始化
|
130
131
|
const [labelingOpen, setLabelingOpen] = useState(true);
|
699ea6e8
杨鑫
完善打印逻辑
|
132
|
const fallbackMenuItems = [
|
884054fb
“wangming”
项目初始化
|
133
134
135
136
137
138
139
140
141
142
143
144
145
|
{ name: 'Dashboard', icon: LayoutDashboard, type: 'item' },
{ type: 'header', name: 'MODULES' },
{
name: 'Labeling',
icon: Tag,
type: 'sub',
isOpen: labelingOpen,
toggle: () => setLabelingOpen(!labelingOpen),
children: [
{ name: 'Labels', icon: Tag },
{ name: 'Label Categories', icon: Layers },
{ name: 'Label Types', icon: Type },
{ name: 'Label Templates', icon: FileBox },
|
ef6b3255
杨鑫
修改BUG
|
146
|
{ name: 'Multiple Option Sets', icon: Settings },
|
884054fb
“wangming”
项目初始化
|
147
148
149
|
]
},
{ type: 'header', name: 'MANAGEMENT' },
|
884054fb
“wangming”
项目初始化
|
150
|
{ name: 'Account Management', icon: Users, type: 'item' },
|
de8956f8
杨鑫
平台端 门店,菜单,角色
|
151
152
|
{ name: 'Menu Management', icon: Package, type: 'item' },
{ name: 'System Menu', icon: Settings, type: 'item' },
|
884054fb
“wangming”
项目初始化
|
153
154
155
156
157
|
{ name: 'Reports', icon: FileText, type: 'item' },
{ name: 'Support', icon: HelpCircle, type: 'item' },
{ name: 'Log Out', icon: LogOut, type: 'item' },
];
|
699ea6e8
杨鑫
完善打印逻辑
|
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
|
const hasRemoteMenus = (menus?.length ?? 0) > 0;
const logoutHandler = () => (onLogout ? onLogout() : setCurrentView("Log Out"));
const remoteMenuNodes = useMemo(() => menus ?? [], [menus]);
const { moduleMenus, accountMenus } = useMemo(() => {
const ACCOUNT_LABELS = new Set([
"Location Manager",
"Account Management",
"Menu Management",
"System Menu",
"Reports",
"Support",
]);
const roots = remoteMenuNodes;
const moduleMenus: CurrentUserMenuNodeDto[] = [];
const accountMenus: CurrentUserMenuNodeDto[] = [];
for (const n of roots) {
const label = normalizeLabel(String(n.menuName ?? n.routerName ?? n.routeUrl ?? n.id ?? ""));
if (label === "Management") {
const children = n.children ?? [];
for (const c of children) {
const cl = normalizeLabel(String(c.menuName ?? c.routerName ?? c.routeUrl ?? c.id ?? ""));
if (ACCOUNT_LABELS.has(cl)) accountMenus.push(c);
else moduleMenus.push(c);
}
continue;
}
if (ACCOUNT_LABELS.has(label)) {
accountMenus.push(n);
continue;
}
moduleMenus.push(n);
}
return { moduleMenus, accountMenus };
}, [remoteMenuNodes]);
|
884054fb
“wangming”
项目初始化
|
198
199
200
201
202
203
204
205
|
return (
<div className="w-64 bg-[#1e3a8a] text-white flex flex-col h-screen border-r border-blue-800 shadow-xl z-20 shrink-0">
<div className="flex items-center justify-center border-b border-blue-800/50 bg-white px-4 shrink-0" style={{ height: 90 }}>
<img src={logo} alt="MedVantage" className="h-16 w-auto object-contain" />
</div>
<ScrollArea className="flex-1 py-4">
<div className="px-3 space-y-1">
|
699ea6e8
杨鑫
完善打印逻辑
|
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
|
{hasRemoteMenus ? (
<>
<button
onClick={() => setCurrentView("Dashboard")}
className={cn(
"w-full flex items-center gap-3 px-4 py-2.5 text-sm font-medium rounded-lg transition-colors",
currentView === "Dashboard" ? "bg-blue-700 text-white shadow-md shadow-blue-900/20" : "text-blue-100 hover:bg-blue-800 hover:text-white",
)}
>
<LayoutDashboard className="w-4 h-4" />
Dashboard
</button>
<div className="px-4 py-2 mt-4 text-xs font-semibold text-blue-300 uppercase tracking-wider">MODULES</div>
{moduleMenus.map((n) => (
<MenuNode key={String(n.id ?? Math.random())} node={n} level={0} currentKey={currentView} onSelect={setCurrentView} />
))}
<div className="px-4 py-2 mt-4 text-xs font-semibold text-blue-300 uppercase tracking-wider">MANAGEMENT</div>
{accountMenus.map((n) => (
<MenuNode key={String(n.id ?? Math.random())} node={n} level={0} currentKey={currentView} onSelect={setCurrentView} />
))}
<button
onClick={logoutHandler}
className={cn(
"w-full flex items-center gap-3 px-4 py-2.5 text-sm font-medium rounded-lg transition-colors",
"text-red-300 hover:bg-red-900/20 hover:text-red-200",
)}
>
<LogOut className="w-4 h-4" />
Log Out
</button>
</>
) : (
fallbackMenuItems.map((item, index) => {
|
884054fb
“wangming”
项目初始化
|
241
242
243
244
245
246
247
248
249
|
if (item.type === 'header') {
return (
<div key={index} className="px-4 py-2 mt-4 text-xs font-semibold text-blue-300 uppercase tracking-wider">
{item.name}
</div>
);
}
if (item.type === 'sub') {
|
699ea6e8
杨鑫
完善打印逻辑
|
250
|
const Icon = item.icon;
|
884054fb
“wangming”
项目初始化
|
251
252
253
254
255
256
257
258
259
260
|
return (
<div key={index} className="space-y-1">
<button
onClick={item.toggle}
className={cn(
"w-full flex items-center justify-between px-4 py-2.5 text-sm font-medium rounded-lg transition-colors",
"hover:bg-blue-800/50 text-blue-100"
)}
>
<div className="flex items-center gap-3">
|
699ea6e8
杨鑫
完善打印逻辑
|
261
|
{Icon ? <Icon className="w-4 h-4" /> : null}
|
884054fb
“wangming”
项目初始化
|
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
{item.name}
</div>
{item.isOpen ? <ChevronDown className="w-4 h-4" /> : <ChevronRight className="w-4 h-4" />}
</button>
{item.isOpen && (
<div className="pl-4 space-y-1">
{item.children?.map((child, childIndex) => (
<button
key={childIndex}
onClick={() => setCurrentView(child.name)}
className={cn(
"w-full flex items-center gap-3 px-4 py-2 text-sm font-medium rounded-lg transition-colors border-l-2",
currentView === child.name
? "bg-blue-800 border-blue-400 text-white"
: "border-transparent hover:bg-blue-800/30 text-blue-200 hover:text-white"
)}
>
<div className="w-1 h-1 rounded-full bg-current" />
{child.name}
</button>
))}
</div>
)}
</div>
);
}
|
699ea6e8
杨鑫
完善打印逻辑
|
290
|
const Icon = item.icon;
|
884054fb
“wangming”
项目初始化
|
291
292
293
|
return (
<button
key={index}
|
699ea6e8
杨鑫
完善打印逻辑
|
294
|
onClick={() => (item.name === "Log Out" ? logoutHandler() : setCurrentView(item.name))}
|
884054fb
“wangming”
项目初始化
|
295
296
297
298
299
300
301
302
303
|
className={cn(
"w-full flex items-center gap-3 px-4 py-2.5 text-sm font-medium rounded-lg transition-colors",
currentView === item.name
? "bg-blue-700 text-white shadow-md shadow-blue-900/20"
: item.name === 'Log Out'
? "text-red-300 hover:bg-red-900/20 hover:text-red-200"
: "text-blue-100 hover:bg-blue-800 hover:text-white"
)}
>
|
699ea6e8
杨鑫
完善打印逻辑
|
304
|
{Icon ? <Icon className="w-4 h-4" /> : null}
|
884054fb
“wangming”
项目初始化
|
305
306
307
|
{item.name}
</button>
);
|
699ea6e8
杨鑫
完善打印逻辑
|
308
|
}))}
|
884054fb
“wangming”
项目初始化
|
309
310
311
312
313
|
</div>
</ScrollArea>
</div>
);
}
|