@orsetra/shared-ui 1.10.3 → 1.10.4

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
 
@@ -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.4",
4
4
  "description": "Shared UI components for Orsetra platform",
5
5
  "main": "./index.ts",
6
6
  "types": "./index.ts",