basalt-ui 2.11.1 → 2.13.0
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/AGENTS.md +1 -1
- package/CHANGELOG.md +39 -0
- package/MIGRATING.md +51 -12
- package/README.md +2 -0
- package/agent/rules/basalt-mantine.md +1 -1
- package/agent/rules/basalt-state.md +1 -1
- package/agent/skills/basalt-app/SKILL.md +2 -2
- package/agent/templates/main.tpl +1 -0
- package/dist/cli/basalt-install.d.ts +10 -0
- package/dist/cli/basalt-install.d.ts.map +1 -0
- package/dist/cli/basalt-install.js +0 -0
- package/dist/cli/check-theme.js +1 -1
- package/dist/cli/doctor-upgrade.d.ts +5 -6
- package/dist/cli/doctor-upgrade.d.ts.map +1 -1
- package/dist/cli/doctor-upgrade.js +85 -56
- package/dist/cli/doctor.js +3 -3
- package/dist/cli/index.d.ts +5 -27
- package/dist/cli/index.d.ts.map +1 -1
- package/dist/cli/index.js +12 -49
- package/dist/cli/provider-tags.d.ts +12 -0
- package/dist/cli/provider-tags.d.ts.map +1 -0
- package/dist/cli/provider-tags.js +28 -0
- package/dist/cli/render-imports.d.ts +24 -0
- package/dist/cli/render-imports.d.ts.map +1 -0
- package/dist/cli/render-imports.js +74 -0
- package/dist/cli/router-wiring.d.ts +12 -0
- package/dist/cli/router-wiring.d.ts.map +1 -1
- package/dist/cli/router-wiring.js +18 -0
- package/dist/cli/source-text.d.ts +7 -0
- package/dist/cli/source-text.d.ts.map +1 -1
- package/dist/cli/source-text.js +34 -0
- package/dist/cli/sync.d.ts.map +1 -1
- package/dist/cli/sync.js +19 -13
- package/dist/commands/command-stash.d.ts +15 -0
- package/dist/commands/command-stash.d.ts.map +1 -0
- package/dist/commands/command-stash.js +31 -0
- package/dist/commands/define-commands.d.ts +2 -3
- package/dist/commands/define-commands.d.ts.map +1 -1
- package/dist/commands/define-commands.js +9 -7
- package/dist/commands/define-overlays.d.ts +6 -12
- package/dist/commands/define-overlays.d.ts.map +1 -1
- package/dist/commands/define-overlays.js +28 -34
- package/dist/commands/projectors.js +1 -1
- package/dist/guard/fonts-hint.d.ts +3 -0
- package/dist/guard/fonts-hint.d.ts.map +1 -0
- package/dist/guard/fonts-hint.js +4 -0
- package/dist/guard/index.d.ts +1 -1
- package/dist/guard/index.d.ts.map +1 -1
- package/dist/guard/index.js +2 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/provider/create-router.d.ts +57 -10
- package/dist/provider/create-router.d.ts.map +1 -1
- package/dist/provider/create-router.js +28 -17
- package/dist/provider/hotkeys.d.ts +2 -6
- package/dist/provider/hotkeys.d.ts.map +1 -1
- package/dist/provider/hotkeys.js +10 -15
- package/dist/provider/index.d.ts +7 -5
- package/dist/provider/index.d.ts.map +1 -1
- package/dist/provider/index.js +35 -42
- package/dist/provider/modals-mount.d.ts +16 -0
- package/dist/provider/modals-mount.d.ts.map +1 -0
- package/dist/provider/modals-mount.js +42 -0
- package/dist/provider/notifier.d.ts +22 -0
- package/dist/provider/notifier.d.ts.map +1 -0
- package/dist/provider/notifier.js +36 -0
- package/dist/provider/route-transition.d.ts +1 -1
- package/dist/provider/route-transition.d.ts.map +1 -1
- package/dist/provider/route-transition.js +1 -1
- package/dist/provider/slots.d.ts +25 -0
- package/dist/provider/slots.d.ts.map +1 -0
- package/dist/provider/slots.js +29 -0
- package/dist/provider/spotlight-actions.d.ts +1 -1
- package/dist/provider/spotlight-actions.d.ts.map +1 -1
- package/dist/provider/spotlight-actions.js +2 -3
- package/dist/provider/spotlight-mount.d.ts +3 -0
- package/dist/provider/spotlight-mount.d.ts.map +1 -0
- package/dist/provider/spotlight-mount.js +26 -0
- package/dist/query-client.d.ts +2 -62
- package/dist/query-client.d.ts.map +1 -1
- package/dist/query-client.js +13 -18
- package/dist/router-tanstack/nav-stash.d.ts +24 -0
- package/dist/router-tanstack/nav-stash.d.ts.map +1 -0
- package/dist/router-tanstack/nav-stash.js +23 -0
- package/dist/router-tanstack/nav.d.ts +5 -15
- package/dist/router-tanstack/nav.d.ts.map +1 -1
- package/dist/router-tanstack/nav.js +6 -16
- package/dist/router-tanstack/search-store-warnings.d.ts +33 -0
- package/dist/router-tanstack/search-store-warnings.d.ts.map +1 -0
- package/dist/router-tanstack/search-store-warnings.js +77 -0
- package/dist/router-tanstack/search-store.d.ts +2 -5
- package/dist/router-tanstack/search-store.d.ts.map +1 -1
- package/dist/router-tanstack/search-store.js +111 -110
- package/dist/router-tanstack/use-nav.js +1 -1
- package/dist/surfaces.d.ts +1 -1
- package/dist/surfaces.js +1 -1
- package/dist/telemetry.d.ts +31 -2
- package/dist/telemetry.d.ts.map +1 -1
- package/dist/telemetry.js +32 -12
- package/dist/theme/ctl-theme.d.ts.map +1 -1
- package/dist/theme/ctl-theme.js +17 -30
- package/dist/theme/extend.d.ts +18 -0
- package/dist/theme/extend.d.ts.map +1 -0
- package/dist/theme/extend.js +6 -0
- package/dist/theme/index.d.ts.map +1 -1
- package/dist/theme/index.js +44 -84
- package/dist/unwrap.d.ts +29 -0
- package/dist/unwrap.d.ts.map +1 -0
- package/dist/unwrap.js +18 -0
- package/llms.txt +11 -7
- package/package.json +2 -2
- package/src/cli/basalt-install.ts +9 -0
- package/src/cli/check-theme.ts +1 -1
- package/src/cli/doctor-upgrade.ts +117 -72
- package/src/cli/doctor.ts +3 -3
- package/src/cli/index.ts +15 -70
- package/src/cli/provider-tags.ts +38 -0
- package/src/cli/render-imports.ts +104 -0
- package/src/cli/router-wiring.ts +23 -0
- package/src/cli/source-text.ts +39 -0
- package/src/cli/sync.ts +28 -22
- package/src/commands/command-stash.ts +44 -0
- package/src/commands/define-commands.ts +10 -17
- package/src/commands/define-overlays.ts +40 -68
- package/src/commands/projectors.ts +1 -1
- package/src/guard/fonts-hint.ts +2 -0
- package/src/guard/index.ts +2 -2
- package/src/index.ts +4 -2
- package/src/provider/create-router.tsx +103 -39
- package/src/provider/hotkeys.ts +14 -22
- package/src/provider/index.tsx +55 -61
- package/src/provider/modals-mount.tsx +66 -0
- package/src/provider/notifier.ts +52 -0
- package/src/provider/route-transition.ts +1 -1
- package/src/provider/slots.ts +59 -0
- package/src/provider/spotlight-actions.tsx +3 -4
- package/src/provider/spotlight-mount.tsx +33 -0
- package/src/query-client.ts +20 -78
- package/src/router-tanstack/nav-stash.ts +41 -0
- package/src/router-tanstack/nav.ts +11 -31
- package/src/router-tanstack/search-store-warnings.ts +120 -0
- package/src/router-tanstack/search-store.ts +200 -206
- package/src/router-tanstack/use-nav.ts +1 -1
- package/src/surfaces.ts +1 -1
- package/src/telemetry.ts +71 -13
- package/src/theme/__snapshots__/plain-theme.test.ts.snap +2354 -0
- package/src/theme/ctl-theme.tsx +20 -19
- package/src/theme/extend.ts +18 -0
- package/src/theme/index.ts +50 -49
- package/src/unwrap.ts +44 -0
|
@@ -5,12 +5,11 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import type { SpotlightActionData } from '@mantine/spotlight'
|
|
7
7
|
import type { AnyRouter } from '@tanstack/react-router'
|
|
8
|
-
import { getActiveCommands } from '../commands/
|
|
8
|
+
import { getActiveCommands, runCommandSafely } from '../commands/command-stash'
|
|
9
9
|
import { detectMac, formatShortcutDisplay } from '../commands/shortcut-format'
|
|
10
|
-
import { flattenNav, getActiveNav } from '../router-tanstack/nav'
|
|
11
|
-
import type { FlatNavItem } from '../router-tanstack/nav'
|
|
10
|
+
import { flattenNav, getActiveNav } from '../router-tanstack/nav-stash'
|
|
11
|
+
import type { FlatNavItem } from '../router-tanstack/nav-stash'
|
|
12
12
|
import type { BasaltDevDockTool } from '../dev-dock'
|
|
13
|
-
import { runCommandSafely } from './hotkeys'
|
|
14
13
|
|
|
15
14
|
const DEV_TOOLS: ReadonlyArray<{ tool: BasaltDevDockTool; label: string }> = [
|
|
16
15
|
{ tool: 'router', label: 'Router devtools' },
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Spotlight mount: `@mantine/spotlight` plus the generated nav + command actions. Registered by
|
|
3
|
+
* `defineNav` and `defineCommands` (never imported by the provider), so an app defining neither ships
|
|
4
|
+
* none of it.
|
|
5
|
+
*/
|
|
6
|
+
import { Spotlight } from '@mantine/spotlight'
|
|
7
|
+
import type { SpotlightActionData } from '@mantine/spotlight'
|
|
8
|
+
import { useCallback, useEffect, useState } from 'react'
|
|
9
|
+
import { buildSpotlightActions } from './spotlight-actions'
|
|
10
|
+
import type { SlotProps } from './slots'
|
|
11
|
+
|
|
12
|
+
export function SpotlightMount({ router, navCommandLabel, openDevTool }: SlotProps) {
|
|
13
|
+
// Rebuilt on mount and again at every open, so a command's `when()` is read when the palette
|
|
14
|
+
// appears, not frozen at mount — and `detectMac` (inside the builder) never runs during render.
|
|
15
|
+
const [actions, setActions] = useState<SpotlightActionData[]>([])
|
|
16
|
+
const refreshActions = useCallback(
|
|
17
|
+
() => setActions(buildSpotlightActions(router, openDevTool, navCommandLabel)),
|
|
18
|
+
[router, openDevTool, navCommandLabel],
|
|
19
|
+
)
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
// oxlint-disable-next-line react/set-state-in-effect -- reads `navigator` and `when()`: never during render
|
|
22
|
+
refreshActions()
|
|
23
|
+
}, [refreshActions])
|
|
24
|
+
|
|
25
|
+
return (
|
|
26
|
+
<Spotlight
|
|
27
|
+
actions={actions}
|
|
28
|
+
onSpotlightOpen={refreshActions}
|
|
29
|
+
shortcut="mod + K"
|
|
30
|
+
maxHeight={420}
|
|
31
|
+
/>
|
|
32
|
+
)
|
|
33
|
+
}
|
package/src/query-client.ts
CHANGED
|
@@ -1,14 +1,31 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* The QueryClient factory `basaltRouterOptions` owns (V2-SPEC § 2): one client per router, so an
|
|
3
|
+
* SSR entry gets one per request. Optional peer: @tanstack/react-query.
|
|
4
4
|
*/
|
|
5
5
|
import { QueryClient } from '@tanstack/react-query'
|
|
6
6
|
import type { QueryClientConfig } from '@tanstack/react-query'
|
|
7
7
|
|
|
8
|
+
const MAX_RETRIES = 2
|
|
9
|
+
/** The 4xx that are transient: request timeout, too early, too many requests. */
|
|
10
|
+
const TRANSIENT_4XX: ReadonlySet<number> = new Set([408, 425, 429])
|
|
11
|
+
|
|
12
|
+
const statusOf = (error: unknown): number | undefined => {
|
|
13
|
+
if (typeof error !== 'object' || error === null || !('status' in error)) return undefined
|
|
14
|
+
return typeof error.status === 'number' ? error.status : undefined
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Twice, and not on a 4xx (the same request cannot start succeeding; a 401 is the app's to handle). */
|
|
18
|
+
const retry = (failureCount: number, error: unknown): boolean => {
|
|
19
|
+
const status = statusOf(error)
|
|
20
|
+
const retryable =
|
|
21
|
+
status === undefined || status < 400 || status >= 500 || TRANSIENT_4XX.has(status)
|
|
22
|
+
return failureCount < MAX_RETRIES && retryable
|
|
23
|
+
}
|
|
24
|
+
|
|
8
25
|
const BASALT_QUERY_DEFAULTS = {
|
|
9
26
|
staleTime: 30_000,
|
|
10
27
|
gcTime: 5 * 60 * 1000,
|
|
11
|
-
retry
|
|
28
|
+
retry,
|
|
12
29
|
refetchOnWindowFocus: false,
|
|
13
30
|
} as const
|
|
14
31
|
|
|
@@ -22,78 +39,3 @@ export function makeQueryClient(config?: QueryClientConfig): QueryClient {
|
|
|
22
39
|
},
|
|
23
40
|
})
|
|
24
41
|
}
|
|
25
|
-
|
|
26
|
-
// ── unwrap ────────────────────────────────────────────────────────────────────────────────────────
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Transport-agnostic unwrap for any `{ data, error }` envelope (Eden Treaty, raw fetch wrappers,
|
|
30
|
-
* etc.). TData is inferred from the data field. Throws on the error branch so failures surface
|
|
31
|
-
* to the nearest error boundary or TanStack Query's error state.
|
|
32
|
-
*
|
|
33
|
-
* Absence guard: if `error` is falsy but `data` is `null` OR `undefined`, unwrap throws with a
|
|
34
|
-
* descriptive message. This catches 204 No Content responses and silent transport failures that
|
|
35
|
-
* return `{ data: null, error: null }` — both signal an unexpected absence of data and should not
|
|
36
|
-
* silently resolve. `undefined` joined `null` here (C5 consolidation) so a hand-rolled fetch
|
|
37
|
-
* wrapper that leaves `data` unset on a miss — rather than explicitly `null` — is caught the same
|
|
38
|
-
* way; the two envelope shapes argo's own pre-basalt `unwrap` and basalt's ONLY differed on.
|
|
39
|
-
*
|
|
40
|
-
* **One generic signature, a conditional return** — a Promise of the envelope (basalt's original
|
|
41
|
-
* shape, `queryFn: () => unwrap(api.x.get())`) or the ALREADY-RESOLVED envelope itself
|
|
42
|
-
* (`unwrap(await api.x.get())`), both resolve `TData` off the same conditional. The two-overload
|
|
43
|
-
* form this replaced (1.29.0) resolved `unwrap` to `unknown` whenever it was passed as a bare
|
|
44
|
-
* callback — `.then(unwrap)` never applies an overload, it just reads the declared type of the
|
|
45
|
-
* `unwrap` reference — so `api.threads.get().then(unwrap)` silently widened. A single generic
|
|
46
|
-
* signature has exactly one type to read regardless of how the reference is used.
|
|
47
|
-
*
|
|
48
|
-
* **The conditional infers the raw `data` type and strips `null`/`undefined` from it**, rather
|
|
49
|
-
* than inferring the already-narrowed `Envelope<TData>`'s `TData` directly — Eden Treaty's
|
|
50
|
-
* response type is a UNION of envelope shapes (`{ data: T; error: null } | { data: null; error:
|
|
51
|
-
* EdenFetchError }`), and inferring through `Envelope<infer TData>` reads `TData` off whichever
|
|
52
|
-
* union member the conditional happens to match, landing on `T | null` at every Eden call site. A
|
|
53
|
-
* conditional type distributes over a union input when the checked type is a bare type parameter,
|
|
54
|
-
* so inferring `D` from `{ data: infer D; error: unknown }` and wrapping the result in
|
|
55
|
-
* `NonNullable` runs the check per union member and unions the results back together —
|
|
56
|
-
* `NonNullable<T> | NonNullable<null>` collapses to `T`.
|
|
57
|
-
*
|
|
58
|
-
* @example
|
|
59
|
-
* import { unwrap } from 'basalt-ui'
|
|
60
|
-
*
|
|
61
|
-
* // Promise-of-envelope — queryFn:
|
|
62
|
-
* queryFn: () => unwrap(api.users.get({ query: params }))
|
|
63
|
-
* // mutation:
|
|
64
|
-
* mutationFn: (body) => unwrap(api.resource.post({ body }))
|
|
65
|
-
*
|
|
66
|
-
* // resolved-envelope — either shape, including a bare `.then(unwrap)` callback:
|
|
67
|
-
* const rows = unwrap(await api.users.get())
|
|
68
|
-
* const rows2 = await api.users.get().then(unwrap)
|
|
69
|
-
*/
|
|
70
|
-
export function unwrap<R extends Envelope | Promise<Envelope>>(response: R): UnwrapResult<R> {
|
|
71
|
-
if (response instanceof Promise) {
|
|
72
|
-
return response.then((envelope) => unwrapEnvelope(envelope)) as unknown as UnwrapResult<R>
|
|
73
|
-
}
|
|
74
|
-
return unwrapEnvelope(response as Envelope) as unknown as UnwrapResult<R>
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/** Transport-agnostic `{ data, error }` result shape — Eden Treaty, a raw fetch wrapper, etc. */
|
|
78
|
-
type Envelope<TData = unknown> = { data: TData | null | undefined; error: unknown }
|
|
79
|
-
|
|
80
|
-
/** `unwrap`'s conditional return: a `Promise<TData>` when `R` is a promised envelope, `TData` when
|
|
81
|
-
* it is an already-resolved one — inferred off the SAME `R` the overload used to split on, and
|
|
82
|
-
* distributing over a union `R` (see the JSDoc above) so an Eden Treaty response's null branch
|
|
83
|
-
* never leaks into the resolved type. */
|
|
84
|
-
type UnwrapResult<R> =
|
|
85
|
-
R extends Promise<{ data: infer TData; error: unknown }>
|
|
86
|
-
? Promise<NonNullable<TData>>
|
|
87
|
-
: R extends { data: infer TData; error: unknown }
|
|
88
|
-
? NonNullable<TData>
|
|
89
|
-
: never
|
|
90
|
-
|
|
91
|
-
function unwrapEnvelope<TData>(envelope: Envelope<TData>): TData {
|
|
92
|
-
const { data, error } = envelope
|
|
93
|
-
if (error) throw error
|
|
94
|
-
if (data === null || data === undefined)
|
|
95
|
-
throw new Error(
|
|
96
|
-
'unwrap: null/undefined data with no error — check for a 204 response or a transport failure',
|
|
97
|
-
)
|
|
98
|
-
return data
|
|
99
|
-
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The nav definition's runtime stash and its flatten reader, as a leaf (types only from `./nav`):
|
|
3
|
+
* `defineNav` fills it; Spotlight's action builder, the route transitions and the provider read it.
|
|
4
|
+
* Kept apart from `nav.ts` so no reader imports the module that registers the Spotlight mount.
|
|
5
|
+
*/
|
|
6
|
+
import type { AnyNavGroup, AnyNavItem } from './nav'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The runtime stash `defineNav` fills (the last call wins, like `defineCommands`), read by
|
|
10
|
+
* `BasaltProvider` to project the nav into Spotlight. Type augmentation is erased, so this is the
|
|
11
|
+
* live source.
|
|
12
|
+
*
|
|
13
|
+
* @internal
|
|
14
|
+
*/
|
|
15
|
+
let activeNav: { groups: ReadonlyArray<AnyNavGroup> } | undefined
|
|
16
|
+
|
|
17
|
+
/** @internal */
|
|
18
|
+
export function getActiveNav(): { groups: ReadonlyArray<AnyNavGroup> } | undefined {
|
|
19
|
+
return activeNav
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** @internal — `defineNav` only. The last call wins. */
|
|
23
|
+
export function setActiveNav(nav: { groups: ReadonlyArray<AnyNavGroup> }): void {
|
|
24
|
+
activeNav = nav
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** A flattened destination, tagged with the group it was declared in. */
|
|
28
|
+
export type FlatNavItem = AnyNavItem & { groupId: string; groupLabel: string }
|
|
29
|
+
|
|
30
|
+
/** Depth-first flatten, parent then children — the leaf every other surface reads. */
|
|
31
|
+
export function flattenNav(config: { groups: ReadonlyArray<AnyNavGroup> }): FlatNavItem[] {
|
|
32
|
+
const out: FlatNavItem[] = []
|
|
33
|
+
const walk = (items: ReadonlyArray<AnyNavItem>, groupId: string, groupLabel: string): void => {
|
|
34
|
+
for (const item of items) {
|
|
35
|
+
out.push({ ...item, groupId, groupLabel })
|
|
36
|
+
if (item.children) walk(item.children, groupId, groupLabel)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
for (const g of config.groups) walk(g.items, g.id, g.label)
|
|
40
|
+
return out
|
|
41
|
+
}
|
|
@@ -9,10 +9,15 @@
|
|
|
9
9
|
* silently. Compile-verified. A config whose selling point is "typos are compile errors" cannot
|
|
10
10
|
* have a hole exactly where metadata typos live.
|
|
11
11
|
*
|
|
12
|
-
* No JSX and no Mantine in this module — `./router-tanstack` is a headless barrel by contract.
|
|
12
|
+
* No JSX and no Mantine in this module — `./router-tanstack` is a headless barrel by contract. The one
|
|
13
|
+
* exception is `defineNav`'s registration of the Spotlight mount (`../provider/slots`): nav entries
|
|
14
|
+
* feed the palette, and the mount is only reachable through this call.
|
|
13
15
|
*/
|
|
14
16
|
import type { RegisteredRouter } from '@tanstack/react-router'
|
|
15
17
|
import type { ReactNode } from 'react'
|
|
18
|
+
import { registerSlot } from '../provider/slots'
|
|
19
|
+
import { setActiveNav } from './nav-stash'
|
|
20
|
+
import { SpotlightMount } from '../provider/spotlight-mount'
|
|
16
21
|
import type { NavMobilePlacement, NavSectionMobile } from '../shell/nav-types'
|
|
17
22
|
|
|
18
23
|
// ── authoring shapes ─────────────────────────────────────────────────────────────────────────────
|
|
@@ -157,7 +162,9 @@ export function defineNav<
|
|
|
157
162
|
const T extends ReadonlyArray<NavTabId<G>>,
|
|
158
163
|
>(config: RegisteredOr<NavConfig<G, T>>): NavConfig<G, T> {
|
|
159
164
|
const nav = config as unknown as NavConfig<G, T>
|
|
160
|
-
|
|
165
|
+
setActiveNav(nav)
|
|
166
|
+
// Nav entries feed the palette, so defining the nav is what puts Spotlight in the bundle.
|
|
167
|
+
registerSlot('spotlight', SpotlightMount)
|
|
161
168
|
return nav
|
|
162
169
|
}
|
|
163
170
|
|
|
@@ -170,20 +177,6 @@ type RegisteredOr<T> = 0 extends 1 & RegisteredRouter['routeTree']
|
|
|
170
177
|
? 'basalt: declare Register { router }'
|
|
171
178
|
: T
|
|
172
179
|
|
|
173
|
-
/**
|
|
174
|
-
* The runtime stash `defineNav` fills (the last call wins, like `defineCommands`), read by
|
|
175
|
-
* `BasaltProvider` to project the nav into Spotlight. Type augmentation is erased, so this is the
|
|
176
|
-
* live source.
|
|
177
|
-
*
|
|
178
|
-
* @internal
|
|
179
|
-
*/
|
|
180
|
-
let activeNav: { groups: ReadonlyArray<AnyNavGroup> } | undefined
|
|
181
|
-
|
|
182
|
-
/** @internal */
|
|
183
|
-
export function getActiveNav(): { groups: ReadonlyArray<AnyNavGroup> } | undefined {
|
|
184
|
-
return activeNav
|
|
185
|
-
}
|
|
186
|
-
|
|
187
180
|
// ── readers ──────────────────────────────────────────────────────────────────────────────────────
|
|
188
181
|
|
|
189
182
|
function findById(items: ReadonlyArray<AnyNavItem>, id: string): AnyNavItem | undefined {
|
|
@@ -214,18 +207,5 @@ export function navTarget<
|
|
|
214
207
|
throw new Error(`navTarget: no nav item with id "${id}"`)
|
|
215
208
|
}
|
|
216
209
|
|
|
217
|
-
|
|
218
|
-
export type
|
|
219
|
-
|
|
220
|
-
/** Depth-first flatten, parent then children — the leaf every other surface reads. */
|
|
221
|
-
export function flattenNav(config: { groups: ReadonlyArray<AnyNavGroup> }): FlatNavItem[] {
|
|
222
|
-
const out: FlatNavItem[] = []
|
|
223
|
-
const walk = (items: ReadonlyArray<AnyNavItem>, groupId: string, groupLabel: string): void => {
|
|
224
|
-
for (const item of items) {
|
|
225
|
-
out.push({ ...item, groupId, groupLabel })
|
|
226
|
-
if (item.children) walk(item.children, groupId, groupLabel)
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
for (const g of config.groups) walk(g.items, g.id, g.label)
|
|
230
|
-
return out
|
|
231
|
-
}
|
|
210
|
+
export { flattenNav, getActiveNav } from './nav-stash'
|
|
211
|
+
export type { FlatNavItem } from './nav-stash'
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `createSearchStore`'s dev diagnostics and the once-only bookkeeping behind them, so the store
|
|
3
|
+
* body is the lane logic only. Every method is silent in production except `patchOwned`, which
|
|
4
|
+
* keeps the write and logs it. Each reads `process.env['NODE_ENV']` literally at its own branch —
|
|
5
|
+
* a shared helper would keep the dev text in a production bundle (`src/common/dev-gate.test.ts`).
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** True the first time `name` is seen in `seen`. */
|
|
9
|
+
function first(seen: Set<string>, name: string): boolean {
|
|
10
|
+
if (seen.has(name)) return false
|
|
11
|
+
seen.add(name)
|
|
12
|
+
return true
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function describeValue(value: unknown): string {
|
|
16
|
+
return typeof value === 'string' ? value : JSON.stringify(value)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function patchOwnsFieldMessage(input: {
|
|
20
|
+
key: string
|
|
21
|
+
name: string
|
|
22
|
+
owner: string
|
|
23
|
+
param: string
|
|
24
|
+
}): string {
|
|
25
|
+
const { key, name, owner, param } = input
|
|
26
|
+
return (
|
|
27
|
+
`basalt-ui: createSearchStore('${key}'): set('${name}', …, { patch }) carries \`${param}\`, ` +
|
|
28
|
+
`which this store's field '${owner}' owns — the URL would take it while the localStorage ` +
|
|
29
|
+
'mirror kept the old value, so the next paramless visit and every `linkSearch` link disagree ' +
|
|
30
|
+
`with the page the write happened on. Write it through \`field.${owner}.use()[1]\` instead; ` +
|
|
31
|
+
'a `patch` is for keys the store does NOT own.'
|
|
32
|
+
)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export type StoreWarnings = {
|
|
36
|
+
linkPinsFallback: (input: { param: string; urlValue: unknown; storedValue: unknown }) => void
|
|
37
|
+
writeHasNowhereToGo: (input: { name: string; param: string }) => void
|
|
38
|
+
patchWasDropped: (input: { name: string; param: string }) => void
|
|
39
|
+
patchOwned: (input: { name: string; owner: string; param: string }) => void
|
|
40
|
+
foreignParam: (input: { name: string; param: string; path: string }) => void
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function createStoreWarnings(key: string): StoreWarnings {
|
|
44
|
+
let pinned = false
|
|
45
|
+
const nowhere = new Set<string>()
|
|
46
|
+
const patchDropped = new Set<string>()
|
|
47
|
+
const patchOwner = new Set<string>()
|
|
48
|
+
const foreign = new Set<string>()
|
|
49
|
+
|
|
50
|
+
const warn = (message: string): void =>
|
|
51
|
+
// oxlint-disable-next-line no-console -- a dev-time wiring warning has no other channel
|
|
52
|
+
console.warn(`[basalt-ui] createSearchStore('${key}'): ${message}`)
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
/**
|
|
56
|
+
* Once per store: the URL pins `fallback` while something else is persisted and no reader was
|
|
57
|
+
* ever called — almost always a nav link declaring the param as a module-scope literal.
|
|
58
|
+
*/
|
|
59
|
+
linkPinsFallback(input: { param: string; urlValue: unknown; storedValue: unknown }): void {
|
|
60
|
+
if (process.env['NODE_ENV'] === 'production' || pinned) return
|
|
61
|
+
pinned = true
|
|
62
|
+
const { param } = input
|
|
63
|
+
const urlValue = describeValue(input.urlValue)
|
|
64
|
+
warn(
|
|
65
|
+
`the URL pinned \`${param}=${urlValue}\` (the fallback) while '${describeValue(input.storedValue)}' ` +
|
|
66
|
+
'was persisted, and nothing has read this store back. A nav link is almost certainly ' +
|
|
67
|
+
`declaring \`search: { ${param}: '${urlValue}' }\` at module scope, which overrides the ` +
|
|
68
|
+
"stored value on every click — pass the store's own reader instead: " +
|
|
69
|
+
'`search: <store>.linkSearch`. (dev only)',
|
|
70
|
+
)
|
|
71
|
+
},
|
|
72
|
+
/** Once per field: a `persist: false` write from a route that does not validate it goes nowhere. */
|
|
73
|
+
writeHasNowhereToGo(input: { name: string; param: string }): void {
|
|
74
|
+
if (process.env['NODE_ENV'] === 'production' || !first(nowhere, input.name)) return
|
|
75
|
+
warn(
|
|
76
|
+
`wrote field '${input.name}' from a route that does not validate \`${input.param}\`, and ` +
|
|
77
|
+
'the field is `persist: false` — so the write was dropped. Either render this control on ' +
|
|
78
|
+
'the route wired to `validateSearch`, or give the field the localStorage lane (drop ' +
|
|
79
|
+
'`persist: false`) so a write from elsewhere survives until the owning route reads it. ' +
|
|
80
|
+
'(dev only)',
|
|
81
|
+
)
|
|
82
|
+
},
|
|
83
|
+
/** Once per field: a patched write from a route that does not validate the field. */
|
|
84
|
+
patchWasDropped(input: { name: string; param: string }): void {
|
|
85
|
+
if (process.env['NODE_ENV'] === 'production' || !first(patchDropped, input.name)) return
|
|
86
|
+
warn(
|
|
87
|
+
`wrote field '${input.name}' with a \`patch\` from a route that does not validate ` +
|
|
88
|
+
`\`${input.param}\` — the field's value persisted (A1), but the patch was DROPPED: there ` +
|
|
89
|
+
'is no navigate to merge it into. Render the control on the route wired to ' +
|
|
90
|
+
'`validateSearch`, or apply that sibling param on the route that owns it. (dev only)',
|
|
91
|
+
)
|
|
92
|
+
},
|
|
93
|
+
/**
|
|
94
|
+
* A `patch` named a param another field of this store owns: thrown in dev (one-line fix, silent
|
|
95
|
+
* damage), kept and logged once in production so a stale param cannot take a page down.
|
|
96
|
+
*/
|
|
97
|
+
patchOwned(input: { name: string; owner: string; param: string }): void {
|
|
98
|
+
const message = patchOwnsFieldMessage({ key, ...input })
|
|
99
|
+
if (process.env['NODE_ENV'] !== 'production') throw new Error(message)
|
|
100
|
+
if (!first(patchOwner, input.param)) return
|
|
101
|
+
// oxlint-disable-next-line no-console -- production keeps the write; the log is the record
|
|
102
|
+
console.error(message)
|
|
103
|
+
},
|
|
104
|
+
/**
|
|
105
|
+
* Once per field and path: the param is in the URL, yet this store's `validateSearch` did not run for the location — so
|
|
106
|
+
* a different store or a hand-rolled schema owns it, and presence-based ownership will let this
|
|
107
|
+
* store's setter navigate on its behalf.
|
|
108
|
+
*/
|
|
109
|
+
foreignParam(input: { name: string; param: string; path: string }): void {
|
|
110
|
+
if (process.env['NODE_ENV'] === 'production') return
|
|
111
|
+
if (!first(foreign, `${input.name}@${input.path}`)) return
|
|
112
|
+
warn(
|
|
113
|
+
`\`${input.param}\` (field '${input.name}') is in the URL but this store's ` +
|
|
114
|
+
'`validateSearch` has never run — another store or schema owns the param, and a write ' +
|
|
115
|
+
'here will navigate for it. Wire `validateSearch: <store>.validateSearch` (or spread it ' +
|
|
116
|
+
'into your own) on the route that owns the param. (dev only)',
|
|
117
|
+
)
|
|
118
|
+
},
|
|
119
|
+
}
|
|
120
|
+
}
|