@jsb188/react 1.1.10 → 1.1.11

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/utils/hooks.ts CHANGED
@@ -8,93 +8,103 @@ import { useEffect, useMemo, useRef, useState } from 'react';
8
8
  */
9
9
 
10
10
  function getDatabaseAction(
11
- currentData: any,
12
- data: any,
13
- allowNull?: boolean,
14
- mergeCurrentData?: boolean,
11
+ currentData: any,
12
+ data: any,
13
+ allowNull?: boolean,
14
+ mergeCurrentData?: boolean,
15
15
  ): any {
16
- if (!currentData) {
17
- return {
18
- action: 'INSERT',
19
- documentData: data,
20
- };
21
- }
22
-
23
- const documentData: Record<string, any> = {};
24
-
25
- let hasChanges = false;
26
- for (const key in data) {
27
- if (Object.prototype.hasOwnProperty.call(data, key) && data[key] !== undefined) {
28
- // trim() on all string databse fields should be OK;
29
- // But if not, add an extra param.
30
- const value = typeof data[key] === 'string' ? data[key].trim() : data[key];
31
-
32
- const isNotNull = value !== null || allowNull;
33
- if (isNotNull) {
34
- documentData[key] = value;
35
- }
36
-
37
- if (
38
- // Doing loose != check instead of !== type check
39
- currentData[key] != value &&
40
- isNotNull
41
- ) {
42
- if (value && typeof value === 'object') {
43
- hasChanges = !areObjectsEqual(currentData[key], value);
44
- } else {
45
- hasChanges = true;
46
- }
47
-
48
- if (hasChanges) {
49
- break;
50
- }
51
- }
52
- }
53
- }
54
-
55
- if (mergeCurrentData) {
56
- for (const key in currentData) {
57
- if (Object.prototype.hasOwnProperty.call(currentData, key) && documentData[key] === undefined) {
58
- documentData[key] = currentData[key];
59
- }
60
- }
61
- }
62
-
63
- const action = hasChanges ? 'UPDATE' : 'NONE';
64
-
65
- return {
66
- action,
67
- documentData,
68
- };
16
+ if (!currentData) {
17
+ return {
18
+ action: 'INSERT',
19
+ documentData: data,
20
+ };
21
+ }
22
+
23
+ const documentData: Record<string, any> = {};
24
+
25
+ let hasChanges = false;
26
+ for (const key in data) {
27
+ if (
28
+ Object.prototype.hasOwnProperty.call(data, key) &&
29
+ data[key] !== undefined
30
+ ) {
31
+ // trim() on all string databse fields should be OK;
32
+ // But if not, add an extra param.
33
+ const value =
34
+ typeof data[key] === 'string' ? data[key].trim() : data[key];
35
+
36
+ const isNotNull = value !== null || allowNull;
37
+ if (isNotNull) {
38
+ documentData[key] = value;
39
+ }
40
+
41
+ if (
42
+ // Doing loose != check instead of !== type check
43
+ currentData[key] != value &&
44
+ isNotNull
45
+ ) {
46
+ if (value && typeof value === 'object') {
47
+ hasChanges = !areObjectsEqual(currentData[key], value);
48
+ } else {
49
+ hasChanges = true;
50
+ }
51
+
52
+ if (hasChanges) {
53
+ break;
54
+ }
55
+ }
56
+ }
57
+ }
58
+
59
+ if (mergeCurrentData) {
60
+ for (const key in currentData) {
61
+ if (
62
+ Object.prototype.hasOwnProperty.call(currentData, key) &&
63
+ documentData[key] === undefined
64
+ ) {
65
+ documentData[key] = currentData[key];
66
+ }
67
+ }
68
+ }
69
+
70
+ const action = hasChanges ? 'UPDATE' : 'NONE';
71
+
72
+ return {
73
+ action,
74
+ documentData,
75
+ };
69
76
  }
70
77
 
71
78
  /**
72
79
  * Basic diff check
73
80
  */
74
81
 
