@orsetra/shared-ui 1.11.7 → 1.11.8

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,40 +3,27 @@
3
3
  import { useEffect, useRef } from "react"
4
4
  import { usePathname } from "next/navigation"
5
5
  import { useQuickAccess } from "../../../hooks/use-quick-access"
6
+ import { quickAccessHref } from "../../../lib/quick-access-href"
6
7
 
7
8
  interface QuickAccessTrackerProps {
8
- /** Human-readable label shown in the quick-access list */
9
9
  label: string
10
- /** Icon name (resolved via resolveIcon). Defaults to "Box". */
11
10
  icon?: string
12
- /** Set to false to skip tracking this page (e.g. list/index pages) */
13
11
  enabled?: boolean
14
- /**
15
- * Canonical href for this resource — overrides the current page URL.
16
- * Pass the resource root (e.g. "/my-app") so that visiting any tab of
17
- * the same resource tracks a single entry instead of one per tab.
18
- * Absolute URL or path relative to the current origin.
19
- */
20
12
  href?: string
21
13
  }
22
14
 
23
- /**
24
- * Drop this inside any page or layout to register that page in the
25
- * sidebar's quick-access list. Tracks on mount and whenever pathname changes.
26
- * Renders nothing.
27
- */
28
15
  export function QuickAccessTracker({ label, icon, enabled = true, href: hrefProp }: QuickAccessTrackerProps) {
29
16
  const pathname = usePathname()
30
17
  const { addItem } = useQuickAccess()
31
- // Stable ref so the effect doesn't re-run when addItem reference changes
32
18
  const addRef = useRef(addItem)
33
19
  addRef.current = addItem
34
20
 
35
21
  useEffect(() => {
36
22
  if (!enabled || !label || !pathname) return
37
- const href = hrefProp
38
- ? (hrefProp.startsWith("http") ? hrefProp : window.location.origin + hrefProp)
39
- : window.location.origin + window.location.pathname
23
+ const href = quickAccessHref(
24
+ { origin: window.location.origin, locationPath: window.location.pathname, routerPath: pathname },
25
+ hrefProp,
26
+ )
40
27
  addRef.current({ label, href, icon })
41
28
  // eslint-disable-next-line react-hooks/exhaustive-deps
42
29
  }, [pathname, label, icon, enabled, hrefProp])
package/index.ts CHANGED
@@ -7,6 +7,7 @@ export * from './lib/config-selection'
7
7
  export * from './lib/environment-context'
8
8
  export * from './lib/environment-controller'
9
9
  export * from './lib/project-requirement'
10
+ export * from './lib/quick-access-href'
10
11
  export * from './lib/status-indicator'
11
12
  export { createPlatformConfigSource, templateFilter, configOfHit, environmentWithTargets, type ConfigSource } from './lib/config-source'
12
13
  export { default as HttpClient, useHttpClient, ApiError } from './lib/http-client'
@@ -0,0 +1,21 @@
1
+ export interface QuickAccessLocation {
2
+ origin: string
3
+ locationPath: string
4
+ routerPath: string
5
+ }
6
+
7
+ const withoutTrailingSlash = (path: string): string => path.replace(/\/+$/, "")
8
+
9
+ export function appBasePath({ locationPath, routerPath }: Pick<QuickAccessLocation, "locationPath" | "routerPath">): string {
10
+ const full = withoutTrailingSlash(locationPath)
11
+ const inner = withoutTrailingSlash(routerPath)
12
+ if (!inner) return full
13
+ return full.endsWith(inner) ? full.slice(0, full.length - inner.length) : ""
14
+ }
15
+
16
+ export function quickAccessHref(location: QuickAccessLocation, target?: string): string {
17
+ if (!target) return location.origin + location.locationPath
18
+ if (/^https?:\/\//i.test(target)) return target
19
+ const path = target.startsWith("/") ? target : `/${target}`
20
+ return location.origin + appBasePath(location) + path
21
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orsetra/shared-ui",
3
- "version": "1.11.7",
3
+ "version": "1.11.8",
4
4
  "description": "Shared UI components for Orsetra platform",
5
5
  "main": "./index.ts",
6
6
  "types": "./index.ts",