@igstack/app-catalog-frontend-core 4.0.0-alpha-20260920014237 → 4.0.0-alpha-20260920023235
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/dist/esm/__tests__/modules/appCatalog/hooks/useUrlSyncedState.test.d.ts +1 -0
- package/dist/esm/modules/appCatalog/hooks/useUrlSyncedState.js +5 -5
- package/dist/esm/modules/appCatalog/hooks/useUrlSyncedState.js.map +1 -1
- package/package.json +4 -4
- package/src/__tests__/modules/appCatalog/hooks/useUrlSyncedState.test.tsx +63 -0
- package/src/__tests__/modules/appCatalog/ui/detail/AppDetails.sectionOrder.test.tsx +3 -1
- package/src/modules/appCatalog/hooks/useUrlSyncedState.ts +23 -5
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -17,19 +17,19 @@ function useUrlSyncedState({
|
|
|
17
17
|
return defaultValue;
|
|
18
18
|
});
|
|
19
19
|
useEffect(() => {
|
|
20
|
+
const currentSearch = router.state.location.search;
|
|
20
21
|
const encodedValue = encode ? encode(state) : state;
|
|
21
|
-
if (encodedValue ===
|
|
22
|
-
const currentPath = router.state.location.pathname;
|
|
22
|
+
if (encodedValue === currentSearch[key]) return;
|
|
23
23
|
navigate({
|
|
24
|
-
to:
|
|
24
|
+
to: router.state.location.pathname,
|
|
25
25
|
search: {
|
|
26
|
-
...
|
|
26
|
+
...currentSearch,
|
|
27
27
|
[key]: encodedValue
|
|
28
28
|
},
|
|
29
29
|
replace: true
|
|
30
30
|
// Use replace to avoid polluting history
|
|
31
31
|
});
|
|
32
|
-
}, [state, key, encode, navigate, router
|
|
32
|
+
}, [state, key, encode, navigate, router, search]);
|
|
33
33
|
return [state, setState];
|
|
34
34
|
}
|
|
35
35
|
export {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useUrlSyncedState.js","sources":["../../../../../src/modules/appCatalog/hooks/useUrlSyncedState.ts"],"sourcesContent":["import { useNavigate, useRouter, useSearch } from '@tanstack/react-router'\nimport { useEffect, useState } from 'react'\n\n/**\n * Options for useUrlSyncedState hook\n */\nexport interface UseUrlSyncedStateOptions<T> {\n /** The URL search parameter key */\n key: string\n /** Default value when URL param is not present */\n defaultValue: T\n /** Optional decoder to transform URL string to state value */\n decode?: (urlValue: string) => T\n /** Optional encoder to transform state value to URL string (return undefined to remove param) */\n encode?: (stateValue: T) => string | undefined\n}\n\n/**\n * Hook for managing state that is synced bidirectionally with URL search params.\n *\n * Key features:\n * - Initializes from URL once on mount\n * - Async sync state → URL after initialization to prevent UI sluggishness\n * - Uses replace: true to avoid polluting browser history\n *\n * @example\n * ```tsx\n * // Boolean state\n * const [recentMode, setRecentMode] = useUrlSyncedState({\n * key: 'recent',\n * defaultValue: false,\n * decode: (value) => value === '1',\n * encode: (value) => value ? '1' : undefined,\n * })\n *\n * // Object state\n * const [filters, setFilters] = useUrlSyncedState({\n * key: 'filters',\n * defaultValue: {},\n * decode: (value) => parseFilters(value),\n * encode: (value) => encodeFilters(value),\n * })\n * ```\n */\nexport function useUrlSyncedState<T>({\n key,\n defaultValue,\n decode,\n encode,\n}: UseUrlSyncedStateOptions<T>): [T, (value: T) => void] {\n const navigate = useNavigate()\n const router = useRouter()\n const search = useSearch({ strict: false })\n\n // Initialize state from URL on mount (once only)\n const [state, setState] = useState<T>(() => {\n const urlValue = (search as Record<string, unknown>)[key]\n if (urlValue !== undefined) {\n return decode ? decode(String(urlValue)) : (urlValue as T)\n }\n return defaultValue\n })\n\n // Sync state to URL (async side effect). The equality check below is what\n // prevents redundant writes / URL pollution from default values — so state\n // changes made from the default (e.g. the first keystroke in an empty search)\n // are still persisted to the URL and survive navigation/remounts.\n useEffect(() => {\n // Encode state value for URL\n const encodedValue = encode ? encode(state) : (state as string | undefined)\n\n // Check if already in sync\n if (encodedValue ===
|
|
1
|
+
{"version":3,"file":"useUrlSyncedState.js","sources":["../../../../../src/modules/appCatalog/hooks/useUrlSyncedState.ts"],"sourcesContent":["import { useNavigate, useRouter, useSearch } from '@tanstack/react-router'\nimport { useEffect, useState } from 'react'\n\n/**\n * Options for useUrlSyncedState hook\n */\nexport interface UseUrlSyncedStateOptions<T> {\n /** The URL search parameter key */\n key: string\n /** Default value when URL param is not present */\n defaultValue: T\n /** Optional decoder to transform URL string to state value */\n decode?: (urlValue: string) => T\n /** Optional encoder to transform state value to URL string (return undefined to remove param) */\n encode?: (stateValue: T) => string | undefined\n}\n\n/**\n * Hook for managing state that is synced bidirectionally with URL search params.\n *\n * Key features:\n * - Initializes from URL once on mount\n * - Async sync state → URL after initialization to prevent UI sluggishness\n * - Uses replace: true to avoid polluting browser history\n *\n * @example\n * ```tsx\n * // Boolean state\n * const [recentMode, setRecentMode] = useUrlSyncedState({\n * key: 'recent',\n * defaultValue: false,\n * decode: (value) => value === '1',\n * encode: (value) => value ? '1' : undefined,\n * })\n *\n * // Object state\n * const [filters, setFilters] = useUrlSyncedState({\n * key: 'filters',\n * defaultValue: {},\n * decode: (value) => parseFilters(value),\n * encode: (value) => encodeFilters(value),\n * })\n * ```\n */\nexport function useUrlSyncedState<T>({\n key,\n defaultValue,\n decode,\n encode,\n}: UseUrlSyncedStateOptions<T>): [T, (value: T) => void] {\n const navigate = useNavigate()\n const router = useRouter()\n const search = useSearch({ strict: false })\n\n // Initialize state from URL on mount (once only)\n const [state, setState] = useState<T>(() => {\n const urlValue = (search as Record<string, unknown>)[key]\n if (urlValue !== undefined) {\n return decode ? decode(String(urlValue)) : (urlValue as T)\n }\n return defaultValue\n })\n\n // Sync state to URL (async side effect). The equality check below is what\n // prevents redundant writes / URL pollution from default values — so state\n // changes made from the default (e.g. the first keystroke in an empty search)\n // are still persisted to the URL and survive navigation/remounts.\n useEffect(() => {\n // Compare against the LIVE router location, not the `useSearch()` snapshot\n // above: that snapshot comes from the resolved route match and still holds\n // the pre-navigation params while a navigation this effect issued is\n // settling. `encode` is normally an inline arrow, so this effect re-runs on\n // every render — and against the stale snapshot the sync check never\n // matched, so each render queued another navigate() and each navigate()\n // caused another render. 50 of those is React's \"Maximum update depth\n // exceeded\", which unmounted the subtree mid-interaction (#152).\n //\n // Spreading the live params matters for the same reason: two instances of\n // this hook writing different keys in one tick would otherwise each spread\n // their own stale snapshot and drop the other's param.\n const currentSearch = router.state.location.search as Record<\n string,\n unknown\n >\n\n // Encode state value for URL\n const encodedValue = encode ? encode(state) : (state as string | undefined)\n\n // Check if already in sync\n if (encodedValue === currentSearch[key]) return\n\n navigate({\n to: router.state.location.pathname,\n search: {\n ...currentSearch,\n [key]: encodedValue,\n },\n replace: true, // Use replace to avoid polluting history\n })\n // `search` stays in the deps so an external URL change (back/forward) still\n // re-runs this and re-asserts state → URL.\n }, [state, key, encode, navigate, router, search])\n\n return [state, setState]\n}\n"],"names":[],"mappings":";;AA4CO,SAAS,kBAAqB;AAAA,EACnC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAAyD;AACvD,QAAM,WAAW,YAAA;AACjB,QAAM,SAAS,UAAA;AACf,QAAM,SAAS,UAAU,EAAE,QAAQ,OAAO;AAG1C,QAAM,CAAC,OAAO,QAAQ,IAAI,SAAY,MAAM;AAC1C,UAAM,WAAY,OAAmC,GAAG;AACxD,QAAI,aAAa,QAAW;AAC1B,aAAO,SAAS,OAAO,OAAO,QAAQ,CAAC,IAAK;AAAA,IAC9C;AACA,WAAO;AAAA,EACT,CAAC;AAMD,YAAU,MAAM;AAad,UAAM,gBAAgB,OAAO,MAAM,SAAS;AAM5C,UAAM,eAAe,SAAS,OAAO,KAAK,IAAK;AAG/C,QAAI,iBAAiB,cAAc,GAAG,EAAG;AAEzC,aAAS;AAAA,MACP,IAAI,OAAO,MAAM,SAAS;AAAA,MAC1B,QAAQ;AAAA,QACN,GAAG;AAAA,QACH,CAAC,GAAG,GAAG;AAAA,MAAA;AAAA,MAET,SAAS;AAAA;AAAA,IAAA,CACV;AAAA,EAGH,GAAG,CAAC,OAAO,KAAK,QAAQ,UAAU,QAAQ,MAAM,CAAC;AAEjD,SAAO,CAAC,OAAO,QAAQ;AACzB;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@igstack/app-catalog-frontend-core",
|
|
3
|
-
"version": "4.0.0-alpha-
|
|
3
|
+
"version": "4.0.0-alpha-20260920023235",
|
|
4
4
|
"description": "Frontend core library for App Catalog",
|
|
5
5
|
"homepage": "https://github.com/lislon/app-catalog",
|
|
6
6
|
"repository": {
|
|
@@ -113,7 +113,7 @@
|
|
|
113
113
|
"tailwindcss": "^4.1.7",
|
|
114
114
|
"tiptap-markdown": "^0.8.0",
|
|
115
115
|
"tslib": "^2.3.0",
|
|
116
|
-
"@igstack/app-catalog-shared-core": "4.0.0-alpha-
|
|
116
|
+
"@igstack/app-catalog-shared-core": "4.0.0-alpha-20260920023235"
|
|
117
117
|
},
|
|
118
118
|
"devDependencies": {
|
|
119
119
|
"@tailwindcss/vite": "^4.0.5",
|
|
@@ -140,7 +140,7 @@
|
|
|
140
140
|
"vite-plugin-static-copy": "^3.1.4",
|
|
141
141
|
"vite-plugin-svgr": "^4.2.0",
|
|
142
142
|
"vitest": "^4.1.2",
|
|
143
|
-
"@igstack/app-catalog-backend-core": "4.0.0-alpha-
|
|
143
|
+
"@igstack/app-catalog-backend-core": "4.0.0-alpha-20260920023235"
|
|
144
144
|
},
|
|
145
145
|
"peerDependencies": {
|
|
146
146
|
"react": "19.1.2",
|
|
@@ -149,7 +149,7 @@
|
|
|
149
149
|
"vite": "^6.3.5",
|
|
150
150
|
"vite-plugin-svgr": "^4.2.0"
|
|
151
151
|
},
|
|
152
|
-
"gitHead": "
|
|
152
|
+
"gitHead": "19b6a337dcda5176416be6325c06dfb4d60031d4",
|
|
153
153
|
"scripts": {
|
|
154
154
|
"build": "vite build",
|
|
155
155
|
"build:lenient": "vite build --mode lenient",
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { act, render } from '@testing-library/react'
|
|
2
|
+
import { useReducer } from 'react'
|
|
3
|
+
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
4
|
+
|
|
5
|
+
// The router's live location. A real `navigate()` updates this synchronously,
|
|
6
|
+
// well before the resolved route match (and therefore `useSearch()`) catches up.
|
|
7
|
+
const live = { pathname: '/app/lims', search: {} as Record<string, unknown> }
|
|
8
|
+
|
|
9
|
+
// The pathological case this hook has to survive: the `useSearch()` snapshot
|
|
10
|
+
// lags a navigation the hook itself issued. Observed on a production build --
|
|
11
|
+
// the snapshot still read `{}` for 52 consecutive renders (#152).
|
|
12
|
+
const staleSnapshot: Record<string, unknown> = {}
|
|
13
|
+
|
|
14
|
+
let forceRender: (() => void) | undefined
|
|
15
|
+
|
|
16
|
+
const navigate = vi.fn((opts: { search: Record<string, unknown> }) => {
|
|
17
|
+
live.search = opts.search
|
|
18
|
+
// A navigation re-renders the subtree. Capped so a regression fails the
|
|
19
|
+
// assertion below instead of hanging the suite.
|
|
20
|
+
if (navigate.mock.calls.length < 30) forceRender?.()
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
vi.mock('@tanstack/react-router', () => ({
|
|
24
|
+
useNavigate: vi.fn(() => navigate),
|
|
25
|
+
useRouter: vi.fn(() => ({ state: { location: live } })),
|
|
26
|
+
useSearch: vi.fn(() => staleSnapshot),
|
|
27
|
+
}))
|
|
28
|
+
|
|
29
|
+
const { useUrlSyncedState } =
|
|
30
|
+
await import('~/modules/appCatalog/hooks/useUrlSyncedState')
|
|
31
|
+
|
|
32
|
+
let setSlug: (value: string) => void
|
|
33
|
+
|
|
34
|
+
function Probe() {
|
|
35
|
+
const [, tick] = useReducer((n: number) => n + 1, 0)
|
|
36
|
+
forceRender = tick
|
|
37
|
+
// Inline `encode`, as every caller writes it: a new function identity on
|
|
38
|
+
// every render, so the sync effect re-runs on every render.
|
|
39
|
+
const [, set] = useUrlSyncedState<string>({
|
|
40
|
+
key: 'qj',
|
|
41
|
+
defaultValue: '',
|
|
42
|
+
encode: (value) => value || undefined,
|
|
43
|
+
})
|
|
44
|
+
setSlug = set
|
|
45
|
+
return null
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
describe('useUrlSyncedState', () => {
|
|
49
|
+
beforeEach(() => {
|
|
50
|
+
navigate.mockClear()
|
|
51
|
+
live.search = {}
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it('writes a state change to the URL exactly once', () => {
|
|
55
|
+
render(<Probe />)
|
|
56
|
+
navigate.mockClear()
|
|
57
|
+
|
|
58
|
+
act(() => setSlug('prodlims.rm-support-review'))
|
|
59
|
+
|
|
60
|
+
expect(navigate).toHaveBeenCalledTimes(1)
|
|
61
|
+
expect(live.search.qj).toBe('prodlims.rm-support-review')
|
|
62
|
+
})
|
|
63
|
+
})
|
|
@@ -10,7 +10,9 @@ vi.mock('@tanstack/react-router', () => ({
|
|
|
10
10
|
// Quick Jump keeps its destination in the url, so the panel reads the router
|
|
11
11
|
// even on an app with no jumps to show.
|
|
12
12
|
useNavigate: vi.fn(() => vi.fn()),
|
|
13
|
-
useRouter: vi.fn(() => ({
|
|
13
|
+
useRouter: vi.fn(() => ({
|
|
14
|
+
state: { location: { pathname: '/', search: {} } },
|
|
15
|
+
})),
|
|
14
16
|
Link: ({ children, ...rest }: { children?: React.ReactNode }) => (
|
|
15
17
|
<a {...rest}>{children}</a>
|
|
16
18
|
),
|
|
@@ -66,22 +66,40 @@ export function useUrlSyncedState<T>({
|
|
|
66
66
|
// changes made from the default (e.g. the first keystroke in an empty search)
|
|
67
67
|
// are still persisted to the URL and survive navigation/remounts.
|
|
68
68
|
useEffect(() => {
|
|
69
|
+
// Compare against the LIVE router location, not the `useSearch()` snapshot
|
|
70
|
+
// above: that snapshot comes from the resolved route match and still holds
|
|
71
|
+
// the pre-navigation params while a navigation this effect issued is
|
|
72
|
+
// settling. `encode` is normally an inline arrow, so this effect re-runs on
|
|
73
|
+
// every render — and against the stale snapshot the sync check never
|
|
74
|
+
// matched, so each render queued another navigate() and each navigate()
|
|
75
|
+
// caused another render. 50 of those is React's "Maximum update depth
|
|
76
|
+
// exceeded", which unmounted the subtree mid-interaction (#152).
|
|
77
|
+
//
|
|
78
|
+
// Spreading the live params matters for the same reason: two instances of
|
|
79
|
+
// this hook writing different keys in one tick would otherwise each spread
|
|
80
|
+
// their own stale snapshot and drop the other's param.
|
|
81
|
+
const currentSearch = router.state.location.search as Record<
|
|
82
|
+
string,
|
|
83
|
+
unknown
|
|
84
|
+
>
|
|
85
|
+
|
|
69
86
|
// Encode state value for URL
|
|
70
87
|
const encodedValue = encode ? encode(state) : (state as string | undefined)
|
|
71
88
|
|
|
72
89
|
// Check if already in sync
|
|
73
|
-
if (encodedValue ===
|
|
90
|
+
if (encodedValue === currentSearch[key]) return
|
|
74
91
|
|
|
75
|
-
const currentPath = router.state.location.pathname
|
|
76
92
|
navigate({
|
|
77
|
-
to:
|
|
93
|
+
to: router.state.location.pathname,
|
|
78
94
|
search: {
|
|
79
|
-
...
|
|
95
|
+
...currentSearch,
|
|
80
96
|
[key]: encodedValue,
|
|
81
97
|
},
|
|
82
98
|
replace: true, // Use replace to avoid polluting history
|
|
83
99
|
})
|
|
84
|
-
|
|
100
|
+
// `search` stays in the deps so an external URL change (back/forward) still
|
|
101
|
+
// re-runs this and re-asserts state → URL.
|
|
102
|
+
}, [state, key, encode, navigate, router, search])
|
|
85
103
|
|
|
86
104
|
return [state, setState]
|
|
87
105
|
}
|