jinrai 1.1.2 → 1.1.4

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.
Files changed (85) hide show
  1. package/front.config.json +2 -1
  2. package/index.ts +4 -1
  3. package/lib/bin/bin.js +123 -59
  4. package/lib/index.d.ts +3 -1
  5. package/lib/index.js +3 -1
  6. package/lib/src/bin/agent/agent.d.ts +2 -0
  7. package/lib/src/bin/agent/agent.js +4 -0
  8. package/lib/src/bin/playwright/pageCollector.d.ts +2 -0
  9. package/lib/src/bin/playwright/pageTestCollector.d.ts +6 -0
  10. package/lib/src/bin/playwright/templates.d.ts +6 -1
  11. package/lib/src/bin/routes/Parser.d.ts +25 -2
  12. package/lib/src/bin/routes/Parser.js +5 -0
  13. package/lib/src/bin/routes/getRoutes.d.ts +1 -0
  14. package/lib/src/front/server/useIsServer.d.ts +1 -0
  15. package/lib/src/front/server/useIsServer.js +7 -0
  16. package/lib/src/front/server-state/DataProxy.d.ts +2 -1
  17. package/lib/src/front/server-state/DataProxy.js +122 -60
  18. package/lib/src/front/server-state/SSR.d.ts +3 -0
  19. package/lib/src/front/server-state/SSR.js +18 -3
  20. package/lib/src/front/server-state/orig.d.ts +2 -0
  21. package/lib/src/front/server-state/{real.js → orig.js} +18 -3
  22. package/lib/src/front/server-state/serverStates.d.ts +3 -1
  23. package/lib/src/front/server-state/serverStates.js +24 -17
  24. package/lib/src/front/server-state/testState.d.ts +3 -0
  25. package/lib/src/front/server-state/testState.js +14 -0
  26. package/lib/src/front/server-state/useServerState.d.ts +1 -1
  27. package/lib/src/front/server-state/useServerState.js +5 -9
  28. package/lib/src/front/translate/TranslateConfig.d.ts +21 -0
  29. package/lib/src/front/translate/TranslateConfig.js +108 -0
  30. package/lib/src/front/url/JinraiContext.d.ts +1 -0
  31. package/lib/src/front/url/JinraiContext.js +1 -0
  32. package/lib/src/front/url/adapter/def.js +1 -1
  33. package/lib/src/front/url/adapter/rrd6.js +2 -2
  34. package/lib/src/front/url/adapter/rrd7.js +2 -2
  35. package/lib/src/front/url/params/useParamsIndex.js +2 -1
  36. package/lib/src/front/url/search/useSearch.js +4 -4
  37. package/lib/src/front/url/search/useSearchValue.d.ts +11 -5
  38. package/lib/src/front/url/search/useSearchValue.js +13 -8
  39. package/lib/src/front/wrapper/Custom.d.ts +3 -3
  40. package/lib/src/front/wrapper/Custom.js +18 -1
  41. package/lib/vite/plugin.js +26 -154
  42. package/package.json +9 -1
  43. package/rollup.config.mjs +2 -1
  44. package/src/bin/agent/agent.ts +2 -0
  45. package/src/bin/build/build.ts +23 -10
  46. package/src/bin/playwright/pageCollector.ts +8 -6
  47. package/src/bin/playwright/pageTestCollector.ts +15 -0
  48. package/src/bin/playwright/templates.ts +16 -5
  49. package/src/bin/routes/Parser.ts +100 -32
  50. package/src/bin/routes/getRoutes.ts +5 -1
  51. package/src/front/server/useIsServer.ts +5 -0
  52. package/src/front/server-state/DataProxy.ts +140 -61
  53. package/src/front/server-state/SSR.ts +22 -2
  54. package/src/front/server-state/{real.ts → orig.ts} +19 -2
  55. package/src/front/server-state/serverStates.ts +33 -18
  56. package/src/front/server-state/testState.ts +15 -0
  57. package/src/front/server-state/useServerState.ts +6 -11
  58. package/src/front/translate/TranslateConfig.tsx +153 -0
  59. package/src/front/url/JinraiContext.tsx +2 -0
  60. package/src/front/url/adapter/def.tsx +1 -1
  61. package/src/front/url/adapter/rrd6.tsx +2 -3
  62. package/src/front/url/adapter/rrd7.tsx +2 -2
  63. package/src/front/url/search/useSearch.ts +3 -4
  64. package/src/front/url/search/useSearchValue.ts +25 -13
  65. package/src/front/wrapper/Custom.tsx +28 -4
  66. package/tests/data-proxy/create-dataproxy.test.ts +116 -0
  67. package/tests/{custom.test.ts → parse/custom.test.ts} +2 -2
  68. package/tests/{parse.test.ts → parse/parse.test.ts} +7 -7
  69. package/tsconfig.types.json +1 -0
  70. package/vite/plugin.ts +40 -22
  71. package/lib/src/front/server-state/real.d.ts +0 -1
  72. /package/tests/{content → parse/content}/1.html +0 -0
  73. /package/tests/{content → parse/content}/1_result.json +0 -0
  74. /package/tests/{content → parse/content}/2.html +0 -0
  75. /package/tests/{content → parse/content}/2_result.json +0 -0
  76. /package/tests/{content → parse/content}/3.html +0 -0
  77. /package/tests/{content → parse/content}/3_result.json +0 -0
  78. /package/tests/{content → parse/content}/4.html +0 -0
  79. /package/tests/{content → parse/content}/4_result.json +0 -0
  80. /package/tests/{content → parse/content}/custom.html +0 -0
  81. /package/tests/{content → parse/content}/custom.json +0 -0
  82. /package/tests/{content → parse/content}/index.html +0 -0
  83. /package/tests/{content → parse/content}/index.json +0 -0
  84. /package/tests/{content → parse/content}/index_with_templates.json +0 -0
  85. /package/tests/{content → parse/content}/templates.json +0 -0
