Sidebar.tsx
11.4 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import React, { useMemo, useState } from 'react';
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";
import type { CurrentUserMenuNodeDto } from '../../types/authSession';
interface SidebarProps {
currentView: string;
setCurrentView: (view: string) => void;
menus?: CurrentUserMenuNodeDto[];
onLogout?: () => void;
}
const FALLBACK_ICONS = {
Dashboard: LayoutDashboard,
Labeling: Tag,
Labels: Tag,
"Label Categories": Layers,
"Label Types": Type,
"Label Templates": FileBox,
"Multiple Option Sets": Settings,
"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";
}
function displayMenuLabel(name: string) {
const s = normalizeLabel(name);
if (s === "Multiple Options") return "Multiple Option Sets";
return s;
}
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 ?? ""));
const viewKey = label === "Multiple Options" ? "Multiple Option Sets" : label;
const active = currentKey === viewKey;
const iconKey = pickIconKeyFromNode(node, viewKey);
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" />}
<span className="truncate">{displayMenuLabel(label)}</span>
</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" />}
<span className="truncate">{displayMenuLabel(label)}</span>
</button>
);
}
export function Sidebar({ currentView, setCurrentView, menus, onLogout }: SidebarProps) {
const [labelingOpen, setLabelingOpen] = useState(true);
const fallbackMenuItems = [
{ 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 },
{ name: 'Multiple Option Sets', icon: Settings },
]
},
{ type: 'header', name: 'MANAGEMENT' },
{ name: 'Account Management', icon: Users, type: 'item' },
{ name: 'Menu Management', icon: Package, type: 'item' },
{ name: 'System Menu', icon: Settings, type: 'item' },
{ name: 'Reports', icon: FileText, type: 'item' },
{ name: 'Support', icon: HelpCircle, type: 'item' },
{ name: 'Log Out', icon: LogOut, type: 'item' },
];
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]);
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">
{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) => {
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') {
const Icon = item.icon;
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">
{Icon ? <Icon className="w-4 h-4" /> : null}
{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>
);
}
const Icon = item.icon;
return (
<button
key={index}
onClick={() => (item.name === "Log Out" ? logoutHandler() : setCurrentView(item.name))}
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"
)}
>
{Icon ? <Icon className="w-4 h-4" /> : null}
{item.name}
</button>
);
}))}
</div>
</ScrollArea>
</div>
);
}