jinrai 1.1.2 → 1.1.3
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/index.ts +2 -0
- package/lib/bin/bin.js +8 -7
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/src/bin/agent/agent.d.ts +1 -0
- package/lib/src/bin/agent/agent.js +3 -0
- package/lib/src/bin/routes/Parser.d.ts +6 -1
- package/lib/src/bin/routes/Parser.js +5 -0
- package/lib/src/front/server-state/DataProxy.d.ts +2 -1
- package/lib/src/front/server-state/DataProxy.js +119 -60
- package/lib/src/front/server-state/SSR.d.ts +2 -0
- package/lib/src/front/server-state/SSR.js +16 -3
- package/lib/src/front/server-state/real.js +15 -1
- package/lib/src/front/server-state/serverStates.d.ts +2 -1
- package/lib/src/front/server-state/serverStates.js +19 -16
- package/lib/src/front/server-state/useServerState.d.ts +1 -1
- package/lib/src/front/server-state/useServerState.js +10 -10
- package/lib/src/front/url/params/useParamsIndex.js +2 -1
- package/lib/src/front/url/search/useSearch.js +2 -1
- package/lib/src/front/url/search/useSearchValue.d.ts +11 -5
- package/lib/src/front/url/search/useSearchValue.js +13 -8
- package/lib/src/front/wrapper/Custom.d.ts +3 -3
- package/lib/src/front/wrapper/Custom.js +14 -1
- package/lib/vite/plugin.js +15 -149
- package/package.json +5 -1
- package/src/bin/agent/agent.ts +1 -0
- package/src/bin/playwright/pageCollector.ts +0 -2
- package/src/bin/playwright/templates.ts +2 -1
- package/src/bin/routes/Parser.ts +12 -5
- package/src/front/server-state/DataProxy.ts +136 -61
- package/src/front/server-state/SSR.ts +18 -2
- package/src/front/server-state/real.ts +16 -1
- package/src/front/server-state/serverStates.ts +26 -17
- package/src/front/server-state/useServerState.ts +10 -10
- package/src/front/url/search/useSearch.ts +1 -1
- package/src/front/url/search/useSearchValue.ts +25 -13
- package/src/front/wrapper/Custom.tsx +20 -4
- package/tests/data-proxy/create-dataproxy.test.ts +116 -0
- package/tests/{custom.test.ts → parse/custom.test.ts} +2 -2
- package/tests/{parse.test.ts → parse/parse.test.ts} +7 -7
- package/vite/plugin.ts +21 -15
- /package/tests/{content → parse/content}/1.html +0 -0
- /package/tests/{content → parse/content}/1_result.json +0 -0
- /package/tests/{content → parse/content}/2.html +0 -0
- /package/tests/{content → parse/content}/2_result.json +0 -0
- /package/tests/{content → parse/content}/3.html +0 -0
- /package/tests/{content → parse/content}/3_result.json +0 -0
- /package/tests/{content → parse/content}/4.html +0 -0
- /package/tests/{content → parse/content}/4_result.json +0 -0
- /package/tests/{content → parse/content}/custom.html +0 -0
- /package/tests/{content → parse/content}/custom.json +0 -0
- /package/tests/{content → parse/content}/index.html +0 -0
- /package/tests/{content → parse/content}/index.json +0 -0
- /package/tests/{content → parse/content}/index_with_templates.json +0 -0
- /package/tests/{content → parse/content}/templates.json +0 -0
package/index.ts
CHANGED
|
@@ -6,3 +6,5 @@ export { useParamsIndex } from "./src/front/url/params/useParamsIndex"
|
|
|
6
6
|
export { useSearchValue, useSearchArray } from "./src/front/url/search/useSearchValue"
|
|
7
7
|
export { useSearch } from "./src/front/url/search/useSearch"
|
|
8
8
|
export { Custom } from "./src/front/wrapper/Custom"
|
|
9
|
+
|
|
10
|
+
export { stringifyInput } from "./src/front/server-state/SSR"
|
package/lib/bin/bin.js
CHANGED
|
@@ -28,6 +28,7 @@ const normalizeHtmlWhitespace = (html) => {
|
|
|
28
28
|
.trim();
|
|
29
29
|
};
|
|
30
30
|
|
|
31
|
+
const SPLIT = "@#UNIQ_SPLITTER#@";
|
|
31
32
|
class Parser {
|
|
32
33
|
options;
|
|
33
34
|
openVar = "{{";
|
|
@@ -95,12 +96,12 @@ class Parser {
|
|
|
95
96
|
}
|
|
96
97
|
}
|
|
97
98
|
createCustomElement(parent, value) {
|
|
98
|
-
const [
|
|
99
|
-
|
|
99
|
+
const [customProps, exampleProps, children] = value.split(SPLIT);
|
|
100
|
+
const custom = JSON.parse(customProps);
|
|
100
101
|
parent.push({
|
|
101
102
|
type: "custom",
|
|
102
|
-
name,
|
|
103
|
-
props:
|
|
103
|
+
name: custom.name,
|
|
104
|
+
props: custom.props,
|
|
104
105
|
});
|
|
105
106
|
}
|
|
106
107
|
createElement(parent, value, isVarible) {
|
|
@@ -195,14 +196,14 @@ class Task {
|
|
|
195
196
|
const pageCollector = async (page) => {
|
|
196
197
|
const state = await page.evaluate(() => {
|
|
197
198
|
const state = Object.fromEntries(window.$exportServerStates);
|
|
198
|
-
console.log("BROWSER", state);
|
|
199
199
|
return state;
|
|
200
200
|
});
|
|
201
|
-
console.log("CLIENT", state);
|
|
202
201
|
const root = await page.locator("#root").innerHTML();
|
|
203
202
|
return { root, state };
|
|
204
203
|
};
|
|
205
204
|
|
|
205
|
+
const JinraiAgent = "____JINRAI_AGENT____";
|
|
206
|
+
|
|
206
207
|
const getRawPageData = async (url, pages, test = false, debug = false) => {
|
|
207
208
|
const task = new Task();
|
|
208
209
|
task.next("Router analysis", "yellow", spinners.dotsCircle);
|
|
@@ -210,7 +211,7 @@ const getRawPageData = async (url, pages, test = false, debug = false) => {
|
|
|
210
211
|
const browser = await chromium.launch({ headless: !debug, devtools: true, channel: "chrome" });
|
|
211
212
|
// const test_browser = await chromium.launch({ headless: true, channel: "chrome" })
|
|
212
213
|
const context = await browser.newContext({
|
|
213
|
-
userAgent:
|
|
214
|
+
userAgent: JinraiAgent,
|
|
214
215
|
locale: "ru-RU",
|
|
215
216
|
});
|
|
216
217
|
for await (const [id, mask] of pages.entries()) {
|
package/lib/index.d.ts
CHANGED
|
@@ -5,3 +5,4 @@ export { useParamsIndex } from "./src/front/url/params/useParamsIndex";
|
|
|
5
5
|
export { useSearchValue, useSearchArray } from "./src/front/url/search/useSearchValue";
|
|
6
6
|
export { useSearch } from "./src/front/url/search/useSearch";
|
|
7
7
|
export { Custom } from "./src/front/wrapper/Custom";
|
|
8
|
+
export { stringifyInput } from "./src/front/server-state/SSR";
|
package/lib/index.js
CHANGED
|
@@ -4,3 +4,4 @@ export { useParamsIndex } from './src/front/url/params/useParamsIndex.js';
|
|
|
4
4
|
export { useSearchArray, useSearchValue } from './src/front/url/search/useSearchValue.js';
|
|
5
5
|
export { useSearch } from './src/front/url/search/useSearch.js';
|
|
6
6
|
export { Custom } from './src/front/wrapper/Custom.js';
|
|
7
|
+
export { stringifyInput } from './src/front/server-state/SSR.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const JinraiAgent = "____JINRAI_AGENT____";
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
export declare const SPLIT = "@#UNIQ_SPLITTER#@";
|
|
2
|
+
interface CustomElement {
|
|
3
|
+
name: string;
|
|
4
|
+
props: object;
|
|
5
|
+
}
|
|
1
6
|
interface ParserOptions {
|
|
2
7
|
templates?: boolean;
|
|
3
8
|
normalize?: boolean;
|
|
@@ -19,7 +24,7 @@ interface ValueElement {
|
|
|
19
24
|
interface CustomElement {
|
|
20
25
|
type: "custom";
|
|
21
26
|
name: string;
|
|
22
|
-
props:
|
|
27
|
+
props: object;
|
|
23
28
|
}
|
|
24
29
|
export declare class Parser {
|
|
25
30
|
options?: ParserOptions;
|
|
@@ -4,5 +4,6 @@ export interface DataProxy {
|
|
|
4
4
|
getValue: () => any;
|
|
5
5
|
}
|
|
6
6
|
export declare const sources: Map<string, any>;
|
|
7
|
-
|
|
7
|
+
type WithDataProxy<T> = T & DataProxy;
|
|
8
|
+
declare function createDataProxy<T>(data: T, path?: string): WithDataProxy<T>;
|
|
8
9
|
export default createDataProxy;
|
|
@@ -13,92 +13,151 @@ const getTarget = (data, path) => {
|
|
|
13
13
|
case "undefined":
|
|
14
14
|
case "symbol":
|
|
15
15
|
// эти типы можно просто завернуть
|
|
16
|
-
return {
|
|
16
|
+
return { $__ROOT__: data };
|
|
17
17
|
default:
|
|
18
18
|
return () => `{{${path}}}`;
|
|
19
19
|
}
|
|
20
20
|
};
|
|
21
|
-
|
|
21
|
+
function createDataProxy(data, path = "") {
|
|
22
22
|
if (path.endsWith("@"))
|
|
23
23
|
sources.set(path.slice(0, -1), data);
|
|
24
24
|
return new Proxy(getTarget(data, path), {
|
|
25
|
-
get: (
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
get: (_target, prop) => {
|
|
26
|
+
if (ssr.exportToJV) {
|
|
27
|
+
return {
|
|
28
|
+
$JV: {
|
|
29
|
+
key: path + "/" + String(prop),
|
|
30
|
+
type: "proxy",
|
|
31
|
+
def: data,
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
// ---------------------------
|
|
36
|
+
// 1. Обработка символов
|
|
37
|
+
// ---------------------------
|
|
38
|
+
if (typeof prop === "symbol") {
|
|
29
39
|
switch (prop) {
|
|
30
|
-
// @ts-ignore
|
|
31
40
|
case Symbol.toPrimitive:
|
|
32
41
|
return (hint) => {
|
|
33
42
|
console.log("PROXYDATA", hint);
|
|
34
43
|
return `{{${path}}}`;
|
|
35
44
|
};
|
|
36
|
-
// @ts-ignore
|
|
37
45
|
case Symbol.toStringTag:
|
|
38
46
|
return "Object";
|
|
39
|
-
// @ts-ignore
|
|
40
47
|
case Symbol.iterator:
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
48
|
+
if (typeof data === "object" && data !== null && Symbol.iterator in data) {
|
|
49
|
+
return data[Symbol.iterator];
|
|
50
|
+
}
|
|
44
51
|
return undefined;
|
|
45
|
-
case "_debugInfo":
|
|
46
|
-
return {
|
|
47
|
-
note: `State From Request (${path})`,
|
|
48
|
-
kind: typeof data,
|
|
49
|
-
timestamp: Date.now(),
|
|
50
|
-
preview: data,
|
|
51
|
-
};
|
|
52
52
|
}
|
|
53
|
-
|
|
54
|
-
|
|
53
|
+
}
|
|
54
|
+
// ---------------------------
|
|
55
|
+
// 2. DEV tools
|
|
56
|
+
// ---------------------------
|
|
57
|
+
if (!(typeof data === "object" && data !== null && prop in data)) {
|
|
58
|
+
if (prop === "$$typeof" || prop === "type") {
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
if (prop === "_debugInfo") {
|
|
62
|
+
return {
|
|
63
|
+
note: `State From Request (${path})`,
|
|
64
|
+
kind: typeof data,
|
|
65
|
+
timestamp: Date.now(),
|
|
66
|
+
preview: data,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
// ---------------------------
|
|
71
|
+
// 3. SELF: $key → returns function
|
|
72
|
+
// ---------------------------
|
|
73
|
+
if (typeof prop === "string" && prop.startsWith("$")) {
|
|
55
74
|
return (key) => `{{${path + "/" + key}${"\\" + prop}}}`;
|
|
56
|
-
|
|
75
|
+
}
|
|
76
|
+
// ---------------------------
|
|
77
|
+
// 4. Types special cases
|
|
78
|
+
// ---------------------------
|
|
57
79
|
switch (typeof data) {
|
|
58
80
|
case "string":
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
return undefined;
|
|
63
|
-
}
|
|
81
|
+
if (prop === "length" || prop === "entries")
|
|
82
|
+
return undefined;
|
|
83
|
+
break;
|
|
64
84
|
case "number":
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
85
|
+
if (prop === "@@iterator")
|
|
86
|
+
return undefined;
|
|
87
|
+
break;
|
|
69
88
|
default:
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
return undefined;
|
|
73
|
-
}
|
|
89
|
+
if (prop === "then")
|
|
90
|
+
return undefined;
|
|
74
91
|
}
|
|
75
|
-
//
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
92
|
+
// ---------------------------
|
|
93
|
+
// 5. Array-like handlers
|
|
94
|
+
// ---------------------------
|
|
95
|
+
if (prop === "find") {
|
|
96
|
+
return data[prop];
|
|
97
|
+
}
|
|
98
|
+
if (prop === "length") {
|
|
99
|
+
if (Array.isArray(data)) {
|
|
100
|
+
return data.length;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (prop === "map" || prop === "forEach") {
|
|
104
|
+
return (callback) => React.createElement("loopwrapper", null, [
|
|
105
|
+
`ArrayDataKey=${path}|`,
|
|
106
|
+
Object.entries(data)
|
|
107
|
+
.slice(0, 1)
|
|
108
|
+
.map(([key, itm]) => callback(createDataProxy(itm, `${path}/[ITEM=${key}]`))),
|
|
109
|
+
]);
|
|
110
|
+
}
|
|
111
|
+
// ---------------------------
|
|
112
|
+
// 6. getValue
|
|
113
|
+
// ---------------------------
|
|
114
|
+
if (prop === "getValue") {
|
|
115
|
+
return () => data;
|
|
99
116
|
}
|
|
117
|
+
// ---------------------------
|
|
118
|
+
// 7. toJSON
|
|
119
|
+
// ---------------------------
|
|
120
|
+
if (prop === "toJSON") {
|
|
121
|
+
return () => {
|
|
122
|
+
console.log("dataproxy toJSON", path, data);
|
|
123
|
+
return ssr.exportParams
|
|
124
|
+
? `{{${path}}}`
|
|
125
|
+
: {
|
|
126
|
+
$JV: {
|
|
127
|
+
key: path,
|
|
128
|
+
type: "proxy",
|
|
129
|
+
def: data,
|
|
130
|
+
separator: "",
|
|
131
|
+
},
|
|
132
|
+
};
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
// ---------------------------
|
|
136
|
+
// 8. Nested object
|
|
137
|
+
// ---------------------------
|
|
138
|
+
const value = data[prop];
|
|
139
|
+
if (!ssr.exportParams) {
|
|
140
|
+
return value;
|
|
141
|
+
}
|
|
142
|
+
if (value && (typeof value === "object" || Array.isArray(value))) {
|
|
143
|
+
return createDataProxy(value, path + "/" + String(prop));
|
|
144
|
+
}
|
|
145
|
+
// if (ssr.exportParams) {
|
|
146
|
+
// if (typeof value == "string") {
|
|
147
|
+
// const key = `{{${path + "/" + String(prop)}}}`
|
|
148
|
+
// const jv = getJinraiValue(key, "proxyValue", "", value)
|
|
149
|
+
// return value.bindSource(jv)
|
|
150
|
+
// }
|
|
151
|
+
// return key
|
|
152
|
+
// } else {
|
|
153
|
+
// return value
|
|
154
|
+
// }
|
|
155
|
+
// ---------------------------
|
|
156
|
+
// 9. Final primitive fallback
|
|
157
|
+
// ---------------------------
|
|
158
|
+
return `{{${path + "/" + String(prop)}}}`;
|
|
100
159
|
},
|
|
101
160
|
});
|
|
102
|
-
}
|
|
161
|
+
}
|
|
103
162
|
|
|
104
163
|
export { createDataProxy as default, sources };
|
|
@@ -1,6 +1,19 @@
|
|
|
1
|
+
import { JinraiAgent } from '../../bin/agent/agent.js';
|
|
2
|
+
|
|
1
3
|
const ssr = {
|
|
2
|
-
current:
|
|
3
|
-
exportParams:
|
|
4
|
+
current: navigator.userAgent == JinraiAgent,
|
|
5
|
+
exportParams: true,
|
|
6
|
+
exportToJV: false,
|
|
7
|
+
};
|
|
8
|
+
if (window != undefined) {
|
|
9
|
+
// @ts-ignore
|
|
10
|
+
window.__ssr = ssr;
|
|
11
|
+
}
|
|
12
|
+
const stringifyInput = (input) => {
|
|
13
|
+
ssr.exportParams = false;
|
|
14
|
+
const result = JSON.stringify(input);
|
|
15
|
+
ssr.exportParams = true;
|
|
16
|
+
return result;
|
|
4
17
|
};
|
|
5
18
|
|
|
6
|
-
export { ssr };
|
|
19
|
+
export { ssr, stringifyInput };
|
|
@@ -1,11 +1,19 @@
|
|
|
1
|
+
import { ssr } from './SSR.js';
|
|
1
2
|
import { sources } from './DataProxy.js';
|
|
3
|
+
import { getJinraiValue } from '../url/search/useSearchValue.js';
|
|
2
4
|
|
|
3
5
|
function real(value) {
|
|
6
|
+
if (!ssr.current)
|
|
7
|
+
return value;
|
|
4
8
|
switch (typeof value) {
|
|
5
9
|
case "number":
|
|
6
10
|
return value;
|
|
7
11
|
case "string":
|
|
8
|
-
|
|
12
|
+
if (value.startsWith("{{") && value.endsWith("}}")) {
|
|
13
|
+
const result = getArrayByPath(value.slice(2, -2));
|
|
14
|
+
return wrapSource(result, getJinraiValue(value, "proxy", "", result));
|
|
15
|
+
}
|
|
16
|
+
return value;
|
|
9
17
|
case "object":
|
|
10
18
|
// @ts-ignore
|
|
11
19
|
if (value && typeof value.getValue === "function") {
|
|
@@ -17,6 +25,12 @@ function real(value) {
|
|
|
17
25
|
return value;
|
|
18
26
|
}
|
|
19
27
|
}
|
|
28
|
+
const wrapSource = (value, source) => {
|
|
29
|
+
if (typeof value == "string") {
|
|
30
|
+
return value.bindSource(source);
|
|
31
|
+
}
|
|
32
|
+
return value;
|
|
33
|
+
};
|
|
20
34
|
const getArrayByPath = (path) => {
|
|
21
35
|
const [sourceIndex, requestPath] = path.split("@", 2);
|
|
22
36
|
const keys = requestPath.split("/");
|
|
@@ -11,8 +11,9 @@ export type ServerValue = {
|
|
|
11
11
|
request?: Request;
|
|
12
12
|
};
|
|
13
13
|
};
|
|
14
|
+
key: ServerKey;
|
|
14
15
|
};
|
|
15
16
|
export declare const serverStates: Map<string, ServerValue>;
|
|
16
17
|
export declare const getServerValue: (key?: ServerKey, def?: ServerValue["value"], options?: ServerValue["options"]) => any[];
|
|
17
|
-
export declare const setServerValue: (key: ServerKey, value: ServerValue["value"], options?: ServerValue["options"]) =>
|
|
18
|
+
export declare const setServerValue: (key: ServerKey, value: ServerValue["value"], options?: ServerValue["options"]) => any;
|
|
18
19
|
export {};
|
|
@@ -1,7 +1,16 @@
|
|
|
1
|
-
import { encode } from 'js-base64';
|
|
2
1
|
import createDataProxy from './DataProxy.js';
|
|
3
|
-
import { ssr } from './SSR.js';
|
|
4
2
|
|
|
3
|
+
// @ts-ignore
|
|
4
|
+
const initialState = { ...(window?.__appc__?.state ?? {}) };
|
|
5
|
+
// @ts-ignore
|
|
6
|
+
const serverErrors = [...(window?.__appc__?.errors ?? [])];
|
|
7
|
+
if (serverErrors.length) {
|
|
8
|
+
console.error("SERVER:", serverErrors);
|
|
9
|
+
}
|
|
10
|
+
if (window != undefined) {
|
|
11
|
+
// @ts-ignore
|
|
12
|
+
delete window.__appc__;
|
|
13
|
+
}
|
|
5
14
|
const serverStates = new Map();
|
|
6
15
|
if (window != undefined) {
|
|
7
16
|
console.log("init $exportServerStates");
|
|
@@ -9,31 +18,25 @@ if (window != undefined) {
|
|
|
9
18
|
window.$exportServerStates = serverStates;
|
|
10
19
|
}
|
|
11
20
|
const getIdent = (key) => {
|
|
12
|
-
|
|
13
|
-
ssr.exportParams = true;
|
|
14
|
-
}
|
|
15
|
-
const result = Array.isArray(key) ? JSON.stringify(key) : key.toString();
|
|
16
|
-
{
|
|
17
|
-
ssr.exportParams = false;
|
|
18
|
-
}
|
|
19
|
-
return encode(result);
|
|
21
|
+
return Array.isArray(key) ? key.join("-") : key;
|
|
20
22
|
};
|
|
21
23
|
const getServerValue = (key, def, options) => {
|
|
22
24
|
if (key == undefined) {
|
|
23
25
|
return [def, false];
|
|
24
26
|
}
|
|
25
27
|
const ident = getIdent(key);
|
|
26
|
-
serverStates.set(ident, { options, value: !options?.source ? def : undefined });
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
//
|
|
30
|
-
|
|
28
|
+
serverStates.set(ident, { options, value: !options?.source ? def : undefined, key });
|
|
29
|
+
if (ident in initialState) {
|
|
30
|
+
const result = initialState[ident];
|
|
31
|
+
// delete initialState[ident]
|
|
32
|
+
console.log("HAS", ident, result);
|
|
33
|
+
return ["data" in result ? result.data : result, true];
|
|
31
34
|
}
|
|
32
35
|
return [def, false];
|
|
33
36
|
};
|
|
34
37
|
const setServerValue = (key, value, options) => {
|
|
35
38
|
const ident = getIdent(key);
|
|
36
|
-
serverStates.set(ident, { options, value: !options?.source ? value : undefined });
|
|
39
|
+
serverStates.set(ident, { options, value: !options?.source ? value : undefined, key });
|
|
37
40
|
return createDataProxy(value, `${ident}@`);
|
|
38
41
|
};
|
|
39
42
|
|
|
@@ -2,4 +2,4 @@ import { Dispatch, SetStateAction } from "react";
|
|
|
2
2
|
import { ServerValue } from "./serverStates";
|
|
3
3
|
export type ServerStateMap = Record<string, ServerValue>;
|
|
4
4
|
export type ServerKey = string | string[];
|
|
5
|
-
export declare const useServerState: <T extends ServerValue["value"]>(serverKey: ServerKey | undefined, initialValue: T, options?: ServerValue["options"]) => [T, Dispatch<SetStateAction<T>>,
|
|
5
|
+
export declare const useServerState: <T extends ServerValue["value"]>(serverKey: ServerKey | undefined, initialValue: T, options?: ServerValue["options"]) => [T, Dispatch<SetStateAction<T>>, boolean];
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { useState } from 'react';
|
|
1
|
+
import { useState, useRef } from 'react';
|
|
2
2
|
import { ssr } from './SSR.js';
|
|
3
3
|
import { getServerValue, setServerValue } from './serverStates.js';
|
|
4
4
|
|
|
5
5
|
const useServerState = (serverKey, initialValue, options) => {
|
|
6
6
|
const [serverValue, isInitOnServer] = getServerValue(serverKey, initialValue, options);
|
|
7
7
|
const [value, setStateValue] = useState(serverValue);
|
|
8
|
-
|
|
8
|
+
useRef(isInitOnServer);
|
|
9
9
|
const setValue = (value) => {
|
|
10
10
|
setStateValue((prev) => {
|
|
11
11
|
const result = value instanceof Function ? value(prev) : value;
|
|
@@ -15,14 +15,14 @@ const useServerState = (serverKey, initialValue, options) => {
|
|
|
15
15
|
return result;
|
|
16
16
|
});
|
|
17
17
|
};
|
|
18
|
-
const initOnServer = () => {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
return [value, setValue,
|
|
18
|
+
// const initOnServer = () => {
|
|
19
|
+
// if (isInit.current) {
|
|
20
|
+
// isInit.current = false
|
|
21
|
+
// return true
|
|
22
|
+
// }
|
|
23
|
+
// return false
|
|
24
|
+
// }
|
|
25
|
+
return [value, setValue, isInitOnServer];
|
|
26
26
|
};
|
|
27
27
|
|
|
28
28
|
export { useServerState };
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { useContext, useMemo } from 'react';
|
|
2
2
|
import { getJinraiValue } from '../search/useSearchValue.js';
|
|
3
3
|
import { JinraiContext } from '../JinraiContext.js';
|
|
4
|
+
import { ssr } from '../../server-state/SSR.js';
|
|
4
5
|
|
|
5
6
|
const useParamsIndex = (index, def = "") => {
|
|
6
7
|
const { deps } = useContext(JinraiContext);
|
|
7
8
|
const value = useMemo(() => location.pathname.split("/")[index + 1] ?? def, deps ? deps : []);
|
|
8
9
|
const stableValue = useMemo(() => {
|
|
9
|
-
return value.bindSource(getJinraiValue(index.toString(), "paramsIndex", "", def)) ;
|
|
10
|
+
return ssr.current ? value.bindSource(getJinraiValue(index.toString(), "paramsIndex", "", def)) : value;
|
|
10
11
|
}, [value]);
|
|
11
12
|
return stableValue;
|
|
12
13
|
};
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { useContext, useMemo } from 'react';
|
|
2
2
|
import { JinraiContext } from '../JinraiContext.js';
|
|
3
|
+
import { ssr } from '../../server-state/SSR.js';
|
|
3
4
|
import { getJinraiValue } from './useSearchValue.js';
|
|
4
5
|
|
|
5
6
|
const useSearch = () => {
|
|
6
7
|
const { deps } = useContext(JinraiContext);
|
|
7
8
|
const value = useMemo(() => location.search.substring(1), deps ? deps : []);
|
|
8
9
|
const stableValue = useMemo(() => {
|
|
9
|
-
return value.bindSource(getJinraiValue("", "
|
|
10
|
+
return ssr.current ? value.bindSource(getJinraiValue("", "searchFull", "", "")) : value;
|
|
10
11
|
}, [value]);
|
|
11
12
|
return stableValue;
|
|
12
13
|
};
|
|
@@ -1,16 +1,22 @@
|
|
|
1
1
|
import { UseQueryStateReturn } from "nuqs";
|
|
2
|
+
export interface JinraiValue {
|
|
3
|
+
key: string;
|
|
4
|
+
type: "searchArray" | "searchString" | "proxy" | "searchFull" | "paramsIndex";
|
|
5
|
+
separator: string;
|
|
6
|
+
def: any;
|
|
7
|
+
}
|
|
2
8
|
declare global {
|
|
3
9
|
interface String {
|
|
4
|
-
|
|
10
|
+
$JV?: JinraiValue;
|
|
5
11
|
toJSON: () => string;
|
|
6
|
-
bindSource: (source:
|
|
12
|
+
bindSource: (source: JinraiValue) => string;
|
|
7
13
|
}
|
|
8
14
|
interface Array<T> {
|
|
9
|
-
|
|
15
|
+
$JV?: JinraiValue;
|
|
10
16
|
toJSON(): any;
|
|
11
|
-
bindSource(source:
|
|
17
|
+
bindSource(source: JinraiValue): T[];
|
|
12
18
|
}
|
|
13
19
|
}
|
|
14
20
|
export declare const useSearchValue: (key: string, defaultValue: string) => UseQueryStateReturn<string, string>;
|
|
15
21
|
export declare const useSearchArray: (key: string, defaultValue?: string[], separator?: string) => UseQueryStateReturn<string[], string[]>;
|
|
16
|
-
export declare const getJinraiValue: (key: string, type:
|
|
22
|
+
export declare const getJinraiValue: (key: string, type: JinraiValue["type"], separator: string, def: any) => JinraiValue;
|
|
@@ -5,19 +5,19 @@ import { ssr } from '../../server-state/SSR.js';
|
|
|
5
5
|
function toJSON() {
|
|
6
6
|
if (ssr.exportParams) {
|
|
7
7
|
// @ts-ignore
|
|
8
|
-
const
|
|
8
|
+
const $JV = this.$JV;
|
|
9
9
|
// @ts-ignore
|
|
10
|
-
return
|
|
10
|
+
return $JV ? { $JV } : this;
|
|
11
11
|
}
|
|
12
12
|
// @ts-ignore
|
|
13
13
|
return this;
|
|
14
14
|
}
|
|
15
15
|
{
|
|
16
|
-
String.prototype
|
|
16
|
+
String.prototype.$JV = undefined;
|
|
17
17
|
String.prototype.toJSON = toJSON;
|
|
18
18
|
String.prototype.bindSource = function (source) {
|
|
19
19
|
const result = new String(this);
|
|
20
|
-
result
|
|
20
|
+
result.$JV = source;
|
|
21
21
|
return result;
|
|
22
22
|
};
|
|
23
23
|
if (!Array.prototype.toJSON) {
|
|
@@ -25,7 +25,7 @@ function toJSON() {
|
|
|
25
25
|
}
|
|
26
26
|
if (!Array.prototype.bindSource) {
|
|
27
27
|
Array.prototype.bindSource = function (source) {
|
|
28
|
-
this
|
|
28
|
+
this.$JV = source;
|
|
29
29
|
return this;
|
|
30
30
|
};
|
|
31
31
|
}
|
|
@@ -33,7 +33,7 @@ function toJSON() {
|
|
|
33
33
|
const useSearchValue = (key, defaultValue) => {
|
|
34
34
|
const [value, setValue] = useQueryState(key, { defaultValue });
|
|
35
35
|
const stableValue = useMemo(() => {
|
|
36
|
-
return value.bindSource(getJinraiValue(key, "searchString", "", defaultValue)) ;
|
|
36
|
+
return ssr.current ? value.bindSource(getJinraiValue(key, "searchString", "", defaultValue)) : value;
|
|
37
37
|
}, [key, value]);
|
|
38
38
|
return [stableValue, setValue];
|
|
39
39
|
};
|
|
@@ -41,12 +41,17 @@ const useSearchArray = (key, defaultValue = [], separator = ",") => {
|
|
|
41
41
|
const stableDefault = useMemo(() => defaultValue, []);
|
|
42
42
|
const [value, setValue] = useQueryState(key, parseAsArrayOf(parseAsString, separator).withDefault(stableDefault));
|
|
43
43
|
const stableValue = useMemo(() => {
|
|
44
|
-
return value.bindSource(getJinraiValue(key, "searchArray", separator, defaultValue)) ;
|
|
44
|
+
return ssr.current ? value.bindSource(getJinraiValue(key, "searchArray", separator, defaultValue)) : value;
|
|
45
45
|
}, [key, value]);
|
|
46
46
|
return [stableValue, setValue];
|
|
47
47
|
};
|
|
48
48
|
const getJinraiValue = (key, type, separator, def) => {
|
|
49
|
-
return
|
|
49
|
+
return {
|
|
50
|
+
key,
|
|
51
|
+
type,
|
|
52
|
+
separator,
|
|
53
|
+
def,
|
|
54
|
+
};
|
|
50
55
|
};
|
|
51
56
|
|
|
52
57
|
export { getJinraiValue, useSearchArray, useSearchValue };
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
2
|
interface CustomProps {
|
|
3
3
|
name: string;
|
|
4
|
-
props: object;
|
|
4
|
+
props: () => object;
|
|
5
5
|
children: ReactNode;
|
|
6
6
|
}
|
|
7
|
-
export declare const Custom: ({ name, props, children }: CustomProps) =>
|
|
7
|
+
export declare const Custom: ({ name, props, children }: CustomProps) => import("react/jsx-runtime").JSX.Element;
|
|
8
8
|
export {};
|