@orsetra/shared-ui 1.11.3 → 1.11.5

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.
@@ -35,4 +35,5 @@ export { RootLayoutWrapper, ibmPlexSans, ibmPlexMono } from './root-layout-wrapp
35
35
  export { LayoutContainer, type LayoutContainerProps } from './layout-container'
36
36
  export { EnvironmentSwitcher, type EnvironmentSwitcherProps } from './environment-switcher'
37
37
  export { EnvironmentGate, type EnvironmentGateProps } from './environment-gate'
38
+ export { ProjectGate, useStoreHydrated, type ProjectGateProps } from './project-gate'
38
39
  export { PageWithSidePanel } from './page-with-side-panel'
@@ -13,6 +13,8 @@ import { Menu } from "lucide-react"
13
13
  import { UserPanel } from "./user-panel"
14
14
  import { EnvironmentSwitcher } from "./environment-switcher"
15
15
  import { EnvironmentGate } from "./environment-gate"
16
+ import { ProjectGate } from "./project-gate"
17
+ import type { ProjectSelection } from "../../lib/project-requirement"
16
18
  import { useMenuContext } from "../../context/menu-context"
17
19
 
18
20
  // ─── Layout ──────────────────────────────────────────────────────────────────
@@ -26,6 +28,8 @@ export interface LayoutContainerProps {
26
28
  getCurrentMenu?: (pathname: string, searchParams: URLSearchParams) => string
27
29
  getCurrentMenuItem?: (pathname: string, searchParams: URLSearchParams) => string
28
30
  isEnvironmentRequired?: (pathname: string) => boolean
31
+ projectSelection?: ProjectSelection
32
+ isProjectRequired?: boolean
29
33
  }
30
34
 
