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
|
@@ -29,8 +29,11 @@
|
|
|
29
29
|
*
|
|
30
30
|
* Headless — no Mantine, no JSX. Same tier as `useBasaltNav` and `useRouterBreadcrumbs`.
|
|
31
31
|
*/
|
|
32
|
-
import {
|
|
32
|
+
import { useEffect, useRef } from 'react'
|
|
33
|
+
import { useNavigate, useRouter, useRouterState, useSearch } from '@tanstack/react-router'
|
|
34
|
+
import type { AnyRouter } from '@tanstack/react-router'
|
|
33
35
|
import { createStoreCore } from '../state/fields'
|
|
36
|
+
import { createStoreWarnings } from './search-store-warnings'
|
|
34
37
|
import type {
|
|
35
38
|
AnyField,
|
|
36
39
|
FieldHandle,
|
|
@@ -100,104 +103,61 @@ export type SearchStore<S extends Record<string, AnyField>> = {
|
|
|
100
103
|
useReset: () => () => void
|
|
101
104
|
}
|
|
102
105
|
|
|
106
|
+
// ── Implementation ─────────────────────────────────────────────────────────
|
|
107
|
+
|
|
103
108
|
/**
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
* piece looks correct and the feature merely never remembers anything.
|
|
108
|
-
*
|
|
109
|
-
* Deliberately NOT a timer over "was this store ever read": that fires in every test that builds a
|
|
110
|
-
* store, and it cannot say what to do about it. This fires only in the broken state, and names the
|
|
111
|
-
* fix.
|
|
109
|
+
* The router's SSR bootstrap (`window.$_TSR`) is present on a dehydrated page. Until the first
|
|
110
|
+
* reader has committed, the client must resolve exactly what the server did; after that the stored
|
|
111
|
+
* layer applies like on any client-only page. One page hydrates once, so this is module state.
|
|
112
112
|
*/
|
|
113
|
-
|
|
114
|
-
key: string
|
|
115
|
-
param: string
|
|
116
|
-
urlValue: string
|
|
117
|
-
storedValue: string
|
|
118
|
-
}): void {
|
|
119
|
-
const { key, param, urlValue, storedValue } = input
|
|
120
|
-
// oxlint-disable-next-line no-console -- a dev-time wiring warning has no other channel
|
|
121
|
-
console.warn(
|
|
122
|
-
`[basalt-ui] createSearchStore('${key}'): the URL pinned \`${param}=${urlValue}\` ` +
|
|
123
|
-
'(the fallback) ' +
|
|
124
|
-
`while '${storedValue}' was persisted, and nothing has read this store back. A nav link is ` +
|
|
125
|
-
`almost certainly declaring \`search: { ${param}: '${urlValue}' }\` at module scope, which ` +
|
|
126
|
-
"overrides the stored value on every click — pass the store's own reader instead: " +
|
|
127
|
-
'`search: <store>.linkSearch`. (dev only)',
|
|
128
|
-
)
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
// ── Implementation ─────────────────────────────────────────────────────────
|
|
113
|
+
let hydrationDone = false
|
|
132
114
|
|
|
133
|
-
/**
|
|
134
|
-
function
|
|
135
|
-
|
|
136
|
-
return typeof search === 'object' && search !== null ? (search as Record<string, unknown>) : {}
|
|
115
|
+
/** @internal Tests only — a fresh page for the next hydration. */
|
|
116
|
+
export function resetHydrationForTests(): void {
|
|
117
|
+
hydrationDone = false
|
|
137
118
|
}
|
|
138
119
|
|
|
139
|
-
|
|
140
|
-
|
|
120
|
+
const isDehydrating = (): boolean =>
|
|
121
|
+
typeof window !== 'undefined' && '$_TSR' in window && !hydrationDone
|
|
122
|
+
|
|
123
|
+
/** A failed store navigate is reported, never swallowed and never an unhandled rejection. */
|
|
124
|
+
function reportFailure(error: unknown): void {
|
|
125
|
+
if (typeof reportError === 'function') reportError(error)
|
|
126
|
+
else
|
|
127
|
+
queueMicrotask(() => {
|
|
128
|
+
throw error
|
|
129
|
+
})
|
|
141
130
|
}
|
|
142
131
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
132
|
+
type SearchNavigate = (options: {
|
|
133
|
+
to: '.'
|
|
134
|
+
search: (prev: Record<string, unknown>) => Record<string, unknown>
|
|
135
|
+
replace: boolean
|
|
136
|
+
resetScroll: false
|
|
137
|
+
}) => Promise<unknown> | void
|
|
138
|
+
|
|
139
|
+
/** One shared write: always replaces scroll-neutrally and reports a rejected navigate. */
|
|
140
|
+
function navigateSearch(
|
|
141
|
+
navigate: SearchNavigate,
|
|
142
|
+
search: (prev: Record<string, unknown>) => Record<string, unknown>,
|
|
143
|
+
replace: boolean,
|
|
144
|
+
): void {
|
|
145
|
+
// A filter lives halfway down a page as often as it lives in the bar, and the router's
|
|
146
|
+
// default scroll restoration treats this same-route search write as a navigation — so
|
|
147
|
+
// without this a select two screens down jumps the reader back to the top on every
|
|
148
|
+
// change. Never a prop: there is no filter write that WANTS the page to scroll.
|
|
149
|
+
const result = navigate({ to: '.', search, replace, resetScroll: false })
|
|
150
|
+
if (result !== undefined) result.catch(reportFailure)
|
|
159
151
|
}
|
|
160
152
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
* The navigate would put it in the URL while the store's own write path never touched the mirror,
|
|
164
|
-
* so the next paramless visit and every `linkSearch` link resolve the OLD value — the URL and the
|
|
165
|
-
* mirror disagree from that click on, and nothing looks broken until a reader shares a link.
|
|
166
|
-
*
|
|
167
|
-
* Thrown in dev rather than warned: the call is a mistake with a one-line fix (write the other
|
|
168
|
-
* field through its own setter), and the damage is silent. Production keeps the write — a thrown
|
|
169
|
-
* error there would take a page down over a stale param — and logs it once.
|
|
170
|
-
*/
|
|
171
|
-
function patchOwnsFieldMessage(input: {
|
|
172
|
-
key: string
|
|
173
|
-
name: string
|
|
174
|
-
owner: string
|
|
175
|
-
param: string
|
|
176
|
-
}): string {
|
|
177
|
-
const { key, name, owner, param } = input
|
|
178
|
-
return (
|
|
179
|
-
`basalt-ui: createSearchStore('${key}'): set('${name}', …, { patch }) carries \`${param}\`, ` +
|
|
180
|
-
`which this store's field '${owner}' owns — the URL would take it while the localStorage ` +
|
|
181
|
-
'mirror kept the old value, so the next paramless visit and every `linkSearch` link disagree ' +
|
|
182
|
-
`with the page the write happened on. Write it through \`field.${owner}.use()[1]\` instead; ` +
|
|
183
|
-
'a `patch` is for keys the store does NOT own.'
|
|
184
|
-
)
|
|
153
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
154
|
+
return typeof value === 'object' && value !== null
|
|
185
155
|
}
|
|
186
156
|
|
|
187
|
-
/**
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
*/
|
|
192
|
-
function warnPatchDropped(input: { key: string; name: string; param: string }): void {
|
|
193
|
-
const { key, name, param } = input
|
|
194
|
-
// oxlint-disable-next-line no-console -- a dev-time wiring warning has no other channel
|
|
195
|
-
console.warn(
|
|
196
|
-
`[basalt-ui] createSearchStore('${key}'): wrote field '${name}' with a \`patch\` from a route ` +
|
|
197
|
-
`that does not validate \`${param}\` — the field's value persisted (A1), but the patch was ` +
|
|
198
|
-
'DROPPED: there is no navigate to merge it into. Render the control on the route wired to ' +
|
|
199
|
-
'`validateSearch`, or apply that sibling param on the route that owns it. (dev only)',
|
|
200
|
-
)
|
|
157
|
+
/** Reads the merged search of every matched route, so a child or sibling needs no `from` (A3). */
|
|
158
|
+
function useCurrentSearch(): Record<string, unknown> {
|
|
159
|
+
const search: unknown = useSearch({ strict: false })
|
|
160
|
+
return isRecord(search) ? search : {}
|
|
201
161
|
}
|
|
202
162
|
|
|
203
163
|
/**
|
|
@@ -248,10 +208,28 @@ function isMemoryLane(entry: StoreEntry): boolean {
|
|
|
248
208
|
}
|
|
249
209
|
|
|
250
210
|
/**
|
|
251
|
-
*
|
|
252
|
-
*
|
|
211
|
+
* One store per page (or per feature), over typed fields. `key` is the localStorage key; each
|
|
212
|
+
* field's map key is its default URL param name.
|
|
213
|
+
*
|
|
214
|
+
* @example
|
|
215
|
+
* export const analytics = createSearchStore({
|
|
216
|
+
* key: 'analytics',
|
|
217
|
+
* fields: {
|
|
218
|
+
* range: field.range({ presets: ['7d', '30d', '90d'], fallback: '30d', custom: true }),
|
|
219
|
+
* compare: field.enum(['none', 'previous'], 'none'),
|
|
220
|
+
* channels: field.multi(CHANNELS, []),
|
|
221
|
+
* },
|
|
222
|
+
* }).labels({ range: { '7d': 'Last 7 days', '30d': 'Last 30 days', '90d': 'Last 90 days' } })
|
|
223
|
+
*
|
|
224
|
+
* export const Route = createFileRoute('/analytics')({
|
|
225
|
+
* validateSearch: analytics.validateSearch,
|
|
226
|
+
* loaderDeps: ({ search }) => search,
|
|
227
|
+
* })
|
|
228
|
+
*
|
|
229
|
+
* // in the page: <RangeFilter field={analytics.field.range} />
|
|
230
|
+
* // in the nav: linkOptions({ to: '/analytics', search: analytics.linkSearch })
|
|
253
231
|
*/
|
|
254
|
-
export function
|
|
232
|
+
export function createSearchStore<const S extends Record<string, AnyField>>(
|
|
255
233
|
o: CreateSearchStoreOptions<S>,
|
|
256
234
|
): SearchStore<S> {
|
|
257
235
|
assertNoLazyUrlFallback(o.key, o.fields)
|
|
@@ -263,13 +241,10 @@ export function buildSearchStore<const S extends Record<string, AnyField>>(
|
|
|
263
241
|
// touches the other two uses exclusively. Missing it there made the pinned-link warning fire on
|
|
264
242
|
// correctly wired pages.
|
|
265
243
|
let readerWired = false
|
|
266
|
-
let warned = false
|
|
267
244
|
const markReaderWired = (): void => {
|
|
268
245
|
readerWired = true
|
|
269
246
|
}
|
|
270
|
-
const
|
|
271
|
-
const patchDroppedWarned = new Set<string>()
|
|
272
|
-
const patchOwnerLogged = new Set<string>()
|
|
247
|
+
const warnings = createStoreWarnings(o.key)
|
|
273
248
|
|
|
274
249
|
/** Every URL param this store owns, mapped to the field that owns it — what a `patch` may not name. */
|
|
275
250
|
const paramOwners = new Map<string, string>()
|
|
@@ -300,24 +275,21 @@ export function buildSearchStore<const S extends Record<string, AnyField>>(
|
|
|
300
275
|
)
|
|
301
276
|
|
|
302
277
|
/**
|
|
303
|
-
* The URL lane's `clear()`
|
|
304
|
-
* `urlUse`
|
|
305
|
-
*
|
|
306
|
-
*
|
|
278
|
+
* The URL lane's `clear()` runs from an event handler, where no hook is in reach. A mounted
|
|
279
|
+
* `urlUse` lends its router here from an EFFECT — never the render body, which a discarded or
|
|
280
|
+
* StrictMode render would leave behind — so `clear()` navigates through the live router and
|
|
281
|
+
* judges ownership against the location it is called at. With no mount reading the store it
|
|
282
|
+
* clears the mirror alone. With several routers mounted it fans out over all of them, each
|
|
283
|
+
* judged against its own location.
|
|
307
284
|
*/
|
|
308
|
-
const
|
|
285
|
+
const mounts = new Map<AnyRouter, number>()
|
|
309
286
|
|
|
310
287
|
const assertPatchNotOwned = (entry: StoreEntry, patch: Record<string, unknown>): void => {
|
|
311
288
|
for (const param of Object.keys(patch)) {
|
|
312
289
|
const owner = paramOwners.get(param)
|
|
313
290
|
// The field's OWN params are harmless: its `toSearch` is spread after the patch and wins.
|
|
314
291
|
if (owner === undefined || entry.codec.params.includes(param)) continue
|
|
315
|
-
|
|
316
|
-
if (process.env['NODE_ENV'] !== 'production') throw new Error(message)
|
|
317
|
-
if (patchOwnerLogged.has(param)) continue
|
|
318
|
-
patchOwnerLogged.add(param)
|
|
319
|
-
// oxlint-disable-next-line no-console -- production keeps the write; the log is the record
|
|
320
|
-
console.error(message)
|
|
292
|
+
warnings.patchOwned({ name: entry.name, owner, param })
|
|
321
293
|
}
|
|
322
294
|
}
|
|
323
295
|
|
|
@@ -333,7 +305,7 @@ export function buildSearchStore<const S extends Record<string, AnyField>>(
|
|
|
333
305
|
}
|
|
334
306
|
|
|
335
307
|
const maybeWarn = (raw: Record<string, unknown>, record: Record<string, unknown>): void => {
|
|
336
|
-
if (
|
|
308
|
+
if (process.env['NODE_ENV'] === 'production' || readerWired) return
|
|
337
309
|
for (const entry of core.urlEntries) {
|
|
338
310
|
const fromUrl = entry.codec.fromSearch(raw)
|
|
339
311
|
if (fromUrl === null || !entry.codec.equals(fromUrl, entry.codec.fallback)) continue
|
|
@@ -341,26 +313,42 @@ export function buildSearchStore<const S extends Record<string, AnyField>>(
|
|
|
341
313
|
if (stored === null || entry.codec.equals(stored, fromUrl)) continue
|
|
342
314
|
// Describe both sides as the URL spells them (a range prints `30d`, not its whole value
|
|
343
315
|
// object) — the message quotes a `search:` literal, so it has to be URL-shaped to be advice.
|
|
344
|
-
|
|
345
|
-
warnLinkPinsFallback({
|
|
346
|
-
key: o.key,
|
|
316
|
+
warnings.linkPinsFallback({
|
|
347
317
|
param: entry.codec.primary,
|
|
348
|
-
urlValue:
|
|
349
|
-
storedValue:
|
|
318
|
+
urlValue: entry.codec.toSearch(fromUrl)[entry.codec.primary],
|
|
319
|
+
storedValue: entry.codec.toSearch(stored)[entry.codec.primary],
|
|
350
320
|
})
|
|
351
321
|
return
|
|
352
322
|
}
|
|
353
323
|
}
|
|
354
324
|
|
|
325
|
+
/**
|
|
326
|
+
* Pure on the server and while a dehydrated page hydrates (see `isDehydrating`): the stored layer
|
|
327
|
+
* would make the client's first search differ from the HTML the server rendered, and a loader
|
|
328
|
+
* keyed on `search` would run with different deps. Everywhere else — a client-only app, or a
|
|
329
|
+
* dehydrated page once hydration committed — a paramless visit resolves URL ⊳ mirror ⊳ fallback.
|
|
330
|
+
*/
|
|
331
|
+
const stored = (): Record<string, unknown> =>
|
|
332
|
+
typeof window === 'undefined' || isDehydrating() ? {} : core.readRecord()
|
|
333
|
+
|
|
355
334
|
const validateSearch = (raw: Record<string, unknown>): SearchValues<S> => {
|
|
356
|
-
const record =
|
|
335
|
+
const record = stored()
|
|
357
336
|
maybeWarn(raw, record)
|
|
358
337
|
return flatten(raw, record) as SearchValues<S>
|
|
359
338
|
}
|
|
360
339
|
|
|
340
|
+
const runsStore = (route: AnyRouter['routesById'][string] | undefined): boolean => {
|
|
341
|
+
const validate: unknown = route?.options.validateSearch
|
|
342
|
+
return (
|
|
343
|
+
typeof validate === 'function' &&
|
|
344
|
+
(validate === validateSearch ||
|
|
345
|
+
Function.prototype.toString.call(validate).includes('validateSearch'))
|
|
346
|
+
)
|
|
347
|
+
}
|
|
348
|
+
|
|
361
349
|
const linkSearch = (): SearchValues<S> => {
|
|
362
350
|
readerWired = true
|
|
363
|
-
return flatten(null,
|
|
351
|
+
return flatten(null, stored()) as SearchValues<S>
|
|
364
352
|
}
|
|
365
353
|
|
|
366
354
|
const readStored = (): Partial<StoredValues<S>> => {
|
|
@@ -371,26 +359,65 @@ export function buildSearchStore<const S extends Record<string, AnyField>>(
|
|
|
371
359
|
const urlUse = (entry: StoreEntry) => (): readonly [unknown, FieldWrite] => {
|
|
372
360
|
const search = useCurrentSearch()
|
|
373
361
|
const navigate = useNavigate()
|
|
362
|
+
const router = useRouter()
|
|
363
|
+
const pathname = useRouterState({ select: (state) => state.location.pathname })
|
|
374
364
|
const [record, setRecord] = core.usePersistedRecord()
|
|
375
365
|
markReaderWired()
|
|
376
366
|
const value = core.resolve(entry, search, record)
|
|
367
|
+
// Captured at render: true for every reader of the hydrating commit, false for a later mount.
|
|
368
|
+
const hydrating = useRef(isDehydrating())
|
|
377
369
|
// The matched route validates this field iff its primary param is present (see the module
|
|
378
370
|
// header). A foreign route gets the persist-only lane.
|
|
379
371
|
const validated = Object.hasOwn(search, entry.codec.primary)
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
372
|
+
|
|
373
|
+
useEffect(() => {
|
|
374
|
+
const count = mounts.get(router) ?? 0
|
|
375
|
+
mounts.set(router, count + 1)
|
|
376
|
+
return () => {
|
|
377
|
+
const left = (mounts.get(router) ?? 1) - 1
|
|
378
|
+
if (left > 0) mounts.set(router, left)
|
|
379
|
+
else mounts.delete(router)
|
|
380
|
+
}
|
|
381
|
+
}, [router])
|
|
382
|
+
|
|
383
|
+
// Hydration committed: the server's HTML was reproduced, so the stored layer may apply now.
|
|
384
|
+
useEffect(() => {
|
|
385
|
+
if (hydrating.current) hydrationDone = true
|
|
386
|
+
}, [])
|
|
387
|
+
|
|
388
|
+
// The record is the server snapshot until React re-renders with the client's, so this runs
|
|
389
|
+
// again once the mirror is readable. A URL still carrying the fallback was not chosen (the
|
|
390
|
+
// server pinned it), so it adopts the mirror — by REPLACING, so a loader keyed on `search`
|
|
391
|
+
// follows what the page shows. A deep link naming any other value is never overridden.
|
|
392
|
+
const mirrored = entry.codec.lane.persist ? entry.codec.decode(record[entry.name]) : null
|
|
393
|
+
const mirroredKey = mirrored === null ? null : JSON.stringify(entry.codec.toSearch(mirrored))
|
|
394
|
+
useEffect(() => {
|
|
395
|
+
if (!hydrating.current || mirrored === null) return
|
|
396
|
+
hydrating.current = false
|
|
397
|
+
if (!validated || entry.codec.equals(mirrored, entry.codec.fallback)) return
|
|
398
|
+
const urlValue = entry.codec.fromSearch(search)
|
|
399
|
+
if (urlValue !== null && !entry.codec.equals(urlValue, entry.codec.fallback)) return
|
|
400
|
+
navigateSearch(
|
|
401
|
+
(o) => router.navigate(o),
|
|
402
|
+
(prev) => ({ ...prev, ...entry.codec.toSearch(mirrored) }),
|
|
403
|
+
true,
|
|
404
|
+
)
|
|
405
|
+
// oxlint-disable-next-line react-hooks/exhaustive-deps -- one-shot per mirror value: the rest is read at that moment
|
|
406
|
+
}, [mirroredKey])
|
|
407
|
+
|
|
408
|
+
// Dev only. Reading the route options never calls a user validator: a route runs this store
|
|
409
|
+
// when its `validateSearch` IS the store's, or composes it (its source names `validateSearch`).
|
|
410
|
+
useEffect(() => {
|
|
411
|
+
if (process.env['NODE_ENV'] === 'production') return
|
|
412
|
+
// Everything from the committed MATCHES, so the verdict never mixes two locations.
|
|
413
|
+
const { matches } = router.state
|
|
414
|
+
const leaf = matches.at(-1)
|
|
415
|
+
if (leaf === undefined || !isRecord(leaf.search)) return
|
|
416
|
+
if (!Object.hasOwn(leaf.search, entry.codec.primary)) return
|
|
417
|
+
if (matches.some((match) => runsStore(router.routesById[match.routeId]))) return
|
|
418
|
+
warnings.foreignParam({ name: entry.name, param: entry.codec.primary, path: leaf.pathname })
|
|
419
|
+
// oxlint-disable-next-line react-hooks/exhaustive-deps -- re-judged per location; the router handle is stable
|
|
420
|
+
}, [pathname])
|
|
394
421
|
|
|
395
422
|
return [
|
|
396
423
|
value,
|
|
@@ -398,45 +425,29 @@ export function buildSearchStore<const S extends Record<string, AnyField>>(
|
|
|
398
425
|
const patch = opts?.patch
|
|
399
426
|
if (patch !== undefined && Object.keys(patch).length > 0) {
|
|
400
427
|
assertPatchNotOwned(entry, patch)
|
|
401
|
-
if (
|
|
402
|
-
!validated &&
|
|
403
|
-
!patchDroppedWarned.has(entry.name) &&
|
|
404
|
-
process.env['NODE_ENV'] !== 'production'
|
|
405
|
-
) {
|
|
406
|
-
patchDroppedWarned.add(entry.name)
|
|
407
|
-
warnPatchDropped({ key: o.key, name: entry.name, param: entry.codec.primary })
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
if (validated) {
|
|
411
|
-
navigate({
|
|
412
|
-
to: '.',
|
|
413
|
-
// `patch` FIRST, the field's own params last: a patch is for keys the store does not
|
|
414
|
-
// own (clearing a sibling `detailDate` with `undefined`), so it must never be able to
|
|
415
|
-
// overwrite the value this very call is setting.
|
|
416
|
-
// `resets` only when the value moves: a no-op write (a debounced box settling back on
|
|
417
|
-
// what it held) must not throw the reader off the page they are on.
|
|
418
|
-
search: (prev: Record<string, unknown>) => ({
|
|
419
|
-
...prev,
|
|
420
|
-
...resetsFor(entry, prev, next),
|
|
421
|
-
...opts?.patch,
|
|
422
|
-
...entry.codec.toSearch(next),
|
|
423
|
-
}),
|
|
424
|
-
replace: entry.codec.lane.history === 'replace',
|
|
425
|
-
// A filter lives halfway down a page as often as it lives in the bar, and the router's
|
|
426
|
-
// default scroll restoration treats this same-route search write as a navigation — so
|
|
427
|
-
// without this a select two screens down jumps the reader back to the top on every
|
|
428
|
-
// change. Never a prop: there is no filter write that WANTS the page to scroll.
|
|
429
|
-
resetScroll: false,
|
|
430
|
-
})
|
|
428
|
+
if (!validated) warnings.patchWasDropped({ name: entry.name, param: entry.codec.primary })
|
|
431
429
|
}
|
|
432
|
-
if (
|
|
433
|
-
core.writeField(setRecord, entry.name, next)
|
|
430
|
+
if (!validated) {
|
|
431
|
+
if (entry.codec.lane.persist) core.writeField(setRecord, entry.name, next)
|
|
432
|
+
else warnings.writeHasNowhereToGo({ name: entry.name, param: entry.codec.primary })
|
|
434
433
|
return
|
|
435
434
|
}
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
435
|
+
// `patch` FIRST, the field's own params last: a patch is for keys the store does not own
|
|
436
|
+
// (clearing a sibling `detailDate` with `undefined`), so it must never be able to overwrite
|
|
437
|
+
// the value this very call is setting.
|
|
438
|
+
// `resets` only when the value moves: a no-op write (a debounced box settling back on what
|
|
439
|
+
// it held) must not throw the reader off the page they are on.
|
|
440
|
+
navigateSearch(
|
|
441
|
+
(o) => navigate(o),
|
|
442
|
+
(prev) => ({
|
|
443
|
+
...prev,
|
|
444
|
+
...resetsFor(entry, prev, next),
|
|
445
|
+
...opts?.patch,
|
|
446
|
+
...entry.codec.toSearch(next),
|
|
447
|
+
}),
|
|
448
|
+
entry.codec.lane.history === 'replace',
|
|
449
|
+
)
|
|
450
|
+
if (entry.codec.lane.persist) core.writeField(setRecord, entry.name, next)
|
|
440
451
|
},
|
|
441
452
|
] as const
|
|
442
453
|
}
|
|
@@ -459,14 +470,28 @@ export function buildSearchStore<const S extends Record<string, AnyField>>(
|
|
|
459
470
|
/**
|
|
460
471
|
* Unsetting one field, per lane: drop the mirror key (never write the fallback — a thunk fallback
|
|
461
472
|
* resolved into localStorage is a value nobody chose), drop the memory value, and on the URL lane
|
|
462
|
-
* navigate back to the fallback params through the
|
|
473
|
+
* navigate back to the fallback params through the mounted router.
|
|
463
474
|
*/
|
|
464
475
|
const clearFor = (entry: StoreEntry) => (): void => {
|
|
465
476
|
if (entry.codec.lane.persist) core.clearField(entry.name)
|
|
466
477
|
else if (!entry.codec.lane.url) core.clearMemory(entry.name)
|
|
467
478
|
if (!entry.codec.lane.url) return
|
|
468
|
-
const
|
|
469
|
-
|
|
479
|
+
for (const router of mounts.keys()) {
|
|
480
|
+
// Ownership is judged where the user is NOW (the committed, validated location), not where
|
|
481
|
+
// the last render happened to be.
|
|
482
|
+
const { search } = router.state.location
|
|
483
|
+
if (!isRecord(search) || !Object.hasOwn(search, entry.codec.primary)) continue
|
|
484
|
+
// Gated like the setter: clearing a field already at its fallback keeps the page.
|
|
485
|
+
navigateSearch(
|
|
486
|
+
(o) => router.navigate(o),
|
|
487
|
+
(prev) => ({
|
|
488
|
+
...prev,
|
|
489
|
+
...resetsFor(entry, prev, entry.codec.fallback),
|
|
490
|
+
...entry.codec.toSearch(entry.codec.fallback),
|
|
491
|
+
}),
|
|
492
|
+
true,
|
|
493
|
+
)
|
|
494
|
+
}
|
|
470
495
|
}
|
|
471
496
|
|
|
472
497
|
const field = {} as { [K in keyof S]: FieldHandle<S[K]> }
|
|
@@ -522,22 +547,19 @@ export function buildSearchStore<const S extends Record<string, AnyField>>(
|
|
|
522
547
|
for (const entry of validatedEntries) {
|
|
523
548
|
Object.assign(patch, entry.codec.toSearch(entry.codec.fallback))
|
|
524
549
|
}
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
550
|
+
// `resets` only when some field actually moves — the same gate as the setter and
|
|
551
|
+
// `clear()`. A `Reset all` with everything already at its fallback must keep the page:
|
|
552
|
+
// this navigate REPLACES, so a page dropped here could not be got back with Back.
|
|
553
|
+
navigateSearch(
|
|
554
|
+
(o) => navigate(o),
|
|
555
|
+
(prev) => {
|
|
531
556
|
const moved = validatedEntries.some(
|
|
532
557
|
(entry) => resetsFor(entry, prev, entry.codec.fallback) !== undefined,
|
|
533
558
|
)
|
|
534
559
|
return { ...prev, ...(moved ? resetPatch : undefined), ...patch }
|
|
535
560
|
},
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
// sheet must not also scroll the page it was pressed on.
|
|
539
|
-
resetScroll: false,
|
|
540
|
-
})
|
|
561
|
+
true,
|
|
562
|
+
)
|
|
541
563
|
}
|
|
542
564
|
if (memoryEntries.length > 0) core.resetMemory()
|
|
543
565
|
if (!core.anyPersisted) return
|
|
@@ -575,31 +597,3 @@ export function buildSearchStore<const S extends Record<string, AnyField>>(
|
|
|
575
597
|
|
|
576
598
|
return store
|
|
577
599
|
}
|
|
578
|
-
|
|
579
|
-
/**
|
|
580
|
-
* One store per page (or per feature), over typed fields. `key` is the localStorage key; each
|
|
581
|
-
* field's map key is its default URL param name.
|
|
582
|
-
*
|
|
583
|
-
* @example
|
|
584
|
-
* export const analytics = createSearchStore({
|
|
585
|
-
* key: 'analytics',
|
|
586
|
-
* fields: {
|
|
587
|
-
* range: field.range({ presets: ['7d', '30d', '90d'], fallback: '30d', custom: true }),
|
|
588
|
-
* compare: field.enum(['none', 'previous'], 'none'),
|
|
589
|
-
* channels: field.multi(CHANNELS, []),
|
|
590
|
-
* },
|
|
591
|
-
* }).labels({ range: { '7d': 'Last 7 days', '30d': 'Last 30 days', '90d': 'Last 90 days' } })
|
|
592
|
-
*
|
|
593
|
-
* export const Route = createFileRoute('/analytics')({
|
|
594
|
-
* validateSearch: analytics.validateSearch,
|
|
595
|
-
* loaderDeps: ({ search }) => search,
|
|
596
|
-
* })
|
|
597
|
-
*
|
|
598
|
-
* // in the page: <RangeFilter field={analytics.field.range} />
|
|
599
|
-
* // in the nav: linkOptions({ to: '/analytics', search: analytics.linkSearch })
|
|
600
|
-
*/
|
|
601
|
-
export function createSearchStore<const S extends Record<string, AnyField>>(
|
|
602
|
-
o: CreateSearchStoreOptions<S>,
|
|
603
|
-
): SearchStore<S> {
|
|
604
|
-
return buildSearchStore(o)
|
|
605
|
-
}
|
|
@@ -13,7 +13,7 @@ import { createElement, forwardRef, useMemo } from 'react'
|
|
|
13
13
|
import type { AnchorHTMLAttributes, ElementType, ReactNode } from 'react'
|
|
14
14
|
import { Link, useMatchRoute, useRouterState } from '@tanstack/react-router'
|
|
15
15
|
import type { MobileNavConfig, NavAnchor, SidebarItem, SidebarSection } from '../shell/nav-types'
|
|
16
|
-
import { flattenNav } from './nav'
|
|
16
|
+
import { flattenNav } from './nav-stash'
|
|
17
17
|
import type { AnyNavGroup, AnyNavItem, NavConfig, NavItemId, NavTabId } from './nav'
|
|
18
18
|
|
|
19
19
|
export type UseNavOptions<G extends ReadonlyArray<AnyNavGroup>> = {
|
package/src/surfaces.ts
CHANGED
|
@@ -407,7 +407,7 @@ export const SURFACES = {
|
|
|
407
407
|
guardKinds: [],
|
|
408
408
|
pluginRules: [],
|
|
409
409
|
description:
|
|
410
|
-
'defineCommands (typed command bus), defineOverlays/overlays (a form dialog is a registered overlay opened with overlays.open), setColorScheme, toggleSidebar, ShortcutsHelp, defineNotifications/emit/NotificationBell (emit only accepts kinds registered in defineNotifications; folded in from ./notifications); BasaltProvider mounts Spotlight, Modals
|
|
410
|
+
'defineCommands (typed command bus), defineOverlays/overlays (a form dialog is a registered overlay opened with overlays.open), setColorScheme, toggleSidebar, ShortcutsHelp, defineNotifications/emit/NotificationBell (emit only accepts kinds registered in defineNotifications; folded in from ./notifications); BasaltProvider mounts Notifications; Spotlight mounts once defineNav/defineCommands ran, Modals once defineOverlays/overlays.confirm ran',
|
|
411
411
|
optionalPeers: ['@tanstack/react-hotkeys'],
|
|
412
412
|
forbiddenImports: [],
|
|
413
413
|
},
|