@orsetra/shared-ui 1.9.0 → 1.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -3,13 +3,24 @@
|
|
|
3
3
|
import * as React from "react"
|
|
4
4
|
import { cn } from "../../../lib/utils"
|
|
5
5
|
import type { MainMenuItem, SubMenuItem } from "./data"
|
|
6
|
+
import { Home, ExternalLink, type LucideIcon } from "lucide-react"
|
|
7
|
+
|
|
8
|
+
export interface FlyoutDefaultLink {
|
|
9
|
+
label: string
|
|
10
|
+
href: string
|
|
11
|
+
description?: string
|
|
12
|
+
icon?: LucideIcon
|
|
13
|
+
}
|
|
6
14
|
|
|
7
15
|
interface MainSidebarFlyoutProps {
|
|
8
|
-
item:
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
16
|
+
item: MainMenuItem | null
|
|
17
|
+
subItems: SubMenuItem[]
|
|
18
|
+
onMouseEnter: () => void
|
|
19
|
+
onMouseLeave: () => void
|
|
20
|
+
main_base_url?: string
|
|
21
|
+
defaultTitle?: string
|
|
22
|
+
defaultDescription?: string
|
|
23
|
+
defaultLinks?: FlyoutDefaultLink[]
|
|
13
24
|
}
|
|
14
25
|
|
|
15
26
|
export function MainSidebarFlyout({
|
|
@@ -17,8 +28,21 @@ export function MainSidebarFlyout({
|
|
|
17
28
|
subItems,
|
|
18
29
|
onMouseEnter,
|
|
19
30
|
onMouseLeave,
|
|
31
|
+
main_base_url = "/",
|
|
32
|
+
defaultTitle = "Camel X Platform",
|
|
33
|
+
defaultDescription,
|
|
34
|
+
defaultLinks = [],
|
|
20
35
|
}: MainSidebarFlyoutProps) {
|
|
21
|
-
const
|
|
36
|
+
const homePath = React.useMemo(() => {
|
|
37
|
+
if (!main_base_url) return "/"
|
|
38
|
+
try {
|
|
39
|
+
return (main_base_url.startsWith("http://") || main_base_url.startsWith("https://"))
|
|
40
|
+
? new URL(main_base_url).pathname
|
|
41
|
+
: main_base_url
|
|
42
|
+
} catch {
|
|
43
|
+
return "/"
|
|
44
|
+
}
|
|
45
|
+
}, [main_base_url])
|
|
22
46
|
|
|
23
47
|
return (
|
|
24
48
|
<div
|
|
@@ -26,46 +50,107 @@ export function MainSidebarFlyout({
|
|
|
26
50
|
onMouseLeave={onMouseLeave}
|
|
27
51
|
className="h-full flex flex-col flex-shrink-0 overflow-hidden bg-gray-50 border-r border-ui-border shadow-[4px_0_20px_-4px_rgba(0,0,0,0.10)] w-72 lg:w-80"
|
|
28
52
|
>
|
|
29
|
-
{
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
53
|
+
{item ? (
|
|
54
|
+
/* ── Hovered item: sub-menu list ─────────────────────────────────── */
|
|
55
|
+
<>
|
|
56
|
+
<div className="flex items-center px-4 lg:px-5 pt-5 pb-1 flex-shrink-0 gap-2.5">
|
|
57
|
+
<item.icon className="h-5 w-5 flex-shrink-0" style={{ color: "#4589ff" }} />
|
|
58
|
+
<h2 className="text-sm font-semibold text-text-primary tracking-wide truncate flex-1">
|
|
59
|
+
{item.label}
|
|
60
|
+
</h2>
|
|
61
|
+
</div>
|
|
36
62
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
63
|
+
{item.description && (
|
|
64
|
+
<p className="px-4 lg:px-5 pt-1 pb-4 text-xs text-text-secondary leading-relaxed">
|
|
65
|
+
{item.description}
|
|
66
|
+
</p>
|
|
67
|
+
)}
|
|
68
|
+
|
|
69
|
+
<nav className="flex-1 overflow-y-auto px-2 lg:px-3 py-2 space-y-0.5">
|
|
70
|
+
{subItems.map((sub) => {
|
|
71
|
+
const SubIcon = sub.icon
|
|
72
|
+
return (
|
|
73
|
+
<a
|
|
74
|
+
key={sub.id}
|
|
75
|
+
href={sub.href}
|
|
76
|
+
className={cn(
|
|
77
|
+
"flex items-start transition-colors border-l-4",
|
|
78
|
+
"px-2 lg:px-3 py-2 gap-x-3",
|
|
79
|
+
"hover:bg-ui-background border-transparent hover:border-interactive",
|
|
80
|
+
)}
|
|
81
|
+
>
|
|
82
|
+
<SubIcon className="h-4 w-4 flex-shrink-0 mt-0.5" style={{ color: "#4589ff" }} />
|
|
83
|
+
<div className="min-w-0 flex-1">
|
|
84
|
+
<p className="text-sm font-medium text-text-primary leading-snug truncate">{sub.name}</p>
|
|
85
|
+
{sub.description && (
|
|
86
|
+
<p className="text-xs text-text-secondary leading-relaxed mt-0.5 line-clamp-2">{sub.description}</p>
|
|
87
|
+
)}
|
|
88
|
+
</div>
|
|
89
|
+
</a>
|
|
90
|
+
)
|
|
91
|
+
})}
|
|
92
|
+
</nav>
|
|
93
|
+
</>
|
|
94
|
+
) : (
|
|
95
|
+
/* ── Default state: welcome + CTA + doc links ───────────────────── */
|
|
96
|
+
<div className="flex flex-col h-full">
|
|
97
|
+
{/* Header */}
|
|
98
|
+
<div className="px-5 pt-6 pb-5 border-b border-ui-border flex-shrink-0">
|
|
99
|
+
<div className="flex items-center gap-2.5 mb-3">
|
|
100
|
+
<div className="h-8 w-8 rounded-md bg-interactive flex items-center justify-center flex-shrink-0">
|
|
101
|
+
<Home className="h-4 w-4 text-white" />
|
|
102
|
+
</div>
|
|
103
|
+
<span className="text-sm font-semibold text-text-primary truncate">{defaultTitle}</span>
|
|
104
|
+
</div>
|
|
105
|
+
|
|
106
|
+
{defaultDescription && (
|
|
107
|
+
<p className="text-xs text-text-secondary leading-relaxed mb-4">
|
|
108
|
+
{defaultDescription}
|
|
109
|
+
</p>
|
|
110
|
+
)}
|
|
43
111
|
|
|
44
|
-
{/* ── Nav items ────────────────────────────────────────────────────────── */}
|
|
45
|
-
<nav className="flex-1 overflow-y-auto px-2 lg:px-3 py-2 space-y-0.5">
|
|
46
|
-
{subItems.map((sub) => {
|
|
47
|
-
const SubIcon = sub.icon
|
|
48
|
-
return (
|
|
49
112
|
<a
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
className={cn(
|
|
53
|
-
"flex items-start transition-colors border-l-4",
|
|
54
|
-
"px-2 lg:px-3 py-2 gap-x-3",
|
|
55
|
-
"hover:bg-ui-background border-transparent hover:border-interactive",
|
|
56
|
-
)}
|
|
113
|
+
href={homePath}
|
|
114
|
+
className="flex items-center justify-center gap-2 w-full px-3 py-2 bg-interactive hover:bg-interactive-hover text-white text-sm font-medium transition-colors"
|
|
57
115
|
>
|
|
58
|
-
<
|
|
59
|
-
|
|
60
|
-
<p className="text-sm font-medium text-text-primary leading-snug truncate">{sub.name}</p>
|
|
61
|
-
{sub.description && (
|
|
62
|
-
<p className="text-xs text-text-secondary leading-relaxed mt-0.5 line-clamp-2">{sub.description}</p>
|
|
63
|
-
)}
|
|
64
|
-
</div>
|
|
116
|
+
<Home className="h-4 w-4 flex-shrink-0" />
|
|
117
|
+
Tableau de bord
|
|
65
118
|
</a>
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
119
|
+
</div>
|
|
120
|
+
|
|
121
|
+
{/* Doc / resource links */}
|
|
122
|
+
{defaultLinks.length > 0 && (
|
|
123
|
+
<nav className="flex-1 overflow-y-auto px-2 lg:px-3 py-3 space-y-0.5">
|
|
124
|
+
<p className="px-2 pb-1 text-[10px] font-semibold text-text-secondary uppercase tracking-widest">
|
|
125
|
+
Ressources
|
|
126
|
+
</p>
|
|
127
|
+
{defaultLinks.map((link, i) => {
|
|
128
|
+
const LinkIcon = link.icon ?? ExternalLink
|
|
129
|
+
const isExternal = link.href.startsWith("http://") || link.href.startsWith("https://")
|
|
130
|
+
return (
|
|
131
|
+
<a
|
|
132
|
+
key={i}
|
|
133
|
+
href={link.href}
|
|
134
|
+
{...(isExternal ? { target: "_blank", rel: "noopener noreferrer" } : {})}
|
|
135
|
+
className="flex items-start gap-x-3 px-2 lg:px-3 py-2 border-l-4 border-transparent hover:bg-ui-background hover:border-interactive transition-colors"
|
|
136
|
+
>
|
|
137
|
+
<LinkIcon className="h-4 w-4 flex-shrink-0 mt-0.5 text-text-secondary" />
|
|
138
|
+
<div className="min-w-0 flex-1">
|
|
139
|
+
<p className="text-sm font-medium text-text-primary leading-snug flex items-center gap-1.5">
|
|
140
|
+
{link.label}
|
|
141
|
+
{isExternal && <ExternalLink className="h-3 w-3 text-text-secondary flex-shrink-0" />}
|
|
142
|
+
</p>
|
|
143
|
+
{link.description && (
|
|
144
|
+
<p className="text-xs text-text-secondary leading-relaxed mt-0.5 line-clamp-2">{link.description}</p>
|
|
145
|
+
)}
|
|
146
|
+
</div>
|
|
147
|
+
</a>
|
|
148
|
+
)
|
|
149
|
+
})}
|
|
150
|
+
</nav>
|
|
151
|
+
)}
|
|
152
|
+
</div>
|
|
153
|
+
)}
|
|
69
154
|
</div>
|
|
70
155
|
)
|
|
71
156
|
}
|
|
@@ -7,7 +7,9 @@ import { Logo } from "../../ui/logo"
|
|
|
7
7
|
import { type MainMenuItem, type SidebarMenus } from "./data"
|
|
8
8
|
import { X, Menu, ChevronDown, ChevronRight } from "lucide-react"
|
|
9
9
|
import { useIsMobile } from "../../../hooks/use-mobile"
|
|
10
|
-
import { MainSidebarFlyout } from "./main-sidebar-flyout"
|
|
10
|
+
import { MainSidebarFlyout, type FlyoutDefaultLink } from "./main-sidebar-flyout"
|
|
11
|
+
|
|
12
|
+
export type { FlyoutDefaultLink }
|
|
11
13
|
|
|
12
14
|
export type SidebarMode = 'expanded' | 'minimized' | 'hidden'
|
|
13
15
|
|
|
@@ -21,6 +23,9 @@ interface MainSidebarProps {
|
|
|
21
23
|
onSecondarySidebarOpen?: () => void
|
|
22
24
|
sidebarMenus?: SidebarMenus
|
|
23
25
|
mainMenuItems?: MainMenuItem[]
|
|
26
|
+
flyoutDefaultTitle?: string
|
|
27
|
+
flyoutDefaultDescription?: string
|
|
28
|
+
flyoutDefaultLinks?: FlyoutDefaultLink[]
|
|
24
29
|
}
|
|
25
30
|
|
|
26
31
|
export function MainSidebar({
|
|
@@ -33,6 +38,9 @@ export function MainSidebar({
|
|
|
33
38
|
onSecondarySidebarOpen,
|
|
34
39
|
sidebarMenus = {},
|
|
35
40
|
mainMenuItems = [],
|
|
41
|
+
flyoutDefaultTitle,
|
|
42
|
+
flyoutDefaultDescription,
|
|
43
|
+
flyoutDefaultLinks = [],
|
|
36
44
|
}: MainSidebarProps) {
|
|
37
45
|
const isMinimized = mode === 'minimized'
|
|
38
46
|
const isMobile = useIsMobile()
|
|
@@ -66,6 +74,40 @@ export function MainSidebar({
|
|
|
66
74
|
setHoveredItem(item)
|
|
67
75
|
}
|
|
68
76
|
|
|
77
|
+
const applyTemplateVars = (href: string): string => {
|
|
78
|
+
// Guard: SSR has no window/localStorage
|
|
79
|
+
if (typeof window === "undefined") return href
|
|
80
|
+
try {
|
|
81
|
+
const projRaw = localStorage.getItem("current-business-unit")
|
|
82
|
+
const envRaw = localStorage.getItem("current-environment")
|
|
83
|
+
// Parse carefully: value may be null (not yet set), invalid JSON, or a non-object
|
|
84
|
+
const currentProject = typeof projRaw === "string"
|
|
85
|
+
? (JSON.parse(projRaw)?.state?.currentProject ?? "")
|
|
86
|
+
: ""
|
|
87
|
+
const currentEnv = typeof envRaw === "string"
|
|
88
|
+
? (JSON.parse(envRaw)?.state?.currentEnv ?? "")
|
|
89
|
+
: ""
|
|
90
|
+
// Only replace when the value is a non-empty string; if empty, leave the
|
|
91
|
+
// placeholder in the URL so the link is visibly broken rather than silently
|
|
92
|
+
// producing a protocol-relative URL (e.g. "//path") that navigates incorrectly.
|
|
93
|
+
let result = href
|
|
94
|
+
if (currentProject && typeof currentProject === "string") {
|
|
95
|
+
result = result.replace(/\$\{currentProject\}/g, currentProject)
|
|
96
|
+
}
|
|
97
|
+
if (currentEnv && typeof currentEnv === "string") {
|
|
98
|
+
result = result.replace(/\$\{currentEnv\}/g, currentEnv)
|
|
99
|
+
}
|
|
100
|
+
return result
|
|
101
|
+
} catch {
|
|
102
|
+
// localStorage blocked (Safari private mode), JSON parse error, etc. — return unchanged
|
|
103
|
+
return href
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const safeUrlPathname = (url: string): string => {
|
|
108
|
+
try { return new URL(url).pathname } catch { return "/" }
|
|
109
|
+
}
|
|
110
|
+
|
|
69
111
|
const handleMenuClick = (item: MainMenuItem) => {
|
|
70
112
|
const menuId = item.id
|
|
71
113
|
const hasSubMenu = (sidebarMenus[menuId]?.length ?? 0) > 0
|
|
@@ -81,24 +123,34 @@ export function MainSidebar({
|
|
|
81
123
|
if (!isMinimized && onSecondarySidebarOpen) onSecondarySidebarOpen()
|
|
82
124
|
|
|
83
125
|
if (item.href) {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
126
|
+
try {
|
|
127
|
+
const resolved = applyTemplateVars(item.href)
|
|
128
|
+
const path = resolved.startsWith('http://') || resolved.startsWith('https://')
|
|
129
|
+
? safeUrlPathname(resolved)
|
|
130
|
+
: resolved
|
|
131
|
+
window.location.href = path
|
|
132
|
+
} catch {
|
|
133
|
+
// navigation error — don't crash the sidebar
|
|
134
|
+
}
|
|
88
135
|
}
|
|
89
136
|
|
|
90
137
|
if (!isMinimized) onToggle()
|
|
91
138
|
}
|
|
92
139
|
|
|
93
140
|
const buildSubItemHref = (menuId: string, href: string): string => {
|
|
94
|
-
|
|
95
|
-
|
|
141
|
+
try {
|
|
142
|
+
const resolved = applyTemplateVars(href)
|
|
143
|
+
if (resolved.startsWith('http://') || resolved.startsWith('https://')) {
|
|
144
|
+
return safeUrlPathname(resolved)
|
|
145
|
+
}
|
|
146
|
+
const cleanHref = resolved.replace(/^\//, '')
|
|
147
|
+
const cleanBase = (main_base_url.startsWith('http://') || main_base_url.startsWith('https://'))
|
|
148
|
+
? safeUrlPathname(main_base_url).replace(/\/$/, '')
|
|
149
|
+
: (main_base_url ?? "").replace(/\/$/, '')
|
|
150
|
+
return `${cleanBase}/${menuId}/${cleanHref}`
|
|
151
|
+
} catch {
|
|
152
|
+
return "/"
|
|
96
153
|
}
|
|
97
|
-
const cleanHref = href.replace(/^\//, '')
|
|
98
|
-
const cleanBase = (main_base_url.startsWith('http://') || main_base_url.startsWith('https://'))
|
|
99
|
-
? new URL(main_base_url).pathname.replace(/\/$/, '')
|
|
100
|
-
: main_base_url.replace(/\/$/, '')
|
|
101
|
-
return `${cleanBase}/${menuId}/${cleanHref}`
|
|
102
154
|
}
|
|
103
155
|
|
|
104
156
|
const handleSubMenuClick = (e: React.MouseEvent, menuId: string, href: string) => {
|
|
@@ -157,9 +209,14 @@ export function MainSidebar({
|
|
|
157
209
|
const isFlyActive = hoveredItem?.id === item.id
|
|
158
210
|
|
|
159
211
|
const itemHref = !hasSubMenu && item.href
|
|
160
|
-
? (
|
|
161
|
-
|
|
162
|
-
|
|
212
|
+
? (() => {
|
|
213
|
+
try {
|
|
214
|
+
const resolved = applyTemplateVars(item.href)
|
|
215
|
+
return resolved.startsWith('http://') || resolved.startsWith('https://')
|
|
216
|
+
? safeUrlPathname(resolved)
|
|
217
|
+
: resolved
|
|
218
|
+
} catch { return item.href }
|
|
219
|
+
})()
|
|
163
220
|
: undefined
|
|
164
221
|
|
|
165
222
|
const itemCls = cn(
|
|
@@ -231,15 +288,23 @@ export function MainSidebar({
|
|
|
231
288
|
</div>
|
|
232
289
|
|
|
233
290
|
{/* ── Flyout — desktop only, flex child to the right of nav ─────────── */}
|
|
234
|
-
{
|
|
291
|
+
{/* Shown when the sidebar is open (default state) or an item is hovered (sub-menu state) */}
|
|
292
|
+
{!isMobile && (hoveredItem || (isOpen && !isMinimized)) && (
|
|
235
293
|
<MainSidebarFlyout
|
|
236
294
|
item={hoveredItem}
|
|
237
|
-
subItems={
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
295
|
+
subItems={hoveredItem
|
|
296
|
+
? (sidebarMenus[hoveredItem.id] ?? []).map(sub => ({
|
|
297
|
+
...sub,
|
|
298
|
+
href: buildSubItemHref(hoveredItem.id, sub.href),
|
|
299
|
+
}))
|
|
300
|
+
: []
|
|
301
|
+
}
|
|
241
302
|
onMouseEnter={cancelCloseTimer}
|
|
242
303
|
onMouseLeave={startCloseTimer}
|
|
304
|
+
main_base_url={main_base_url}
|
|
305
|
+
defaultTitle={flyoutDefaultTitle}
|
|
306
|
+
defaultDescription={flyoutDefaultDescription}
|
|
307
|
+
defaultLinks={flyoutDefaultLinks}
|
|
243
308
|
/>
|
|
244
309
|
)}
|
|
245
310
|
|