@jsb188/react 1.1.1 → 1.1.2
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/libs/README.md +75 -0
- package/libs/page-filters.helpers.ts +65 -0
- package/libs/page-filters.ts +223 -0
- package/libs/use-page-filters.ts +147 -0
- package/package.json +24 -19
- package/states/apiData.ts +18 -14
- package/states/appContext.ts +423 -1
- package/states/connected.ts +135 -11
- package/states/index.ts +4 -0
- package/states/keyDown.ts +167 -0
- package/states/keyboardLayer.ts +57 -0
- package/states/modal.ts +403 -239
- package/states/popOver.ts +394 -178
- package/states/reactiveEvents.ts +99 -0
- package/states/uploads.ts +70 -0
- package/types/PopOver.d.ts +636 -88
- package/utils/hooks.ts +406 -234
- package/utils/lazy.ts +183 -0
package/utils/lazy.ts
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { Component, type ComponentType, createElement, type ErrorInfo, type ReactNode, use } from 'react';
|
|
2
|
+
|
|
3
|
+
export interface ComponentModule<P> {
|
|
4
|
+
default: ComponentType<P>;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface PreloadableComponent<P> {
|
|
8
|
+
// Render inside <Suspense>; suspends only while the code is still downloading
|
|
9
|
+
Component: ComponentType<P>;
|
|
10
|
+
// Start (or reuse) the download; resolves null on failure and never rejects
|
|
11
|
+
preload: () => Promise<ComponentModule<P> | null>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// A promise carrying the settled-state fields React's use() reads to skip suspending
|
|
15
|
+
type TrackedPromise<T> = Promise<T> & {
|
|
16
|
+
status?: 'pending' | 'fulfilled' | 'rejected';
|
|
17
|
+
value?: T;
|
|
18
|
+
reason?: unknown;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
interface LazyWithPreloadOptions<P> {
|
|
22
|
+
// Rendered instead of the component when its code fails to download (defaults to rendering nothing)
|
|
23
|
+
fallback?: ComponentType<P>;
|
|
24
|
+
// Called when a render needed the code and it failed to download
|
|
25
|
+
onError?: (error: unknown) => void;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/*
|
|
29
|
+
* Error thrown when a lazy component's code failed to download.
|
|
30
|
+
*/
|
|
31
|
+
export class LazyLoadError extends Error {
|
|
32
|
+
constructor(cause: unknown) {
|
|
33
|
+
super('Failed to load lazy component code', { cause });
|
|
34
|
+
this.name = 'LazyLoadError';
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/*
|
|
39
|
+
* Catch only code download failures (LazyLoadError) and render a fallback;
|
|
40
|
+
* every other error is rethrown to the next error boundary as before.
|
|
41
|
+
*/
|
|
42
|
+
class LazyLoadErrorBoundary extends Component<
|
|
43
|
+
{
|
|
44
|
+
children: ReactNode;
|
|
45
|
+
renderFallback: () => ReactNode;
|
|
46
|
+
onLoadError: (error: LazyLoadError) => void;
|
|
47
|
+
},
|
|
48
|
+
{ error: unknown }
|
|
49
|
+
> {
|
|
50
|
+
state = { error: null as unknown };
|
|
51
|
+
|
|
52
|
+
static getDerivedStateFromError(error: unknown) {
|
|
53
|
+
return { error };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
componentDidCatch(error: unknown, _errorInfo: ErrorInfo) {
|
|
57
|
+
if (error instanceof LazyLoadError) {
|
|
58
|
+
this.props.onLoadError(error);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
render() {
|
|
63
|
+
const { error } = this.state;
|
|
64
|
+
if (!error) {
|
|
65
|
+
return this.props.children;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (error instanceof LazyLoadError) {
|
|
69
|
+
return this.props.renderFallback();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Not a download failure; let the app's error handling deal with it
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/*
|
|
78
|
+
* Create a component whose code downloads on first render, or earlier through
|
|
79
|
+
* preload() (idle or hover prefetch). Unlike React.lazy, a component whose code
|
|
80
|
+
* was already preloaded renders synchronously without suspending, so prefetched
|
|
81
|
+
* content never flashes a Suspense fallback. The download is shared by
|
|
82
|
+
* preload() and every render. A failed download renders `fallback` instead of
|
|
83
|
+
* throwing to the app's error boundary. A later render or preload() starts a new
|
|
84
|
+
* download, though some browsers (Chromium) keep failing a dynamic import that
|
|
85
|
+
* failed once until the page reloads, so `fallback` should offer a reload.
|
|
86
|
+
*/
|
|
87
|
+
export function lazyWithPreload<P>(
|
|
88
|
+
load: () => Promise<ComponentModule<P>>,
|
|
89
|
+
options: LazyWithPreloadOptions<P> = {},
|
|
90
|
+
): PreloadableComponent<P> {
|
|
91
|
+
let modulePromise: TrackedPromise<ComponentModule<P>> | null = null;
|
|
92
|
+
|
|
93
|
+
const loadOnce = () => {
|
|
94
|
+
if (!modulePromise) {
|
|
95
|
+
const promise: TrackedPromise<ComponentModule<P>> = load().catch((error) => {
|
|
96
|
+
throw new LazyLoadError(error);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
// Record the settled state in the fields React's use() reads, so a loaded module
|
|
100
|
+
// is returned synchronously (no suspending) on every later render
|
|
101
|
+
promise.status = 'pending';
|
|
102
|
+
promise.then(
|
|
103
|
+
(module) => {
|
|
104
|
+
promise.status = 'fulfilled';
|
|
105
|
+
promise.value = module;
|
|
106
|
+
},
|
|
107
|
+
(error) => {
|
|
108
|
+
promise.status = 'rejected';
|
|
109
|
+
promise.reason = error;
|
|
110
|
+
},
|
|
111
|
+
);
|
|
112
|
+
modulePromise = promise;
|
|
113
|
+
}
|
|
114
|
+
return modulePromise;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
// Forget a failed download so the next attempt fetches again (the same promise is kept
|
|
118
|
+
// while a render is still retrying, so React's use() sees one stable promise)
|
|
119
|
+
const forgetFailedLoad = () => {
|
|
120
|
+
if (modulePromise?.status === 'rejected') {
|
|
121
|
+
modulePromise = null;
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
function LoadedComponent(props: P) {
|
|
126
|
+
const Loaded = use(loadOnce() as Promise<ComponentModule<P>>).default;
|
|
127
|
+
return createElement(Loaded as ComponentType<any>, props as any);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function PreloadableComponent(props: P) {
|
|
131
|
+
return createElement(LazyLoadErrorBoundary, {
|
|
132
|
+
onLoadError: (error: LazyLoadError) => {
|
|
133
|
+
forgetFailedLoad();
|
|
134
|
+
options.onError?.(error.cause);
|
|
135
|
+
},
|
|
136
|
+
renderFallback: () => (options.fallback ? createElement(options.fallback as ComponentType<any>, props as any) : null),
|
|
137
|
+
children: createElement(LoadedComponent as ComponentType<any>, props as any),
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return {
|
|
142
|
+
Component: PreloadableComponent,
|
|
143
|
+
preload: () =>
|
|
144
|
+
loadOnce().catch(() => {
|
|
145
|
+
forgetFailedLoad();
|
|
146
|
+
return null;
|
|
147
|
+
}),
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/*
|
|
152
|
+
* Wrap an already imported component in the same shape as lazyWithPreload(),
|
|
153
|
+
* for registries that mix lazy and static entries (e.g. prompts that must open instantly).
|
|
154
|
+
*/
|
|
155
|
+
export function preloadedComponent<P>(Loaded: ComponentType<P>): PreloadableComponent<P> {
|
|
156
|
+
const module = { default: Loaded };
|
|
157
|
+
return {
|
|
158
|
+
Component: Loaded,
|
|
159
|
+
preload: () => Promise.resolve(module),
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/*
|
|
164
|
+
* Reload the page after a code chunk failed to load (e.g. a tab still running
|
|
165
|
+
* an old deploy), at most once per `windowMs` so a persistent failure can't
|
|
166
|
+
* cause a reload loop. Returns true when a reload was started.
|
|
167
|
+
*/
|
|
168
|
+
export function reloadOnceAfterChunkError(storageKey: string, windowMs = 60000) {
|
|
169
|
+
try {
|
|
170
|
+
const lastReloadAt = Number(sessionStorage.getItem(storageKey) || 0);
|
|
171
|
+
if (Date.now() - lastReloadAt < windowMs) {
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
sessionStorage.setItem(storageKey, String(Date.now()));
|
|
176
|
+
} catch {
|
|
177
|
+
// Without sessionStorage there's no loop guard; don't reload
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
globalThis.location.reload();
|
|
182
|
+
return true;
|
|
183
|
+
}
|