@e7w/easy-routes 0.0.1 → 0.0.3
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 +25 -0
- package/dist/components/DefaultOutlet.d.ts +2 -0
- package/dist/components/DefaultOutlet.js +3 -0
- package/dist/components/ErrorBoundary.d.ts +2 -0
- package/dist/components/ErrorBoundary.js +6 -0
- package/dist/components/Loading.d.ts +3 -0
- package/dist/components/Loading.js +12 -0
- package/dist/components/ProgressLayout.d.ts +3 -0
- package/dist/components/ProgressLayout.js +25 -0
- package/{src/index.ts → dist/index.d.ts} +3 -3
- package/dist/index.js +2 -0
- package/dist/models/system.d.ts +15 -0
- package/dist/models/system.js +84 -0
- package/dist/router.d.ts +3 -0
- package/dist/router.js +85 -0
- package/dist/types/index.d.ts +14 -0
- package/dist/types/index.js +1 -0
- package/dist/utils/hooks.d.ts +1 -0
- package/dist/utils/hooks.js +15 -0
- package/package.json +5 -4
- package/src/assets/index.css +0 -109
- package/src/components/DefaultOutlet.tsx +0 -8
- package/src/components/ErrorBoundary.tsx +0 -25
- package/src/components/Loading.tsx +0 -23
- package/src/components/ProgressLayout.tsx +0 -31
- package/src/models/system.ts +0 -49
- package/src/router.tsx +0 -96
- package/src/types/index.ts +0 -18
- package/src/utils/hooks.ts +0 -16
package/README.md
CHANGED
|
@@ -1 +1,26 @@
|
|
|
1
1
|
# 自用React-router配置
|
|
2
|
+
|
|
3
|
+
/src/pages/\*\*/page.tsx为页面
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
export function Page() {}
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
/src/pages/\*\*/layout.tsx为layout
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
export function Layout() {
|
|
15
|
+
return <><Outlet /></>
|
|
16
|
+
}
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
/src/pages/\*\*/meta.ts配置如下
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
export const permission: number = 0b0; // 默认是0b0代表需要登录,只有最右边的一位代表登录,其他位可以自己定enum代表权限
|
|
25
|
+
export const loader: Loader = async (ctx, meta) => {}; // 可以动态修改ctx,ctx.data是返回给页面组件的值,ctx.title是页面的title,signed代表是否登录成功,noPermission代表是否没有权限
|
|
26
|
+
```
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { isRouteErrorResponse, useRouteError } from "react-router";
|
|
3
|
+
export const ErrorBoundary = () => {
|
|
4
|
+
const error = useRouteError();
|
|
5
|
+
return (_jsx("div", { className: "flex h-full w-full items-center justify-center", children: _jsxs("div", { className: "flex flex-col items-center justify-center", children: [_jsx("h1", { className: "text-2xl font-bold mb-4", children: "Error" }), _jsx("p", { children: "Something went wrong." }), isRouteErrorResponse(error) && (_jsxs(_Fragment, { children: [_jsxs("p", { className: "text-red-300", children: [error.status, " ", error.statusText] }), _jsx("p", { className: "text-red-300", children: JSON.stringify(error.data) })] })), error instanceof Error && (_jsx("p", { className: "text-red-300", children: error.message }))] }) }));
|
|
6
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import {} from "react";
|
|
3
|
+
import { useDebouncedValue } from "../utils/hooks";
|
|
4
|
+
import { useLoader } from "@e7w/easy-model";
|
|
5
|
+
import "../assets/index.css";
|
|
6
|
+
export const Loading = () => {
|
|
7
|
+
const { isGlobalLoading } = useLoader();
|
|
8
|
+
const visible = useDebouncedValue(isGlobalLoading);
|
|
9
|
+
if (!visible)
|
|
10
|
+
return;
|
|
11
|
+
return (_jsxs("div", { className: "spinner-box", children: [_jsx("div", { className: "blue-orbit leo" }), _jsx("div", { className: "green-orbit leo" }), _jsx("div", { className: "red-orbit leo" }), _jsx("div", { className: "white-orbit w1 leo" }), _jsx("div", { className: "white-orbit w2 leo" }), _jsx("div", { className: "white-orbit w3 leo" })] }));
|
|
12
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect } from "react";
|
|
3
|
+
import { Outlet, useNavigation } from "react-router";
|
|
4
|
+
import nProgress from "nprogress";
|
|
5
|
+
import "nprogress/nprogress.css";
|
|
6
|
+
import { Loading } from "./Loading";
|
|
7
|
+
nProgress.configure({
|
|
8
|
+
showSpinner: false, // 是否显示右上角螺旋加载提示(通常关掉更简洁)
|
|
9
|
+
speed: 500, // 动画速度(毫秒)
|
|
10
|
+
minimum: 0.1, // 进度条开始时的最小百分比
|
|
11
|
+
easing: "ease", // 动画缓动函数
|
|
12
|
+
trickleSpeed: 200, // 进度条自动递增的频率(毫秒)
|
|
13
|
+
});
|
|
14
|
+
export const ProgressLayout = () => {
|
|
15
|
+
const { state } = useNavigation();
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
if (state === "loading") {
|
|
18
|
+
nProgress.start();
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
nProgress.done();
|
|
22
|
+
}
|
|
23
|
+
}, [state]);
|
|
24
|
+
return (_jsxs(_Fragment, { children: [_jsx(Outlet, {}), _jsx(Loading, {})] }));
|
|
25
|
+
};
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export type { Loader } from "./types";
|
|
2
|
-
export { routerSchema } from "./models/system";
|
|
3
|
-
export { routes } from "./router";
|
|
1
|
+
export type { Loader } from "./types";
|
|
2
|
+
export { routerSchema } from "./models/system";
|
|
3
|
+
export { routes } from "./router";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import z from "zod";
|
|
2
|
+
import type { Ctx } from "../types";
|
|
3
|
+
import type { Params } from "react-router";
|
|
4
|
+
export interface RouterSchema {
|
|
5
|
+
init(): void | Promise<void>;
|
|
6
|
+
permission: number;
|
|
7
|
+
defaultTitle?: string;
|
|
8
|
+
}
|
|
9
|
+
export declare const routerSchema: z.ZodType<RouterSchema, RouterSchema>;
|
|
10
|
+
export declare const system: {
|
|
11
|
+
router: RouterSchema;
|
|
12
|
+
init(): Promise<void>;
|
|
13
|
+
hasPermission(permission?: number): boolean;
|
|
14
|
+
getCtx(params: Params, request: Request, permission?: number): Ctx;
|
|
15
|
+
};
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
2
|
+
var useValue = arguments.length > 2;
|
|
3
|
+
for (var i = 0; i < initializers.length; i++) {
|
|
4
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
5
|
+
}
|
|
6
|
+
return useValue ? value : void 0;
|
|
7
|
+
};
|
|
8
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
9
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
10
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
11
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
12
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
13
|
+
var _, done = false;
|
|
14
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
15
|
+
var context = {};
|
|
16
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
17
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
18
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
19
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
20
|
+
if (kind === "accessor") {
|
|
21
|
+
if (result === void 0) continue;
|
|
22
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
23
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
24
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
25
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
26
|
+
}
|
|
27
|
+
else if (_ = accept(result)) {
|
|
28
|
+
if (kind === "field") initializers.unshift(_);
|
|
29
|
+
else descriptor[key] = _;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
33
|
+
done = true;
|
|
34
|
+
};
|
|
35
|
+
import { inject, loader, provide } from "@e7w/easy-model";
|
|
36
|
+
import z from "zod";
|
|
37
|
+
export const routerSchema = z.object({
|
|
38
|
+
init: z.function({
|
|
39
|
+
output: z.union([z.promise(z.void()), z.void()]),
|
|
40
|
+
}),
|
|
41
|
+
permission: z.number(),
|
|
42
|
+
defaultTitle: z.string().optional(),
|
|
43
|
+
});
|
|
44
|
+
export const system = provide((() => {
|
|
45
|
+
var _a;
|
|
46
|
+
let _instanceExtraInitializers = [];
|
|
47
|
+
let _router_decorators;
|
|
48
|
+
let _router_initializers = [];
|
|
49
|
+
let _router_extraInitializers = [];
|
|
50
|
+
let _init_decorators;
|
|
51
|
+
return class System {
|
|
52
|
+
static {
|
|
53
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
54
|
+
_router_decorators = [inject(routerSchema)];
|
|
55
|
+
_init_decorators = [(_a = loader).once.bind(_a), loader.load(true)];
|
|
56
|
+
__esDecorate(this, null, _init_decorators, { kind: "method", name: "init", static: false, private: false, access: { has: obj => "init" in obj, get: obj => obj.init }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
57
|
+
__esDecorate(null, null, _router_decorators, { kind: "field", name: "router", static: false, private: false, access: { has: obj => "router" in obj, get: obj => obj.router, set: (obj, value) => { obj.router = value; } }, metadata: _metadata }, _router_initializers, _router_extraInitializers);
|
|
58
|
+
if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
59
|
+
}
|
|
60
|
+
router = (__runInitializers(this, _instanceExtraInitializers), __runInitializers(this, _router_initializers, {
|
|
61
|
+
init() { },
|
|
62
|
+
permission: 0,
|
|
63
|
+
defaultTitle: document.title,
|
|
64
|
+
}));
|
|
65
|
+
async init() {
|
|
66
|
+
await this.router.init();
|
|
67
|
+
}
|
|
68
|
+
hasPermission(permission = 0) {
|
|
69
|
+
return (this.router.permission & permission) === permission;
|
|
70
|
+
}
|
|
71
|
+
getCtx(params, request, permission = 1) {
|
|
72
|
+
return {
|
|
73
|
+
title: this.router.defaultTitle || document.title,
|
|
74
|
+
signed: (permission & 1) === 0 || this.hasPermission(1),
|
|
75
|
+
noPermission: !this.hasPermission(permission),
|
|
76
|
+
params,
|
|
77
|
+
query: Object.fromEntries(new URL(request.url).searchParams),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
constructor() {
|
|
81
|
+
__runInitializers(this, _router_extraInitializers);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
})())();
|
package/dist/router.d.ts
ADDED
package/dist/router.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { Navigate, redirect } from "react-router";
|
|
3
|
+
import { DefaultOutlet } from "./components/DefaultOutlet";
|
|
4
|
+
import { ErrorBoundary } from "./components/ErrorBoundary";
|
|
5
|
+
import { ProgressLayout } from "./components/ProgressLayout";
|
|
6
|
+
import { system } from "./models/system";
|
|
7
|
+
const pages = import.meta.glob("/src/pages/**/index.tsx", {
|
|
8
|
+
eager: true,
|
|
9
|
+
import: "Page",
|
|
10
|
+
});
|
|
11
|
+
const layouts = import.meta.glob("/src/pages/**/layout.tsx", {
|
|
12
|
+
eager: true,
|
|
13
|
+
import: "Layout",
|
|
14
|
+
});
|
|
15
|
+
const metas = import.meta.glob("/src/pages/**/meta.ts", {
|
|
16
|
+
eager: true,
|
|
17
|
+
});
|
|
18
|
+
const routes = [];
|
|
19
|
+
const obj = {};
|
|
20
|
+
const filePaths = new Set(Object.keys({ ...pages, ...layouts }).map((p) => p.replace(/\/(index|layout)\.tsx$/, "")));
|
|
21
|
+
filePaths.forEach((filePath) => {
|
|
22
|
+
const pagePath = filePath + "/index.tsx";
|
|
23
|
+
const Page = pages[pagePath];
|
|
24
|
+
const layoutPath = filePath + "/layout.tsx";
|
|
25
|
+
const Layout = layouts[layoutPath] || DefaultOutlet;
|
|
26
|
+
const metaPath = filePath + "/meta.ts";
|
|
27
|
+
const meta = metas[metaPath] || {};
|
|
28
|
+
const path = filePath.replace(/.*\/pages/, "").replace(/\[([^\]]+)\]/, ":$1") || "/";
|
|
29
|
+
const hasPage = Boolean(Page);
|
|
30
|
+
obj[path] = {
|
|
31
|
+
path,
|
|
32
|
+
element: _jsx(Layout, {}),
|
|
33
|
+
children: [
|
|
34
|
+
{
|
|
35
|
+
element: hasPage ? _jsx(Page, {}) : _jsx(Navigate, { to: "/", replace: true }),
|
|
36
|
+
index: true,
|
|
37
|
+
loader: hasPage
|
|
38
|
+
? async ({ params, request }) => {
|
|
39
|
+
await system.init();
|
|
40
|
+
const ctx = system.getCtx(params, request, meta.permission);
|
|
41
|
+
await meta.loader?.(ctx, meta);
|
|
42
|
+
if (!ctx.signed)
|
|
43
|
+
return redirect("/login");
|
|
44
|
+
if (ctx.noPermission)
|
|
45
|
+
return redirect("/403");
|
|
46
|
+
document.title = ctx.title;
|
|
47
|
+
return ctx.data;
|
|
48
|
+
}
|
|
49
|
+
: undefined,
|
|
50
|
+
},
|
|
51
|
+
],
|
|
52
|
+
};
|
|
53
|
+
});
|
|
54
|
+
filePaths.forEach((filePath) => {
|
|
55
|
+
const path = filePath.replace(/.*\/pages/, "").replace(/\[([^\]]+)\]/, ":$1") || "/";
|
|
56
|
+
if (path === "/")
|
|
57
|
+
return;
|
|
58
|
+
const currentObj = obj[path];
|
|
59
|
+
const segments = path.split("/").filter(Boolean);
|
|
60
|
+
const paths = ["/"];
|
|
61
|
+
segments.reduce((acc, curr) => {
|
|
62
|
+
const newPath = acc + "/" + curr;
|
|
63
|
+
if (newPath !== path)
|
|
64
|
+
paths.unshift(newPath);
|
|
65
|
+
return newPath;
|
|
66
|
+
}, "");
|
|
67
|
+
for (let index = 0; index < paths.length; index++) {
|
|
68
|
+
const p = paths[index];
|
|
69
|
+
if (obj[p]) {
|
|
70
|
+
obj[p].children.push(currentObj);
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
obj["/"].errorElement = _jsx(ErrorBoundary, {});
|
|
76
|
+
routes.push({
|
|
77
|
+
path: "/",
|
|
78
|
+
element: _jsx(ProgressLayout, {}),
|
|
79
|
+
children: [obj["/"]],
|
|
80
|
+
});
|
|
81
|
+
routes.push({
|
|
82
|
+
path: "*",
|
|
83
|
+
element: _jsx(Navigate, { to: obj["/404"] ? "/404" : "/", replace: true }),
|
|
84
|
+
});
|
|
85
|
+
export { routes };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Params } from "react-router";
|
|
2
|
+
export interface Ctx {
|
|
3
|
+
title: string;
|
|
4
|
+
signed: boolean;
|
|
5
|
+
noPermission: boolean;
|
|
6
|
+
params: Params;
|
|
7
|
+
query: Params;
|
|
8
|
+
data?: any;
|
|
9
|
+
}
|
|
10
|
+
export type Meta = {
|
|
11
|
+
permission?: number;
|
|
12
|
+
loader?: Loader;
|
|
13
|
+
} & Record<string, unknown>;
|
|
14
|
+
export type Loader = (ctx: Ctx, meta: Meta) => void | Promise<void>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function useDebouncedValue<T>(value: T): T;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from "react";
|
|
2
|
+
export function useDebouncedValue(value) {
|
|
3
|
+
const [result, setResult] = useState(value);
|
|
4
|
+
const timer = useRef(undefined);
|
|
5
|
+
useEffect(() => {
|
|
6
|
+
clearTimeout(timer.current);
|
|
7
|
+
timer.current = window.setTimeout(() => {
|
|
8
|
+
setResult(value);
|
|
9
|
+
}, 700);
|
|
10
|
+
return () => {
|
|
11
|
+
window.clearTimeout(timer.current);
|
|
12
|
+
};
|
|
13
|
+
}, [result, value]);
|
|
14
|
+
return result;
|
|
15
|
+
}
|
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e7w/easy-routes",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public",
|
|
6
6
|
"registry": "https://registry.npmjs.org/"
|
|
7
7
|
},
|
|
8
8
|
"type": "module",
|
|
9
9
|
"exports": {
|
|
10
|
-
"types": "./
|
|
11
|
-
"import": "./
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
14
14
|
"@e7w/easy-model": "^0.2.1",
|
|
@@ -35,12 +35,13 @@
|
|
|
35
35
|
"vitest": "^4.0.18"
|
|
36
36
|
},
|
|
37
37
|
"files": [
|
|
38
|
-
"
|
|
38
|
+
"dist",
|
|
39
39
|
"README.md",
|
|
40
40
|
"LICENSE.md"
|
|
41
41
|
],
|
|
42
42
|
"license": "MIT",
|
|
43
43
|
"scripts": {
|
|
44
|
+
"build": "tsc --build",
|
|
44
45
|
"lint": "eslint ."
|
|
45
46
|
}
|
|
46
47
|
}
|
package/src/assets/index.css
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
@import "tailwindcss";
|
|
2
|
-
|
|
3
|
-
/* KEYFRAMES */
|
|
4
|
-
|
|
5
|
-
@keyframes spin {
|
|
6
|
-
from {
|
|
7
|
-
transform: rotate(0);
|
|
8
|
-
}
|
|
9
|
-
to {
|
|
10
|
-
transform: rotate(359deg);
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
@keyframes spin3D {
|
|
15
|
-
from {
|
|
16
|
-
transform: rotate3d(0.5, 0.5, 0.5, 360deg);
|
|
17
|
-
}
|
|
18
|
-
to {
|
|
19
|
-
transform: rotate3d(0deg);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
@keyframes configure-clockwise {
|
|
24
|
-
0% {
|
|
25
|
-
transform: rotate(0);
|
|
26
|
-
}
|
|
27
|
-
25% {
|
|
28
|
-
transform: rotate(90deg);
|
|
29
|
-
}
|
|
30
|
-
50% {
|
|
31
|
-
transform: rotate(180deg);
|
|
32
|
-
}
|
|
33
|
-
75% {
|
|
34
|
-
transform: rotate(270deg);
|
|
35
|
-
}
|
|
36
|
-
100% {
|
|
37
|
-
transform: rotate(360deg);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
@keyframes configure-xclockwise {
|
|
42
|
-
0% {
|
|
43
|
-
transform: rotate(45deg);
|
|
44
|
-
}
|
|
45
|
-
25% {
|
|
46
|
-
transform: rotate(-45deg);
|
|
47
|
-
}
|
|
48
|
-
50% {
|
|
49
|
-
transform: rotate(-135deg);
|
|
50
|
-
}
|
|
51
|
-
75% {
|
|
52
|
-
transform: rotate(-225deg);
|
|
53
|
-
}
|
|
54
|
-
100% {
|
|
55
|
-
transform: rotate(-315deg);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
@keyframes pulse {
|
|
60
|
-
from {
|
|
61
|
-
opacity: 1;
|
|
62
|
-
transform: scale(1);
|
|
63
|
-
}
|
|
64
|
-
to {
|
|
65
|
-
opacity: 0.25;
|
|
66
|
-
transform: scale(0.75);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/* GRID STYLING */
|
|
71
|
-
.spinner-box {
|
|
72
|
-
@apply top-0 left-0 w-full h-full flex justify-center items-center bg-transparent fixed z-50 backdrop-blur-sm;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
.leo {
|
|
76
|
-
@apply absolute flex justify-center items-center rounded-full;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
.blue-orbit {
|
|
80
|
-
@apply w-[165px] h-[165px] border-[1px] border-blue-400/70 border-solid animate-[spin3D_3s_linear_0.2s_infinite];
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
.green-orbit {
|
|
84
|
-
@apply w-[120px] h-[120px] border-[1px] border-solid border-green-400/70 animate-[spin3D_2s_linear_0s_infinite];
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
.red-orbit {
|
|
88
|
-
@apply w-[90px] h-[30px] border-[1px] border-solid border-orange-400/70 animate-[spin3D_1s_linear_0s_infinite];
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
.white-orbit {
|
|
92
|
-
@apply w-[60px] h-[60px] border-2 border-solid border-white animate-[spin3D_10s_linear_0s_infinite];
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
.w1 {
|
|
96
|
-
transform: rotate3D(1, 1, 1, 90deg);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
.w2 {
|
|
100
|
-
transform: rotate3D(1, 2, 0.5, 90deg);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
.w3 {
|
|
104
|
-
transform: rotate3D(0.5, 1, 2, 90deg);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
.markdown-body {
|
|
108
|
-
background-color: var(--color-base-300);
|
|
109
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import type { FC } from "react";
|
|
2
|
-
import { isRouteErrorResponse, useRouteError } from "react-router";
|
|
3
|
-
|
|
4
|
-
export const ErrorBoundary: FC = () => {
|
|
5
|
-
const error = useRouteError();
|
|
6
|
-
return (
|
|
7
|
-
<div className="flex h-full w-full items-center justify-center">
|
|
8
|
-
<div className="flex flex-col items-center justify-center">
|
|
9
|
-
<h1 className="text-2xl font-bold mb-4">Error</h1>
|
|
10
|
-
<p>Something went wrong.</p>
|
|
11
|
-
{isRouteErrorResponse(error) && (
|
|
12
|
-
<>
|
|
13
|
-
<p className="text-red-300">
|
|
14
|
-
{error.status} {error.statusText}
|
|
15
|
-
</p>
|
|
16
|
-
<p className="text-red-300">{JSON.stringify(error.data)}</p>
|
|
17
|
-
</>
|
|
18
|
-
)}
|
|
19
|
-
{error instanceof Error && (
|
|
20
|
-
<p className="text-red-300">{error.message}</p>
|
|
21
|
-
)}
|
|
22
|
-
</div>
|
|
23
|
-
</div>
|
|
24
|
-
);
|
|
25
|
-
};
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { type FC } from "react";
|
|
2
|
-
import { useDebouncedValue } from "../utils/hooks";
|
|
3
|
-
import { useLoader } from "@e7w/easy-model";
|
|
4
|
-
import "../assets/index.css";
|
|
5
|
-
|
|
6
|
-
export const Loading: FC = () => {
|
|
7
|
-
const { isGlobalLoading } = useLoader();
|
|
8
|
-
const visible = useDebouncedValue(isGlobalLoading);
|
|
9
|
-
if (!visible) return;
|
|
10
|
-
return (
|
|
11
|
-
<div className="spinner-box">
|
|
12
|
-
<div className="blue-orbit leo"></div>
|
|
13
|
-
|
|
14
|
-
<div className="green-orbit leo"></div>
|
|
15
|
-
|
|
16
|
-
<div className="red-orbit leo"></div>
|
|
17
|
-
|
|
18
|
-
<div className="white-orbit w1 leo"></div>
|
|
19
|
-
<div className="white-orbit w2 leo"></div>
|
|
20
|
-
<div className="white-orbit w3 leo"></div>
|
|
21
|
-
</div>
|
|
22
|
-
);
|
|
23
|
-
};
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { useEffect, type FC } from "react";
|
|
2
|
-
import { Outlet, useNavigation } from "react-router";
|
|
3
|
-
import nProgress from "nprogress";
|
|
4
|
-
import "nprogress/nprogress.css";
|
|
5
|
-
import { Loading } from "./Loading";
|
|
6
|
-
|
|
7
|
-
nProgress.configure({
|
|
8
|
-
showSpinner: false, // 是否显示右上角螺旋加载提示(通常关掉更简洁)
|
|
9
|
-
speed: 500, // 动画速度(毫秒)
|
|
10
|
-
minimum: 0.1, // 进度条开始时的最小百分比
|
|
11
|
-
easing: "ease", // 动画缓动函数
|
|
12
|
-
trickleSpeed: 200, // 进度条自动递增的频率(毫秒)
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
export const ProgressLayout: FC = () => {
|
|
16
|
-
const { state } = useNavigation();
|
|
17
|
-
useEffect(() => {
|
|
18
|
-
if (state === "loading") {
|
|
19
|
-
nProgress.start();
|
|
20
|
-
} else {
|
|
21
|
-
nProgress.done();
|
|
22
|
-
}
|
|
23
|
-
}, [state]);
|
|
24
|
-
|
|
25
|
-
return (
|
|
26
|
-
<>
|
|
27
|
-
<Outlet />
|
|
28
|
-
<Loading />
|
|
29
|
-
</>
|
|
30
|
-
);
|
|
31
|
-
};
|
package/src/models/system.ts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { inject, loader, provide } from "@e7w/easy-model";
|
|
2
|
-
import z from "zod";
|
|
3
|
-
import type { Ctx } from "../types";
|
|
4
|
-
import type { Params } from "react-router";
|
|
5
|
-
|
|
6
|
-
export interface RouterSchema {
|
|
7
|
-
init(): void | Promise<void>;
|
|
8
|
-
permission: number;
|
|
9
|
-
defaultTitle?: string;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export const routerSchema: z.ZodType<RouterSchema, RouterSchema> = z.object({
|
|
13
|
-
init: z.function({
|
|
14
|
-
output: z.union([z.promise(z.void()), z.void()]),
|
|
15
|
-
}),
|
|
16
|
-
permission: z.number(),
|
|
17
|
-
defaultTitle: z.string().optional(),
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
export const system = provide(
|
|
21
|
-
class System {
|
|
22
|
-
@inject(routerSchema)
|
|
23
|
-
router: RouterSchema = {
|
|
24
|
-
init() {},
|
|
25
|
-
permission: 0,
|
|
26
|
-
defaultTitle: document.title,
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
@loader.once
|
|
30
|
-
@loader.load(true)
|
|
31
|
-
async init() {
|
|
32
|
-
await this.router.init();
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
hasPermission(permission: number = 0) {
|
|
36
|
-
return (this.router.permission & permission) === permission;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
getCtx(params: Params, request: Request, permission = 0): Ctx {
|
|
40
|
-
return {
|
|
41
|
-
title: this.router.defaultTitle || document.title,
|
|
42
|
-
signed: (permission & 1) === 0 || this.hasPermission(1),
|
|
43
|
-
noPermission: !this.hasPermission(permission),
|
|
44
|
-
params,
|
|
45
|
-
query: Object.fromEntries(new URL(request.url).searchParams),
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
},
|
|
49
|
-
)();
|
package/src/router.tsx
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import type { FC } from "react";
|
|
2
|
-
import { Navigate, redirect, type RouteObject } from "react-router";
|
|
3
|
-
import { DefaultOutlet } from "./components/DefaultOutlet";
|
|
4
|
-
import { ErrorBoundary } from "./components/ErrorBoundary";
|
|
5
|
-
import { ProgressLayout } from "./components/ProgressLayout";
|
|
6
|
-
import type { Meta } from "./types";
|
|
7
|
-
import { system } from "./models/system";
|
|
8
|
-
|
|
9
|
-
const pages: Record<string, FC> = import.meta.glob("/src/pages/**/index.tsx", {
|
|
10
|
-
eager: true,
|
|
11
|
-
import: "Page",
|
|
12
|
-
}) as Record<string, FC>;
|
|
13
|
-
const layouts: Record<string, FC> = import.meta.glob(
|
|
14
|
-
"/src/pages/**/layout.tsx",
|
|
15
|
-
{
|
|
16
|
-
eager: true,
|
|
17
|
-
import: "Layout",
|
|
18
|
-
},
|
|
19
|
-
) as Record<string, FC>;
|
|
20
|
-
const metas: Record<string, Meta> = import.meta.glob("/src/pages/**/meta.ts", {
|
|
21
|
-
eager: true,
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
const routes: RouteObject[] = [];
|
|
25
|
-
const obj: Record<string, RouteObject> = {};
|
|
26
|
-
const filePaths = new Set(
|
|
27
|
-
Object.keys({ ...pages, ...layouts }).map((p) =>
|
|
28
|
-
p.replace(/\/(index|layout)\.tsx$/, ""),
|
|
29
|
-
),
|
|
30
|
-
);
|
|
31
|
-
filePaths.forEach((filePath) => {
|
|
32
|
-
const pagePath = filePath + "/index.tsx";
|
|
33
|
-
const Page = pages[pagePath];
|
|
34
|
-
const layoutPath = filePath + "/layout.tsx";
|
|
35
|
-
const Layout = layouts[layoutPath] || DefaultOutlet;
|
|
36
|
-
const metaPath = filePath + "/meta.ts";
|
|
37
|
-
const meta = metas[metaPath] || {};
|
|
38
|
-
const path =
|
|
39
|
-
filePath.replace(/.*\/pages/, "").replace(/\[([^\]]+)\]/, ":$1") || "/";
|
|
40
|
-
|
|
41
|
-
const hasPage = Boolean(Page);
|
|
42
|
-
obj[path] = {
|
|
43
|
-
path,
|
|
44
|
-
element: <Layout />,
|
|
45
|
-
children: [
|
|
46
|
-
{
|
|
47
|
-
element: hasPage ? <Page /> : <Navigate to="/" replace />,
|
|
48
|
-
index: true,
|
|
49
|
-
loader: hasPage
|
|
50
|
-
? async ({ params, request }) => {
|
|
51
|
-
await system.init();
|
|
52
|
-
const ctx = system.getCtx(params, request, meta.permission);
|
|
53
|
-
await meta.loader?.(ctx, meta);
|
|
54
|
-
if (!ctx.signed) return redirect("/login");
|
|
55
|
-
if (ctx.noPermission) return redirect("/403");
|
|
56
|
-
document.title = ctx.title;
|
|
57
|
-
return ctx.data;
|
|
58
|
-
}
|
|
59
|
-
: undefined,
|
|
60
|
-
},
|
|
61
|
-
],
|
|
62
|
-
};
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
filePaths.forEach((filePath) => {
|
|
66
|
-
const path =
|
|
67
|
-
filePath.replace(/.*\/pages/, "").replace(/\[([^\]]+)\]/, ":$1") || "/";
|
|
68
|
-
if (path === "/") return;
|
|
69
|
-
const currentObj = obj[path];
|
|
70
|
-
const segments = path.split("/").filter(Boolean);
|
|
71
|
-
const paths = ["/"];
|
|
72
|
-
segments.reduce((acc, curr) => {
|
|
73
|
-
const newPath = acc + "/" + curr;
|
|
74
|
-
if (newPath !== path) paths.unshift(newPath);
|
|
75
|
-
return newPath;
|
|
76
|
-
}, "");
|
|
77
|
-
for (let index = 0; index < paths.length; index++) {
|
|
78
|
-
const p = paths[index];
|
|
79
|
-
if (obj[p]) {
|
|
80
|
-
obj[p].children!.push(currentObj);
|
|
81
|
-
break;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
obj["/"].errorElement = <ErrorBoundary />;
|
|
86
|
-
routes.push({
|
|
87
|
-
path: "/",
|
|
88
|
-
element: <ProgressLayout />,
|
|
89
|
-
children: [obj["/"]],
|
|
90
|
-
});
|
|
91
|
-
routes.push({
|
|
92
|
-
path: "*",
|
|
93
|
-
element: <Navigate to={obj["/404"] ? "/404" : "/"} replace />,
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
export { routes };
|
package/src/types/index.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { Params } from "react-router";
|
|
2
|
-
|
|
3
|
-
export interface Ctx {
|
|
4
|
-
title: string;
|
|
5
|
-
signed: boolean;
|
|
6
|
-
noPermission: boolean;
|
|
7
|
-
params: Params;
|
|
8
|
-
query: Params;
|
|
9
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
10
|
-
data?: any;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export type Meta = {
|
|
14
|
-
permission?: number;
|
|
15
|
-
loader?: Loader;
|
|
16
|
-
} & Record<string, unknown>;
|
|
17
|
-
|
|
18
|
-
export type Loader = (ctx: Ctx, meta: Meta) => void | Promise<void>;
|
package/src/utils/hooks.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { useEffect, useRef, useState } from "react";
|
|
2
|
-
|
|
3
|
-
export function useDebouncedValue<T>(value: T): T {
|
|
4
|
-
const [result, setResult] = useState(value);
|
|
5
|
-
const timer = useRef<number | undefined>(undefined);
|
|
6
|
-
useEffect(() => {
|
|
7
|
-
clearTimeout(timer.current);
|
|
8
|
-
timer.current = setTimeout(() => {
|
|
9
|
-
setResult(value);
|
|
10
|
-
}, 700);
|
|
11
|
-
return () => {
|
|
12
|
-
clearTimeout(timer.current);
|
|
13
|
-
};
|
|
14
|
-
}, [result, value]);
|
|
15
|
-
return result;
|
|
16
|
-
}
|