@lxzy/code-snippet 0.0.10 → 0.0.12
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/.opencode/commands/push-yw.md +1 -1
- package/package.json +1 -1
- package/routes/menu.ts +2 -0
- package/src/App.tsx +33 -6
- package/src/layouts/index.tsx +13 -2
- package/src/pages/home.tsx +3 -1
- package/src/utils/getIcon.tsx +16 -0
package/package.json
CHANGED
package/routes/menu.ts
CHANGED
package/src/App.tsx
CHANGED
|
@@ -1,18 +1,48 @@
|
|
|
1
1
|
import React, { Suspense, lazy } from 'react';
|
|
2
|
-
import { Switch, Route, Redirect } from 'react-router-dom';
|
|
2
|
+
import { Switch, Route, Redirect, useHistory, useLocation } from 'react-router-dom';
|
|
3
3
|
import { Router } from 'react-router-dom';
|
|
4
4
|
import history from '@/umi';
|
|
5
5
|
import BasicLayout from '@/layouts/index';
|
|
6
6
|
import routes from '../routes';
|
|
7
7
|
|
|
8
|
+
const pageModules = import.meta.glob('./pages/**/*.tsx');
|
|
9
|
+
|
|
8
10
|
const componentCache = {};
|
|
9
11
|
const loadComponent = (path) => {
|
|
10
12
|
if (!componentCache[path]) {
|
|
11
|
-
|
|
13
|
+
const factory: any =
|
|
14
|
+
pageModules[`./pages/${path}.tsx`] ||
|
|
15
|
+
pageModules[`./pages/${path}/index.tsx`];
|
|
16
|
+
if (!factory) {
|
|
17
|
+
throw new Error(`Page component not found: ${path}`);
|
|
18
|
+
}
|
|
19
|
+
componentCache[path] = lazy(factory);
|
|
12
20
|
}
|
|
13
21
|
return componentCache[path];
|
|
14
22
|
};
|
|
15
23
|
|
|
24
|
+
const parseQuery = (search) => {
|
|
25
|
+
if (!search) return {};
|
|
26
|
+
const params = new URLSearchParams(search);
|
|
27
|
+
const query = {};
|
|
28
|
+
for (const [key, value] of params) {
|
|
29
|
+
query[key] = value;
|
|
30
|
+
}
|
|
31
|
+
return query;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const RouteWrapper = ({ componentPath }) => {
|
|
35
|
+
const routeHistory = useHistory();
|
|
36
|
+
const routeLocation = useLocation();
|
|
37
|
+
const Component = loadComponent(componentPath);
|
|
38
|
+
return (
|
|
39
|
+
<Component
|
|
40
|
+
history={routeHistory}
|
|
41
|
+
location={{ ...routeLocation, query: parseQuery(routeLocation.search) }}
|
|
42
|
+
/>
|
|
43
|
+
);
|
|
44
|
+
};
|
|
45
|
+
|
|
16
46
|
const App = () => {
|
|
17
47
|
return (
|
|
18
48
|
<Router history={history}>
|
|
@@ -29,10 +59,7 @@ const App = () => {
|
|
|
29
59
|
key={route.path || i}
|
|
30
60
|
path={route.path}
|
|
31
61
|
exact={route.path !== '/404'}
|
|
32
|
-
render={() => {
|
|
33
|
-
const Component = loadComponent(route.component);
|
|
34
|
-
return <Component />;
|
|
35
|
-
}}
|
|
62
|
+
render={() => <RouteWrapper componentPath={route.component} />}
|
|
36
63
|
/>
|
|
37
64
|
),
|
|
38
65
|
)}
|
package/src/layouts/index.tsx
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
|
-
import React from 'react';
|
|
1
|
+
import React, { useState, useEffect } from 'react';
|
|
2
2
|
import { Avatar, Dropdown, Menu, Space } from 'antd';
|
|
3
3
|
import ProLayout from '@ant-design/pro-layout';
|
|
4
4
|
import history from '@/umi';
|
|
5
5
|
import menuRoutes from '../../routes/menu';
|
|
6
|
+
import getIcon from '@/utils/getIcon';
|
|
6
7
|
|
|
7
8
|
const menuDataRender = (menuList) => {
|
|
8
9
|
return menuList.map((item) => {
|
|
9
10
|
const localItem = {
|
|
10
11
|
...item,
|
|
12
|
+
icon: item.icon ? getIcon(item.icon) : undefined,
|
|
11
13
|
children: item.children ? menuDataRender(item.children) : undefined,
|
|
12
14
|
};
|
|
13
15
|
return localItem;
|
|
@@ -16,6 +18,14 @@ const menuDataRender = (menuList) => {
|
|
|
16
18
|
|
|
17
19
|
const BasicLayout = (props) => {
|
|
18
20
|
const { children } = props;
|
|
21
|
+
const [location, setLocation] = useState(history.location);
|
|
22
|
+
|
|
23
|
+
useEffect(() => {
|
|
24
|
+
const unlisten = history.listen((location) => {
|
|
25
|
+
setLocation(location);
|
|
26
|
+
});
|
|
27
|
+
return () => unlisten();
|
|
28
|
+
}, []);
|
|
19
29
|
|
|
20
30
|
return (
|
|
21
31
|
<ProLayout
|
|
@@ -24,9 +34,10 @@ const BasicLayout = (props) => {
|
|
|
24
34
|
layout="mix"
|
|
25
35
|
fixSiderbar={false}
|
|
26
36
|
fixedHeader
|
|
37
|
+
location={location}
|
|
27
38
|
route={{
|
|
28
39
|
path: '/',
|
|
29
|
-
routes: menuRoutes,
|
|
40
|
+
routes: menuDataRender(menuRoutes),
|
|
30
41
|
}}
|
|
31
42
|
menuItemRender={(item, dom) => (
|
|
32
43
|
<a onClick={() => history.push(item.path || '/')}>{dom}</a>
|
package/src/pages/home.tsx
CHANGED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import * as Icons from '@ant-design/icons';
|
|
3
|
+
|
|
4
|
+
const getIcon = (name) => {
|
|
5
|
+
if (!name) return undefined;
|
|
6
|
+
const capitalize = (s) => s.charAt(0).toUpperCase() + s.slice(1);
|
|
7
|
+
const parts = name.split('-');
|
|
8
|
+
const baseName = parts.map(capitalize).join('');
|
|
9
|
+
const hasSuffix = name.endsWith('Filled') || name.endsWith('TwoTone');
|
|
10
|
+
const componentName = hasSuffix ? baseName : baseName + 'Outlined';
|
|
11
|
+
const IconComp = Icons[componentName];
|
|
12
|
+
if (!IconComp) return undefined;
|
|
13
|
+
return React.createElement(IconComp);
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export default getIcon;
|