@re-venue/ui-core-react 0.2.1 → 0.3.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@re-venue/ui-core-react",
3
- "version": "0.2.1",
3
+ "version": "0.3.1",
4
4
  "type": "module",
5
5
  "main": "./dist/ui-core-react.umd.cjs",
6
6
  "module": "./dist/ui-core-react.js",
@@ -1,7 +1,6 @@
1
- import { useMemo, useState, useEffect } from 'react'
2
- import type { ReactNode } from 'react'
1
+ import { useMemo, useRef, useState, useEffect } from 'react'
2
+ import type { FocusEvent, ReactNode } from 'react'
3
3
  import { ConfirmModal } from './ConfirmModal'
4
- import { VenueSwitcher } from './VenueSwitcher'
5
4
  import { useTheme } from '../hooks/useTheme'
6
5
  import type { AuthUser, SidebarNavItem, Venue } from '../types'
7
6
  import '../styles/shell.css'
@@ -14,11 +13,20 @@ interface Props {
14
13
  navItems: SidebarNavItem[]
15
14
  cookieDomain?: string
16
15
  onSwitchVenue?: (venueId: string) => void
16
+ onSwitchWorkspace?: (workspaceId: string) => void
17
17
  onLogout: () => void
18
18
  accountSettingsUrl?: string
19
19
  headerSlot?: ReactNode
20
+ headerActions?: ReactNode
20
21
  sidebarPanel?: ReactNode
21
22
  scrollable?: boolean
23
+ /**
24
+ * When the consumer updates the active venue inline (reactively, from
25
+ * `onSwitchVenue`) the AppShell skips the otherwise-redundant full-page reload
26
+ * that would just refresh the app you're already on. Apps that re-route on a
27
+ * reload (rather than reactively) should leave this off.
28
+ */
29
+ inlineVenueSwitch?: boolean
22
30
  children: ReactNode
23
31
  }
24
32
 
@@ -55,6 +63,8 @@ function writeSidebarCookie(value: boolean, cookieDomain?: string): void {
55
63
  document.cookie = `${SIDEBAR_COOKIE}=${value}; Path=/; Max-Age=${SIDEBAR_COOKIE_MAX_AGE}; SameSite=Lax${domainPart}`
56
64
  }
57
65
 
