@alepha/react 0.9.1 → 0.9.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/README.md +7 -0
- package/dist/index.browser.js +93 -33
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +95 -31
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +52 -29
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +51 -28
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +93 -33
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
- package/src/contexts/AlephaContext.ts +4 -0
- package/src/contexts/RouterContext.ts +0 -2
- package/src/hooks/useAlepha.ts +5 -5
- package/src/hooks/useInject.ts +5 -8
- package/src/hooks/useQueryParams.ts +6 -9
- package/src/hooks/useRouter.ts +4 -4
- package/src/hooks/useRouterEvents.ts +7 -10
- package/src/hooks/useRouterState.ts +6 -4
- package/src/hooks/useSchema.ts +93 -0
- package/src/hooks/useStore.ts +39 -0
- package/src/index.shared.ts +3 -0
- package/src/providers/PageDescriptorProvider.ts +12 -8
package/dist/index.cjs
CHANGED
|
@@ -230,20 +230,31 @@ const RouterContext = (0, react.createContext)(void 0);
|
|
|
230
230
|
//#region src/contexts/RouterLayerContext.ts
|
|
231
231
|
const RouterLayerContext = (0, react.createContext)(void 0);
|
|
232
232
|
|
|
233
|
+
//#endregion
|
|
234
|
+
//#region src/contexts/AlephaContext.ts
|
|
235
|
+
const AlephaContext = (0, react.createContext)(void 0);
|
|
236
|
+
|
|
237
|
+
//#endregion
|
|
238
|
+
//#region src/hooks/useAlepha.ts
|
|
239
|
+
const useAlepha = () => {
|
|
240
|
+
const alepha = (0, react.useContext)(AlephaContext);
|
|
241
|
+
if (!alepha) throw new Error("useAlepha must be used within an AlephaContext.Provider");
|
|
242
|
+
return alepha;
|
|
243
|
+
};
|
|
244
|
+
|
|
233
245
|
//#endregion
|
|
234
246
|
//#region src/hooks/useRouterEvents.ts
|
|
235
247
|
const useRouterEvents = (opts = {}, deps = []) => {
|
|
236
|
-
const
|
|
237
|
-
if (!ctx) throw new Error("useRouter must be used within a RouterProvider");
|
|
248
|
+
const alepha = useAlepha();
|
|
238
249
|
(0, react.useEffect)(() => {
|
|
239
|
-
if (!
|
|
250
|
+
if (!alepha.isBrowser()) return;
|
|
240
251
|
const subs = [];
|
|
241
252
|
const onBegin = opts.onBegin;
|
|
242
253
|
const onEnd = opts.onEnd;
|
|
243
254
|
const onError = opts.onError;
|
|
244
|
-
if (onBegin) subs.push(
|
|
245
|
-
if (onEnd) subs.push(
|
|
246
|
-
if (onError) subs.push(
|
|
255
|
+
if (onBegin) subs.push(alepha.on("react:transition:begin", { callback: onBegin }));
|
|
256
|
+
if (onEnd) subs.push(alepha.on("react:transition:end", { callback: onEnd }));
|
|
257
|
+
if (onError) subs.push(alepha.on("react:transition:error", { callback: onError }));
|
|
247
258
|
return () => {
|
|
248
259
|
for (const sub of subs) sub();
|
|
249
260
|
};
|
|
@@ -383,11 +394,10 @@ var PageDescriptorProvider = class {
|
|
|
383
394
|
return new URL(url.replace(/\/\/+/g, "/") || "/", options.base ?? `http://localhost`);
|
|
384
395
|
}
|
|
385
396
|
root(state, context) {
|
|
386
|
-
const root = (0, react.createElement)(RouterContext.Provider, { value: {
|
|
387
|
-
alepha: this.alepha,
|
|
397
|
+
const root = (0, react.createElement)(AlephaContext.Provider, { value: this.alepha }, (0, react.createElement)(RouterContext.Provider, { value: {
|
|
388
398
|
state,
|
|
389
399
|
context
|
|
390
|
-
} }, (0, react.createElement)(NestedView_default, {}, state.layers[0]?.element));
|
|
400
|
+
} }, (0, react.createElement)(NestedView_default, {}, state.layers[0]?.element)));
|
|
391
401
|
if (this.env.REACT_STRICT_MODE) return (0, react.createElement)(react.StrictMode, {}, root);
|
|
392
402
|
return root;
|
|
393
403
|
}
|
|
@@ -1150,13 +1160,14 @@ var RouterHookApi = class {
|
|
|
1150
1160
|
//#endregion
|
|
1151
1161
|
//#region src/hooks/useRouter.ts
|
|
1152
1162
|
const useRouter = () => {
|
|
1163
|
+
const alepha = useAlepha();
|
|
1153
1164
|
const ctx = (0, react.useContext)(RouterContext);
|
|
1154
1165
|
const layer = (0, react.useContext)(RouterLayerContext);
|
|
1155
1166
|
if (!ctx || !layer) throw new Error("useRouter must be used within a RouterProvider");
|
|
1156
1167
|
const pages = (0, react.useMemo)(() => {
|
|
1157
|
-
return
|
|
1168
|
+
return alepha.inject(PageDescriptorProvider).getPages();
|
|
1158
1169
|
}, []);
|
|
1159
|
-
return (0, react.useMemo)(() => new RouterHookApi(pages, ctx.context, ctx.state, layer,
|
|
1170
|
+
return (0, react.useMemo)(() => new RouterHookApi(pages, ctx.context, ctx.state, layer, alepha.isBrowser() ? alepha.inject(ReactBrowserProvider) : void 0), [layer]);
|
|
1160
1171
|
};
|
|
1161
1172
|
|
|
1162
1173
|
//#endregion
|
|
@@ -1215,20 +1226,11 @@ const useActive = (path) => {
|
|
|
1215
1226
|
};
|
|
1216
1227
|
};
|
|
1217
1228
|
|
|
1218
|
-
//#endregion
|
|
1219
|
-
//#region src/hooks/useAlepha.ts
|
|
1220
|
-
const useAlepha = () => {
|
|
1221
|
-
const routerContext = (0, react.useContext)(RouterContext);
|
|
1222
|
-
if (!routerContext) throw new Error("useAlepha must be used within a RouterProvider");
|
|
1223
|
-
return routerContext.alepha;
|
|
1224
|
-
};
|
|
1225
|
-
|
|
1226
1229
|
//#endregion
|
|
1227
1230
|
//#region src/hooks/useInject.ts
|
|
1228
|
-
const useInject = (
|
|
1229
|
-
const
|
|
1230
|
-
|
|
1231
|
-
return (0, react.useMemo)(() => ctx.alepha.inject(clazz), []);
|
|
1231
|
+
const useInject = (service) => {
|
|
1232
|
+
const alepha = useAlepha();
|
|
1233
|
+
return (0, react.useMemo)(() => alepha.inject(service), []);
|
|
1232
1234
|
};
|
|
1233
1235
|
|
|
1234
1236
|
//#endregion
|
|
@@ -1240,21 +1242,20 @@ const useClient = (_scope) => {
|
|
|
1240
1242
|
//#endregion
|
|
1241
1243
|
//#region src/hooks/useQueryParams.ts
|
|
1242
1244
|
const useQueryParams = (schema, options = {}) => {
|
|
1243
|
-
const
|
|
1244
|
-
if (!ctx) throw new Error("useQueryParams must be used within a RouterProvider");
|
|
1245
|
+
const alepha = useAlepha();
|
|
1245
1246
|
const key = options.key ?? "q";
|
|
1246
1247
|
const router = useRouter();
|
|
1247
1248
|
const querystring = router.query[key];
|
|
1248
|
-
const [queryParams, setQueryParams] = (0, react.useState)(decode(
|
|
1249
|
+
const [queryParams, setQueryParams] = (0, react.useState)(decode(alepha, schema, router.query[key]));
|
|
1249
1250
|
(0, react.useEffect)(() => {
|
|
1250
|
-
setQueryParams(decode(
|
|
1251
|
+
setQueryParams(decode(alepha, schema, querystring));
|
|
1251
1252
|
}, [querystring]);
|
|
1252
1253
|
return [queryParams, (queryParams$1) => {
|
|
1253
1254
|
setQueryParams(queryParams$1);
|
|
1254
1255
|
router.setQueryParams((data) => {
|
|
1255
1256
|
return {
|
|
1256
1257
|
...data,
|
|
1257
|
-
[key]: encode(
|
|
1258
|
+
[key]: encode(alepha, schema, queryParams$1)
|
|
1258
1259
|
};
|
|
1259
1260
|
});
|
|
1260
1261
|
}];
|
|
@@ -1273,14 +1274,73 @@ const decode = (alepha, schema, data) => {
|
|
|
1273
1274
|
//#endregion
|
|
1274
1275
|
//#region src/hooks/useRouterState.ts
|
|
1275
1276
|
const useRouterState = () => {
|
|
1276
|
-
const
|
|
1277
|
+
const router = (0, react.useContext)(RouterContext);
|
|
1277
1278
|
const layer = (0, react.useContext)(RouterLayerContext);
|
|
1278
|
-
if (!
|
|
1279
|
-
const [state, setState] = (0, react.useState)(
|
|
1279
|
+
if (!router || !layer) throw new Error("useRouterState must be used within a RouterContext.Provider");
|
|
1280
|
+
const [state, setState] = (0, react.useState)(router.state);
|
|
1280
1281
|
useRouterEvents({ onEnd: ({ state: state$1 }) => setState({ ...state$1 }) });
|
|
1281
1282
|
return state;
|
|
1282
1283
|
};
|
|
1283
1284
|
|
|
1285
|
+
//#endregion
|
|
1286
|
+
//#region src/hooks/useSchema.ts
|
|
1287
|
+
const useSchema = (action) => {
|
|
1288
|
+
const name = action.name;
|
|
1289
|
+
const alepha = useAlepha();
|
|
1290
|
+
const httpClient = useInject(__alepha_server.HttpClient);
|
|
1291
|
+
const linkProvider = useInject(__alepha_server_links.LinkProvider);
|
|
1292
|
+
const [schema, setSchema] = (0, react.useState)(ssrSchemaLoading(alepha, name));
|
|
1293
|
+
(0, react.useEffect)(() => {
|
|
1294
|
+
if (!schema.loading) return;
|
|
1295
|
+
const opts = { cache: true };
|
|
1296
|
+
httpClient.fetch(`${linkProvider.URL_LINKS}/${name}/schema`, {}, opts).then((it) => setSchema(it.data));
|
|
1297
|
+
}, [name]);
|
|
1298
|
+
return schema;
|
|
1299
|
+
};
|
|
1300
|
+
/**
|
|
1301
|
+
* Get an action schema during server-side rendering (SSR) or client-side rendering (CSR).
|
|
1302
|
+
*/
|
|
1303
|
+
const ssrSchemaLoading = (alepha, name) => {
|
|
1304
|
+
if (!alepha.isBrowser()) {
|
|
1305
|
+
const links = alepha.context.get("links")?.links ?? [];
|
|
1306
|
+
const can = links.find((it) => it.name === name);
|
|
1307
|
+
if (can) {
|
|
1308
|
+
const schema$1 = alepha.inject(__alepha_server_links.LinkProvider).links?.find((it) => it.name === name)?.schema;
|
|
1309
|
+
if (schema$1) {
|
|
1310
|
+
can.schema = schema$1;
|
|
1311
|
+
return schema$1;
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1314
|
+
return { loading: true };
|
|
1315
|
+
}
|
|
1316
|
+
const schema = alepha.inject(__alepha_server_links.LinkProvider).links?.find((it) => it.name === name)?.schema;
|
|
1317
|
+
if (schema) return schema;
|
|
1318
|
+
return { loading: true };
|
|
1319
|
+
};
|
|
1320
|
+
|
|
1321
|
+
//#endregion
|
|
1322
|
+
//#region src/hooks/useStore.ts
|
|
1323
|
+
/**
|
|
1324
|
+
* Hook to access and mutate the Alepha state.
|
|
1325
|
+
*/
|
|
1326
|
+
const useStore = (key) => {
|
|
1327
|
+
const alepha = useAlepha();
|
|
1328
|
+
const [state, setState] = (0, react.useState)(alepha.state(key));
|
|
1329
|
+
(0, react.useEffect)(() => {
|
|
1330
|
+
if (!alepha.isBrowser()) return;
|
|
1331
|
+
return alepha.on("state:mutate", (ev) => {
|
|
1332
|
+
if (ev.key === key) setState(ev.value);
|
|
1333
|
+
});
|
|
1334
|
+
}, []);
|
|
1335
|
+
if (!alepha.isBrowser()) {
|
|
1336
|
+
const value = alepha.context.get(key);
|
|
1337
|
+
if (value !== null) return [value, (_) => {}];
|
|
1338
|
+
}
|
|
1339
|
+
return [state, (value) => {
|
|
1340
|
+
alepha.state(key, value);
|
|
1341
|
+
}];
|
|
1342
|
+
};
|
|
1343
|
+
|
|
1284
1344
|
//#endregion
|
|
1285
1345
|
//#region src/index.ts
|
|
1286
1346
|
/**
|
|
@@ -1306,6 +1366,7 @@ const AlephaReact = (0, __alepha_core.$module)({
|
|
|
1306
1366
|
|
|
1307
1367
|
//#endregion
|
|
1308
1368
|
exports.$page = $page;
|
|
1369
|
+
exports.AlephaContext = AlephaContext;
|
|
1309
1370
|
exports.AlephaReact = AlephaReact;
|
|
1310
1371
|
exports.ClientOnly = ClientOnly_default;
|
|
1311
1372
|
exports.ErrorBoundary = ErrorBoundary_default;
|
|
@@ -1321,6 +1382,7 @@ exports.RouterContext = RouterContext;
|
|
|
1321
1382
|
exports.RouterHookApi = RouterHookApi;
|
|
1322
1383
|
exports.RouterLayerContext = RouterLayerContext;
|
|
1323
1384
|
exports.isPageRoute = isPageRoute;
|
|
1385
|
+
exports.ssrSchemaLoading = ssrSchemaLoading;
|
|
1324
1386
|
exports.useActive = useActive;
|
|
1325
1387
|
exports.useAlepha = useAlepha;
|
|
1326
1388
|
exports.useClient = useClient;
|
|
@@ -1329,4 +1391,6 @@ exports.useQueryParams = useQueryParams;
|
|
|
1329
1391
|
exports.useRouter = useRouter;
|
|
1330
1392
|
exports.useRouterEvents = useRouterEvents;
|
|
1331
1393
|
exports.useRouterState = useRouterState;
|
|
1394
|
+
exports.useSchema = useSchema;
|
|
1395
|
+
exports.useStore = useStore;
|
|
1332
1396
|
//# sourceMappingURL=index.cjs.map
|