@orsetra/shared-ui 1.10.3 → 1.10.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.
@@ -27,8 +27,9 @@ export function QuickAccessTracker({ label, icon, enabled = true }: QuickAccessT
27
27
 
28
28
  useEffect(() => {
29
29
  if (!enabled || !label || !pathname) return
30
- addRef.current({ label, href: window.location.href, icon })
31
- // pathname triggers the effect on navigation; window.location.href captures the full URL
30
+ // Use clean URL (no query params) — useQuickAccess normalises further
31
+ const href = window.location.origin + window.location.pathname
32
+ addRef.current({ label, href, icon })
32
33
  // eslint-disable-next-line react-hooks/exhaustive-deps
33
34
  }, [pathname, label, icon, enabled])
34
35
 
@@ -47,6 +47,7 @@ export { Pagination, PaginationContent, PaginationEllipsis, PaginationItem, Pagi
47
47
  export { Popover, PopoverTrigger, PopoverContent } from './popover'
48
48
  export { Progress } from './progress'
49
49
  export { RadioGroup, RadioGroupItem } from './radio-group'
50
+ export { RelativeTimeRangePicker, type RelativeTimeRangeOption, type RelativeTimeRangePickerProps } from './relative-time-range-picker'
50
51
  export { ResizablePanelGroup, ResizablePanel, ResizableHandle } from './resizable'
51
52
  export { ScrollArea, ScrollBar } from './scroll-area'
52
53
  export { Select, SelectGroup, SelectValue, SelectTrigger, SelectContent, SelectLabel, SelectItem, SelectSeparator } from './select'
@@ -0,0 +1,93 @@
1
+ "use client"
2
+
3
+ import * as React from "react"
4
+ import { Check, ChevronDown, Clock } from "lucide-react"
5
+ import { cn } from "../../lib/utils"
6
+ import { Button } from "./button"
7
+ import { Popover, PopoverContent, PopoverTrigger } from "./popover"
8
+
9
+ export interface RelativeTimeRangeOption {
10
+ label: string
11
+ /** Length of the window, in seconds (e.g. 3600 for "1h"). Used as the option's identity. */
12
+ seconds: number
13
+ }
14
+
15
+ export interface RelativeTimeRangePickerProps {
16
+ id?: string
17
+ value: number
18
+ onChange: (seconds: number) => void
19
+ options: RelativeTimeRangeOption[]
20
+ disabled?: boolean
21
+ className?: string
22
+ /** Popover alignment relative to the trigger. Defaults to "end" (picker anchored right — the
23
+ * common placement next to a panel's close button). */
24
+ align?: "start" | "center" | "end"
25
+ /** Accessible name and popover heading. Defaults to "Time range". */
26
+ triggerLabel?: string
27
+ }
28
+
29
+ /**
30
+ * Flyout picker for a small set of relative time ranges (20m, 1h, 3h, ...), styled as a gallery
31
+ * of choices rather than a dropdown list — the current selection is highlighted with both a
32
+ * filled background and a check mark (not color alone), mirroring how Calendar marks the
33
+ * selected day and Combobox marks the selected item in this same component set.
34
+ */
35
+ export function RelativeTimeRangePicker({
36
+ id,
37
+ value,
38
+ onChange,
39
+ options,
40
+ disabled = false,
41
+ className,
42
+ align = "end",
43
+ triggerLabel = "Time range",
44
+ }: RelativeTimeRangePickerProps) {
45
+ const [open, setOpen] = React.useState(false)
46
+ const selected = options.find(o => o.seconds === value)
47
+
48
+ return (
49
+ <Popover open={open} onOpenChange={setOpen}>
50
+ <PopoverTrigger asChild>
51
+ <Button
52
+ id={id}
53
+ type="button"
54
+ variant="secondary"
55
+ size="sm"
56
+ disabled={disabled}
57
+ aria-label={triggerLabel}
58
+ className={cn("rounded-none", className)}
59
+ leftIcon={<Clock className="h-3.5 w-3.5" />}
60
+ rightIcon={<ChevronDown className="h-3.5 w-3.5 opacity-60" />}
61
+ >
62
+ {selected?.label ?? "Select…"}
63
+ </Button>
64
+ </PopoverTrigger>
65
+ <PopoverContent align={align} sideOffset={6} className="w-auto rounded-none p-2">
66
+ <p className="px-1 pb-2 text-xs font-medium text-ibm-gray-50">{triggerLabel}</p>
67
+ <div role="listbox" aria-label={triggerLabel} className="grid grid-cols-3 gap-1.5">
68
+ {options.map(opt => {
69
+ const isSelected = opt.seconds === value
70
+ return (
71
+ <button
72
+ key={opt.seconds}
73
+ type="button"
74
+ role="option"
75
+ aria-selected={isSelected}
76
+ onClick={() => { onChange(opt.seconds); setOpen(false) }}
77
+ className={cn(
78
+ "flex items-center justify-center gap-1 h-9 px-2 text-sm font-medium border transition-colors whitespace-nowrap",
79
+ isSelected
80
+ ? "bg-ibm-blue-60 text-white border-ibm-blue-60"
81
+ : "bg-white text-ibm-gray-100 border-ibm-gray-20 hover:bg-ibm-blue-10 hover:border-ibm-blue-40"
82
+ )}
83
+ >
84
+ {isSelected && <Check className="h-3 w-3 flex-shrink-0" />}
85
+ {opt.label}
86
+ </button>
87
+ )
88
+ })}
89
+ </div>
90
+ </PopoverContent>
91
+ </Popover>
92
+ )
93
+ }
@@ -2,14 +2,26 @@
2
2
 
3
3
  import { useState, useEffect, useCallback } from "react"
4
4
 
5
- const STORAGE_KEY = "sidebar:quick-access"
6
- const MAX_ITEMS = 4
5
+ const STORAGE_KEY = "sidebar:quick-access"
6
+ const POOL_SIZE = 20 // internal storage
7
+ const DISPLAY_SIZE = 4 // shown in UI
8
+ const DECAY_DAYS = 14 // recency half-life
7
9
 
8
10
  export interface QuickAccessItem {
9
- id: string
10
- label: string
11
- href: string
12
- icon?: string
11
+ id: string // origin + pathname (stable, no query params)
12
+ label: string
13
+ href: string // same as id — clean URL for navigation
14
+ icon?: string
15
+ visitCount: number
16
+ lastVisit: number // unix ms
17
+ }
18
+
19
+ /** Score: balances visit frequency with recency. Items fade after DECAY_DAYS days. */
20
+ function score(item: QuickAccessItem): number {
21
+ const ageMs = Date.now() - item.lastVisit
22
+ const ageDays = ageMs / (1000 * 60 * 60 * 24)
23
+ const recency = Math.exp(-ageDays / DECAY_DAYS) // 1.0 → 0 over time
24
+ return item.visitCount * 0.6 + recency * 0.4
13
25
  }
14
26
 
15
27
  function readStorage(): QuickAccessItem[] {
@@ -21,7 +33,12 @@ function readStorage(): QuickAccessItem[] {
21
33
  return Array.isArray(parsed)
22
34
  ? parsed.filter(
23
35
  (i): i is QuickAccessItem =>
24
- i && typeof i.id === "string" && typeof i.label === "string" && typeof i.href === "string"
36
+ i &&
37
+ typeof i.id === "string" &&
38
+ typeof i.label === "string" &&
39
+ typeof i.href === "string" &&
40
+ typeof i.visitCount === "number" &&
41
+ typeof i.lastVisit === "number"
25
42
  )
26
43
  : []
27
44
  } catch {
@@ -33,42 +50,69 @@ function writeStorage(items: QuickAccessItem[]): void {
33
50
  try { localStorage.setItem(STORAGE_KEY, JSON.stringify(items)) } catch {}
34
51
  }
35
52
 
53
+ /** Top DISPLAY_SIZE items from pool, sorted by score descending. */
54
+ function topItems(pool: QuickAccessItem[]): QuickAccessItem[] {
55
+ return [...pool].sort((a, b) => score(b) - score(a)).slice(0, DISPLAY_SIZE)
56
+ }
57
+
36
58
  export function useQuickAccess() {
37
- const [items, setItems] = useState<QuickAccessItem[]>(readStorage)
59
+ const [pool, setPool] = useState<QuickAccessItem[]>(readStorage)
60
+ const [items, setItems] = useState<QuickAccessItem[]>(() => topItems(readStorage()))
38
61
 
39
62
  // Sync across tabs
40
63
  useEffect(() => {
41
64
  const onStorage = (e: StorageEvent) => {
42
- if (e.key === STORAGE_KEY) setItems(readStorage())
65
+ if (e.key === STORAGE_KEY) {
66
+ const p = readStorage()
67
+ setPool(p)
68
+ setItems(topItems(p))
69
+ }
43
70
  }
44
71
  window.addEventListener("storage", onStorage)
45
72
  return () => window.removeEventListener("storage", onStorage)
46
73
  }, [])
47
74
 
48
- const addItem = useCallback((item: Omit<QuickAccessItem, "id"> & { id?: string }) => {
49
- setItems(prev => {
50
- const id = item.id ?? item.href
51
- const next = [
52
- { id, label: item.label, href: item.href, icon: item.icon },
53
- ...prev.filter(i => i.href !== item.href),
54
- ].slice(0, MAX_ITEMS)
75
+ const addItem = useCallback((item: { label: string; href: string; icon?: string }) => {
76
+ // Normalize: strip query params and fragments — use origin + pathname as stable id
77
+ let id: string
78
+ try {
79
+ const u = new URL(item.href)
80
+ id = u.origin + u.pathname
81
+ } catch {
82
+ id = item.href
83
+ }
84
+
85
+ setPool(prev => {
86
+ const existing = prev.find(i => i.id === id)
87
+ const updated: QuickAccessItem = existing
88
+ ? { ...existing, label: item.label, icon: item.icon, visitCount: existing.visitCount + 1, lastVisit: Date.now() }
89
+ : { id, label: item.label, href: id, icon: item.icon, visitCount: 1, lastVisit: Date.now() }
90
+
91
+ // Merge into pool, sort by score, keep top POOL_SIZE
92
+ const next = [updated, ...prev.filter(i => i.id !== id)]
93
+ .sort((a, b) => score(b) - score(a))
94
+ .slice(0, POOL_SIZE)
95
+
55
96
  writeStorage(next)
97
+ setItems(topItems(next))
56
98
  return next
57
99
  })
58
100
  }, [])
59
101
 
60
- const removeItem = useCallback((href: string) => {
61
- setItems(prev => {
62
- const next = prev.filter(i => i.href !== href)
102
+ const removeItem = useCallback((id: string) => {
103
+ setPool(prev => {
104
+ const next = prev.filter(i => i.id !== id)
63
105
  writeStorage(next)
106
+ setItems(topItems(next))
64
107
  return next
65
108
  })
66
109
  }, [])
67
110
 
68
111
  const clearItems = useCallback(() => {
112
+ setPool([])
69
113
  setItems([])
70
114
  try { localStorage.removeItem(STORAGE_KEY) } catch {}
71
115
  }, [])
72
116
 
73
- return { items, addItem, removeItem, clearItems }
117
+ return { items, pool, addItem, removeItem, clearItems }
74
118
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orsetra/shared-ui",
3
- "version": "1.10.3",
3
+ "version": "1.10.5",
4
4
  "description": "Shared UI components for Orsetra platform",
5
5
  "main": "./index.ts",
6
6
  "types": "./index.ts",