66
+ const placementOf = (item: SidebarNavItem) => item.placement ?? 'header'
67
+
58
68
  export function AppShell({
59
69
  user,
60
70
  isAdmin,
@@ -63,17 +73,31 @@ export function AppShell({
63
73
  navItems,
64
74
  cookieDomain,
65
75
  onSwitchVenue,
76
+ onSwitchWorkspace,
66
77
  onLogout,
67
78
  accountSettingsUrl,
68
79
  headerSlot,
80
+ headerActions,
69
81
  sidebarPanel,
70
82
  scrollable = true,
83
+ inlineVenueSwitch,
71
84
  children,
72
85
  }: Props) {
73
86
  const { theme, toggleTheme } = useTheme()
74
87
  const [collapsed, setCollapsed] = useState(() => readSidebarCookie())
75
88
  const [showLogoutConfirm, setShowLogoutConfirm] = useState(false)
76
89
  const [currentHref, setCurrentHref] = useState(() => window.location.href)
90
+ const [expanded, setExpanded] = useState<Set<string>>(() => new Set())
91
+
92
+ // When collapsed, the sidebar is a thin icon rail. Hovering (or focusing into) the
93
+ // nav reveals the full sidebar as an overlay drawer floating over the page content —
94
+ // so navigation stays usable without permanently reflowing the page. `railMode` is
95
+ // the visual "mini rail" state: collapsed AND the drawer is not currently revealed.
96
+ const [flyoutOpen, setFlyoutOpen] = useState(false)
97
+ const railMode = collapsed && !flyoutOpen
98
+ const asideRef = useRef<HTMLElement>(null)
99
+ const openTimer = useRef<ReturnType<typeof setTimeout>>()
100
+ const closeTimer = useRef<ReturnType<typeof setTimeout>>()
77
101
 
78
102
  useEffect(() => {
79
103
  ensureHistoryPatched()
@@ -86,14 +110,65 @@ export function AppShell({
86
110
  }
87
111
  }, [])
88
112
 
113
+ function clearFlyoutTimers() {
114
+ if (openTimer.current) clearTimeout(openTimer.current)
115
+ if (closeTimer.current) clearTimeout(closeTimer.current)
116
+ openTimer.current = undefined
117
+ closeTimer.current = undefined
118
+ }
119
+
120
+ useEffect(() => clearFlyoutTimers, [])
121
+
122
+ const FLYOUT_DELAY = 150
123
+
124
+ function scheduleOpen() {
125
+ if (!collapsed) return
126
+ clearFlyoutTimers()
127
+ openTimer.current = setTimeout(() => setFlyoutOpen(true), FLYOUT_DELAY)
128
+ }
129
+
130
+ function scheduleClose() {
131
+ clearFlyoutTimers()
132
+ closeTimer.current = setTimeout(() => setFlyoutOpen(false), FLYOUT_DELAY)
133
+ }
134
+
135
+ function closeFlyoutNow() {
136
+ clearFlyoutTimers()
137
+ setFlyoutOpen(false)
138
+ }
139
+
140
+ function openFlyout() {
141
+ if (!collapsed) return
142
+ clearFlyoutTimers()
143
+ setFlyoutOpen(true)
144
+ }
145
+
146
+ function onFocusIn() {
147
+ openFlyout()
148
+ }
149
+
150
+ function onFocusOut(event: FocusEvent<HTMLElement>) {
151
+ const next = event.relatedTarget as Node | null
152
+ if (next && asideRef.current?.contains(next)) return
153
+ closeFlyoutNow()
154
+ }
155
+
89
156
  function toggleCollapse() {
90
157
  const next = !collapsed
91
158
  setCollapsed(next)
92
159
  writeSidebarCookie(next, cookieDomain)
160
+ closeFlyoutNow()
93
161
  }
94
162
 
95
163
  const activeVenue = useMemo(() => venues.find((v) => v.id === activeVenueId), [venues, activeVenueId])
96
- const workspaceName = activeVenue?.workspace?.name ?? ''
164
+ const activeWorkspaceId = activeVenue?.workspace?.id ?? ''
165
+
166
+ // Keep the active workspace expanded by default.
167
+ useEffect(() => {
168
+ if (activeWorkspaceId) {
169
+ setExpanded((prev) => (prev.has(activeWorkspaceId) ? prev : new Set(prev).add(activeWorkspaceId)))
170
+ }
171
+ }, [activeWorkspaceId])
97
172
 
98
173
  const userInitials = useMemo(() => {
99
174
  if (!user?.email) return '?'
@@ -101,35 +176,98 @@ export function AppShell({
101
176
  }, [user?.email])
102
177
 
103
178
  const visibleNavItems = useMemo(() => {
104
- const activeWorkspaceId = activeVenue?.workspace?.id
179
+ const wsId = activeVenue?.workspace?.id
105
180
  return navItems.filter((item) => {
106
181
  if (item.hidden) return false
107
182
  if (!item.permission) return true
108
183
  if (isAdmin) return true
109
184
  if (user?.globalPermissions.includes(item.permission)) return true
110
- if (activeWorkspaceId && user?.workspacePermissions?.[activeWorkspaceId]?.includes(item.permission)) return true
185
+ if (wsId && user?.workspacePermissions?.[wsId]?.includes(item.permission)) return true
111
186
  if (activeVenueId && user?.venuePermissions?.[activeVenueId]?.includes(item.permission)) return true
112
187
  return false
113
188
  })
114
189
  }, [navItems, isAdmin, user, activeVenue, activeVenueId])
115
190
 
116
- const mainNavItems = useMemo(() => visibleNavItems.filter((item) => !item.pinned), [visibleNavItems])
117
- const pinnedNavItems = useMemo(() => visibleNavItems.filter((item) => item.pinned), [visibleNavItems])
191
+ const sidebarNavItems = useMemo(() => visibleNavItems.filter((i) => placementOf(i) === 'sidebar'), [visibleNavItems])
192
+ const headerNavItems = useMemo(() => visibleNavItems.filter((i) => placementOf(i) === 'header'), [visibleNavItems])
193
+ const settingsNavItems = useMemo(() => visibleNavItems.filter((i) => placementOf(i) === 'settings'), [visibleNavItems])
194
+ const workspaceNavItem = useMemo(() => visibleNavItems.find((i) => placementOf(i) === 'workspace'), [visibleNavItems])
195
+ const firstHeaderItem = headerNavItems[0]
196
+
197
+ const tree = useMemo(() => {
198
+ const groups: { id: string; name: string; venues: Venue[] }[] = []
199
+ for (const venue of venues) {
200
+ const ws = venue.workspace
201
+ if (!ws?.id) continue
202
+ let group = groups.find((g) => g.id === ws.id)
203
+ if (!group) {
204
+ group = { id: ws.id, name: ws.name, venues: [] }
205
+ groups.push(group)
206
+ }
207
+ group.venues.push(venue)
208
+ }
209
+ return groups
210
+ }, [venues])
118
211
 
119
- const activeNavUrl = useMemo(() => {
212
+ const activeNavItem = useMemo(() => {
120
213
  const matches = navItems
121
214
  .filter(item => item.url && currentHref.startsWith(item.url))
122
215
  .sort((a, b) => b.url.length - a.url.length)
123
- return matches[0]?.url ?? null
216
+ return matches[0] ?? null
124
217
  }, [navItems, currentHref])
125
218
 
126
- const showHeader = venues.length > 0 || headerSlot !== undefined
219
+ const activeNavUrl = activeNavItem?.url ?? null
220
+
221
+ function toggleWorkspace(id: string) {
222
+ setExpanded((prev) => {
223
+ const next = new Set(prev)
224
+ next.has(id) ? next.delete(id) : next.add(id)
225
+ return next
226
+ })
227
+ }
228
+
229
+ function onWorkspaceTitleClick(workspaceId: string) {
230
+ onSwitchWorkspace?.(workspaceId)
231
+ const url = workspaceNavItem?.url
232
+ if (url) {
233
+ window.location.assign(url)
234
+ } else if (!expanded.has(workspaceId)) {
235
+ toggleWorkspace(workspaceId)
236
+ }
237
+ }
238
+
239
+ function onVenueClick(venueId: string) {
240
+ // Stay within the current venue/workspace-scoped app (heatmap, flow builder,
241
+ // venue/workspace settings) when switching venue, instead of always jumping to
242
+ // the heatmap. From the navigational hubs (Portfolio/Dashboard) we still drill
243
+ // into the venue's heatmap — the first header tool.
244
+ const stayHere = !!activeNavItem && (placementOf(activeNavItem) === 'header' || placementOf(activeNavItem) === 'settings')
245
+ const url = stayHere ? activeNavItem!.url : firstHeaderItem?.url
246
+ const alreadyOnDestination = url ? currentHref.startsWith(url) : false
247
+ if (venueId === activeVenueId && alreadyOnDestination) return
248
+ if (venueId !== activeVenueId) onSwitchVenue?.(venueId)
249
+ // If the consumer applies the venue switch inline and we'd only be reloading the
250
+ // page we're already on, skip the disruptive full-page reload.
251
+ if (inlineVenueSwitch && alreadyOnDestination) return
252
+ if (url) window.location.assign(url)
253
+ }
254
+
255
+ const showHeader = headerNavItems.length > 0 || settingsNavItems.length > 0 || headerActions !== undefined || headerSlot !== undefined || (collapsed && !!activeVenue)
127
256
 
128
257
  return (
129
258
  <div className="rv-app-shell">
130
- <aside className={`rv-sidebar${collapsed ? ' rv-collapsed' : ''}`}>
259
+ {/* Holds the rail's width in the layout flow while the real sidebar floats over the page. */}
260
+ {collapsed && <div className="rv-sidebar-rail-spacer" />}
261
+ <aside
262
+ ref={asideRef}
263
+ className={`rv-sidebar${railMode ? ' rv-collapsed' : ''}${collapsed ? ' rv-floating' : ''}${collapsed && flyoutOpen ? ' rv-flyout' : ''}`}
264
+ onMouseLeave={scheduleClose}
265
+ onFocus={onFocusIn}
266
+ onBlur={onFocusOut}
267
+ onKeyDown={(e) => { if (e.key === 'Escape') closeFlyoutNow() }}
268
+ >
131
269
  <div className="rv-sidebar-top">
132
- {!collapsed && (
270
+ {!railMode && (
133
271
  <div className="rv-sidebar-brand">
134
272
  <svg
135
273
  className="rv-sidebar-logo"
@@ -154,7 +292,6 @@ export function AppShell({
154
292
  </clipPath>
155
293
  </defs>
156
294
  </svg>
157
- {workspaceName && <span className="rv-sidebar-workspace">{workspaceName}</span>}
158
295
  </div>
159
296
  )}
160
297
  <button
@@ -176,41 +313,88 @@ export function AppShell({
176
313
  </button>
177
314
  </div>
178
315
 
179
- {sidebarPanel && <div className="rv-sidebar-panel">{sidebarPanel}</div>}
180
-
181
- <nav className="rv-sidebar-nav">
182
- {mainNavItems.map((item) => (
183
- <a
184
- key={item.url}
185
- href={item.url}
186
- className={`rv-nav-item${item.url === activeNavUrl ? ' rv-active' : ''}`}
187
- title={collapsed ? item.label : undefined}
188
- >
189
- <span className="rv-nav-icon" dangerouslySetInnerHTML={{ __html: item.icon }} />
190
- {!collapsed && <span className="rv-nav-label">{item.label}</span>}
191
- </a>
192
- ))}
193
- </nav>
316
+ {sidebarPanel && <div className="rv-sidebar-panel" onMouseEnter={scheduleOpen}>{sidebarPanel}</div>}
194
317
 
195
- {pinnedNavItems.length > 0 && (
196
- <nav className="rv-sidebar-pinned">
197
- {pinnedNavItems.map((item) => (
318
+ {sidebarNavItems.length > 0 && (
319
+ <nav className="rv-sidebar-top-nav" onMouseEnter={scheduleOpen}>
320
+ {sidebarNavItems.map((item) => (
198
321
  <a
199
322
  key={item.url}
200
323
  href={item.url}
201
324
  className={`rv-nav-item${item.url === activeNavUrl ? ' rv-active' : ''}`}
202
- title={collapsed ? item.label : undefined}
325
+ title={railMode ? item.label : undefined}
203
326
  >
204
327
  <span className="rv-nav-icon" dangerouslySetInnerHTML={{ __html: item.icon }} />
205
- {!collapsed && <span className="rv-nav-label">{item.label}</span>}
328
+ {!railMode && <span className="rv-nav-label">{item.label}</span>}
206
329
  </a>
207
330
  ))}
208
331
  </nav>
209
332
  )}
210
333
 
211
- <div className="rv-sidebar-user">
334
+ <div className="rv-sidebar-tree" onMouseEnter={scheduleOpen}>
335
+ {railMode ? (
336
+ /* Collapsed rail: a single workspace icon stands in for the whole tree (hover/click reveals it). */
337
+ tree.length > 0 && (
338
+ <div className="rv-tree-ws">
339
+ <button
340
+ className="rv-tree-ws-title"
341
+ title="Workspaces & venues"
342
+ aria-label="Show workspaces and venues"
343
+ onClick={openFlyout}
344
+ >
345
+ <svg className="rv-tree-ws-icon" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
346
+ <path d="M3 21h18" /><path d="M5 21V7l7-4 7 4v14" /><path d="M9 9h.01M15 9h.01M9 13h.01M15 13h.01M9 17h.01M15 17h.01" />
347
+ </svg>
348
+ </button>
349
+ </div>
350
+ )
351
+ ) : (
352
+ <>
353
+ {tree.length > 0 && <div className="rv-tree-section-label">Workspaces &amp; venues</div>}
354
+ {tree.map((ws) => (
355
+ <div key={ws.id} className="rv-tree-group">
356
+ <div className="rv-tree-ws">
357
+ <button
358
+ className={`rv-tree-disclosure${expanded.has(ws.id) ? ' rv-open' : ''}`}
359
+ aria-label={expanded.has(ws.id) ? 'Collapse workspace' : 'Expand workspace'}
360
+ onClick={(e) => { e.stopPropagation(); toggleWorkspace(ws.id) }}
361
+ >
362
+ <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
363
+ <polyline points="6 9 12 15 18 9" />
364
+ </svg>
365
+ </button>
366
+ <button className="rv-tree-ws-title" onClick={() => onWorkspaceTitleClick(ws.id)}>
367
+ <svg className="rv-tree-ws-icon" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
368
+ <path d="M3 21h18" /><path d="M5 21V7l7-4 7 4v14" /><path d="M9 9h.01M15 9h.01M9 13h.01M15 13h.01M9 17h.01M15 17h.01" />
369
+ </svg>
370
+ <span className="rv-tree-ws-name">{ws.name}</span>
371
+ </button>
372
+ </div>
373
+ {expanded.has(ws.id) && (
374
+ <div className="rv-tree-venues">
375
+ {ws.venues.map((venue) => (
376
+ <button
377
+ key={venue.id}
378
+ className={`rv-tree-venue${venue.id === activeVenueId ? ' rv-active' : ''}`}
379
+ onClick={() => onVenueClick(venue.id)}
380
+ >
381
+ <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
382
+ <path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0 1 18 0z" /><circle cx="12" cy="10" r="3" />
383
+ </svg>
384
+ <span className="rv-tree-venue-name">{venue.name}</span>
385
+ </button>
386
+ ))}
387
+ </div>
388
+ )}
389
+ </div>
390
+ ))}
391
+ </>
392
+ )}
393
+ </div>
394
+
395
+ <div className="rv-sidebar-user" onMouseEnter={scheduleOpen}>
212
396
  <div className="rv-user-avatar">{userInitials}</div>
213
- {!collapsed && (
397
+ {!railMode && (
214
398
  <div className="rv-user-info">
215
399
  <span className="rv-user-email" title={user?.email ?? ''}>{user?.email}</span>
216
400
  </div>
@@ -230,7 +414,7 @@ export function AppShell({
230
414
  )}
231
415
  </button>
232
416
  )}
233
- {accountSettingsUrl && (
417
+ {accountSettingsUrl && !railMode && (
234
418
  <a className="rv-account-settings-btn" href={accountSettingsUrl} title="Account settings">
235
419
  <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
236
420
  <path d="M12.22 2h-.44a2 2 0 0 0-2 2v.18a2 2 0 0 1-1 1.73l-.43.25a2 2 0 0 1-2 0l-.15-.08a2 2 0 0 0-2.73.73l-.22.38a2 2 0 0 0 .73 2.73l.15.1a2 2 0 0 1 1 1.72v.51a2 2 0 0 1-1 1.74l-.15.09a2 2 0 0 0-.73 2.73l.22.38a2 2 0 0 0 2.73.73l.15-.08a2 2 0 0 1 2 0l.43.25a2 2 0 0 1 1 1.73V20a2 2 0 0 0 2 2h.44a2 2 0 0 0 2-2v-.18a2 2 0 0 1 1-1.73l.43-.25a2 2 0 0 1 2 0l.15.08a2 2 0 0 0 2.73-.73l.22-.39a2 2 0 0 0-.73-2.73l-.15-.08a2 2 0 0 1-1-1.74v-.5a2 2 0 0 1 1-1.74l.15-.09a2 2 0 0 0 .73-2.73l-.22-.38a2 2 0 0 0-2.73-.73l-.15.08a2 2 0 0 1-2 0l-.43-.25a2 2 0 0 1-1-1.73V4a2 2 0 0 0-2-2z" />
@@ -238,6 +422,7 @@ export function AppShell({
238
422
  </svg>
239
423
  </a>
240
424
  )}
425
+ {!railMode && (
241
426
  <button className="rv-logout-btn" title="Sign out" onClick={() => setShowLogoutConfirm(true)}>
242
427
  <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
243
428
  <path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4" />
@@ -245,18 +430,55 @@ export function AppShell({
245
430
  <line x1="21" y1="12" x2="9" y2="12" />
246
431
  </svg>
247
432
  </button>
433
+ )}
248
434
  </div>
249
435
  </aside>
250
436
 
251
437
  <div className="rv-shell-body">
252
438
  {showHeader && (
253
439
  <header className="rv-shell-header">
254
- {headerSlot !== undefined ? headerSlot : (
255
- <VenueSwitcher
256
- value={activeVenueId ?? ''}
257
- venues={venues}
258
- onChange={onSwitchVenue ?? (() => undefined)}
259
- />
440
+ {/* When collapsed the active venue isn't named in the rail, so surface it here. */}
441
+ {collapsed && activeVenue && (
442
+ <div className="rv-header-venue" title={activeVenue.name}>
443
+ <svg xmlns="http://www.w3.org/2000/svg" width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
444
+ <path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0 1 18 0z" /><circle cx="12" cy="10" r="3" />
445
+ </svg>
446
+ <span className="rv-header-venue-name">{activeVenue.name}</span>
447
+ </div>
448
+ )}
449
+ {headerNavItems.length > 0 && (
450
+ <nav className="rv-header-nav">
451
+ {headerNavItems.map((item) => (
452
+ <a
453
+ key={item.url}
454
+ href={item.url}
455
+ className={`rv-header-nav-item${item.url === activeNavUrl ? ' rv-active' : ''}`}
456
+ >
457
+ <span className="rv-nav-icon" dangerouslySetInnerHTML={{ __html: item.icon }} />
458
+ <span className="rv-nav-label">{item.label}</span>
459
+ </a>
460
+ ))}
461
+ </nav>
462
+ )}
463
+ {headerSlot}
464
+ {(headerActions || settingsNavItems.length > 0) && (
465
+ <div className="rv-header-right">
466
+ {headerActions}
467
+ {settingsNavItems.length > 0 && (
468
+ <div className="rv-header-settings">
469
+ {settingsNavItems.map((item) => (
470
+ <a
471
+ key={item.url}
472
+ href={item.url}
473
+ className={`rv-header-action${item.url === activeNavUrl ? ' rv-active' : ''}`}
474
+ >
475
+ <span className="rv-header-action-icon" dangerouslySetInnerHTML={{ __html: item.icon }} />
476
+ <span className="rv-header-action-label">{item.label}</span>
477
+ </a>
478
+ ))}
479
+ </div>
480
+ )}
481
+ </div>
260
482
  )}
261
483
  </header>
262
484
  )}