@teleporthq/teleport-plugin-common 0.45.3-alpha.0 → 0.45.13
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/__tests__/builders/html-builders.ts +9 -2
- package/__tests__/builders/style-builders.ts +135 -1
- package/__tests__/node-handlers/node-to-html/index.ts +2 -0
- package/__tests__/node-handlers/node-to-jsx/attribute-value-mapper.ts +77 -0
- package/__tests__/node-handlers/node-to-jsx/calendar-events-reviver.ts +176 -0
- package/__tests__/node-handlers/node-to-jsx/chart-data-guard.ts +101 -0
- package/__tests__/node-handlers/node-to-jsx/cms-list-repeater-ecommerce-source.ts +79 -0
- package/__tests__/node-handlers/node-to-jsx/cms-list-repeater-loading.ts +122 -0
- package/__tests__/node-handlers/node-to-jsx/index.ts +719 -3
- package/__tests__/node-handlers/node-to-jsx/url-search-params-expression.ts +59 -0
- package/__tests__/node-handlers/node-to-jsx/utils.ts +635 -3
- package/__tests__/utils/ast-utils.ts +188 -0
- package/__tests__/utils/content-property-sanitiser.ts +156 -0
- package/__tests__/utils/page-seeds-state-from-data-source.ts +70 -0
- package/__tests__/utils/parse-string-with-template-expressions.ts +82 -0
- package/__tests__/utils/resource-auth-scheme.ts +55 -0
- package/__tests__/utils/route-utils.ts +118 -0
- package/__tests__/utils/style-utils.ts +59 -0
- package/__tests__/utils/template-expression-balance.ts +83 -0
- package/__tests__/utils/url-query-writer.ts +239 -0
- package/__tests__/utils/url-search-param-sync.ts +138 -0
- package/__tests__/utils/url-search-params.ts +48 -0
- package/dist/cjs/builders/ast-builders.d.ts +2 -0
- package/dist/cjs/builders/ast-builders.d.ts.map +1 -1
- package/dist/cjs/builders/ast-builders.js +41 -3
- package/dist/cjs/builders/ast-builders.js.map +1 -1
- package/dist/cjs/builders/hast-builders.d.ts +1 -0
- package/dist/cjs/builders/hast-builders.d.ts.map +1 -1
- package/dist/cjs/builders/hast-builders.js +11 -1
- package/dist/cjs/builders/hast-builders.js.map +1 -1
- package/dist/cjs/builders/style-builders.d.ts +8 -2
- package/dist/cjs/builders/style-builders.d.ts.map +1 -1
- package/dist/cjs/builders/style-builders.js +80 -33
- package/dist/cjs/builders/style-builders.js.map +1 -1
- package/dist/cjs/index.d.ts +5 -1
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +10 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/node-handlers/node-to-html/constants.d.ts.map +1 -1
- package/dist/cjs/node-handlers/node-to-html/constants.js +2 -0
- package/dist/cjs/node-handlers/node-to-html/constants.js.map +1 -1
- package/dist/cjs/node-handlers/node-to-html/index.d.ts.map +1 -1
- package/dist/cjs/node-handlers/node-to-html/index.js +69 -1
- package/dist/cjs/node-handlers/node-to-html/index.js.map +1 -1
- package/dist/cjs/node-handlers/node-to-html/types.d.ts +5 -1
- package/dist/cjs/node-handlers/node-to-html/types.d.ts.map +1 -1
- package/dist/cjs/node-handlers/node-to-html/utils.d.ts +2 -1
- package/dist/cjs/node-handlers/node-to-html/utils.d.ts.map +1 -1
- package/dist/cjs/node-handlers/node-to-html/utils.js +174 -13
- package/dist/cjs/node-handlers/node-to-html/utils.js.map +1 -1
- package/dist/cjs/node-handlers/node-to-jsx/index.d.ts.map +1 -1
- package/dist/cjs/node-handlers/node-to-jsx/index.js +1443 -45
- package/dist/cjs/node-handlers/node-to-jsx/index.js.map +1 -1
- package/dist/cjs/node-handlers/node-to-jsx/types.d.ts +60 -6
- package/dist/cjs/node-handlers/node-to-jsx/types.d.ts.map +1 -1
- package/dist/cjs/node-handlers/node-to-jsx/utils.d.ts +80 -7
- package/dist/cjs/node-handlers/node-to-jsx/utils.d.ts.map +1 -1
- package/dist/cjs/node-handlers/node-to-jsx/utils.js +883 -72
- package/dist/cjs/node-handlers/node-to-jsx/utils.js.map +1 -1
- package/dist/cjs/tsconfig.tsbuildinfo +1 -1
- package/dist/cjs/utils/ast-utils.d.ts +163 -11
- package/dist/cjs/utils/ast-utils.d.ts.map +1 -1
- package/dist/cjs/utils/ast-utils.js +1013 -75
- package/dist/cjs/utils/ast-utils.js.map +1 -1
- package/dist/cjs/utils/hast-utils.d.ts +8 -6
- package/dist/cjs/utils/hast-utils.d.ts.map +1 -1
- package/dist/cjs/utils/hast-utils.js +28 -4
- package/dist/cjs/utils/hast-utils.js.map +1 -1
- package/dist/cjs/utils/nested-selector-commas.d.ts +2 -0
- package/dist/cjs/utils/nested-selector-commas.d.ts.map +1 -0
- package/dist/cjs/utils/nested-selector-commas.js +81 -0
- package/dist/cjs/utils/nested-selector-commas.js.map +1 -0
- package/dist/cjs/utils/route-utils.d.ts +117 -0
- package/dist/cjs/utils/route-utils.d.ts.map +1 -0
- package/dist/cjs/utils/route-utils.js +212 -0
- package/dist/cjs/utils/route-utils.js.map +1 -0
- package/dist/cjs/utils/style-utils.d.ts +1 -0
- package/dist/cjs/utils/style-utils.d.ts.map +1 -1
- package/dist/cjs/utils/style-utils.js +181 -2
- package/dist/cjs/utils/style-utils.js.map +1 -1
- package/dist/cjs/utils/template-expression-balance.d.ts +58 -0
- package/dist/cjs/utils/template-expression-balance.d.ts.map +1 -0
- package/dist/cjs/utils/template-expression-balance.js +181 -0
- package/dist/cjs/utils/template-expression-balance.js.map +1 -0
- package/dist/cjs/utils/types.d.ts +3 -0
- package/dist/cjs/utils/types.d.ts.map +1 -0
- package/dist/cjs/utils/types.js +3 -0
- package/dist/cjs/utils/types.js.map +1 -0
- package/dist/cjs/utils/url-query-writer.d.ts +123 -0
- package/dist/cjs/utils/url-query-writer.d.ts.map +1 -0
- package/dist/cjs/utils/url-query-writer.js +260 -0
- package/dist/cjs/utils/url-query-writer.js.map +1 -0
- package/dist/cjs/utils/url-search-param-sync.d.ts +4 -0
- package/dist/cjs/utils/url-search-param-sync.d.ts.map +1 -0
- package/dist/cjs/utils/url-search-param-sync.js +241 -0
- package/dist/cjs/utils/url-search-param-sync.js.map +1 -0
- package/dist/cjs/utils/url-search-params.d.ts +48 -0
- package/dist/cjs/utils/url-search-params.d.ts.map +1 -0
- package/dist/cjs/utils/url-search-params.js +109 -0
- package/dist/cjs/utils/url-search-params.js.map +1 -0
- package/dist/esm/builders/ast-builders.d.ts +2 -0
- package/dist/esm/builders/ast-builders.d.ts.map +1 -1
- package/dist/esm/builders/ast-builders.js +40 -4
- package/dist/esm/builders/ast-builders.js.map +1 -1
- package/dist/esm/builders/hast-builders.d.ts +1 -0
- package/dist/esm/builders/hast-builders.d.ts.map +1 -1
- package/dist/esm/builders/hast-builders.js +9 -0
- package/dist/esm/builders/hast-builders.js.map +1 -1
- package/dist/esm/builders/style-builders.d.ts +8 -2
- package/dist/esm/builders/style-builders.d.ts.map +1 -1
- package/dist/esm/builders/style-builders.js +78 -32
- package/dist/esm/builders/style-builders.js.map +1 -1
- package/dist/esm/index.d.ts +5 -1
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +5 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/node-handlers/node-to-html/constants.d.ts.map +1 -1
- package/dist/esm/node-handlers/node-to-html/constants.js +2 -0
- package/dist/esm/node-handlers/node-to-html/constants.js.map +1 -1
- package/dist/esm/node-handlers/node-to-html/index.d.ts.map +1 -1
- package/dist/esm/node-handlers/node-to-html/index.js +71 -3
- package/dist/esm/node-handlers/node-to-html/index.js.map +1 -1
- package/dist/esm/node-handlers/node-to-html/types.d.ts +5 -1
- package/dist/esm/node-handlers/node-to-html/types.d.ts.map +1 -1
- package/dist/esm/node-handlers/node-to-html/utils.d.ts +2 -1
- package/dist/esm/node-handlers/node-to-html/utils.d.ts.map +1 -1
- package/dist/esm/node-handlers/node-to-html/utils.js +170 -13
- package/dist/esm/node-handlers/node-to-html/utils.js.map +1 -1
- package/dist/esm/node-handlers/node-to-jsx/index.d.ts.map +1 -1
- package/dist/esm/node-handlers/node-to-jsx/index.js +1442 -47
- package/dist/esm/node-handlers/node-to-jsx/index.js.map +1 -1
- package/dist/esm/node-handlers/node-to-jsx/types.d.ts +60 -6
- package/dist/esm/node-handlers/node-to-jsx/types.d.ts.map +1 -1
- package/dist/esm/node-handlers/node-to-jsx/utils.d.ts +80 -7
- package/dist/esm/node-handlers/node-to-jsx/utils.d.ts.map +1 -1
- package/dist/esm/node-handlers/node-to-jsx/utils.js +875 -70
- package/dist/esm/node-handlers/node-to-jsx/utils.js.map +1 -1
- package/dist/esm/tsconfig.tsbuildinfo +1 -1
- package/dist/esm/utils/ast-utils.d.ts +163 -11
- package/dist/esm/utils/ast-utils.d.ts.map +1 -1
- package/dist/esm/utils/ast-utils.js +989 -75
- package/dist/esm/utils/ast-utils.js.map +1 -1
- package/dist/esm/utils/hast-utils.d.ts +8 -6
- package/dist/esm/utils/hast-utils.d.ts.map +1 -1
- package/dist/esm/utils/hast-utils.js +26 -3
- package/dist/esm/utils/hast-utils.js.map +1 -1
- package/dist/esm/utils/nested-selector-commas.d.ts +2 -0
- package/dist/esm/utils/nested-selector-commas.d.ts.map +1 -0
- package/dist/esm/utils/nested-selector-commas.js +77 -0
- package/dist/esm/utils/nested-selector-commas.js.map +1 -0
- package/dist/esm/utils/route-utils.d.ts +117 -0
- package/dist/esm/utils/route-utils.d.ts.map +1 -0
- package/dist/esm/utils/route-utils.js +179 -0
- package/dist/esm/utils/route-utils.js.map +1 -0
- package/dist/esm/utils/style-utils.d.ts +1 -0
- package/dist/esm/utils/style-utils.d.ts.map +1 -1
- package/dist/esm/utils/style-utils.js +179 -1
- package/dist/esm/utils/style-utils.js.map +1 -1
- package/dist/esm/utils/template-expression-balance.d.ts +58 -0
- package/dist/esm/utils/template-expression-balance.d.ts.map +1 -0
- package/dist/esm/utils/template-expression-balance.js +174 -0
- package/dist/esm/utils/template-expression-balance.js.map +1 -0
- package/dist/esm/utils/types.d.ts +3 -0
- package/dist/esm/utils/types.d.ts.map +1 -0
- package/dist/esm/utils/types.js +2 -0
- package/dist/esm/utils/types.js.map +1 -0
- package/dist/esm/utils/url-query-writer.d.ts +123 -0
- package/dist/esm/utils/url-query-writer.d.ts.map +1 -0
- package/dist/esm/utils/url-query-writer.js +229 -0
- package/dist/esm/utils/url-query-writer.js.map +1 -0
- package/dist/esm/utils/url-search-param-sync.d.ts +4 -0
- package/dist/esm/utils/url-search-param-sync.d.ts.map +1 -0
- package/dist/esm/utils/url-search-param-sync.js +213 -0
- package/dist/esm/utils/url-search-param-sync.js.map +1 -0
- package/dist/esm/utils/url-search-params.d.ts +48 -0
- package/dist/esm/utils/url-search-params.d.ts.map +1 -0
- package/dist/esm/utils/url-search-params.js +81 -0
- package/dist/esm/utils/url-search-params.js.map +1 -0
- package/package.json +4 -4
- package/src/builders/ast-builders.ts +80 -3
- package/src/builders/hast-builders.ts +11 -0
- package/src/builders/style-builders.ts +121 -43
- package/src/index.ts +22 -1
- package/src/node-handlers/node-to-html/constants.ts +2 -0
- package/src/node-handlers/node-to-html/index.ts +126 -6
- package/src/node-handlers/node-to-html/types.ts +6 -0
- package/src/node-handlers/node-to-html/utils.ts +228 -14
- package/src/node-handlers/node-to-jsx/index.ts +2135 -62
- package/src/node-handlers/node-to-jsx/types.ts +75 -32
- package/src/node-handlers/node-to-jsx/utils.ts +1173 -84
- package/src/utils/ast-utils.ts +1316 -127
- package/src/utils/hast-utils.ts +37 -9
- package/src/utils/nested-selector-commas.ts +88 -0
- package/src/utils/route-utils.ts +214 -0
- package/src/utils/style-utils.ts +183 -1
- package/src/utils/template-expression-balance.ts +196 -0
- package/src/utils/types.ts +25 -0
- package/src/utils/url-query-writer.ts +318 -0
- package/src/utils/url-search-param-sync.ts +303 -0
- package/src/utils/url-search-params.ts +122 -0
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import * as types from '@babel/types'
|
|
2
|
+
import * as URLQueryWriter from './url-query-writer'
|
|
3
|
+
|
|
4
|
+
// Shared builders for two-way `<state> ⇄ ?key=` URL search-param syncing.
|
|
5
|
+
//
|
|
6
|
+
// Used by BOTH:
|
|
7
|
+
// • `createNextUrlSearchParamsPlugin` (teleport-project-generator-next) for
|
|
8
|
+
// page/component state defs that declare `urlSearchParamBinding` — e.g.
|
|
9
|
+
// the products-list `selectedCategory` / `sortBy` dropdowns.
|
|
10
|
+
// • `createNextArrayMapperPaginationPlugin` (teleport-plugin-next-data-source)
|
|
11
|
+
// for the products-list search input, whose query lives in a
|
|
12
|
+
// pagination-managed state (`ds_N_searchQuery` / `ds_N_state.debouncedQuery`)
|
|
13
|
+
// rather than a UIDL state def, so it cannot ride the plugin above.
|
|
14
|
+
//
|
|
15
|
+
// Both emit the SAME loop-free read-back / write-back effect shapes, so the
|
|
16
|
+
// builders live here to keep the two call sites byte-identical and the
|
|
17
|
+
// loop-prevention reasoning in one place.
|
|
18
|
+
|
|
19
|
+
// Strict JS identifier (Latin only is fine: UIDL URL keys are limited to the
|
|
20
|
+
// subset Next.js supports as a query param key, which the GUI guarantees to
|
|
21
|
+
// be at minimum URL-safe; but the *generated* code must be valid JS, and
|
|
22
|
+
// `__nextQuery.foo-bar` parses as a subtraction). Anything off-pattern goes
|
|
23
|
+
// through bracket notation: `__nextQuery['foo-bar']`.
|
|
24
|
+
const VALID_JS_IDENTIFIER = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/
|
|
25
|
+
|
|
26
|
+
// `object.<paramKey>` for identifier-safe keys, `object['param-key']` otherwise,
|
|
27
|
+
// so the emitted code is always valid JS regardless of the URL key shape.
|
|
28
|
+
const accessKeyOf = (object: types.Expression, paramKey: string): types.MemberExpression => {
|
|
29
|
+
if (VALID_JS_IDENTIFIER.test(paramKey)) {
|
|
30
|
+
return types.memberExpression(object, types.identifier(paramKey))
|
|
31
|
+
}
|
|
32
|
+
return types.memberExpression(object, types.stringLiteral(paramKey), true)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const adoptedRefCurrent = (refName: string): types.MemberExpression =>
|
|
36
|
+
types.memberExpression(types.identifier(refName), types.identifier('current'))
|
|
37
|
+
|
|
38
|
+
const routerQueryAccess = (paramKey: string): types.MemberExpression =>
|
|
39
|
+
accessKeyOf(
|
|
40
|
+
types.memberExpression(types.identifier('router'), types.identifier('query')),
|
|
41
|
+
paramKey
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
// Clones an expression so it can be slotted into multiple positions of the
|
|
45
|
+
// emitted AST — Babel does not support sharing the same node reference across
|
|
46
|
+
// the tree, and callers pass arbitrary value/dependency expressions.
|
|
47
|
+
const clone = (expr: types.Expression): types.Expression =>
|
|
48
|
+
types.cloneNode(expr, /* deep */ true) as types.Expression
|
|
49
|
+
|
|
50
|
+
// Builds the state→URL write-back useEffect. Emitted shape (for a value of
|
|
51
|
+
// `selectedCategory` bound to URL key `categoryFilter`):
|
|
52
|
+
//
|
|
53
|
+
// useEffect(() => {
|
|
54
|
+
// if (typeof window === 'undefined') return
|
|
55
|
+
// if (!router.isReady) return
|
|
56
|
+
// __tqWriteQueryParam(
|
|
57
|
+
// 'categoryFilter',
|
|
58
|
+
// selectedCategory === '' || selectedCategory == null ? undefined : selectedCategory
|
|
59
|
+
// )
|
|
60
|
+
// }, [selectedCategory, router.isReady])
|
|
61
|
+
//
|
|
62
|
+
// `valueExpr` is the expression read each render and pushed to the URL (a bare
|
|
63
|
+
// state identifier for dropdowns, `ds_N_state.debouncedQuery` for the search
|
|
64
|
+
// input). `depExpr` is the effect dependency that re-triggers the write-back
|
|
65
|
+
// when that value changes — usually the same expression as `valueExpr`.
|
|
66
|
+
//
|
|
67
|
+
// `defaultValueExpr` (optional) is the state's default value. When supplied
|
|
68
|
+
// (only for bindings whose default is a non-empty value, e.g. the sort
|
|
69
|
+
// dropdown's `name-asc`), the current value being EQUAL TO THE DEFAULT also
|
|
70
|
+
// deletes the key — so the canonical/default view keeps a clean, param-free
|
|
71
|
+
// URL instead of stickily writing `?sortBy=name-asc` on first load. This is
|
|
72
|
+
// the write-back half of the default-aware pairing that keeps the URL⇄state
|
|
73
|
+
// sync loop-free: the read-back below resolves a missing key back to the same
|
|
74
|
+
// default, so the two never disagree.
|
|
75
|
+
//
|
|
76
|
+
// Notes:
|
|
77
|
+
// 1. `router.isReady` gate: on the very first SSG render `router.query` is
|
|
78
|
+
// empty, so without this gate any value initialized from
|
|
79
|
+
// `window.location.search` would immediately replace itself with the same
|
|
80
|
+
// value, racing with Next's own hydration. `router.isReady` is also in the
|
|
81
|
+
// deps array — once it flips to true, the effect re-runs with the
|
|
82
|
+
// now-hydrated query and decides correctly whether to write.
|
|
83
|
+
// 2. ⛔ THE EFFECT NO LONGER BUILDS THE NEXT QUERY ITSELF. It hands its single
|
|
84
|
+
// key to `URLQueryWriter`, which merges into the last query the component
|
|
85
|
+
// ASKED for rather than the one Next has got round to rendering. Two controls
|
|
86
|
+
// writing in the same flush — which is guaranteed, because changing a filter
|
|
87
|
+
// resets the page — used to spread the same stale `router.query` and erase
|
|
88
|
+
// each other's keys; the read-backs then fought over the wreckage forever.
|
|
89
|
+
// `url-query-writer.ts` has the full account.
|
|
90
|
+
// 3. Because no key can be clobbered any more, `router.query` is NOT a
|
|
91
|
+
// dependency here. It used to be, as a way to RE-ASSERT a key some other
|
|
92
|
+
// writer had just dropped — which is exactly the feedback edge that turned a
|
|
93
|
+
// one-shot loss into a non-terminating swap. Fixing the write at its source
|
|
94
|
+
// means there is nothing to re-assert.
|
|
95
|
+
// 4. Empty / null value deletes the key entirely so the URL never keeps a
|
|
96
|
+
// sticky `?key=` empty param. The writer treats `undefined` as "remove".
|
|
97
|
+
// 5. `adoptedRefName` (optional) names a `useRef(false)` the paired read-back
|
|
98
|
+
// flips once it has adopted the URL value; until then the write-back stays
|
|
99
|
+
// silent. For a state seeded with its DEFAULT rather than from the URL
|
|
100
|
+
// (`urlSearchParamBinding.hydrateAfterMount`): on the first ready render
|
|
101
|
+
// the state still holds the default, and a write-back running before the
|
|
102
|
+
// read-back would push that default onto the URL — deleting the very key
|
|
103
|
+
// the read-back was about to adopt — after which the two chase each other
|
|
104
|
+
// forever (`/products?view=list` → `/products` → …).
|
|
105
|
+
export const buildUrlWriteBackEffect = (
|
|
106
|
+
paramKey: string,
|
|
107
|
+
valueExpr: types.Expression,
|
|
108
|
+
depExpr: types.Expression,
|
|
109
|
+
defaultValueExpr?: types.Expression,
|
|
110
|
+
adoptedRefName?: string
|
|
111
|
+
): types.ExpressionStatement => {
|
|
112
|
+
// value === '' || value == null [|| value === <default>]
|
|
113
|
+
let deleteCondition: types.Expression = types.logicalExpression(
|
|
114
|
+
'||',
|
|
115
|
+
types.binaryExpression('===', clone(valueExpr), types.stringLiteral('')),
|
|
116
|
+
types.binaryExpression('==', clone(valueExpr), types.nullLiteral())
|
|
117
|
+
)
|
|
118
|
+
if (defaultValueExpr) {
|
|
119
|
+
deleteCondition = types.logicalExpression(
|
|
120
|
+
'||',
|
|
121
|
+
deleteCondition,
|
|
122
|
+
types.binaryExpression('===', clone(valueExpr), clone(defaultValueExpr))
|
|
123
|
+
)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const effectBody = types.blockStatement([
|
|
127
|
+
// if (typeof window === 'undefined') return
|
|
128
|
+
types.ifStatement(
|
|
129
|
+
types.binaryExpression(
|
|
130
|
+
'===',
|
|
131
|
+
types.unaryExpression('typeof', types.identifier('window')),
|
|
132
|
+
types.stringLiteral('undefined')
|
|
133
|
+
),
|
|
134
|
+
types.returnStatement(null)
|
|
135
|
+
),
|
|
136
|
+
// if (!router.isReady) return
|
|
137
|
+
types.ifStatement(
|
|
138
|
+
types.unaryExpression(
|
|
139
|
+
'!',
|
|
140
|
+
types.memberExpression(types.identifier('router'), types.identifier('isReady'))
|
|
141
|
+
),
|
|
142
|
+
types.returnStatement(null)
|
|
143
|
+
),
|
|
144
|
+
// if (!<adoptedRef>.current) return — only with `adoptedRefName`, see note 5
|
|
145
|
+
...(adoptedRefName
|
|
146
|
+
? [
|
|
147
|
+
types.ifStatement(
|
|
148
|
+
types.unaryExpression('!', adoptedRefCurrent(adoptedRefName)),
|
|
149
|
+
types.returnStatement(null)
|
|
150
|
+
),
|
|
151
|
+
]
|
|
152
|
+
: []),
|
|
153
|
+
// __tqWriteQueryParam('<key>', <deleteCondition> ? undefined : <value>)
|
|
154
|
+
URLQueryWriter.buildQueryWriteCall(
|
|
155
|
+
paramKey,
|
|
156
|
+
types.conditionalExpression(deleteCondition, types.identifier('undefined'), clone(valueExpr))
|
|
157
|
+
),
|
|
158
|
+
])
|
|
159
|
+
|
|
160
|
+
return types.expressionStatement(
|
|
161
|
+
types.callExpression(types.identifier('useEffect'), [
|
|
162
|
+
types.arrowFunctionExpression([], effectBody),
|
|
163
|
+
types.arrayExpression([
|
|
164
|
+
clone(depExpr),
|
|
165
|
+
types.memberExpression(types.identifier('router'), types.identifier('isReady')),
|
|
166
|
+
]),
|
|
167
|
+
])
|
|
168
|
+
)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// Builds the URL→state read-back useEffect. Emitted shape (for setter
|
|
172
|
+
// `setSelectedCategory` bound to URL key `categoryFilter`):
|
|
173
|
+
//
|
|
174
|
+
// useEffect(() => {
|
|
175
|
+
// if (!router.isReady) return
|
|
176
|
+
// const __urlValue = router.query.categoryFilter
|
|
177
|
+
// const __nextValue =
|
|
178
|
+
// typeof __urlValue === 'string' ? __urlValue : Array.isArray(__urlValue) ? (__urlValue[0] || '') : ''
|
|
179
|
+
// setSelectedCategory((prev) => (prev === __nextValue ? prev : __nextValue))
|
|
180
|
+
// }, [router.query.categoryFilter, router.isReady])
|
|
181
|
+
//
|
|
182
|
+
// Why pair this with the write-back effect:
|
|
183
|
+
// - On `<Link>` / `router.push` shallow navigations that land on the same path
|
|
184
|
+
// with a different query (or browser back/forward, which can change the query
|
|
185
|
+
// without remounting the page), `router.query` updates but the state hook does
|
|
186
|
+
// not. Without this effect, the control would be stuck on whatever the user
|
|
187
|
+
// last picked even though the URL has moved on.
|
|
188
|
+
// - The functional `setState((prev) => prev === next ? prev : next)` form
|
|
189
|
+
// guarantees React bails out when the URL value matches state, which prevents
|
|
190
|
+
// an infinite write-back ↔ read-back loop: when the user picks a new value,
|
|
191
|
+
// state→URL fires once, URL→state observes the matching value and returns
|
|
192
|
+
// `prev`, so React does not re-render or fire any deps.
|
|
193
|
+
// - `router.query[key]` can be either `string` (single value) or `string[]`
|
|
194
|
+
// (duplicate keys) per Next.js's typings; the helper normalizes both to a
|
|
195
|
+
// string before comparing, defaulting to `''` for the missing-key case so the
|
|
196
|
+
// comparison aligns with the write-back's `value === ''` empty-check.
|
|
197
|
+
//
|
|
198
|
+
// `defaultValueExpr` (optional) is the state's default value. When supplied
|
|
199
|
+
// (bindings with a non-empty default such as the sort dropdown's `name-asc`),
|
|
200
|
+
// an ABSENT or EMPTY URL value resolves to the default rather than `''` — via a
|
|
201
|
+
// trailing `(<normalized>) || <default>`. This is what makes the default sort
|
|
202
|
+
// "stick" on first load: without it the read-back would clobber the default
|
|
203
|
+
// state (`name-asc`) with `''` the instant the router hydrates with no `?sortBy`
|
|
204
|
+
// key, forcing an extra unsorted fetch and losing the ordering. It also keeps
|
|
205
|
+
// browser back/forward to the param-free URL restoring the default view.
|
|
206
|
+
// Omitting it reproduces the pre-default `''` fallback byte-for-byte.
|
|
207
|
+
//
|
|
208
|
+
// `adoptedRefName` (optional): the flag the paired write-back waits on — set
|
|
209
|
+
// once the URL value has been handed to the state. See the write-back's note 5.
|
|
210
|
+
export const buildUrlReadBackEffect = (
|
|
211
|
+
paramKey: string,
|
|
212
|
+
setterName: string,
|
|
213
|
+
defaultValueExpr?: types.Expression,
|
|
214
|
+
adoptedRefName?: string
|
|
215
|
+
): types.ExpressionStatement => {
|
|
216
|
+
const normalizeArg = types.conditionalExpression(
|
|
217
|
+
// typeof __urlValue === 'string' ? __urlValue
|
|
218
|
+
types.binaryExpression(
|
|
219
|
+
'===',
|
|
220
|
+
types.unaryExpression('typeof', types.identifier('__urlValue')),
|
|
221
|
+
types.stringLiteral('string')
|
|
222
|
+
),
|
|
223
|
+
types.identifier('__urlValue'),
|
|
224
|
+
types.conditionalExpression(
|
|
225
|
+
// Array.isArray(__urlValue) ? (__urlValue[0] || '') : ''
|
|
226
|
+
types.callExpression(
|
|
227
|
+
types.memberExpression(types.identifier('Array'), types.identifier('isArray')),
|
|
228
|
+
[types.identifier('__urlValue')]
|
|
229
|
+
),
|
|
230
|
+
types.logicalExpression(
|
|
231
|
+
'||',
|
|
232
|
+
types.memberExpression(types.identifier('__urlValue'), types.numericLiteral(0), true),
|
|
233
|
+
types.stringLiteral('')
|
|
234
|
+
),
|
|
235
|
+
types.stringLiteral('')
|
|
236
|
+
)
|
|
237
|
+
)
|
|
238
|
+
|
|
239
|
+
// With a default, an absent/empty normalized value (falsy `''`) falls through
|
|
240
|
+
// to the default: `(<normalized>) || <default>`. Without one, the bare
|
|
241
|
+
// normalized expression is emitted unchanged.
|
|
242
|
+
const nextValueExpr: types.Expression = defaultValueExpr
|
|
243
|
+
? types.logicalExpression('||', normalizeArg, clone(defaultValueExpr))
|
|
244
|
+
: normalizeArg
|
|
245
|
+
|
|
246
|
+
const effectBody = types.blockStatement([
|
|
247
|
+
// if (!router.isReady) return
|
|
248
|
+
types.ifStatement(
|
|
249
|
+
types.unaryExpression(
|
|
250
|
+
'!',
|
|
251
|
+
types.memberExpression(types.identifier('router'), types.identifier('isReady'))
|
|
252
|
+
),
|
|
253
|
+
types.returnStatement(null)
|
|
254
|
+
),
|
|
255
|
+
// const __urlValue = router.query.<paramKey>
|
|
256
|
+
types.variableDeclaration('const', [
|
|
257
|
+
types.variableDeclarator(types.identifier('__urlValue'), routerQueryAccess(paramKey)),
|
|
258
|
+
]),
|
|
259
|
+
// const __nextValue = (normalized) [|| <default>]
|
|
260
|
+
types.variableDeclaration('const', [
|
|
261
|
+
types.variableDeclarator(types.identifier('__nextValue'), nextValueExpr),
|
|
262
|
+
]),
|
|
263
|
+
// setState((prev) => prev === __nextValue ? prev : __nextValue)
|
|
264
|
+
types.expressionStatement(
|
|
265
|
+
types.callExpression(types.identifier(setterName), [
|
|
266
|
+
types.arrowFunctionExpression(
|
|
267
|
+
[types.identifier('prev')],
|
|
268
|
+
types.conditionalExpression(
|
|
269
|
+
types.binaryExpression(
|
|
270
|
+
'===',
|
|
271
|
+
types.identifier('prev'),
|
|
272
|
+
types.identifier('__nextValue')
|
|
273
|
+
),
|
|
274
|
+
types.identifier('prev'),
|
|
275
|
+
types.identifier('__nextValue')
|
|
276
|
+
)
|
|
277
|
+
),
|
|
278
|
+
])
|
|
279
|
+
),
|
|
280
|
+
// <adoptedRef>.current = true — only with `adoptedRefName`
|
|
281
|
+
...(adoptedRefName
|
|
282
|
+
? [
|
|
283
|
+
types.expressionStatement(
|
|
284
|
+
types.assignmentExpression(
|
|
285
|
+
'=',
|
|
286
|
+
adoptedRefCurrent(adoptedRefName),
|
|
287
|
+
types.booleanLiteral(true)
|
|
288
|
+
)
|
|
289
|
+
),
|
|
290
|
+
]
|
|
291
|
+
: []),
|
|
292
|
+
])
|
|
293
|
+
|
|
294
|
+
return types.expressionStatement(
|
|
295
|
+
types.callExpression(types.identifier('useEffect'), [
|
|
296
|
+
types.arrowFunctionExpression([], effectBody),
|
|
297
|
+
types.arrayExpression([
|
|
298
|
+
routerQueryAccess(paramKey),
|
|
299
|
+
types.memberExpression(types.identifier('router'), types.identifier('isReady')),
|
|
300
|
+
]),
|
|
301
|
+
])
|
|
302
|
+
)
|
|
303
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import * as types from '@babel/types'
|
|
2
|
+
import type { UIDLSearchParamsDefinition } from '@teleporthq/teleport-types'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Builds the Next.js-style prelude nodes for a page that declares URL search
|
|
6
|
+
* params. Emits:
|
|
7
|
+
*
|
|
8
|
+
* const router = useRouter()
|
|
9
|
+
*
|
|
10
|
+
* The returned `registry` carries per-key defaults so `createDynamicValueExpression`
|
|
11
|
+
* can fall back to the declared default via `router?.query?.<key> ?? '<default>'`.
|
|
12
|
+
*/
|
|
13
|
+
export const buildNextJsUrlSearchParamsPrelude = (
|
|
14
|
+
searchParams: UIDLSearchParamsDefinition | undefined
|
|
15
|
+
): {
|
|
16
|
+
statements: types.Statement[]
|
|
17
|
+
registry: Record<string, { defaultValue?: string }>
|
|
18
|
+
prefixMap: Record<string, string>
|
|
19
|
+
} => {
|
|
20
|
+
if (!searchParams || searchParams.length === 0) {
|
|
21
|
+
return { statements: [], registry: {}, prefixMap: {} }
|
|
22
|
+
}
|
|
23
|
+
const registry: Record<string, { defaultValue?: string }> = {}
|
|
24
|
+
for (const entry of searchParams) {
|
|
25
|
+
registry[entry.key] = {
|
|
26
|
+
defaultValue: entry.defaultValue,
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
// const router = useRouter()
|
|
30
|
+
const routerDecl = types.variableDeclaration('const', [
|
|
31
|
+
types.variableDeclarator(
|
|
32
|
+
types.identifier('router'),
|
|
33
|
+
types.callExpression(types.identifier('useRouter'), [])
|
|
34
|
+
),
|
|
35
|
+
])
|
|
36
|
+
return {
|
|
37
|
+
statements: [routerDecl],
|
|
38
|
+
registry,
|
|
39
|
+
prefixMap: { urlSearchParams: 'router.query' },
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Builds the static-export prelude for a page that declares URL search params.
|
|
45
|
+
* Emits:
|
|
46
|
+
*
|
|
47
|
+
* const __urlSearchParams =
|
|
48
|
+
* typeof window !== 'undefined'
|
|
49
|
+
* ? Object.fromEntries(new URLSearchParams(window.location.search).entries())
|
|
50
|
+
* : {}
|
|
51
|
+
* useEffect(() => {
|
|
52
|
+
* if (typeof window === 'undefined') return
|
|
53
|
+
* const handler = () =>
|
|
54
|
+
* setUrlSearchParams(
|
|
55
|
+
* Object.fromEntries(new URLSearchParams(window.location.search).entries())
|
|
56
|
+
* )
|
|
57
|
+
* window.addEventListener('popstate', handler)
|
|
58
|
+
* return () => window.removeEventListener('popstate', handler)
|
|
59
|
+
* }, [])
|
|
60
|
+
*
|
|
61
|
+
* For simplicity the snippet stays at the "read on mount" level — the listing
|
|
62
|
+
* renders with the current query params. Implementations that want live
|
|
63
|
+
* re-filtering on popstate can subscribe via the returned hook shape.
|
|
64
|
+
*/
|
|
65
|
+
export const buildStaticUrlSearchParamsPrelude = (
|
|
66
|
+
searchParams: UIDLSearchParamsDefinition | undefined
|
|
67
|
+
): {
|
|
68
|
+
statements: types.Statement[]
|
|
69
|
+
registry: Record<string, { defaultValue?: string }>
|
|
70
|
+
prefixMap: Record<string, string>
|
|
71
|
+
} => {
|
|
72
|
+
if (!searchParams || searchParams.length === 0) {
|
|
73
|
+
return { statements: [], registry: {}, prefixMap: {} }
|
|
74
|
+
}
|
|
75
|
+
const registry: Record<string, { defaultValue?: string }> = {}
|
|
76
|
+
for (const entry of searchParams) {
|
|
77
|
+
registry[entry.key] = {
|
|
78
|
+
defaultValue: entry.defaultValue,
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
// const __urlSearchParams = typeof window !== 'undefined'
|
|
82
|
+
// ? Object.fromEntries(new URLSearchParams(window.location.search).entries())
|
|
83
|
+
// : {}
|
|
84
|
+
const decl = types.variableDeclaration('const', [
|
|
85
|
+
types.variableDeclarator(
|
|
86
|
+
types.identifier('__urlSearchParams'),
|
|
87
|
+
types.conditionalExpression(
|
|
88
|
+
types.binaryExpression(
|
|
89
|
+
'!==',
|
|
90
|
+
types.unaryExpression('typeof', types.identifier('window')),
|
|
91
|
+
types.stringLiteral('undefined')
|
|
92
|
+
),
|
|
93
|
+
types.callExpression(
|
|
94
|
+
types.memberExpression(types.identifier('Object'), types.identifier('fromEntries')),
|
|
95
|
+
[
|
|
96
|
+
types.callExpression(
|
|
97
|
+
types.memberExpression(
|
|
98
|
+
types.newExpression(types.identifier('URLSearchParams'), [
|
|
99
|
+
types.memberExpression(
|
|
100
|
+
types.memberExpression(
|
|
101
|
+
types.identifier('window'),
|
|
102
|
+
types.identifier('location')
|
|
103
|
+
),
|
|
104
|
+
types.identifier('search')
|
|
105
|
+
),
|
|
106
|
+
]),
|
|
107
|
+
types.identifier('entries')
|
|
108
|
+
),
|
|
109
|
+
[]
|
|
110
|
+
),
|
|
111
|
+
]
|
|
112
|
+
),
|
|
113
|
+
types.objectExpression([])
|
|
114
|
+
)
|
|
115
|
+
),
|
|
116
|
+
])
|
|
117
|
+
return {
|
|
118
|
+
statements: [decl],
|
|
119
|
+
registry,
|
|
120
|
+
prefixMap: { urlSearchParams: '__urlSearchParams' },
|
|
121
|
+
}
|
|
122
|
+
}
|