create-secra 0.1.5 → 0.1.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.
Files changed (64) hide show
  1. package/README.md +6 -0
  2. package/README.zh-CN.md +6 -0
  3. package/antd-adapter-template/apps/core/index.html +13 -0
  4. package/antd-adapter-template/apps/core/package.json +18 -0
  5. package/antd-adapter-template/apps/core/public/favicon.ico +1 -0
  6. package/antd-adapter-template/apps/core/public/favicon.svg +1 -0
  7. package/antd-adapter-template/apps/core/public/logo.svg +1 -0
  8. package/antd-adapter-template/apps/core/src/api/auth.ts +49 -0
  9. package/antd-adapter-template/apps/core/src/assets/react.svg +1 -0
  10. package/antd-adapter-template/apps/core/src/components/AntdGlobalProvider.tsx +87 -0
  11. package/antd-adapter-template/apps/core/src/components/AntdRootLayout.tsx +10 -0
  12. package/antd-adapter-template/apps/core/src/components/layout.tsx +387 -0
  13. package/antd-adapter-template/apps/core/src/guards/auth-route-guard.ts +45 -0
  14. package/antd-adapter-template/apps/core/src/main.tsx +65 -0
  15. package/antd-adapter-template/apps/core/src/pages/auth/components/account-login-fields.tsx +60 -0
  16. package/antd-adapter-template/apps/core/src/pages/auth/components/phone-login-fields.tsx +60 -0
  17. package/antd-adapter-template/apps/core/src/pages/auth/login.tsx +169 -0
  18. package/antd-adapter-template/apps/core/src/pages/index.tsx +156 -0
  19. package/antd-adapter-template/apps/core/src/router.ts +42 -0
  20. package/antd-adapter-template/apps/core/src/shims/use-sync-external-store-shim.ts +3 -0
  21. package/antd-adapter-template/apps/core/src/theme/theme.css +48 -0
  22. package/antd-adapter-template/apps/core/src/types/crypto-js.d.ts +5 -0
  23. package/antd-adapter-template/apps/core/src/utils/index.ts +12 -0
  24. package/antd-adapter-template/apps/core/src/utils/md5.ts +6 -0
  25. package/antd-adapter-template/apps/core/tsconfig.app.json +11 -0
  26. package/antd-adapter-template/apps/core/tsconfig.json +13 -0
  27. package/antd-adapter-template/apps/core/tsconfig.node.json +7 -0
  28. package/antd-adapter-template/apps/core/vite.config.ts +118 -0
  29. package/antd-adapter-template/eslint.config.js +23 -0
  30. package/antd-adapter-template/package.json +63 -0
  31. package/antd-adapter-template/packages/sdk/.swcrc +18 -0
  32. package/antd-adapter-template/packages/sdk/package.json +52 -0
  33. package/antd-adapter-template/packages/sdk/src/build/index.ts +28 -0
  34. package/antd-adapter-template/packages/sdk/src/build/plugins/auto-import.ts +46 -0
  35. package/antd-adapter-template/packages/sdk/src/build/plugins/bundle-analyzer.ts +33 -0
  36. package/antd-adapter-template/packages/sdk/src/build/plugins/remove-console.ts +23 -0
  37. package/antd-adapter-template/packages/sdk/src/build/plugins/unocss.ts +202 -0
  38. package/antd-adapter-template/packages/sdk/src/build/plugins/unplugin-icon.ts +43 -0
  39. package/antd-adapter-template/packages/sdk/src/components/i18n-switch-dropdown.tsx +139 -0
  40. package/antd-adapter-template/packages/sdk/src/components/index.ts +2 -0
  41. package/antd-adapter-template/packages/sdk/src/components/theme-switch-dropdown.tsx +131 -0
  42. package/antd-adapter-template/packages/sdk/src/hooks/auth/core.ts +101 -0
  43. package/antd-adapter-template/packages/sdk/src/hooks/auth/index.ts +139 -0
  44. package/antd-adapter-template/packages/sdk/src/hooks/auth/with-auth.tsx +41 -0
  45. package/antd-adapter-template/packages/sdk/src/hooks/index.ts +1 -0
  46. package/antd-adapter-template/packages/sdk/src/i18n/index.ts +150 -0
  47. package/antd-adapter-template/packages/sdk/src/index.ts +11 -0
  48. package/antd-adapter-template/packages/sdk/src/request/index.ts +436 -0
  49. package/antd-adapter-template/packages/sdk/src/storage/README.md +30 -0
  50. package/antd-adapter-template/packages/sdk/src/storage/index.ts +57 -0
  51. package/antd-adapter-template/packages/sdk/src/styles/reset.css +111 -0
  52. package/antd-adapter-template/packages/sdk/src/theme/index.ts +466 -0
  53. package/antd-adapter-template/packages/sdk/tsconfig.json +16 -0
  54. package/antd-adapter-template/pnpm-workspace.yaml +3 -0
  55. package/antd-adapter-template/tsconfig.app.json +29 -0
  56. package/antd-adapter-template/tsconfig.json +7 -0
  57. package/antd-adapter-template/tsconfig.node.json +27 -0
  58. package/antd-adapter-template/turbo.json +17 -0
  59. package/bin/index.mjs +165 -33
  60. package/package.json +3 -2
  61. package/template/apps/core/src/main.tsx +11 -18
  62. package/template/apps/core/src/router.ts +5 -1
  63. package/template/package.json +1 -1
  64. package/template/packages/sdk/src/build/plugins/unocss.ts +3 -0