75
- export function formValuesAreDiff(currentValues: any, formValues: any): boolean {
76
- if (!formValues && formValues !== 0) {
77
- // If formValues are null, it's not set yet
78
- return false;
79
- }
80
-
81
- if (typeof formValues === 'object') {
82
- // for (const key in currentValues) {
83
- // if (
84
- // currentValues[key] !== formValues[key] &&
85
- // // This would not work for values with number 0, but I don't think that's an issue for any mutations right now
86
- // (currentValues[key] !== null || formValues[key] !== undefined)
87
- // ) {
88
- // console.log('DIFF!:', key, currentValues[key], formValues[key]);
89
- // return true;
90
- // }
91
- // }
92
-
93
- const result = getDatabaseAction(currentValues, formValues);
94
- return result.action !== 'NONE';
95
- }
96
-
97
- return currentValues !== formValues;
82
+ export function formValuesAreDiff(
83
+ currentValues: any,
84
+ formValues: any,
85
+ ): boolean {
86
+ if (!formValues && formValues !== 0) {
87
+ // If formValues are null, it's not set yet
88
+ return false;
89
+ }
90
+
91
+ if (typeof formValues === 'object') {
92
+ // for (const key in currentValues) {
93
+ // if (
94
+ // currentValues[key] !== formValues[key] &&
95
+ // // This would not work for values with number 0, but I don't think that's an issue for any mutations right now
96
+ // (currentValues[key] !== null || formValues[key] !== undefined)
97
+ // ) {
98
+ // console.log('DIFF!:', key, currentValues[key], formValues[key]);
99
+ // return true;
100
+ // }
101
+ // }
102
+
103
+ const result = getDatabaseAction(currentValues, formValues);
104
+ return result.action !== 'NONE';
105
+ }
106
+
107
+ return currentValues !== formValues;
98
108
  }
99
109
 
100
110
  /**
@@ -102,18 +112,37 @@ export function formValuesAreDiff(currentValues: any, formValues: any): boolean
102
112
  */
103
113
 
104
114
  export function useMounted() {
105
- const mounted = useRef(true);
106
-
107
- useEffect(() => {
108
- // This is necessary, I don't know why, this hook unmounteds itself and remounts even though the dependencies are empty
109
- // NOTE: This behavior was seen in [gqlClient.ts]
110
- mounted.current = true;
111
- return () => {
112
- mounted.current = false;
113
- };
114
- }, []);
115
-
116
- return mounted;
115
+ const mounted = useRef(true);
116
+
117
+ useEffect(() => {
118
+ // This is necessary, I don't know why, this hook unmounteds itself and remounts even though the dependencies are empty
119
+ // NOTE: This behavior was seen in [gqlClient.ts]
120
+ mounted.current = true;
121
+ return () => {
122
+ mounted.current = false;
123
+ };
124
+ }, []);
125
+
126
+ return mounted;
127
+ }
128
+
129
+ /**
130
+ * Return the computer timezone after client hydration.
131
+ */
132
+
133
+ let cachedComputerTimeZone: string | null = null;
134
+
135
+ export function useComputerTimeZone() {
136
+ const [timeZone, setTimeZone] = useState<string | null>(
137
+ cachedComputerTimeZone,
138
+ );
139
+
140
+ useEffect(() => {
141
+ cachedComputerTimeZone ??= Intl.DateTimeFormat().resolvedOptions().timeZone;
142
+ setTimeZone(cachedComputerTimeZone);
143
+ }, []);
144
+
145
+ return timeZone;
117
146
  }
118
147
 
119
148
  /**
@@ -121,26 +150,26 @@ export function useMounted() {
121
150
  */
122
151
 
123
152
  export function useWatchChange(
124
- currentValues: any,
125
- formValues: any,
126
- saving: boolean,
127
- onChangeDiff: (hasChanges: boolean, saving: boolean) => void,
128
- onDataUpdated: () => void,
153
+ currentValues: any,
154
+ formValues: any,
155
+ saving: boolean,
156
+ onChangeDiff: (hasChanges: boolean, saving: boolean) => void,
157
+ onDataUpdated: () => void,
129
158
  ) {
130
- const isDiff = useMemo(() => {
131
- return formValuesAreDiff(currentValues, formValues);
132
- }, [currentValues, formValues]);
133
-
134
- useEffect(() => {
135
- onChangeDiff(isDiff, saving);
136
- }, [isDiff, saving]);
137
-
138
- useEffect(() => {
139
- if (!saving) {
140
- // Always update, because isDiff memo() only listens to formValues (and not currentValues)
141
- onDataUpdated();
142
- }
143
- }, [saving]);
159
+ const isDiff = useMemo(() => {
160
+ return formValuesAreDiff(currentValues, formValues);
161
+ }, [currentValues, formValues]);
162
+
163
+ useEffect(() => {
164
+ onChangeDiff(isDiff, saving);
165
+ }, [isDiff, saving]);
166
+
167
+ useEffect(() => {
168
+ if (!saving) {
169
+ // Always update, because isDiff memo() only listens to formValues (and not currentValues)
170
+ onDataUpdated();
171
+ }
172
+ }, [saving]);
144
173
  }
145
174
 
146
175
  /**
@@ -148,31 +177,31 @@ export function useWatchChange(
148
177
  */
149
178
 
150
179
  export function checkPropsDiff(pp: any, np: any) {
151
- let isSame = true;
152
- for (const k in pp) {
153
- if (Object.prototype.hasOwnProperty.call(pp, k) && pp[k] !== np[k]) {
154
- console.log('Diff:', k);
155
- isSame = false;
156
- }
157
- }
158
-
159
- for (const k in np) {
160
- if (
161
- Object.prototype.hasOwnProperty.call(np, k) &&
162
- !Object.prototype.hasOwnProperty.call(pp, k) &&
163
- np[k] !== pp[k]
164
- ) {
165
- console.log('New Prop:', k);
166
- isSame = false;
167
- }
168
- }
169
-
170
- return isSame;
180
+ let isSame = true;
181
+ for (const k in pp) {
182
+ if (Object.prototype.hasOwnProperty.call(pp, k) && pp[k] !== np[k]) {
183
+ console.log('Diff:', k);
184
+ isSame = false;
185
+ }
186
+ }
187
+
188
+ for (const k in np) {
189
+ if (
190
+ Object.prototype.hasOwnProperty.call(np, k) &&
191
+ !Object.prototype.hasOwnProperty.call(pp, k) &&
192
+ np[k] !== pp[k]
193
+ ) {
194
+ console.log('New Prop:', k);
195
+ isSame = false;
196
+ }
197
+ }
198
+
199
+ return isSame;
171
200
  }
172
201
 
173
202
  if (globalThis && globalThis.location?.origin?.indexOf('localhost') >= 0) {
174
- // @ts-ignore - Dev only shim
175
- globalThis.checkPropsDiff = checkPropsDiff;
203
+ // @ts-ignore - Dev only shim
204
+ globalThis.checkPropsDiff = checkPropsDiff;
176
205
  }
177
206
 
178
207
  /**
@@ -180,75 +209,83 @@ if (globalThis && globalThis.location?.origin?.indexOf('localhost') >= 0) {
180
209
  */
181
210
 
182
211
  export function arePropsEqualExceptChildren(pp: any, np: any) {
183
- for (const k in np) {
184
- if (
185
- Object.prototype.hasOwnProperty.call(np, k) &&
186
- k !== 'children' &&
187
- (
188
- !Object.prototype.hasOwnProperty.call(pp, k) ||
189
- np[k] !== pp[k]
190
- )
191
- ) {
192
- // console.log('DIFF:', k, pp[k], np[k]);
193
- return false;
194
- }
195
- }
196
- return true;
212
+ for (const k in np) {
213
+ if (
214
+ Object.prototype.hasOwnProperty.call(np, k) &&
215
+ k !== 'children' &&
216
+ (!Object.prototype.hasOwnProperty.call(pp, k) || np[k] !== pp[k])
217
+ ) {
218
+ // console.log('DIFF:', k, pp[k], np[k]);
219
+ return false;
220
+ }
221
+ }
222
+ return true;
197
223
  }
198
224
 
199
225
  /**
200
226
  * Keep track of visibility state change and copy props;
201
- * To be used in animation components
227
+ * To be used in animation components.
228
+ *
229
+ * You can use this hook to animate an element to fade out before returning
230
+ * null. Pass a truthy value while the element should be visible, render while
231
+ * visibility is not 0, and apply fade classes from the visibility value.
232
+ *
233
+ * Example:
234
+ *
235
+ * const [, visibility] = useAnimationVisibility(visible);
236
+ * const active = visibility >= 2;
237
+ *
238
+ * if (!visibility) {
239
+ * return null;
240
+ * }
241
+ *
242
+ * return <div className={cn('trans_op spd_2', active ? 'op_100' : 'op_0')} />;
202
243
  */
203
244
 
204
- type VisibilityValue = 0 | .5 | 1 | 2;
245
+ type VisibilityValue = 0 | 0.5 | 1 | 2;
205
246
 
206
247
  export function useAnimationVisibility<T>(
207
- props: T,
208
- onHasProps?: (hasProps: boolean) => void,
209
- ): [
210
- any,
211
- VisibilityValue,
212
- (VisibilityValue: VisibilityValue) => void,
213
- ] {
214
- const [visibility, setVisibility] = useState(0 as VisibilityValue);
215
- const [persistedProps, setPersistedProps] = useState<T>(props as T);
216
- const hasData = !!props;
217
-
218
- useEffect(() => {
219
- if (props) {
220
- setPersistedProps(props);
221
- }
222
- onHasProps?.(hasData);
223
- }, [props]);
224
-
225
- useEffect(() => {
226
- if (visibility === 1) {
227
- if (hasData) {
228
- const timer = setTimeout(() => {
229
- setVisibility(2);
230
- }, 100);
231
- return () => clearTimeout(timer);
232
- } else {
233
- const timer = setTimeout(() => {
234
- setVisibility(0);
235
- }, 300);
236
- return () => clearTimeout(timer);
237
- }
238
- } else if (visibility === .5) {
239
- const timer = setTimeout(() => {
240
- setVisibility(0);
241
- }, 300);
242
- return () => clearTimeout(timer);
243
- }
244
-
245
- const shouldBeValue = hasData ? 2 : 0;
246
- if (shouldBeValue !== visibility) {
247
- setVisibility(hasData ? 1 : .5);
248
- }
249
- }, [visibility, hasData]);
250
-
251
- return [persistedProps, visibility, setVisibility];
248
+ props: T,
249
+ onHasProps?: (hasProps: boolean) => void,
250
+ ): [any, VisibilityValue, (VisibilityValue: VisibilityValue) => void] {
251
+ const [visibility, setVisibility] = useState(0 as VisibilityValue);
252
+ const [persistedProps, setPersistedProps] = useState<T>(props as T);
253
+ const hasData = !!props;
254
+
255
+ useEffect(() => {
256
+ if (props) {
257
+ setPersistedProps(props);
258
+ }
259
+ onHasProps?.(hasData);
260
+ }, [props]);
261
+
262
+ useEffect(() => {
263
+ if (visibility === 1) {
264
+ if (hasData) {
265
+ const timer = setTimeout(() => {
266
+ setVisibility(2);
267
+ }, 100);
268
+ return () => clearTimeout(timer);
269
+ } else {
270
+ const timer = setTimeout(() => {
271
+ setVisibility(0);
272
+ }, 300);
273
+ return () => clearTimeout(timer);
274
+ }
275
+ } else if (visibility === 0.5) {
276
+ const timer = setTimeout(() => {
277
+ setVisibility(0);
278
+ }, 300);
279
+ return () => clearTimeout(timer);
280
+ }
281
+
282
+ const shouldBeValue = hasData ? 2 : 0;
283
+ if (shouldBeValue !== visibility) {
284
+ setVisibility(hasData ? 1 : 0.5);
285
+ }
286
+ }, [visibility, hasData]);
287
+
288
+ return [persistedProps, visibility, setVisibility];
252
289
  }
253
290
 
254
291
  /**
@@ -257,94 +294,92 @@ export function useAnimationVisibility<T>(
257
294
  */
258
295
 
259
296
  export function useImagesLoadStatus(
260
- imageUrls: string[],
261
- timeoutMS = 10000 // default timeout miliseconds
297
+ imageUrls: string[],
298
+ timeoutMS = 10000, // default timeout miliseconds
262
299
  ): boolean {
263
- const [loaded, setLoaded] = useState(false);
264
-
265
- useEffect(() => {
266
- if (!imageUrls || imageUrls.length === 0) {
267
- setLoaded(true);
268
- return;
269
- }
270
-
271
- let isCancelled = false;
272
- let loadedCount = 0;
273
- const timeout = setTimeout(() => {
274
- if (!isCancelled) {
275
- setLoaded(true);
276
- }
277
- }, timeoutMS);
278
-
279
- const handleLoad = () => {
280
- loadedCount += 1;
281
- if (loadedCount === imageUrls.length && !isCancelled) {
282
- clearTimeout(timeout);
283
- setLoaded(true);
284
- }
285
- };
286
-
287
- const handleError = () => {
288
- loadedCount += 1; // count errors too, so they don’t block loading
289
- if (loadedCount === imageUrls.length && !isCancelled) {
290
- clearTimeout(timeout);
291
- setLoaded(true);
292
- }
293
- };
294
-
295
- const imageElements = imageUrls.map((url) => {
296
- const img = new Image();
297
- img.onload = handleLoad;
298
- img.onerror = handleError;
299
- img.src = url;
300
- return img;
301
- });
302
-
303
- return () => {
304
- isCancelled = true;
305
- clearTimeout(timeout);
306
- // Optional: clear handlers to prevent leaks
307
- imageElements.forEach((img) => {
308
- img.onload = null;
309
- img.onerror = null;
310
- });
311
- };
312
- }, []);
313
-
314
- return loaded;
300
+ const [loaded, setLoaded] = useState(false);
301
+
302
+ useEffect(() => {
303
+ if (!imageUrls || imageUrls.length === 0) {
304
+ setLoaded(true);
305
+ return;
306
+ }
307
+
308
+ let isCancelled = false;
309
+ let loadedCount = 0;
310
+ const timeout = setTimeout(() => {
311
+ if (!isCancelled) {
312
+ setLoaded(true);
313
+ }
314
+ }, timeoutMS);
315
+
316
+ const handleLoad = () => {
317
+ loadedCount += 1;
318
+ if (loadedCount === imageUrls.length && !isCancelled) {
319
+ clearTimeout(timeout);
320
+ setLoaded(true);
321
+ }
322
+ };
323
+
324
+ const handleError = () => {
325
+ loadedCount += 1; // count errors too, so they don’t block loading
326
+ if (loadedCount === imageUrls.length && !isCancelled) {
327
+ clearTimeout(timeout);
328
+ setLoaded(true);
329
+ }
330
+ };
331
+
332
+ const imageElements = imageUrls.map((url) => {
333
+ const img = new Image();
334
+ img.onload = handleLoad;
335
+ img.onerror = handleError;
336
+ img.src = url;
337
+ return img;
338
+ });
339
+
340
+ return () => {
341
+ isCancelled = true;
342
+ clearTimeout(timeout);
343
+ // Optional: clear handlers to prevent leaks
344
+ imageElements.forEach((img) => {
345
+ img.onload = null;
346
+ img.onerror = null;
347
+ });
348
+ };
349
+ }, []);
350
+
351
+ return loaded;
315
352
  }
316
353
 
317
354
  /*
318
355
  * Track whether a referenced div's scroll position has passed a pixel offset.
319
356
  */
320
357
 
321
- export function useScrollPastPosition(
322
- pixels: number,
323
- ) {
324
- const divRef = useRef<HTMLDivElement>(null);
325
- const [hasScrolledPast, setHasScrolledPast] = useState(false);
358
+ export function useScrollPastPosition(pixels: number) {
359
+ const divRef = useRef<HTMLDivElement>(null);
360
+ const [hasScrolledPast, setHasScrolledPast] = useState(false);
326
361
 
327
- useEffect(() => {
328
- const element = divRef.current;
329
- if (!element) {
330
- return;
331
- }
362
+ useEffect(() => {
363
+ const element = divRef.current;
364
+ if (!element) {
365
+ return;
366
+ }
332
367
 
333
- const handleScroll = () => {
334
- const nextValue = element.scrollTop > pixels;
368
+ const handleScroll = () => {
369
+ const nextValue = element.scrollTop > pixels;
335
370
 
336
- setHasScrolledPast((currentValue) => {
337
- return currentValue === nextValue ? currentValue : nextValue;
338
- });
339
- };
371
+ setHasScrolledPast((currentValue) => {
372
+ return currentValue === nextValue ? currentValue : nextValue;
373
+ });
374
+ };
340
375
 
341
- element.addEventListener('scroll', handleScroll, { passive: true });
342
- handleScroll();
376
+ element.addEventListener('scroll', handleScroll, { passive: true });
377
+ handleScroll();
343
378
 
344
- return () => {
345
- element.removeEventListener('scroll', handleScroll);
346
- };
347
- }, [pixels]);
379
+ return () => {
380
+ element.removeEventListener('scroll', handleScroll);
381
+ };
382
+ }, [pixels]);
348
383
 
349
- return [divRef, hasScrolledPast] as const;
384
+ return [divRef, hasScrolledPast] as const;
350
385
  }