31
35
  function LayoutContent({
@@ -37,6 +41,8 @@ function LayoutContent({
37
41
  getCurrentMenu,
38
42
  getCurrentMenuItem,
39
43
  isEnvironmentRequired,
44
+ projectSelection,
45
+ isProjectRequired,
40
46
  }: LayoutContainerProps) {
41
47
  const { fetchMenus } = useMenuContext()
42
48
  const pathname = usePathname()
@@ -175,9 +181,11 @@ function LayoutContent({
175
181
  />
176
182
 
177
183
  <main className="flex-1 bg-ibm-gray-10 min-h-0 overflow-auto">
178
- <EnvironmentGate required={environmentRequired} createHref={createEnvironmentHref}>
179
- {children}
180
- </EnvironmentGate>
184
+ <ProjectGate selection={projectSelection} required={isProjectRequired} mainBaseUrl={main_base_url}>
185
+ <EnvironmentGate required={environmentRequired} createHref={createEnvironmentHref}>
186
+ {children}
187
+ </EnvironmentGate>
188
+ </ProjectGate>
181
189
  </main>
182
190
  </div>
183
191
  </div>
@@ -0,0 +1,47 @@
1
+ "use client"
2
+
3
+ import React, { useEffect } from "react"
4
+ import { Spinner } from "../ui/spinner"
5
+ import { mainAppHomeUrl, mustLeaveForMainApp, type ProjectSelection } from "../../lib/project-requirement"
6
+
7
+ export interface ProjectGateProps {
8
+ selection?: ProjectSelection
9
+ required?: boolean
10
+ mainBaseUrl: string
11
+ children: React.ReactNode
12
+ }
13
+
14
+ export function ProjectGate({ selection, required, mainBaseUrl, children }: ProjectGateProps) {
15
+ const leaving = mustLeaveForMainApp(selection, required)
16
+
17
+ useEffect(() => {
18
+ if (leaving) window.location.replace(mainAppHomeUrl(mainBaseUrl))
19
+ }, [leaving, mainBaseUrl])
20
+
21
+ if (!leaving) return <>{children}</>
22
+
23
+ return (
24
+ <div className="flex items-center justify-center min-h-[60vh]">
25
+ <Spinner size="lg" label="Opening the home page" />
26
+ </div>
27
+ )
28
+ }
29
+
30
+ interface PersistedStore {
31
+ persist?: {
32
+ hasHydrated: () => boolean
33
+ onFinishHydration: (listener: () => void) => () => void
34
+ }
35
+ }
36
+
37
+ export function useStoreHydrated(store: PersistedStore): boolean {
38
+ const [hydrated, setHydrated] = React.useState(() => store.persist?.hasHydrated() ?? true)
39
+
40
+ useEffect(() => {
41
+ if (!store.persist) return
42
+ if (store.persist.hasHydrated()) setHydrated(true)
43
+ return store.persist.onFinishHydration(() => setHydrated(true))
44
+ }, [store])
45
+
46
+ return hydrated
47
+ }
@@ -36,10 +36,11 @@ export interface AlertBannerProps
36
36
  onClose?: () => void
37
37
  closable?: boolean
38
38
  icon?: React.ReactNode
39
+ action?: React.ReactNode
39
40
  }
40
41
 
41
42
  const AlertBanner = React.forwardRef<HTMLDivElement, AlertBannerProps>(
42
- ({ className, variant = "info", message, onClose, closable = true, icon, ...props }, ref) => {
43
+ ({ className, variant = "info", message, onClose, closable = true, icon, action, ...props }, ref) => {
43
44
  const Icon = iconMap[variant || "info"]
44
45
 
45
46
  return (
@@ -51,6 +52,7 @@ const AlertBanner = React.forwardRef<HTMLDivElement, AlertBannerProps>(
51
52
  >
52
53
  {icon || <Icon className="h-5 w-5 flex-shrink-0" />}
53
54
  <span className="flex-1">{message}</span>
55
+ {action && <div className="flex-shrink-0 flex items-center">{action}</div>}
54
56
  {closable && onClose && (
55
57
  <button
56
58
  onClick={onClose}
@@ -41,6 +41,7 @@ export { ProjectSelectorModal, type Project as ProjectSelectorItem, type Project
41
41
  export { Drawer, DrawerTrigger, DrawerContent, DrawerHeader, DrawerFooter, DrawerTitle, DrawerDescription } from './drawer'
42
42
  export { Form, FormItem, FormLabel, FormControl, FormDescription, FormMessage, FormField } from './form'
43
43
  export { HoverCard, HoverCardTrigger, HoverCardContent } from './hover-card'
44
+ export { StatusIndicator, type StatusIndicatorProps } from './status-indicator'
44
45
  export { InputOTP, InputOTPGroup, InputOTPSlot, InputOTPSeparator } from './input-otp'
45
46
  export { Label } from './label'
46
47
  export { Menubar, MenubarMenu, MenubarTrigger, MenubarContent, MenubarItem, MenubarSeparator, MenubarLabel, MenubarCheckboxItem, MenubarRadioGroup, MenubarRadioItem, MenubarPortal, MenubarSubContent, MenubarSubTrigger, MenubarGroup, MenubarSub, MenubarShortcut } from './menubar'
@@ -0,0 +1,88 @@
1
+ "use client"
2
+
3
+ import * as React from "react"
4
+ import { cn } from "../../lib/utils"
5
+ import { HoverCard, HoverCardContent, HoverCardTrigger } from "./hover-card"
6
+ import {
7
+ STATUS_IDLE_COLOR, STATUS_WAVE_COLOR, STATUS_WAVE_PERIOD_MS, STATUS_WAVE_SQUARES,
8
+ statusLabel, statusToneColor, waveDelayMs,
9
+ type StatusTone,
10
+ } from "../../lib/status-indicator"
11
+
12
+ const SQUARE = 6
13
+ const GAP = 2
14
+
15
+ export interface StatusIndicatorProps {
16
+ loading?: boolean
17
+ tone?: StatusTone | null
18
+ label?: string
19
+ details?: React.ReactNode
20
+ detailsClassName?: string
21
+ side?: "top" | "right" | "bottom" | "left"
22
+ align?: "start" | "center" | "end"
23
+ openDelay?: number
24
+ closeDelay?: number
25
+ className?: string
26
+ }
27
+
28
+ function Wave() {
29
+ const width = STATUS_WAVE_SQUARES * SQUARE + (STATUS_WAVE_SQUARES - 1) * GAP
30
+ return (
31
+ <svg width={width} height={SQUARE} viewBox={`0 0 ${width} ${SQUARE}`} aria-hidden="true" focusable="false">
32
+ {Array.from({ length: STATUS_WAVE_SQUARES }, (_, index) => (
33
+ <rect key={index} x={index * (SQUARE + GAP)} y={0} width={SQUARE} height={SQUARE} fill={STATUS_IDLE_COLOR}>
34
+ <animate
35
+ attributeName="fill"
36
+ values={`${STATUS_IDLE_COLOR};${STATUS_WAVE_COLOR};${STATUS_IDLE_COLOR};${STATUS_IDLE_COLOR}`}
37
+ keyTimes="0;0.25;0.5;1"
38
+ dur={`${STATUS_WAVE_PERIOD_MS}ms`}
39
+ begin={`${waveDelayMs(index)}ms`}
40
+ repeatCount="indefinite"
41
+ />
42
+ </rect>
43
+ ))}
44
+ </svg>
45
+ )
46
+ }
47
+
48
+ function Settled({ tone }: { tone: StatusTone | null | undefined }) {
49
+ return (
50
+ <svg width={SQUARE} height={SQUARE} viewBox={`0 0 ${SQUARE} ${SQUARE}`} aria-hidden="true" focusable="false">
51
+ <rect x={0} y={0} width={SQUARE} height={SQUARE} fill={statusToneColor(tone)} />
52
+ </svg>
53
+ )
54
+ }
55
+
56
+ export function StatusIndicator({
57
+ loading = false, tone, label, details, detailsClassName,
58
+ side = "bottom", align = "start", openDelay = 150, closeDelay = 120, className,
59
+ }: StatusIndicatorProps) {
60
+ const spoken = statusLabel(loading, tone, label)
61
+ const mark = (
62
+ <span role="status" aria-label={spoken} className={cn("inline-flex items-center align-middle", className)}>
63
+ {loading ? <Wave /> : <Settled tone={tone} />}
64
+ </span>
65
+ )
66
+
67
+ if (!details) return mark
68
+
69
+ return (
70
+ <HoverCard openDelay={openDelay} closeDelay={closeDelay}>
71
+ <HoverCardTrigger asChild>
72
+ <button
73
+ type="button"
74
+ aria-label={spoken}
75
+ className={cn(
76
+ "inline-flex items-center align-middle p-1 -m-1 cursor-default focus:outline-none focus-visible:ring-2 focus-visible:ring-ibm-blue-60",
77
+ className,
78
+ )}
79
+ >
80
+ {loading ? <Wave /> : <Settled tone={tone} />}
81
+ </button>
82
+ </HoverCardTrigger>
83
+ <HoverCardContent side={side} align={align} className={cn("w-80 rounded-none p-4 text-sm", detailsClassName)}>
84
+ {details}
85
+ </HoverCardContent>
86
+ </HoverCard>
87
+ )
88
+ }
package/index.ts CHANGED
@@ -6,6 +6,8 @@ export { BaseService } from './lib/base-service'
6
6
  export * from './lib/config-selection'
7
7
  export * from './lib/environment-context'
8
8
  export * from './lib/environment-controller'
9
+ export * from './lib/project-requirement'
10
+ export * from './lib/status-indicator'
9
11
  export { createPlatformConfigSource, templateFilter, configOfHit, environmentWithTargets, type ConfigSource } from './lib/config-source'
10
12
  export { default as HttpClient, useHttpClient, ApiError } from './lib/http-client'
11
13
 
@@ -0,0 +1,24 @@
1
+ export type ProjectSelection = "pending" | "none" | "selected"
2
+
3
+ export interface ProjectSelectionInput {
4
+ hydrated: boolean
5
+ projectId: string | null | undefined
6
+ projectsLoaded: boolean
7
+ isKnown: boolean
8
+ }
9
+
10
+ export function projectSelectionOf({ hydrated, projectId, projectsLoaded, isKnown }: ProjectSelectionInput): ProjectSelection {
11
+ if (!hydrated) return "pending"
12
+ if (!projectId) return "none"
13
+ if (!projectsLoaded) return "selected"
14
+ return isKnown ? "selected" : "none"
15
+ }
16
+
17
+ export function mustLeaveForMainApp(selection: ProjectSelection | undefined, required: boolean | undefined): boolean {
18
+ return required === true && selection === "none"
19
+ }
20
+
21
+ export function mainAppHomeUrl(mainBaseUrl: string | null | undefined): string {
22
+ const base = (mainBaseUrl ?? "").trim().replace(/\/+$/, "")
23
+ return `${base}/`
24
+ }
@@ -0,0 +1,28 @@
1
+ export type StatusTone = "success" | "warning" | "error"
2
+
3
+ export const STATUS_TONE_COLOR: Readonly<Record<StatusTone, string>> = {
4
+ success: "#24a148",
5
+ warning: "#ff832b",
6
+ error: "#da1e28",
7
+ }
8
+
9
+ export const STATUS_IDLE_COLOR = "#c6c6c6"
10
+ export const STATUS_WAVE_COLOR = "#0f62fe"
11
+ export const STATUS_WAVE_SQUARES = 3
12
+ export const STATUS_WAVE_PERIOD_MS = 1_200
13
+
14
+ export function statusToneColor(tone: StatusTone | null | undefined): string {
15
+ return (tone && STATUS_TONE_COLOR[tone]) || STATUS_IDLE_COLOR
16
+ }
17
+
18
+ export function waveDelayMs(index: number, squares: number = STATUS_WAVE_SQUARES, periodMs: number = STATUS_WAVE_PERIOD_MS): number {
19
+ if (squares <= 0) return 0
20
+ const position = ((index % squares) + squares) % squares
21
+ return Math.round((periodMs / (squares + 1)) * position)
22
+ }
23
+
24
+ export function statusLabel(loading: boolean, tone: StatusTone | null | undefined, label?: string): string {
25
+ if (label) return label
26
+ if (loading) return "In progress"
27
+ return tone === "success" ? "Healthy" : tone === "warning" ? "Needs attention" : tone === "error" ? "Failed" : "Unknown"
28
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orsetra/shared-ui",
3
- "version": "1.11.3",
3
+ "version": "1.11.5",
4
4
  "description": "Shared UI components for Orsetra platform",
5
5
  "main": "./index.ts",
6
6
  "types": "./index.ts",