-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathSidebar.tsx
More file actions
537 lines (512 loc) · 17.4 KB
/
Copy pathSidebar.tsx
File metadata and controls
537 lines (512 loc) · 17.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
import { useEffect, useState } from 'react'
import { Link, useLocation } from 'react-router-dom'
import {
Activity,
AlertTriangle,
ArrowLeft,
BadgeCheck,
Bell,
Bot,
Building2,
CalendarClock,
ChevronDown,
Database,
Flame,
HardDriveDownload,
History,
Home,
Info,
KeyRound,
KeySquare,
Languages,
LayoutDashboard,
LifeBuoy,
Mail,
Palette,
PanelLeftClose,
PanelLeftOpen,
Plug,
Radar,
ScrollText,
Settings,
Shield,
ShieldCheck,
SlidersHorizontal,
Filter,
Tags,
Terminal,
UserCheck,
UserX,
Users,
Workflow,
type LucideIcon,
} from 'lucide-react'
import { useTranslation } from 'react-i18next'
import { cn } from '@/shared/lib/utils'
import { Tooltip, TooltipContent, TooltipTrigger } from '@/shared/components/ui/tooltip'
import { useAuth } from '@/features/auth'
import { useBilling } from '@/features/billing'
import { useSupportTenant } from '@/shared/lib/current-tenant'
import { IS_FEDERATION } from '@/shared/config/mode'
type LeafItem = {
to: string
label: string
icon: LucideIcon
/** Light up for any path under `to` (e.g. Dashboards spans /dashboards/*). */
matchPrefix?: boolean
/** Only for whoever runs the instance, and only where there are tenants to
* run: a customer's own administrator holds ROLE_ADMIN too, and without MSSP
* the single tenant's administrator *is* the platform identity. */
platformOnly?: boolean
}
type GroupItem = {
id: string
label: string
icon: LucideIcon
basePath: string
children: { to: string; label: string; icon: LucideIcon }[]
}
type Item = LeafItem | GroupItem
function isGroup(item: Item): item is GroupItem {
return 'children' in item
}
type Section = {
id: string
label: string | null
items: Item[]
// Hidden from non-admins (ROLE_USER). System configuration is admin-only.
adminOnly?: boolean
}
// `label` values are i18n keys, resolved with t() at render time.
const sections: Section[] = [
{
id: 'main',
label: null,
items: [
{ to: '/home', label: 'nav.overview', icon: Home },
{ to: '/tenants', label: 'nav.tenants', icon: Building2, platformOnly: true },
{ to: '/dashboards', label: 'nav.dashboards', icon: LayoutDashboard, matchPrefix: true },
{
id: 'threat-management',
label: 'nav.threatManagement',
icon: Shield,
basePath: '/threat-management',
children: [
{ to: '/threat-management/alerts', label: 'nav.alerts', icon: AlertTriangle },
{ to: '/threat-management/alerts/tagging-rules', label: 'nav.taggingRules', icon: Tags },
{ to: '/threat-management/incidents', label: 'nav.incidents', icon: Flame },
{ to: '/threat-management/adversaries', label: 'nav.adversaryView', icon: UserX },
],
},
{
id: 'soar',
label: 'nav.soar',
icon: Workflow,
basePath: '/soar',
children: [
{ to: '/soar/flows', label: 'nav.flows', icon: Workflow },
{ to: '/soar/execution-history', label: 'nav.executionHistory', icon: History },
{ to: '/soar/interactive-console', label: 'nav.interactiveConsole', icon: Terminal },
],
},
{ to: '/log-explorer', label: 'nav.logExplorer', icon: ScrollText },
{ to: '/user-auditor', label: 'nav.userAuditor', icon: UserCheck },
{ to: '/threat-intelligence', label: 'nav.threatIntelligence', icon: Radar },
{ to: '/compliance', label: 'nav.compliance', icon: BadgeCheck },
],
},
{
id: 'configure',
label: 'nav.configure',
adminOnly: true,
items: [
{ to: '/datasources', label: 'nav.dataSources', icon: Database },
{ to: '/integrations', label: 'nav.integrations', icon: Plug },
{ to: '/alerting-rules', label: 'nav.alertingRules', icon: SlidersHorizontal },
{ to: '/pipelines', label: 'nav.parsingFilters', icon: Filter },
{ to: '/data-processing', label: 'nav.dataProcessing', icon: Activity },
{ to: '/team', label: 'nav.team', icon: Users },
{ to: '/settings', label: 'nav.settings', icon: Settings },
],
},
]
// Settings drill-down — when pathname is under /settings the sidebar swaps
// its content for this list + a Back button. `label` values are i18n keys.
const settingsItems: {
to: string
label: string
icon: LucideIcon
/** For customer tenants only: the platform's own tenant has nobody to grant
* support access to. */
tenantOnly?: boolean
/** Properties of the instance rather than of a customer on it — the licence,
* the retention policy, the build. Only the default tenant sees these. */
platformOnly?: boolean
}[] = [
{ to: '/settings/license', label: 'settings.license', icon: BadgeCheck, platformOnly: true },
{ to: '/settings/notifications', label: 'settings.notifications', icon: Bell },
{ to: '/settings/connection-key', label: 'settings.connectionKey', icon: KeyRound },
{
to: '/settings/data-retention',
label: 'settings.dataRetention',
icon: HardDriveDownload,
platformOnly: true,
},
{ to: '/settings/branding', label: 'settings.branding', icon: Palette },
...(!IS_FEDERATION
? [{ to: '/settings/api-keys', label: 'settings.apiKeys', icon: KeySquare }]
: []),
{ to: '/settings/identity-providers', label: 'settings.identityProviders', icon: ShieldCheck },
{
to: '/settings/support-access',
label: 'settings.supportAccess',
icon: LifeBuoy,
tenantOnly: true,
},
{ to: '/settings/email', label: 'settings.email', icon: Mail },
{ to: '/settings/soc-ai', label: 'settings.socAi', icon: Bot },
{ to: '/settings/date-format', label: 'settings.dateFormat', icon: CalendarClock },
{ to: '/settings/language', label: 'settings.language', icon: Languages },
{ to: '/settings/audit-logs', label: 'settings.auditLogs', icon: History },
{ to: '/settings/about', label: 'settings.about', icon: Info, platformOnly: true },
]
const SECTIONS_KEY = 'utmstack-sidebar-sections-closed'
const GROUPS_KEY = 'utmstack-sidebar-groups-closed'
const COLLAPSED_KEY = 'utmstack-sidebar-collapsed'
function loadSet(key: string): Set<string> {
if (typeof window === 'undefined') return new Set()
try {
const raw = localStorage.getItem(key)
if (!raw) return new Set()
const arr = JSON.parse(raw)
return Array.isArray(arr) ? new Set(arr) : new Set()
} catch {
return new Set()
}
}
function loadCollapsed(): boolean {
if (typeof window === 'undefined') return false
return localStorage.getItem(COLLAPSED_KEY) === '1'
}
function useCollapsed(): [boolean, () => void] {
const [collapsed, setCollapsed] = useState<boolean>(loadCollapsed)
useEffect(() => {
localStorage.setItem(COLLAPSED_KEY, collapsed ? '1' : '0')
}, [collapsed])
return [collapsed, () => setCollapsed((v) => !v)]
}
function CollapseToggle({ collapsed, onToggle }: { collapsed: boolean; onToggle: () => void }) {
const Icon = collapsed ? PanelLeftOpen : PanelLeftClose
return (
<button
type="button"
onClick={onToggle}
aria-label={collapsed ? 'Expand sidebar' : 'Collapse sidebar'}
className={cn(
'flex items-center rounded-lg p-2 text-sidebar-foreground/60 transition-colors hover:bg-sidebar-accent hover:text-sidebar-foreground',
collapsed ? 'justify-center' : 'ml-auto'
)}
>
<Icon size={16} strokeWidth={1.75} />
</button>
)
}
export function Sidebar() {
const { t } = useTranslation()
const { isAdmin, isPlatformAdmin } = useAuth()
const { license } = useBilling()
const isMSSP = license?.mssp === true
const supportTenant = useSupportTenant()
// Inside a support session the operator *is* that tenant as far as the API is
// concerned, so the platform-plane entries would only lead to a 403.
const actingAsPlatform = isPlatformAdmin && supportTenant === null
const [closedSections, setClosedSections] = useState<Set<string>>(() => loadSet(SECTIONS_KEY))
const [closedGroups, setClosedGroups] = useState<Set<string>>(() => loadSet(GROUPS_KEY))
const [collapsed, toggleCollapsed] = useCollapsed()
const { pathname } = useLocation()
useEffect(() => {
localStorage.setItem(SECTIONS_KEY, JSON.stringify([...closedSections]))
}, [closedSections])
useEffect(() => {
localStorage.setItem(GROUPS_KEY, JSON.stringify([...closedGroups]))
}, [closedGroups])
const toggleSection = (id: string) => {
setClosedSections((prev) => {
const next = new Set(prev)
next.has(id) ? next.delete(id) : next.add(id)
return next
})
}
const toggleGroup = (id: string) => {
setClosedGroups((prev) => {
const next = new Set(prev)
next.has(id) ? next.delete(id) : next.add(id)
return next
})
}
const isPathActive = (to: string) => pathname === to
const isBaseActive = (basePath: string) =>
pathname === basePath || pathname.startsWith(basePath + '/')
// Drill-down: when navigating under /settings the sidebar swaps to a sub-nav.
if (pathname.startsWith('/settings')) {
return <SettingsSidebar isPathActive={isPathActive} collapsed={collapsed} onToggleCollapsed={toggleCollapsed} />
}
return (
<aside
className={cn(
'flex h-full shrink-0 flex-col border-r border-sidebar-border bg-sidebar text-sidebar-foreground transition-[width] duration-150',
collapsed ? 'w-14' : 'w-60'
)}
>
<div className="flex p-2">
<CollapseToggle collapsed={collapsed} onToggle={toggleCollapsed} />
</div>
<nav className="flex-1 overflow-y-auto overflow-x-hidden p-2 pt-0">
{sections
.filter((section) => !section.adminOnly || isAdmin)
.map((section, idx) => {
const isClosed = closedSections.has(section.id)
return (
<div
key={section.id}
className={cn(idx > 0 && 'mt-2 pt-2 border-t border-sidebar-border/60')}
>
{section.label && !collapsed && (
<button
onClick={() => toggleSection(section.id)}
className="mb-1 flex w-full items-center justify-between px-3 py-1 text-[10px] font-semibold uppercase tracking-wider text-sidebar-foreground/50 hover:text-sidebar-foreground"
>
<span>{t(section.label)}</span>
<ChevronDown
size={12}
className={cn('transition-transform duration-150', isClosed && '-rotate-90')}
/>
</button>
)}
{(collapsed || !isClosed) && (
<div className="space-y-0.5">
{section.items
.filter(
(item) =>
isGroup(item) || !item.platformOnly || (actingAsPlatform && isMSSP)
)
.map((item) =>
isGroup(item) ? (
<SidebarGroup
key={item.id}
item={item}
open={!closedGroups.has(item.id)}
onToggle={() => toggleGroup(item.id)}
isPathActive={isPathActive}
isBaseActive={isBaseActive}
collapsed={collapsed}
onExpandRequest={toggleCollapsed}
/>
) : (
<SidebarLeaf
key={item.to}
item={item}
active={item.matchPrefix ? isBaseActive(item.to) : isPathActive(item.to)}
collapsed={collapsed}
/>
)
)}
</div>
)}
</div>
)
})}
</nav>
</aside>
)
}
function SidebarLeaf({
item,
active,
nested,
collapsed,
}: {
item: { to: string; label: string; icon: LucideIcon }
active: boolean
nested?: boolean
collapsed?: boolean
}) {
const { t } = useTranslation()
const Icon = item.icon
const label = t(item.label)
const link = (
<Link
to={item.to}
className={cn(
'flex items-center rounded-lg text-sm transition-colors',
collapsed ? 'justify-center px-2 py-2' : 'gap-3 px-3 py-2',
active
? 'bg-primary/15 text-primary'
: 'text-sidebar-foreground/70 hover:bg-sidebar-accent hover:text-sidebar-foreground',
!collapsed && nested && 'pl-7'
)}
aria-label={collapsed ? label : undefined}
>
<Icon size={16} strokeWidth={1.75} className="shrink-0" />
{!collapsed && <span className="whitespace-nowrap">{label}</span>}
</Link>
)
if (!collapsed) return link
return (
<Tooltip>
<TooltipTrigger asChild>{link}</TooltipTrigger>
<TooltipContent side="right">{label}</TooltipContent>
</Tooltip>
)
}
function SidebarGroup({
item,
open,
onToggle,
isPathActive,
isBaseActive,
collapsed,
onExpandRequest,
}: {
item: GroupItem
open: boolean
onToggle: () => void
isPathActive: (to: string) => boolean
isBaseActive: (base: string) => boolean
collapsed?: boolean
onExpandRequest?: () => void
}) {
const { t } = useTranslation()
const Icon = item.icon
const baseActive = isBaseActive(item.basePath)
const label = t(item.label)
// Collapsed: no sub-menu room, so clicking a group just re-opens the sidebar.
if (collapsed) {
const btn = (
<button
onClick={onExpandRequest}
aria-label={label}
className={cn(
'flex w-full items-center justify-center rounded-lg px-2 py-2 text-sm transition-colors',
baseActive
? 'bg-primary/10 text-primary'
: 'text-sidebar-foreground/70 hover:bg-sidebar-accent hover:text-sidebar-foreground'
)}
>
<Icon size={16} strokeWidth={1.75} className="shrink-0" />
</button>
)
return (
<Tooltip>
<TooltipTrigger asChild>{btn}</TooltipTrigger>
<TooltipContent side="right">{label}</TooltipContent>
</Tooltip>
)
}
return (
<div>
<button
onClick={onToggle}
className={cn(
'flex w-full items-center gap-3 rounded-lg px-3 py-2 text-sm transition-colors',
baseActive
? 'bg-primary/10 text-primary'
: 'text-sidebar-foreground/70 hover:bg-sidebar-accent hover:text-sidebar-foreground'
)}
>
<Icon size={16} strokeWidth={1.75} className="shrink-0" />
<span className="flex-1 whitespace-nowrap text-left">{label}</span>
<ChevronDown
size={14}
className={cn(
'shrink-0 transition-transform duration-150',
open ? 'rotate-0' : '-rotate-90'
)}
/>
</button>
{open && (
<div className="mt-0.5 space-y-0.5">
{item.children.map((child) => (
<SidebarLeaf
key={child.to}
item={{ to: child.to, label: child.label, icon: child.icon }}
active={isPathActive(child.to)}
nested
/>
))}
</div>
)}
</div>
)
}
function SettingsSidebar({
isPathActive,
collapsed,
onToggleCollapsed,
}: {
isPathActive: (to: string) => boolean
collapsed: boolean
onToggleCollapsed: () => void
}) {
const { t } = useTranslation()
const { isAdmin, isPlatformAdmin } = useAuth()
const { license } = useBilling()
const supportTenant = useSupportTenant()
const items = settingsItems.filter((item) => {
if (item.platformOnly) return isPlatformAdmin && supportTenant === null
if (item.tenantOnly) return isAdmin && !isPlatformAdmin && license?.mssp === true
return true
})
const backLabel = t('nav.back')
return (
<aside
className={cn(
'flex h-full shrink-0 flex-col border-r border-sidebar-border bg-sidebar text-sidebar-foreground transition-[width] duration-150',
collapsed ? 'w-14' : 'w-60'
)}
>
<div className="flex border-b border-sidebar-border p-2">
{collapsed ? (
<Tooltip>
<TooltipTrigger asChild>
<Link
to="/home"
aria-label={backLabel}
className="flex flex-1 items-center justify-center rounded-lg px-2 py-2 text-sm text-sidebar-foreground/70 transition-colors hover:bg-sidebar-accent hover:text-sidebar-foreground"
>
<ArrowLeft size={16} strokeWidth={1.75} className="shrink-0" />
</Link>
</TooltipTrigger>
<TooltipContent side="right">{backLabel}</TooltipContent>
</Tooltip>
) : (
<Link
to="/home"
className="flex flex-1 items-center gap-2 rounded-lg px-3 py-2 text-sm text-sidebar-foreground/70 transition-colors hover:bg-sidebar-accent hover:text-sidebar-foreground"
>
<ArrowLeft size={16} strokeWidth={1.75} className="shrink-0" />
<span>{backLabel}</span>
</Link>
)}
</div>
<div className="flex p-2 pb-0">
<CollapseToggle collapsed={collapsed} onToggle={onToggleCollapsed} />
</div>
{!collapsed && (
<div className="px-3 pt-3 pb-1 text-[10px] font-semibold uppercase tracking-wider text-sidebar-foreground/50">
{t('settings.title')}
</div>
)}
<nav className="flex-1 space-y-0.5 overflow-y-auto overflow-x-hidden p-2 pt-1">
{items.map((item) => (
<SidebarLeaf
key={item.to}
item={{ to: item.to, label: item.label, icon: item.icon }}
active={isPathActive(item.to)}
collapsed={collapsed}
/>
))}
</nav>
</aside>
)
}