package/README.md CHANGED
@@ -38,6 +38,12 @@ You can temporarily point to another local template path:
38
38
  SECRA_TEMPLATE=/absolute/path/to/template pnpm create secra my-app
39
39
  ```
40
40
 
41
+ Use the built-in antd adapter template:
42
+
43
+ ```bash
44
+ SECRA_TEMPLATE_NAME=antd-adapter-template pnpm create secra my-app
45
+ ```
46
+
41
47
  ## Maintain the Built-in Template
42
48
 
43
49
  If you want to refresh `template/` from another project (for example `secra-front`), copy files with excludes:
package/README.zh-CN.md CHANGED
@@ -38,6 +38,12 @@ npm create secra@latest my-app
38
38
  SECRA_TEMPLATE=/absolute/path/to/template pnpm create secra my-app
39
39
  ```
40
40
 
41
+ 使用内置 antd 适配模板:
42
+
43
+ ```bash
44
+ SECRA_TEMPLATE_NAME=antd-adapter-template pnpm create secra my-app
45
+ ```
46
+
41
47
  ## 维护内置模板
42
48
 
43
49
  如果你想从其他项目(例如 `secra-front`)更新 `template/`,可执行:
@@ -0,0 +1,13 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <link rel="icon" type="image/svg+xml" href="/favicon.svg" />
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
+ <title>core</title>
8
+ </head>
9
+ <body>
10
+ <div id="root"></div>
11
+ <script type="module" src="/src/main.tsx"></script>
12
+ </body>
13
+ </html>
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "core",
3
+ "private": true,
4
+ "version": "0.0.15",
5
+ "type": "module",
6
+ "scripts": {
7
+ "dev": "vite",
8
+ "build": "tsc -b && vite build",
9
+ "lint": "eslint .",
10
+ "preview": "vite preview"
11
+ },
12
+ "dependencies": {
13
+ "@reduxjs/toolkit": "^2.11.0",
14
+ "@vlian/sdk": "workspace:*",
15
+ "crypto-js": "^4.2.0",
16
+ "lucide-react": "^0.575.0"
17
+ }
18
+ }
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 109.07 100"><path fill="#24d6fe" d="M54.65 100l18.21-25.37H36.22L54.65 100z"/><path fill="#24d6fe" d="M72.86 24.94l-.07-.09L54.54 0 36.28 24.85l18.26 24.84 18.32 24.94 18.18 24.75 18.03-25.14-36.21-49.3z"/><path fill="#9e5df9" opacity=".5" d="M72.79 24.85L54.54 0 36.28 24.85l18.26 24.84 18.25-24.84z"/><path fill="#9e5df9" d="M36.22 49.69H18.03L0 74.24 18.09 99.3l18.13-24.67 18.32-24.94H36.22z"/></svg>
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 109.07 100"><path fill="#24d6fe" d="M54.65 100l18.21-25.37H36.22L54.65 100z"/><path fill="#24d6fe" d="M72.86 24.94l-.07-.09L54.54 0 36.28 24.85l18.26 24.84 18.32 24.94 18.18 24.75 18.03-25.14-36.21-49.3z"/><path fill="#9e5df9" opacity=".5" d="M72.79 24.85L54.54 0 36.28 24.85l18.26 24.84 18.25-24.84z"/><path fill="#9e5df9" d="M36.22 49.69H18.03L0 74.24 18.09 99.3l18.13-24.67 18.32-24.94H36.22z"/></svg>
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 109.07 100"><path fill="#24d6fe" d="M54.65 100l18.21-25.37H36.22L54.65 100z"/><path fill="#24d6fe" d="M72.86 24.94l-.07-.09L54.54 0 36.28 24.85l18.26 24.84 18.32 24.94 18.18 24.75 18.03-25.14-36.21-49.3z"/><path fill="#9e5df9" opacity=".5" d="M72.79 24.85L54.54 0 36.28 24.85l18.26 24.84 18.25-24.84z"/><path fill="#9e5df9" d="M36.22 49.69H18.03L0 74.24 18.09 99.3l18.13-24.67 18.32-24.94H36.22z"/></svg>
@@ -0,0 +1,49 @@
1
+ export interface LoginRequest extends Record<string, unknown> {
2
+ identifier: string;
3
+ password: string;
4
+ }
5
+
6
+ export interface LoginUser {
7
+ id: string;
8
+ tenant_id: string;
9
+ display_name: string;
10
+ email: string | null;
11
+ phone: string | null;
12
+ }
13
+
14
+ export interface LoginResponse {
15
+ access_token: string;
16
+ refresh_token: string;
17
+ token_type: string;
18
+ expires_in: number;
19
+ refresh_expires_in: number;
20
+ user: LoginUser;
21
+ require_password_change: boolean;
22
+ }
23
+
24
+ const MOCK_PASSWORD_MD5 = "0192023a7bbd73250516f069df18b500";
25
+
26
+ export const login = async (payload: LoginRequest): Promise<LoginResponse> => {
27
+ await new Promise((resolve) => setTimeout(resolve, 200));
28
+ const identifier = payload.identifier.trim();
29
+ if (!identifier || payload.password !== MOCK_PASSWORD_MD5) {
30
+ throw new Error("用户名或密码错误");
31
+ }
32
+
33
+ const now = Date.now();
34
+ return {
35
+ access_token: `mock-access-token-${now}`,
36
+ refresh_token: `mock-refresh-token-${now}`,
37
+ token_type: "Bearer",
38
+ expires_in: 7200,
39
+ refresh_expires_in: 2592000,
40
+ user: {
41
+ id: "mock-user-1",
42
+ tenant_id: "mock-tenant-1",
43
+ display_name: identifier,
44
+ email: `${identifier}@example.com`,
45
+ phone: null,
46
+ },
47
+ require_password_change: false,
48
+ };
49
+ };
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="35.93" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 228"><path fill="#00D8FF" d="M210.483 73.824a171.49 171.49 0 0 0-8.24-2.597c.465-1.9.893-3.777 1.273-5.621c6.238-30.281 2.16-54.676-11.769-62.708c-13.355-7.7-35.196.329-57.254 19.526a171.23 171.23 0 0 0-6.375 5.848a155.866 155.866 0 0 0-4.241-3.917C100.759 3.829 77.587-4.822 63.673 3.233C50.33 10.957 46.379 33.89 51.995 62.588a170.974 170.974 0 0 0 1.892 8.48c-3.28.932-6.445 1.924-9.474 2.98C17.309 83.498 0 98.307 0 113.668c0 15.865 18.582 31.778 46.812 41.427a145.52 145.52 0 0 0 6.921 2.165a167.467 167.467 0 0 0-2.01 9.138c-5.354 28.2-1.173 50.591 12.134 58.266c13.744 7.926 36.812-.22 59.273-19.855a145.567 145.567 0 0 0 5.342-4.923a168.064 168.064 0 0 0 6.92 6.314c21.758 18.722 43.246 26.282 56.54 18.586c13.731-7.949 18.194-32.003 12.4-61.268a145.016 145.016 0 0 0-1.535-6.842c1.62-.48 3.21-.974 4.76-1.488c29.348-9.723 48.443-25.443 48.443-41.52c0-15.417-17.868-30.326-45.517-39.844Zm-6.365 70.984c-1.4.463-2.836.91-4.3 1.345c-3.24-10.257-7.612-21.163-12.963-32.432c5.106-11 9.31-21.767 12.459-31.957c2.619.758 5.16 1.557 7.61 2.4c23.69 8.156 38.14 20.213 38.14 29.504c0 9.896-15.606 22.743-40.946 31.14Zm-10.514 20.834c2.562 12.94 2.927 24.64 1.23 33.787c-1.524 8.219-4.59 13.698-8.382 15.893c-8.067 4.67-25.32-1.4-43.927-17.412a156.726 156.726 0 0 1-6.437-5.87c7.214-7.889 14.423-17.06 21.459-27.246c12.376-1.098 24.068-2.894 34.671-5.345a134.17 134.17 0 0 1 1.386 6.193ZM87.276 214.515c-7.882 2.783-14.16 2.863-17.955.675c-8.075-4.657-11.432-22.636-6.853-46.752a156.923 156.923 0 0 1 1.869-8.499c10.486 2.32 22.093 3.988 34.498 4.994c7.084 9.967 14.501 19.128 21.976 27.15a134.668 134.668 0 0 1-4.877 4.492c-9.933 8.682-19.886 14.842-28.658 17.94ZM50.35 144.747c-12.483-4.267-22.792-9.812-29.858-15.863c-6.35-5.437-9.555-10.836-9.555-15.216c0-9.322 13.897-21.212 37.076-29.293c2.813-.98 5.757-1.905 8.812-2.773c3.204 10.42 7.406 21.315 12.477 32.332c-5.137 11.18-9.399 22.249-12.634 32.792a134.718 134.718 0 0 1-6.318-1.979Zm12.378-84.26c-4.811-24.587-1.616-43.134 6.425-47.789c8.564-4.958 27.502 2.111 47.463 19.835a144.318 144.318 0 0 1 3.841 3.545c-7.438 7.987-14.787 17.08-21.808 26.988c-12.04 1.116-23.565 2.908-34.161 5.309a160.342 160.342 0 0 1-1.76-7.887Zm110.427 27.268a347.8 347.8 0 0 0-7.785-12.803c8.168 1.033 15.994 2.404 23.343 4.08c-2.206 7.072-4.956 14.465-8.193 22.045a381.151 381.151 0 0 0-7.365-13.322Zm-45.032-43.861c5.044 5.465 10.096 11.566 15.065 18.186a322.04 322.04 0 0 0-30.257-.006c4.974-6.559 10.069-12.652 15.192-18.18ZM82.802 87.83a323.167 323.167 0 0 0-7.227 13.238c-3.184-7.553-5.909-14.98-8.134-22.152c7.304-1.634 15.093-2.97 23.209-3.984a321.524 321.524 0 0 0-7.848 12.897Zm8.081 65.352c-8.385-.936-16.291-2.203-23.593-3.793c2.26-7.3 5.045-14.885 8.298-22.6a321.187 321.187 0 0 0 7.257 13.246c2.594 4.48 5.28 8.868 8.038 13.147Zm37.542 31.03c-5.184-5.592-10.354-11.779-15.403-18.433c4.902.192 9.899.29 14.978.29c5.218 0 10.376-.117 15.453-.343c-4.985 6.774-10.018 12.97-15.028 18.486Zm52.198-57.817c3.422 7.8 6.306 15.345 8.596 22.52c-7.422 1.694-15.436 3.058-23.88 4.071a382.417 382.417 0 0 0 7.859-13.026a347.403 347.403 0 0 0 7.425-13.565Zm-16.898 8.101a358.557 358.557 0 0 1-12.281 19.815a329.4 329.4 0 0 1-23.444.823c-7.967 0-15.716-.248-23.178-.732a310.202 310.202 0 0 1-12.513-19.846h.001a307.41 307.41 0 0 1-10.923-20.627a310.278 310.278 0 0 1 10.89-20.637l-.001.001a307.318 307.318 0 0 1 12.413-19.761c7.613-.576 15.42-.876 23.31-.876H128c7.926 0 15.743.303 23.354.883a329.357 329.357 0 0 1 12.335 19.695a358.489 358.489 0 0 1 11.036 20.54a329.472 329.472 0 0 1-11 20.722Zm22.56-122.124c8.572 4.944 11.906 24.881 6.52 51.026c-.344 1.668-.73 3.367-1.15 5.09c-10.622-2.452-22.155-4.275-34.23-5.408c-7.034-10.017-14.323-19.124-21.64-27.008a160.789 160.789 0 0 1 5.888-5.4c18.9-16.447 36.564-22.941 44.612-18.3ZM128 90.808c12.625 0 22.86 10.235 22.86 22.86s-10.235 22.86-22.86 22.86s-22.86-10.235-22.86-22.86s10.235-22.86 22.86-22.86Z"></path></svg>
@@ -0,0 +1,87 @@
1
+ import { useEffect, useMemo, useState } from 'react';
2
+ import type { PropsWithChildren } from 'react';
3
+ import { buildAntdCompatTheme, useConfig, useTheme } from '@vlian/framework/core';
4
+ import type { ThemeSnapshot } from '@vlian/framework/core';
5
+ import { App as AntdApp, ConfigProvider, theme as antdTheme } from 'antd';
6
+ import type { AppProps, ConfigProviderProps } from 'antd';
7
+
8
+ type UIProviderShape = {
9
+ antd?: Record<string, unknown>;
10
+ };
11
+
12
+ const defaultTheme: ThemeSnapshot = {
13
+ mode: 'light',
14
+ };
15
+
16
+ const getSystemDark = (): boolean => {
17
+ if (typeof window === 'undefined') {
18
+ return false;
19
+ }
20
+ return window.matchMedia('(prefers-color-scheme: dark)').matches;
21
+ };
22
+
23
+ export function useAntdRuntimeTheme(): ConfigProviderProps['theme'] {
24
+ const { theme } = useTheme();
25
+ const { antdConfig } = useConfig();
26
+ const providerConfig = antdConfig as UIProviderShape | undefined;
27
+ const [isSystemDark, setIsSystemDark] = useState<boolean>(getSystemDark);
28
+
29
+ useEffect(() => {
30
+ if (typeof window === 'undefined') {
31
+ return;
32
+ }
33
+ const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
34
+ const handleChange = (event: MediaQueryListEvent) => {
35
+ setIsSystemDark(event.matches);
36
+ };
37
+ setIsSystemDark(mediaQuery.matches);
38
+
39
+ if (typeof mediaQuery.addEventListener === 'function') {
40
+ mediaQuery.addEventListener('change', handleChange);
41
+ return () => {
42
+ mediaQuery.removeEventListener('change', handleChange);
43
+ };
44
+ }
45
+
46
+ mediaQuery.addListener(handleChange);
47
+ return () => {
48
+ mediaQuery.removeListener(handleChange);
49
+ };
50
+ }, []);
51
+
52
+ return useMemo(() => {
53
+ const resolvedMode: ThemeSnapshot['mode'] =
54
+ theme.mode === 'system' ? (isSystemDark ? 'dark' : 'light') : ((theme.mode || 'light') as ThemeSnapshot['mode']);
55
+
56
+ const mergedTheme: ThemeSnapshot = {
57
+ ...defaultTheme,
58
+ mode: resolvedMode,
59
+ primaryColor: theme.primaryColor || '#1677ff',
60
+ compatibility: {
61
+ antd: providerConfig?.antd || {},
62
+ },
63
+ };
64
+
65
+ return {
66
+ ...buildAntdCompatTheme(mergedTheme),
67
+ algorithm:
68
+ mergedTheme.mode === 'dark' ? antdTheme.darkAlgorithm : antdTheme.defaultAlgorithm,
69
+ } as ConfigProviderProps['theme'];
70
+ }, [isSystemDark, providerConfig?.antd, theme.mode, theme.primaryColor]);
71
+ }
72
+
73
+ interface AntdGlobalProviderProps extends PropsWithChildren {
74
+ appProps?: AppProps;
75
+ }
76
+
77
+ export function AntdGlobalProvider({ children, appProps }: AntdGlobalProviderProps) {
78
+ const runtimeTheme = useAntdRuntimeTheme();
79
+
80
+ return (
81
+ <ConfigProvider theme={runtimeTheme}>
82
+ <AntdApp {...appProps}>{children}</AntdApp>
83
+ </ConfigProvider>
84
+ );
85
+ }
86
+
87
+ export default AntdGlobalProvider;
@@ -0,0 +1,10 @@
1
+ import { Outlet } from "react-router-dom";
2
+ import { AntdGlobalProvider } from "./AntdGlobalProvider";
3
+
4
+ export default function AntdRootLayout() {
5
+ return (
6
+ <AntdGlobalProvider>
7
+ <Outlet />
8
+ </AntdGlobalProvider>
9
+ );
10
+ }
@@ -0,0 +1,387 @@
1
+ import { useMemo, useState, type ReactNode } from "react";
2
+ import {
3
+ AudioWaveform,
4
+ BadgeCheck,
5
+ Bell,
6
+ BookOpen,
7
+ Bot,
8
+ ChevronRight,
9
+ ChevronsUpDown,
10
+ Command,
11
+ CreditCard,
12
+ Folder,
13
+ Forward,
14
+ Frame,
15
+ GalleryVerticalEnd,
16
+ LogOut,
17
+ Map,
18
+ MoreHorizontal,
19
+ PanelLeft,
20
+ PieChart,
21
+ Plus,
22
+ Settings2,
23
+ Sparkles,
24
+ SquareTerminal,
25
+ Trash2,
26
+ type LucideIcon,
27
+ } from "lucide-react";
28
+
29
+ const cx = (...values: Array<string | false | null | undefined>) => values.filter(Boolean).join(" ");
30
+
31
+ type LayoutProps = {
32
+ children?: ReactNode;
33
+ };
34
+
35
+ type Team = {
36
+ name: string;
37
+ logo: LucideIcon;
38
+ plan: string;
39
+ };
40
+
41
+ type MainNavItem = {
42
+ title: string;
43
+ url: string;
44
+ icon: LucideIcon;
45
+ isActive?: boolean;
46
+ items: { title: string; url: string }[];
47
+ };
48
+
49
+ type ProjectItem = {
50
+ name: string;
51
+ url: string;
52
+ icon: LucideIcon;
53
+ };
54
+
55
+ const DATA = {
56
+ user: {
57
+ name: "Secra Admin",
58
+ email: "m@example.com",
59
+ },
60
+ teams: [
61
+ { name: "Acme Inc", logo: GalleryVerticalEnd, plan: "Enterprise" },
62
+ { name: "Acme Corp.", logo: AudioWaveform, plan: "Startup" },
63
+ { name: "Evil Corp.", logo: Command, plan: "Free" },
64
+ ] as Team[],
65
+ navMain: [
66
+ {
67
+ title: "Playground",
68
+ url: "#",
69
+ icon: SquareTerminal,
70
+ isActive: true,
71
+ items: [
72
+ { title: "History", url: "#" },
73
+ { title: "Starred", url: "#" },
74
+ { title: "Settings", url: "#" },
75
+ ],
76
+ },
77
+ {
78
+ title: "Models",
79
+ url: "#",
80
+ icon: Bot,
81
+ items: [
82
+ { title: "Genesis", url: "#" },
83
+ { title: "Explorer", url: "#" },
84
+ { title: "Quantum", url: "#" },
85
+ ],
86
+ },
87
+ {
88
+ title: "Documentation",
89
+ url: "#",
90
+ icon: BookOpen,
91
+ items: [
92
+ { title: "Introduction", url: "#" },
93
+ { title: "Get Started", url: "#" },
94
+ { title: "Tutorials", url: "#" },
95
+ { title: "Changelog", url: "#" },
96
+ ],
97
+ },
98
+ {
99
+ title: "Settings",
100
+ url: "#",
101
+ icon: Settings2,
102
+ items: [
103
+ { title: "General", url: "#" },
104
+ { title: "Team", url: "#" },
105
+ { title: "Billing", url: "#" },
106
+ { title: "Limits", url: "#" },
107
+ ],
108
+ },
109
+ ] as MainNavItem[],
110
+ projects: [
111
+ { name: "Design Engineering", url: "#", icon: Frame },
112
+ { name: "Sales & Marketing", url: "#", icon: PieChart },
113
+ { name: "Travel", url: "#", icon: Map },
114
+ ] as ProjectItem[],
115
+ };
116
+
117
+ function Layout({ children }: LayoutProps) {
118
+ const [collapsed, setCollapsed] = useState(false);
119
+ const [activeTeam, setActiveTeam] = useState(DATA.teams[0]);
120
+ const [teamMenuOpen, setTeamMenuOpen] = useState(false);
121
+ const [userMenuOpen, setUserMenuOpen] = useState(false);
122
+ const [projectMenuOpen, setProjectMenuOpen] = useState<string | null>(null);
123
+ const [openMap, setOpenMap] = useState<Record<string, boolean>>(
124
+ Object.fromEntries(DATA.navMain.map((item) => [item.title, Boolean(item.isActive)])),
125
+ );
126
+
127
+ const pageTitle = useMemo(() => "Data Fetching", []);
128
+
129
+ return (
130
+ <div className="flex min-h-screen w-full bg-[hsl(var(--background))] text-[hsl(var(--foreground))]">
131
+ <aside
132
+ className={cx(
133
+ "relative flex shrink-0 flex-col bg-[hsl(var(--card))] transition-[width] duration-200",
134
+ collapsed ? "w-14" : "w-64",
135
+ )}
136
+ >
137
+ <div className="border-b border-[hsl(var(--border))] p-2">
138
+ <div className="relative">
139
+ <button
140
+ type="button"
141
+ onClick={() => setTeamMenuOpen((v) => !v)}
142
+ className={cx(
143
+ "flex h-12 w-full items-center gap-2 rounded-md px-2 text-left hover:bg-[hsl(var(--accent))]",
144
+ collapsed && "justify-center px-0",
145
+ )}
146
+ >
147
+ <div className="flex h-8 w-8 shrink-0 items-center justify-center rounded-lg bg-[hsl(var(--primary))] text-[hsl(var(--primary-foreground))]">
148
+ <activeTeam.logo className="h-4 w-4" aria-hidden />
149
+ </div>
150
+ {collapsed ? null : (
151
+ <>
152
+ <div className="grid min-w-0 flex-1 text-sm leading-tight">
153
+ <span className="truncate font-medium">{activeTeam.name}</span>
154
+ <span className="truncate text-xs text-[hsl(var(--muted-foreground))]">{activeTeam.plan}</span>
155
+ </div>
156
+ <ChevronsUpDown className="h-4 w-4" aria-hidden />
157
+ </>
158
+ )}
159
+ </button>
160
+
161
+ {!collapsed && teamMenuOpen ? (
162
+ <div className="absolute top-full left-0 z-50 mt-1 w-full min-w-56 rounded-lg border border-[hsl(var(--border))] bg-[hsl(var(--popover))] p-1 text-[hsl(var(--popover-foreground))] shadow-md">
163
+ <div className="px-2 py-1 text-xs text-[hsl(var(--muted-foreground))]">Teams</div>
164
+ {DATA.teams.map((team, index) => (
165
+ <button
166
+ key={team.name}
167
+ type="button"
168
+ onClick={() => {
169
+ setActiveTeam(team);
170
+ setTeamMenuOpen(false);
171
+ }}
172
+ className="flex w-full items-center gap-2 rounded-sm p-2 text-left text-sm hover:bg-[hsl(var(--accent))]"
173
+ >
174
+ <div className="flex h-6 w-6 items-center justify-center rounded-md border border-[hsl(var(--border))]">
175
+ <team.logo className="h-3.5 w-3.5" aria-hidden />
176
+ </div>
177
+ <span className="flex-1 truncate">{team.name}</span>
178
+ <span className="text-xs text-[hsl(var(--muted-foreground))]">{index + 1}</span>
179
+ </button>
180
+ ))}
181
+ <div className="my-1 h-px bg-[hsl(var(--border))]" />
182
+ <button
183
+ type="button"
184
+ className="flex w-full items-center gap-2 rounded-sm p-2 text-left text-sm hover:bg-[hsl(var(--accent))]"
185
+ >
186
+ <div className="flex h-6 w-6 items-center justify-center rounded-md border border-[hsl(var(--border))]">
187
+ <Plus className="h-4 w-4" aria-hidden />
188
+ </div>
189
+ <span className="text-[hsl(var(--muted-foreground))]">Add team</span>
190
+ </button>
191
+ </div>
192
+ ) : null}
193
+ </div>
194
+ </div>
195
+
196
+ <div className="flex min-h-0 flex-1 flex-col gap-3 overflow-auto p-2">
197
+ {!collapsed ? <div className="px-2 text-xs text-[hsl(var(--muted-foreground))]">Platform</div> : null}
198
+ <ul className="m-0 list-none space-y-1 p-0">
199
+ {DATA.navMain.map((item) => {
200
+ const open = openMap[item.title];
201
+
202
+ return (
203
+ <li key={item.title}>
204
+ <button
205
+ type="button"
206
+ onClick={() => setOpenMap((prev) => ({ ...prev, [item.title]: !prev[item.title] }))}
207
+ className={cx(
208
+ "flex h-9 w-full items-center gap-2 rounded-md px-2 text-left text-sm hover:bg-[hsl(var(--accent))]",
209
+ item.isActive && "bg-[hsl(var(--accent))] text-[hsl(var(--accent-foreground))]",
210
+ collapsed && "justify-center px-0",
211
+ )}
212
+ >
213
+ <item.icon className="h-4 w-4 shrink-0" aria-hidden />
214
+ {collapsed ? null : <span className="truncate">{item.title}</span>}
215
+ {!collapsed ? (
216
+ <ChevronRight className={cx("ml-auto h-4 w-4 transition-transform duration-200", open && "rotate-90")} aria-hidden />
217
+ ) : null}
218
+ </button>
219
+
220
+ {!collapsed && open ? (
221
+ <ul className="m-0 ml-4 mt-1 list-none space-y-1 border-l border-[hsl(var(--border))] p-0 pl-2">
222
+ {item.items.map((subItem) => (
223
+ <li key={subItem.title}>
224
+ <a
225
+ href={subItem.url}
226
+ className="flex h-8 items-center rounded-md px-2 text-sm text-[hsl(var(--muted-foreground))] hover:bg-[hsl(var(--accent))] hover:text-[hsl(var(--accent-foreground))]"
227
+ >
228
+ {subItem.title}
229
+ </a>
230
+ </li>
231
+ ))}
232
+ </ul>
233
+ ) : null}
234
+ </li>
235
+ );
236
+ })}
237
+ </ul>
238
+
239
+ {!collapsed ? <div className="mt-2 px-2 text-xs text-[hsl(var(--muted-foreground))]">Projects</div> : null}
240
+ <ul className="m-0 list-none space-y-1 p-0">
241
+ {DATA.projects.map((item) => (
242
+ <li key={item.name} className="relative">
243
+ <a
244
+ href={item.url}
245
+ className={cx(
246
+ "flex h-9 items-center gap-2 rounded-md px-2 text-sm hover:bg-[hsl(var(--accent))]",
247
+ collapsed ? "justify-center px-0" : "pr-10",
248
+ )}
249
+ >
250
+ <item.icon className="h-4 w-4 shrink-0" aria-hidden />
251
+ {collapsed ? null : <span className="truncate">{item.name}</span>}
252
+ </a>
253
+ {!collapsed ? (
254
+ <>
255
+ <button
256
+ type="button"
257
+ onClick={() => setProjectMenuOpen((prev) => (prev === item.name ? null : item.name))}
258
+ className="absolute top-1.5 right-1 inline-flex h-6 w-6 items-center justify-center rounded-md text-[hsl(var(--muted-foreground))] hover:bg-[hsl(var(--accent))]"
259
+ >
260
+ <MoreHorizontal className="h-4 w-4" aria-hidden />
261
+ <span className="sr-only">More</span>
262
+ </button>
263
+ {projectMenuOpen === item.name ? (
264
+ <div className="absolute top-9 right-0 z-50 w-48 rounded-lg border border-[hsl(var(--border))] bg-[hsl(var(--popover))] p-1 text-[hsl(var(--popover-foreground))] shadow-md">
265
+ <button type="button" className="flex w-full items-center gap-2 rounded-sm px-2 py-1.5 text-sm hover:bg-[hsl(var(--accent))]">
266
+ <Folder className="h-4 w-4 text-[hsl(var(--muted-foreground))]" aria-hidden />
267
+ View Project
268
+ </button>
269
+ <button type="button" className="flex w-full items-center gap-2 rounded-sm px-2 py-1.5 text-sm hover:bg-[hsl(var(--accent))]">
270
+ <Forward className="h-4 w-4 text-[hsl(var(--muted-foreground))]" aria-hidden />
271
+ Share Project
272
+ </button>
273
+ <div className="my-1 h-px bg-[hsl(var(--border))]" />
274
+ <button type="button" className="flex w-full items-center gap-2 rounded-sm px-2 py-1.5 text-sm hover:bg-[hsl(var(--accent))]">
275
+ <Trash2 className="h-4 w-4 text-[hsl(var(--muted-foreground))]" aria-hidden />
276
+ Delete Project
277
+ </button>
278
+ </div>
279
+ ) : null}
280
+ </>
281
+ ) : null}
282
+ </li>
283
+ ))}
284
+ </ul>
285
+ </div>
286
+
287
+ <div className="border-t border-[hsl(var(--border))] p-2">
288
+ <div className="relative">
289
+ <button
290
+ type="button"
291
+ onClick={() => setUserMenuOpen((v) => !v)}
292
+ className={cx(
293
+ "flex h-12 w-full items-center gap-2 rounded-md px-2 text-left hover:bg-[hsl(var(--accent))]",
294
+ collapsed && "justify-center px-0",
295
+ )}
296
+ >
297
+ <span className="inline-flex h-8 w-8 items-center justify-center rounded-lg bg-[hsl(var(--muted))] text-xs">CN</span>
298
+ {collapsed ? null : (
299
+ <>
300
+ <div className="grid min-w-0 flex-1 text-sm leading-tight">
301
+ <span className="truncate font-medium">{DATA.user.name}</span>
302
+ <span className="truncate text-xs text-[hsl(var(--muted-foreground))]">{DATA.user.email}</span>
303
+ </div>
304
+ <ChevronsUpDown className="h-4 w-4" aria-hidden />
305
+ </>
306
+ )}
307
+ </button>
308
+
309
+ {!collapsed && userMenuOpen ? (
310
+ <div className="absolute right-0 bottom-14 z-50 min-w-56 rounded-lg border border-[hsl(var(--border))] bg-[hsl(var(--popover))] p-1 text-[hsl(var(--popover-foreground))] shadow-md">
311
+ <button type="button" className="flex w-full items-center gap-2 rounded-sm px-2 py-1.5 text-sm hover:bg-[hsl(var(--accent))]">
312
+ <Sparkles className="h-4 w-4" aria-hidden />
313
+ Upgrade to Pro
314
+ </button>
315
+ <div className="my-1 h-px bg-[hsl(var(--border))]" />
316
+ <button type="button" className="flex w-full items-center gap-2 rounded-sm px-2 py-1.5 text-sm hover:bg-[hsl(var(--accent))]">
317
+ <BadgeCheck className="h-4 w-4" aria-hidden />
318
+ Account
319
+ </button>
320
+ <button type="button" className="flex w-full items-center gap-2 rounded-sm px-2 py-1.5 text-sm hover:bg-[hsl(var(--accent))]">
321
+ <CreditCard className="h-4 w-4" aria-hidden />
322
+ Billing
323
+ </button>
324
+ <button type="button" className="flex w-full items-center gap-2 rounded-sm px-2 py-1.5 text-sm hover:bg-[hsl(var(--accent))]">
325
+ <Bell className="h-4 w-4" aria-hidden />
326
+ Notifications
327
+ </button>
328
+ <div className="my-1 h-px bg-[hsl(var(--border))]" />
329
+ <button type="button" className="flex w-full items-center gap-2 rounded-sm px-2 py-1.5 text-sm hover:bg-[hsl(var(--accent))]">
330
+ <LogOut className="h-4 w-4" aria-hidden />
331
+ Log out
332
+ </button>
333
+ </div>
334
+ ) : null}
335
+ </div>
336
+ </div>
337
+ </aside>
338
+ <div className="w-px shrink-0 bg-[hsl(var(--border))]" />
339
+
340
+ <main className="min-w-0 flex-1">
341
+ <header className="flex h-16 shrink-0 items-center gap-2 transition-[height] ease-linear">
342
+ <div className="flex items-center gap-2 px-4">
343
+ <button
344
+ type="button"
345
+ onClick={() => setCollapsed((prev) => !prev)}
346
+ className="-ml-1 inline-flex h-8 w-8 items-center justify-center rounded-md border border-[hsl(var(--border))] hover:bg-[hsl(var(--accent))]"
347
+ aria-label="Toggle sidebar"
348
+ >
349
+ <PanelLeft className="h-4 w-4" aria-hidden />
350
+ </button>
351
+ <div className="mr-2 h-4 w-px bg-[hsl(var(--border))]" />
352
+ <nav aria-label="breadcrumb">
353
+ <ol className="m-0 flex list-none items-center gap-1.5 p-0 text-sm text-[hsl(var(--muted-foreground))]">
354
+ <li className="hidden md:block">
355
+ <a href="#" className="transition-colors hover:text-[hsl(var(--foreground))]">
356
+ Build Your Application
357
+ </a>
358
+ </li>
359
+ <li className="hidden md:block">/</li>
360
+ <li>
361
+ <span className="text-[hsl(var(--foreground))]">{pageTitle}</span>
362
+ </li>
363
+ </ol>
364
+ </nav>
365
+ </div>
366
+ </header>
367
+ <div className="h-px w-full shrink-0 bg-[hsl(var(--border))]" />
368
+
369
+ <div className="flex flex-1 flex-col gap-4 p-4 pt-0">
370
+ {children ?? (
371
+ <>
372
+ <div className="grid auto-rows-min gap-4 md:grid-cols-3">
373
+ <div className="bg-[hsl(var(--muted))]/50 aspect-video rounded-xl" />
374
+ <div className="bg-[hsl(var(--muted))]/50 aspect-video rounded-xl" />
375
+ <div className="bg-[hsl(var(--muted))]/50 aspect-video rounded-xl" />
376
+ </div>
377
+ <div className="bg-[hsl(var(--muted))]/50 min-h-[100vh] flex-1 rounded-xl md:min-h-min" />
378
+ </>
379
+ )}
380
+ </div>
381
+ </main>
382
+ </div>
383
+ );
384
+ }
385
+
386
+ export type { LayoutProps };
387
+ export default Layout;
@@ -0,0 +1,45 @@
1
+ import type { RouteLocation } from "@vlian/framework/core";
2
+ import {
3
+ ACCESS_TOKEN_KEY,
4
+ getToken,
5
+ REFRESH_TOKEN_KEY,
6
+ } from "@vlian/sdk/hooks/auth";
7
+
8
+ const LOGIN_PATH = "/auth/login";
9
+
10
+ // 登录白名单:这些路由允许匿名访问
11
+ const AUTH_ROUTE_WHITELIST = new Set<string>([LOGIN_PATH]);
12
+
13
+ const isWhitelistedPath = (path: string): boolean => {
14
+ return AUTH_ROUTE_WHITELIST.has(path);
15
+ };
16
+
17
+ const isLoggedIn = async (): Promise<boolean> => {
18
+ const [accessToken, refreshToken] = await Promise.all([
19
+ getToken({ key: ACCESS_TOKEN_KEY }),
20
+ getToken({ key: REFRESH_TOKEN_KEY }),
21
+ ]);
22
+ return Boolean(accessToken || refreshToken);
23
+ };
24
+
25
+ const normalizePathname = (path: string): string => {
26
+ return path.split("?")[0]?.split("#")[0] || "/";
27
+ };
28
+
29
+ export const getAuthRedirectPath = async (targetPath: string): Promise<string | void> => {
30
+ const pathname = normalizePathname(targetPath);
31
+ if (isWhitelistedPath(pathname)) {
32
+ return;
33
+ }
34
+
35
+ if (await isLoggedIn()) {
36
+ return;
37
+ }
38
+
39
+ const redirect = encodeURIComponent(targetPath || "/");
40
+ return `${LOGIN_PATH}?redirect=${redirect}`;
41
+ };
42
+
43
+ export const authRouteGuard = async (to: RouteLocation): Promise<string | void> => {
44
+ return getAuthRedirectPath(to.path || "/");
45
+ };