@alepha/react 0.10.5 → 0.10.7
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/dist/index.browser.js +32 -48
- package/dist/index.browser.js.map +1 -1
- package/dist/index.d.ts +39 -39
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +40 -63
- package/dist/index.js.map +1 -1
- package/package.json +12 -12
- package/src/components/ClientOnly.tsx +12 -12
- package/src/components/ErrorBoundary.tsx +43 -43
- package/src/components/ErrorViewer.tsx +140 -140
- package/src/components/Link.tsx +7 -7
- package/src/components/NestedView.tsx +177 -177
- package/src/components/NotFound.tsx +19 -19
- package/src/contexts/RouterLayerContext.ts +3 -3
- package/src/descriptors/$page.ts +292 -290
- package/src/errors/Redirection.ts +5 -5
- package/src/hooks/useActive.ts +41 -41
- package/src/hooks/useAlepha.ts +7 -7
- package/src/hooks/useClient.ts +5 -5
- package/src/hooks/useInject.ts +2 -2
- package/src/hooks/useQueryParams.ts +37 -37
- package/src/hooks/useRouter.ts +1 -1
- package/src/hooks/useRouterEvents.ts +46 -46
- package/src/hooks/useRouterState.ts +5 -5
- package/src/hooks/useSchema.ts +55 -55
- package/src/hooks/useStore.ts +25 -25
- package/src/index.browser.ts +18 -18
- package/src/index.ts +49 -49
- package/src/providers/ReactBrowserProvider.ts +268 -261
- package/src/providers/ReactBrowserRendererProvider.ts +15 -15
- package/src/providers/ReactBrowserRouterProvider.ts +124 -124
- package/src/providers/ReactPageProvider.ts +616 -618
- package/src/providers/ReactServerProvider.ts +505 -505
- package/src/services/ReactRouter.ts +191 -191
|
@@ -4,134 +4,134 @@ import { type Route, RouterProvider } from "@alepha/router";
|
|
|
4
4
|
import { createElement, type ReactNode } from "react";
|
|
5
5
|
import NotFoundPage from "../components/NotFound.tsx";
|
|
6
6
|
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
isPageRoute,
|
|
8
|
+
type PageRoute,
|
|
9
|
+
type PageRouteEntry,
|
|
10
|
+
type PreviousLayerData,
|
|
11
|
+
ReactPageProvider,
|
|
12
|
+
type ReactRouterState,
|
|
13
13
|
} from "./ReactPageProvider.ts";
|
|
14
14
|
|
|
15
15
|
export interface BrowserRoute extends Route {
|
|
16
|
-
|
|
16
|
+
page: PageRoute;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
export class ReactBrowserRouterProvider extends RouterProvider<BrowserRoute> {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
20
|
+
protected readonly log = $logger();
|
|
21
|
+
protected readonly alepha = $inject(Alepha);
|
|
22
|
+
protected readonly pageApi = $inject(ReactPageProvider);
|
|
23
|
+
|
|
24
|
+
public add(entry: PageRouteEntry) {
|
|
25
|
+
this.pageApi.add(entry);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
protected readonly configure = $hook({
|
|
29
|
+
on: "configure",
|
|
30
|
+
handler: async () => {
|
|
31
|
+
for (const page of this.pageApi.getPages()) {
|
|
32
|
+
// mount only if a view is provided
|
|
33
|
+
if (page.component || page.lazy) {
|
|
34
|
+
this.push({
|
|
35
|
+
path: page.match,
|
|
36
|
+
page,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
public async transition(
|
|
44
|
+
url: URL,
|
|
45
|
+
previous: PreviousLayerData[] = [],
|
|
46
|
+
meta = {},
|
|
47
|
+
): Promise<string | void> {
|
|
48
|
+
const { pathname, search } = url;
|
|
49
|
+
|
|
50
|
+
const entry: Partial<ReactRouterState> = {
|
|
51
|
+
url,
|
|
52
|
+
query: {},
|
|
53
|
+
params: {},
|
|
54
|
+
layers: [],
|
|
55
|
+
onError: () => null,
|
|
56
|
+
meta,
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const state = entry as ReactRouterState;
|
|
60
|
+
|
|
61
|
+
await this.alepha.events.emit("react:transition:begin", {
|
|
62
|
+
previous: this.alepha.state.get("react.router.state")!,
|
|
63
|
+
state,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
try {
|
|
67
|
+
const { route, params } = this.match(pathname);
|
|
68
|
+
|
|
69
|
+
const query: Record<string, string> = {};
|
|
70
|
+
if (search) {
|
|
71
|
+
for (const [key, value] of new URLSearchParams(search).entries()) {
|
|
72
|
+
query[key] = String(value);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
state.query = query;
|
|
77
|
+
state.params = params ?? {};
|
|
78
|
+
|
|
79
|
+
if (isPageRoute(route)) {
|
|
80
|
+
const { redirect } = await this.pageApi.createLayers(
|
|
81
|
+
route.page,
|
|
82
|
+
state,
|
|
83
|
+
previous,
|
|
84
|
+
);
|
|
85
|
+
if (redirect) {
|
|
86
|
+
return redirect;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (state.layers.length === 0) {
|
|
91
|
+
state.layers.push({
|
|
92
|
+
name: "not-found",
|
|
93
|
+
element: createElement(NotFoundPage),
|
|
94
|
+
index: 0,
|
|
95
|
+
path: "/",
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
await this.alepha.events.emit("react:transition:success", { state });
|
|
100
|
+
} catch (e) {
|
|
101
|
+
this.log.error("Transition has failed", e);
|
|
102
|
+
state.layers = [
|
|
103
|
+
{
|
|
104
|
+
name: "error",
|
|
105
|
+
element: this.pageApi.renderError(e as Error),
|
|
106
|
+
index: 0,
|
|
107
|
+
path: "/",
|
|
108
|
+
},
|
|
109
|
+
];
|
|
110
|
+
|
|
111
|
+
await this.alepha.events.emit("react:transition:error", {
|
|
112
|
+
error: e as Error,
|
|
113
|
+
state,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// [feature]: local hook for leaving a page
|
|
118
|
+
if (previous) {
|
|
119
|
+
for (let i = 0; i < previous.length; i++) {
|
|
120
|
+
const layer = previous[i];
|
|
121
|
+
if (state.layers[i]?.name !== layer.name) {
|
|
122
|
+
this.pageApi.page(layer.name)?.onLeave?.();
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
this.alepha.state.set("react.router.state", state);
|
|
128
|
+
|
|
129
|
+
await this.alepha.events.emit("react:transition:end", {
|
|
130
|
+
state,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
public root(state: ReactRouterState): ReactNode {
|
|
135
|
+
return this.pageApi.root(state);
|
|
136
|
+
}
|
|
137
137
|
}
|