@@ -1,12 +1,19 @@
1
1
  import { normalizeHtmlWhitespace } from "../content/normalizeContent"
2
2
  import { createHash } from "node:crypto"
3
3
 
4
+ export const SPLIT = "@#UNIQ_SPLITTER#@"
5
+
6
+ interface CustomElement {
7
+ name: string
8
+ props: object
9
+ }
10
+
4
11
  interface ParserOptions {
5
12
  templates?: boolean
6
13
  normalize?: boolean
7
14
  }
8
15
 
9
- export type Element = ArrayElement | HtmlElement | ValueElement | CustomElement
16
+ export type Element = ArrayElement | HtmlElement | ValueElement | CustomElement | TranslateText | TValueElement
10
17
 
11
18
  interface ArrayElement {
12
19
  type: "array"
@@ -23,11 +30,26 @@ interface ValueElement {
23
30
  type: "value"
24
31
  key: string
25
32
  }
33
+ interface TValueElement {
34
+ type: "tvalue"
35
+ value: string
36
+ }
37
+
38
+ interface TranslateText {
39
+ type: "t"
40
+ text: string
41
+ }
26
42
 
27
43
  interface CustomElement {
28
44
  type: "custom"
29
45
  name: string
30
- props: string
46
+ props: object
47
+ }
48
+
49
+ export interface CustomExample {
50
+ name: string
51
+ html: string
52
+ props: any
31
53
  }
32
54
 
33
55
  export class Parser {
@@ -35,12 +57,16 @@ export class Parser {
35
57
 
36
58
  openVar = "{{"
37
59
  createVar = "}}"
60
+ openTVar = "{!"
61
+ createTVar = "!}"
62
+
38
63
  createArray = "</loopwrapper"
39
64
  createCustom = "</custom"
40
65
  deepUp = "<loopwrapper"
41
66
  deepUp2 = "<custom"
42
67
 
43
68
  templates: Record<string, string> = {}
69
+ custom: CustomExample[] = []
44
70
 
45
71
  constructor(options?: ParserOptions) {
46
72
  this.options = options
@@ -56,9 +82,10 @@ export class Parser {
56
82
  let match
57
83
  let deep = 0
58
84
  let lastIndex = 0
85
+ let translate = false
59
86
 
60
87
  const tagPattern = new RegExp(
61
- `(<loopwrapper(\\s+[^>]*)?>|</loopwrapper>|\{\{|\}\}|<custom(\\s+[^>]*)?>|</custom>)`,
88
+ `(<loopwrapper(\\s+[^>]*)?>|</loopwrapper>|\{\{|\}\}|\{\!|\!\}|<custom(\\s+[^>]*)?>|</custom>)`,
62
89
  "gi",
63
90
  )
64
91
 
@@ -66,28 +93,42 @@ export class Parser {
66
93
  const currentTag = match[0]
67
94
  const value = content.substring(lastIndex, match.index)
68
95
 
69
- if (currentTag.startsWith(this.createArray)) {
70
- deep--
71
- if (deep > 0) continue
72
- this.createElement(tree, value)
73
- } else if (currentTag.startsWith(this.deepUp)) {
74
- deep++
75
- if (deep > 1) continue
76
- this.createElement(tree, value)
77
- } else if (currentTag.startsWith(this.createCustom)) {
78
- deep--
79
- if (deep != 0) continue
80
- this.createCustomElement(tree, value)
81
- } else if (currentTag.startsWith(this.deepUp2)) {
82
- deep++
83
- if (deep > 1) continue
84
- this.createElement(tree, value)
85
- } else if (currentTag == this.createVar) {
86
- if (deep != 0) continue
87
- this.createElement(tree, value, true)
96
+ if (translate) {
97
+ if (currentTag.startsWith(this.createTVar)) {
98
+ translate = false
99
+ this.createTranslate(tree, value)
100
+ } else {
101
+ continue
102
+ }
88
103
  } else {
89
- if (deep != 0) continue
90
- this.createElement(tree, value)
104
+ if (currentTag.startsWith(this.createArray)) {
105
+ deep--
106
+ if (deep > 0) continue
107
+ this.createElement(tree, value)
108
+ } else if (currentTag.startsWith(this.deepUp)) {
109
+ deep++
110
+ if (deep > 1) continue
111
+ this.createElement(tree, value)
112
+ } else if (currentTag.startsWith(this.createCustom)) {
113
+ deep--
114
+ if (deep != 0) continue
115
+ this.createCustomElement(tree, value)
116
+ } else if (currentTag.startsWith(this.deepUp2)) {
117
+ deep++
118
+ if (deep > 1) continue
119
+ this.createElement(tree, value)
120
+ } else if (currentTag.startsWith(this.openTVar)) {
121
+ ////////////// >>>>
122
+ if (deep != 0) continue
123
+ translate = true
124
+ this.createElement(tree, value)
125
+ } else if (currentTag == this.createVar) {
126
+ if (deep != 0) continue
127
+ this.createElement(tree, value, true)
128
+ } else {
129
+ if (deep != 0) continue
130
+ this.createElement(tree, value)
131
+ }
91
132
  }
92
133
 
93
134
  lastIndex = match.index + currentTag.length
@@ -100,22 +141,49 @@ export class Parser {
100
141
  }
101
142
 
102
143
  createCustomElement(parent: Element[], value: string) {
103
- const [name, ...props] = value.trimStart().split("|")
104
- value = props.join("|")
144
+ const [customProps, exampleProps, children] = value.split(SPLIT)
145
+ const custom = JSON.parse(customProps) as CustomElement
146
+
147
+ if (this.custom.find(itm => itm.name == custom.name) == undefined) {
148
+ this.custom.push({
149
+ name: custom.name,
150
+ html: children,
151
+ props: exampleProps,
152
+ })
153
+ }
105
154
 
106
155
  parent.push({
107
156
  type: "custom",
108
- name,
109
- props: value,
157
+ name: custom.name,
158
+ props: custom.props,
110
159
  })
111
160
  }
112
161
 
113
- createElement(parent: Element[], value: string, isVarible?: boolean) {
114
- if (isVarible)
162
+ createVarible(parent: Element[], value: string) {
163
+ return parent.push({
164
+ type: "value",
165
+ key: value,
166
+ })
167
+ }
168
+
169
+ createTranslate(parent: Element[], value: string) {
170
+ if (value.startsWith("{{") && value.endsWith("}}")) {
115
171
  return parent.push({
116
- type: "value",
117
- key: value,
172
+ type: "tvalue",
173
+ value: value.slice(2, -2),
118
174
  })
175
+ }
176
+
177
+ return parent.push({
178
+ type: "t",
179
+ text: value,
180
+ })
181
+ }
182
+
183
+ createElement(parent: Element[], value: string, isVarible?: boolean) {
184
+ if (isVarible) {
185
+ return this.createVarible(parent, value)
186
+ }
119
187
 
120
188
  if (value.trimStart().startsWith("ArrayDataKey=")) {
121
189
  const [key, ...val] = value.trimStart().substring(13).split("|")
@@ -16,7 +16,10 @@ export const getRoutesAndTemplates = (pages: PageData[], normalize: boolean = tr
16
16
  for (const [id, template] of pages.entries()) {
17
17
  const content = parser.parse(template.root)
18
18
 
19
- const mask = template.mask.replaceAll("/", "\\/").replace(/{(.*?)}/, ".+?")
19
+ const mask = template.mask
20
+ .replaceAll("/", "\\/")
21
+ .replace(/{(.*?)}/, ".+?")
22
+ .replace("\/*", "\/?.*")
20
23
 
21
24
  routes.push({
22
25
  id,
@@ -29,5 +32,6 @@ export const getRoutesAndTemplates = (pages: PageData[], normalize: boolean = tr
29
32
  return {
30
33
  routes,
31
34
  templates: parser.templates,
35
+ customComponents: parser.custom,
32
36
  }
33
37
  }
@@ -0,0 +1,5 @@
1
+ import { ssr } from "../server-state/SSR"
2
+
3
+ export const useIsServer = () : boolean => {
4
+ return ssr.current
5
+ }
@@ -1,5 +1,6 @@
1
1
  import React from "react"
2
2
  import { ssr } from "./SSR"
3
+ import { JinraiValue } from "../url/search/useSearchValue"
3
4
 
4
5
  // IMPORT REACT
5
6
 
@@ -22,99 +23,177 @@ const getTarget = (data: any, path: string) => {
22
23
  case "undefined":
23
24
  case "symbol":
24
25
  // эти типы можно просто завернуть
25
- return { value: data }
26
+ return { $__ROOT__: data }
26
27
  default:
27
28
  return () => `{{${path}}}`
28
29
  }
29
30
  }
30
31
 
31
- const createDataProxy = (data: any, path: string = ""): DataProxy => {
32
+ type WithDataProxy<T> = T & DataProxy
33
+ function createDataProxy<T>(data: T, path: string = ""): WithDataProxy<T> {
32
34
  if (path.endsWith("@")) sources.set(path.slice(0, -1), data)
33
35
 
34
36
  return new Proxy(getTarget(data, path), {
35
- get: (_: any, prop: string) => {
36
- // if (typeof prop == "symbol") return data[prop]
37
+ get: (_target: any, prop: PropertyKey) => {
38
+ if (ssr.exportToJV) {
39
+ return {
40
+ $JV: {
41
+ key: path + "/" + String(prop),
42
+ type: "proxy",
43
+ def: data,
44
+ },
45
+ } as { $JV?: JinraiValue }
46
+ }
37
47
 
38
- if (!(typeof data == "object" && data !== null && prop in data))
39
- // DEV TOOLS
48
+ // ---------------------------
49
+ // 1. Обработка символов
50
+ // ---------------------------
51
+ if (typeof prop === "symbol") {
40
52
  switch (prop) {
41
- // @ts-ignore
42
53
  case Symbol.toPrimitive:
43
54
  return (hint: string) => {
44
55
  console.log("PROXYDATA", hint)
45
56
  return `{{${path}}}`
46
57
  }
47
- // @ts-ignore
58
+
48
59
  case Symbol.toStringTag:
49
60
  return "Object"
50
- // @ts-ignore
61
+
51
62
  case Symbol.iterator:
52
- return data[Symbol.iterator]
53
- case "$$typeof":
54
- case "type":
63
+ if (typeof data === "object" && data !== null && Symbol.iterator in (data as any)) {
64
+ return (data as any)[Symbol.iterator]
65
+ }
55
66
  return undefined
67
+ }
68
+ }
56
69
 
57
- case "_debugInfo":
58
- return {
59
- note: `State From Request (${path})`,
60
- kind: typeof data,
61
- timestamp: Date.now(),
62
- preview: data,
63
- }
70
+ // ---------------------------
71
+ // 2. DEV tools
72
+ // ---------------------------
73
+ if (!(typeof data === "object" && data !== null && prop in (data as any))) {
74
+ if (prop === "$$typeof" || prop === "type") {
75
+ return undefined
64
76
  }
65
77
 
66
- // SELF
67
- if (prop.startsWith("$")) return (key: string) => `{{${path + "/" + key}${"\\" + prop}}}`
78
+ if (prop === "_debugInfo") {
79
+ return {
80
+ note: `State From Request (${path})`,
81
+ kind: typeof data,
82
+ timestamp: Date.now(),
83
+ preview: data,
84
+ }
85
+ }
86
+ }
68
87
 
69
- // TYPES
88
+ // ---------------------------
89
+ // 3. SELF: $key → returns function
90
+ // ---------------------------
91
+ if (typeof prop === "string" && prop.startsWith("$")) {
92
+ return (key: string) => `{{${path + "/" + key}${"\\" + prop}}}`
93
+ }
94
+
95
+ // ---------------------------
96
+ // 4. Types special cases
97
+ // ---------------------------
70
98
  switch (typeof data) {
71
99
  case "string":
72
- switch (prop) {
73
- case "length":
74
- case "entries":
75
- return undefined
76
- }
100
+ if (prop === "length" || prop === "entries") return undefined
101
+ break
102
+
77
103
  case "number":
78
- switch (prop) {
79
- case "@@iterator":
80
- return undefined
81
- }
104
+ if (prop === "@@iterator") return undefined
105
+ break
82
106
 
83
107
  default:
84
- switch (prop) {
85
- case "then":
86
- return undefined
87
- }
108
+ if (prop === "then") return undefined
88
109
  }
89
110
 
90
- // OTHER
91
- switch (prop) {
92
- case "find":
93
- return data[prop]
94
- case "map":
95
- case "forEach":
96
- return (callback: (arg0: DataProxy) => any) =>
97
- React.createElement("loopwrapper", null, [
98
- `ArrayDataKey=${path}|`,
99
- Object.entries(data)
100
- .slice(0, 1)
101
- .map(([key, itm]) => callback(createDataProxy(itm, `${path}/[ITEM=${key}]`))),
102
- ])
103
- case "getValue":
104
- return () => data
105
- case "toJSON":
106
- return () => {
107
- console.log("dataproxy toJSON", path, data)
108
- return ssr.exportParams ? `{{${path}}}` : data
109
- }
110
- default:
111
- if (data && (typeof data[prop] == "object" || Array.isArray(data[prop]))) {
112
- return createDataProxy(data[prop], path + "/" + prop)
113
- }
114
- return `{{${path + "/" + prop}}}`
111
+ // ---------------------------
112
+ // 5. Array-like handlers
113
+ // ---------------------------
114
+ if (prop === "find") {
115
+ return (data as any)[prop]
115
116
  }
117
+
118
+ if (prop === "length") {
119
+ if (Array.isArray(data)) {
120
+ return data.length
121
+ }
122
+ }
123
+
124
+ if (prop === "map" || prop === "forEach") {
125
+ return (callback: (arg: DataProxy) => any) =>
126
+ React.createElement("loopwrapper", null, [
127
+ `ArrayDataKey=${path}|`,
128
+ Object.entries(data as any)
129
+ .slice(0, 1)
130
+ .map(([key, itm]) => callback(createDataProxy(itm, `${path}/[ITEM=${key}]`))),
131
+ ])
132
+ }
133
+
134
+ // ---------------------------
135
+ // 6. getValue
136
+ // ---------------------------
137
+ if (prop === "getValue") {
138
+ return () => data
139
+ }
140
+
141
+ // ---------------------------
142
+ // 7. toJSON
143
+ // ---------------------------
144
+ if (prop === "toJSON") {
145
+ return () => {
146
+ console.log("dataproxy toJSON", path, data)
147
+
148
+ return ssr.exportParams
149
+ ? `{{${path}}}`
150
+ : ({
151
+ $JV: {
152
+ key: path,
153
+ type: "proxy",
154
+ def: data,
155
+ separator: "",
156
+ },
157
+ } as { $JV?: JinraiValue })
158
+ }
159
+ }
160
+
161
+ // ---------------------------
162
+ // 8. Nested object
163
+ // ---------------------------
164
+ if (data === null) {
165
+ return undefined
166
+ }
167
+
168
+ const value = (data as any)[prop]
169
+ if (!ssr.exportParams) {
170
+ return value
171
+ }
172
+
173
+ if (value && (typeof value === "object" || Array.isArray(value))) {
174
+ return createDataProxy(value, path + "/" + String(prop))
175
+ }
176
+
177
+ // if (ssr.exportParams) {
178
+
179
+ // if (typeof value == "string") {
180
+ // const key = `{{${path + "/" + String(prop)}}}`
181
+ // const jv = getJinraiValue(key, "proxyValue", "", value)
182
+
183
+ // return value.bindSource(jv)
184
+ // }
185
+
186
+ // return key
187
+ // } else {
188
+ // return value
189
+ // }
190
+
191
+ // ---------------------------
192
+ // 9. Final primitive fallback
193
+ // ---------------------------
194
+ return `{{${path + "/" + String(prop)}}}`
116
195
  },
117
- })
196
+ }) as any
118
197
  }
119
198
 
120
199
  export default createDataProxy
@@ -1,4 +1,24 @@
1
+ import { JinraiAgent, ViteAgent } from "../../bin/agent/agent"
2
+
1
3
  export const ssr = {
2
- current: true, //navigator.userAgent == "____JINRAI_CLIENT____",
3
- exportParams: false,
4
+ current: navigator.userAgent == JinraiAgent,
5
+ // current: true,
6
+
7
+ test: navigator.userAgent == ViteAgent,
8
+ exportParams: true,
9
+ exportToJV: false,
10
+ }
11
+
12
+ if (window != undefined) {
13
+ // @ts-ignore
14
+ window.__ssr = ssr
15
+ }
16
+
17
+ export const stringifyInput = (input: any) => {
18
+ ssr.exportParams = false
19
+
20
+ const result = JSON.stringify(input)
21
+ ssr.exportParams = true
22
+
23
+ return result
4
24
  }
@@ -1,14 +1,21 @@
1
1
  import { ssr } from "./SSR"
2
2
  import { sources } from "./DataProxy"
3
+ import { getJinraiValue, JinraiValue } from "../url/search/useSearchValue"
3
4
 
4
- export function real<T>(value: T): T {
5
+ export function orig<T>(value: T): T {
5
6
  if (!ssr.current) return value
6
7
 
7
8
  switch (typeof value) {
8
9
  case "number":
9
10
  return value
10
11
  case "string":
11
- return value.startsWith("{{") && value.endsWith("}}") ? getArrayByPath(value.slice(2, -2)) : value
12
+ if (value.startsWith("{{") && value.endsWith("}}")) {
13
+ const result = getArrayByPath(value.slice(2, -2))
14
+
15
+ return wrapSource<T>(result, getJinraiValue(value, "proxy", "", result))
16
+ }
17
+
18
+ return value
12
19
 
13
20
  case "object":
14
21
  // @ts-ignore
@@ -24,6 +31,16 @@ export function real<T>(value: T): T {
24
31
  }
25
32
  }
26
33
 
34
+ export const original = orig
35
+
36
+ const wrapSource = <T>(value: T, source: JinraiValue): T => {
37
+ if (typeof value == "string") {
38
+ return value.bindSource(source) as T
39
+ }
40
+
41
+ return value
42
+ }
43
+
27
44
  const getArrayByPath = (path: string) => {
28
45
  const [sourceIndex, requestPath] = path.split("@", 2)
29
46
  const keys = requestPath.split("/")
@@ -1,8 +1,20 @@
1
- import { encode } from "js-base64"
2
1
  import createDataProxy from "./DataProxy"
3
- import { ssr } from "./SSR"
4
2
  import { ServerKey } from "./useServerState"
5
3
 
4
+ // @ts-ignore
5
+ const initialState = { ...(window?.__appc__?.state ?? {}) }
6
+ // @ts-ignore
7
+ const serverErrors = [...(window?.__appc__?.errors ?? [])]
8
+
9
+ if (serverErrors.length) {
10
+ console.error("SERVER:", serverErrors)
11
+ }
12
+
13
+ if (window != undefined) {
14
+ // @ts-ignore
15
+ delete window.__appc__
16
+ }
17
+
6
18
  interface Request {
7
19
  method: string
8
20
  url: string
@@ -16,9 +28,11 @@ export type ServerValue = {
16
28
  request?: Request
17
29
  }
18
30
  }
31
+ key: ServerKey
19
32
  }
20
33
 
21
34
  export const serverStates = new Map<string, ServerValue>()
35
+ const idents: string[] = []
22
36
 
23
37
  if (window != undefined) {
24
38
  console.log("init $exportServerStates")
@@ -26,16 +40,8 @@ if (window != undefined) {
26
40
  window.$exportServerStates = serverStates
27
41
  }
28
42
 
29
- const getIdent = (key: ServerKey): string => {
30
- if (ssr.current) {
31
- ssr.exportParams = true
32
- }
33
- const result = Array.isArray(key) ? JSON.stringify(key) : key.toString()
34
- if (ssr.current) {
35
- ssr.exportParams = false
36
- }
37
-
38
- return encode(result)
43
+ export const getIdent = (key: ServerKey): string => {
44
+ return Array.isArray(key) ? key.join("-") : key
39
45
  }
40
46
 
41
47
  export const getServerValue = (key?: ServerKey, def?: ServerValue["value"], options?: ServerValue["options"]) => {
@@ -43,12 +49,21 @@ export const getServerValue = (key?: ServerKey, def?: ServerValue["value"], opti
43
49
  return [def, false]
44
50
  }
45
51
  const ident = getIdent(key)
46
- serverStates.set(ident, { options, value: !options?.source ? def : undefined })
52
+ serverStates.set(ident, { options, value: !options?.source ? def : undefined, key })
47
53
 
48
- // @ts-ignore
49
- if (window != undefined && window?.__serverInitialStates__ && ident in window.__serverInitialStates__) {
50
- // @ts-ignore
51
- return [window.__serverInitialStates__[ident], true]
54
+ console.log("CHECK", ident)
55
+
56
+ if (ident in initialState) {
57
+ const result = initialState[ident]
58
+ // delete initialState[ident]
59
+
60
+ console.log("HAS", ident, result)
61
+
62
+ if (result != null && typeof result == 'object' && "data" in result) {
63
+ return [result.data, true]
64
+ }
65
+
66
+ return [result, true]
52
67
  }
53
68
 
54
69
  return [def, false]
@@ -56,6 +71,6 @@ export const getServerValue = (key?: ServerKey, def?: ServerValue["value"], opti
56
71
 
57
72
  export const setServerValue = (key: ServerKey, value: ServerValue["value"], options?: ServerValue["options"]) => {
58
73
  const ident = getIdent(key)
59
- serverStates.set(ident, { options, value: !options?.source ? value : undefined })
74
+ serverStates.set(ident, { options, value: !options?.source ? value : undefined, key })
60
75
  return createDataProxy(value, `${ident}@`)
61
76
  }
@@ -0,0 +1,15 @@
1
+ import { getIdent, ServerValue } from "./serverStates";
2
+ import { ssr } from "./SSR";
3
+ import { ServerKey } from "./useServerState";
4
+
5
+ const test_states = new Map<string, any>()
6
+
7
+ if (window != undefined && ssr.test) {
8
+ // @ts-ignore
9
+ window.$testStates = test_states
10
+ }
11
+
12
+ export const setTestState = (key: ServerKey, value: ServerValue["value"], options?: ServerValue["options"]) => {
13
+ const ident = getIdent(key)
14
+ test_states.set(ident, value)
15
+ }
@@ -1,6 +1,7 @@
1
1
  import { Dispatch, SetStateAction, useState } from "react"
2
2
  import { ssr } from "./SSR"
3
3
  import { getServerValue, ServerValue, setServerValue } from "./serverStates"
4
+ import { setTestState } from "./testState"
4
5
 
5
6
  export type ServerStateMap = Record<string, ServerValue>
6
7
  export type ServerKey = string | string[]
@@ -12,7 +13,6 @@ export const useServerState = <T extends ServerValue["value"]>(
12
13
  ) => {
13
14
  const [serverValue, isInitOnServer] = getServerValue(serverKey, initialValue, options)
14
15
  const [value, setStateValue] = useState(serverValue)
15
- const [isInit, setIsInit] = useState(isInitOnServer)
16
16
 
17
17
  const setValue: Dispatch<SetStateAction<T>> = (value: T | ((prevState: T) => T)) => {
18
18
  setStateValue((prev: T) => {
@@ -22,18 +22,13 @@ export const useServerState = <T extends ServerValue["value"]>(
22
22
  return setServerValue(serverKey, result, options)
23
23
  }
24
24
 
25
+ if (serverKey != undefined && ssr.test) {
26
+ setTestState(serverKey, result, options)
27
+ }
28
+
25
29
  return result
26
30
  })
27
31
  }
28
32
 
29
- const initOnServer = () => {
30
- if (isInit) {
31
- setIsInit(false)
32
- return true
33
- }
34
-
35
- return false
36
- }
37
-
38
- return [value, setValue, initOnServer] as [T, Dispatch<SetStateAction<T>>, () => boolean]
33
+ return [value, setValue, isInitOnServer] as [T, Dispatch<SetStateAction<T>>, boolean]
39
34
  }