@qlover/create-app 0.10.1 → 0.10.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.
Files changed (108) hide show
  1. package/CHANGELOG.md +141 -0
  2. package/dist/index.cjs +1 -1
  3. package/dist/index.js +1 -1
  4. package/dist/templates/next-app/config/IOCIdentifier.ts +2 -2
  5. package/dist/templates/next-app/config/Identifier/common/common.ts +14 -0
  6. package/dist/templates/next-app/config/Identifier/pages/index.ts +1 -0
  7. package/dist/templates/next-app/config/Identifier/pages/page.about.ts +20 -0
  8. package/dist/templates/next-app/config/common.ts +1 -1
  9. package/dist/templates/next-app/config/cookies.ts +23 -0
  10. package/dist/templates/next-app/config/i18n/AboutI18n.ts +14 -0
  11. package/dist/templates/next-app/config/i18n/i18nConfig.ts +3 -1
  12. package/dist/templates/next-app/config/i18n/index.ts +1 -0
  13. package/dist/templates/next-app/config/i18n/loginI18n.ts +8 -0
  14. package/dist/templates/next-app/config/theme.ts +4 -0
  15. package/dist/templates/next-app/eslint.config.mjs +4 -1
  16. package/dist/templates/next-app/next.config.ts +1 -0
  17. package/dist/templates/next-app/package.json +13 -4
  18. package/dist/templates/next-app/public/locales/en.json +5 -0
  19. package/dist/templates/next-app/public/locales/zh.json +5 -0
  20. package/dist/templates/next-app/src/app/[locale]/admin/AdminI18nProvider.tsx +37 -0
  21. package/dist/templates/next-app/src/app/[locale]/admin/layout.tsx +30 -6
  22. package/dist/templates/next-app/src/app/[locale]/admin/locales/page.tsx +1 -1
  23. package/dist/templates/next-app/src/app/[locale]/layout.tsx +47 -10
  24. package/dist/templates/next-app/src/app/[locale]/login/LoginForm.tsx +1 -1
  25. package/dist/templates/next-app/src/app/[locale]/login/page.tsx +22 -10
  26. package/dist/templates/next-app/src/app/[locale]/page.tsx +23 -8
  27. package/dist/templates/next-app/src/app/[locale]/register/page.tsx +21 -9
  28. package/dist/templates/next-app/src/app/api/admin/locales/create/route.ts +7 -28
  29. package/dist/templates/next-app/src/app/api/admin/locales/import/route.ts +7 -34
  30. package/dist/templates/next-app/src/app/api/admin/locales/route.ts +12 -34
  31. package/dist/templates/next-app/src/app/api/admin/locales/update/route.ts +7 -26
  32. package/dist/templates/next-app/src/app/api/admin/users/route.ts +14 -33
  33. package/dist/templates/next-app/src/app/api/locales/json/route.ts +13 -25
  34. package/dist/templates/next-app/src/app/api/user/login/route.ts +6 -46
  35. package/dist/templates/next-app/src/app/api/user/logout/route.ts +5 -24
  36. package/dist/templates/next-app/src/app/api/user/register/route.ts +6 -45
  37. package/dist/templates/next-app/src/app/manifest.ts +16 -0
  38. package/dist/templates/next-app/src/app/robots.txt +2 -0
  39. package/dist/templates/next-app/src/base/cases/NavigateBridge.ts +12 -2
  40. package/dist/templates/next-app/src/base/cases/RouterService.ts +5 -5
  41. package/dist/templates/next-app/src/base/port/AppApiInterface.ts +22 -0
  42. package/dist/templates/next-app/src/base/port/IOCInterface.ts +9 -0
  43. package/dist/templates/next-app/src/base/types/{PageProps.ts → AppPageRouter.ts} +4 -1
  44. package/dist/templates/next-app/src/base/types/PagesRouter.ts +9 -0
  45. package/dist/templates/next-app/src/core/bootstraps/BootstrapClient.ts +19 -5
  46. package/dist/templates/next-app/src/core/bootstraps/BootstrapServer.ts +2 -2
  47. package/dist/templates/next-app/src/core/bootstraps/BootstrapsRegistry.ts +0 -1
  48. package/dist/templates/next-app/src/core/clientIoc/ClientIOC.ts +29 -8
  49. package/dist/templates/next-app/src/core/clientIoc/ClientIOCRegister.ts +4 -2
  50. package/dist/templates/next-app/src/core/serverIoc/ServerIOC.ts +25 -7
  51. package/dist/templates/next-app/src/core/serverIoc/ServerIOCRegister.ts +1 -1
  52. package/dist/templates/next-app/src/i18n/loadMessages.ts +103 -0
  53. package/dist/templates/next-app/src/i18n/request.ts +3 -22
  54. package/dist/templates/next-app/src/pages/[locale]/about.tsx +61 -0
  55. package/dist/templates/next-app/src/pages/_app.tsx +50 -0
  56. package/dist/templates/next-app/src/pages/_document.tsx +13 -0
  57. package/dist/templates/next-app/src/{middleware.ts → proxy.ts} +2 -1
  58. package/dist/templates/next-app/src/server/AppPageRouteParams.ts +94 -0
  59. package/dist/templates/next-app/src/server/NextApiServer.ts +53 -0
  60. package/dist/templates/next-app/src/server/PagesRouteParams.ts +136 -0
  61. package/dist/templates/next-app/src/server/{sqlBridges/SupabaseBridge.ts → SupabaseBridge.ts} +2 -0
  62. package/dist/templates/next-app/src/server/controllers/AdminLocalesController.ts +74 -0
  63. package/dist/templates/next-app/src/server/controllers/AdminUserController.ts +39 -0
  64. package/dist/templates/next-app/src/server/controllers/LocalesController.ts +33 -0
  65. package/dist/templates/next-app/src/server/controllers/UserController.ts +77 -0
  66. package/dist/templates/next-app/src/server/port/AIControllerInterface.ts +8 -0
  67. package/dist/templates/next-app/src/server/port/AdminLocalesControllerInterface.ts +21 -0
  68. package/dist/templates/next-app/src/server/port/AdminUserControllerInterface.ts +11 -0
  69. package/dist/templates/next-app/src/server/port/LocalesControllerInterface.ts +10 -0
  70. package/dist/templates/next-app/src/server/port/{ParamsHandlerInterface.ts → RouteParamsnHandlerInterface.ts} +9 -2
  71. package/dist/templates/next-app/src/server/port/ServerInterface.ts +2 -2
  72. package/dist/templates/next-app/src/server/port/UserControllerInerface.ts +8 -0
  73. package/dist/templates/next-app/src/server/port/UserServiceInterface.ts +1 -1
  74. package/dist/templates/next-app/src/server/port/ValidatorInterface.ts +2 -2
  75. package/dist/templates/next-app/src/server/repositorys/LocalesRepository.ts +8 -2
  76. package/dist/templates/next-app/src/{base → server}/services/AdminLocalesService.ts +2 -2
  77. package/dist/templates/next-app/src/server/services/ApiLocaleService.ts +25 -10
  78. package/dist/templates/next-app/src/server/services/ApiUserService.ts +5 -2
  79. package/dist/templates/next-app/src/server/validators/LocalesValidator.ts +4 -2
  80. package/dist/templates/next-app/src/server/validators/LoginValidator.ts +1 -1
  81. package/dist/templates/next-app/src/server/validators/PaginationValidator.ts +10 -10
  82. package/dist/templates/next-app/src/styles/css/antd-themes/_common/_default.css +0 -44
  83. package/dist/templates/next-app/src/styles/css/antd-themes/_common/dark.css +0 -44
  84. package/dist/templates/next-app/src/styles/css/antd-themes/_common/pink.css +0 -44
  85. package/dist/templates/next-app/src/styles/css/index.css +1 -1
  86. package/dist/templates/next-app/src/styles/css/scrollbar.css +34 -0
  87. package/dist/templates/next-app/src/uikit/components/AdminLayout.tsx +34 -11
  88. package/dist/templates/next-app/src/uikit/components/BootstrapsProvider.tsx +69 -39
  89. package/dist/templates/next-app/src/uikit/components/ClientRootProvider.tsx +64 -0
  90. package/dist/templates/next-app/src/uikit/components/ClinetRenderProvider.tsx +42 -0
  91. package/dist/templates/next-app/src/uikit/components/IOCProvider.tsx +34 -0
  92. package/dist/templates/next-app/src/uikit/components-app/AppBridge.tsx +17 -0
  93. package/dist/templates/next-app/src/uikit/components-app/AppRoutePage.tsx +112 -0
  94. package/dist/templates/next-app/src/uikit/{components → components-app}/LanguageSwitcher.tsx +15 -19
  95. package/dist/templates/next-app/src/uikit/{components → components-app}/ThemeSwitcher.tsx +53 -52
  96. package/dist/templates/next-app/src/uikit/components-pages/LanguageSwitcher.tsx +98 -0
  97. package/dist/templates/next-app/src/uikit/components-pages/PagesRoutePage.tsx +93 -0
  98. package/dist/templates/next-app/src/uikit/context/IOCContext.ts +16 -4
  99. package/dist/templates/next-app/src/uikit/hook/useStrictEffect.ts +32 -0
  100. package/dist/templates/next-app/tsconfig.json +3 -2
  101. package/package.json +1 -1
  102. package/dist/templates/next-app/src/server/PageParams.ts +0 -66
  103. package/dist/templates/next-app/src/uikit/components/BaseHeader.tsx +0 -80
  104. package/dist/templates/next-app/src/uikit/components/BaseLayout.tsx +0 -65
  105. package/dist/templates/next-app/src/uikit/components/ComboProvider.tsx +0 -58
  106. package/dist/templates/next-app/src/uikit/components/NextIntlProvider.tsx +0 -21
  107. /package/dist/templates/next-app/{src/app/[locale] → public}/favicon.ico +0 -0
  108. /package/dist/templates/next-app/src/uikit/{components → components-app}/LogoutButton.tsx +0 -0
@@ -1,45 +1,75 @@
1
1
  'use client';
2
2
  import '@ant-design/v5-patch-for-react-19';
3
- import { useRouter } from 'next/navigation';
4
- import { useLocale } from 'next-intl';
5
- import { useEffect } from 'react';
6
- import { NavigateBridge } from '@/base/cases/NavigateBridge';
7
- import type { I18nServiceLocale } from '@/base/port/I18nServiceInterface';
3
+
4
+ import { useState } from 'react';
8
5
  import { BootstrapClient } from '@/core/bootstraps/BootstrapClient';
9
- import { clientIOC } from '@/core/clientIoc/ClientIOC';
10
- import { I } from '@config/IOCIdentifier';
11
- import { IOCContext } from '../context/IOCContext';
12
- import { useWarnTranslations } from '../hook/useWarnTranslations';
6
+ import { useIOC } from '../hook/useIOC';
7
+ import { useStrictEffect } from '../hook/useStrictEffect';
13
8
 
14
9
  export function BootstrapsProvider(props: { children: React.ReactNode }) {
15
- const IOC = clientIOC.create();
16
- const locale = useLocale();
17
- const router = useRouter();
18
- const t = useWarnTranslations();
19
-
20
- useEffect(() => {
21
- IOC(I.RouterServiceInterface).setLocale(locale);
22
- IOC(NavigateBridge).setUIBridge(router);
23
- }, [locale, router, IOC]);
24
-
25
- useEffect(() => {
26
- IOC(I.I18nServiceInterface).changeLanguage(locale as I18nServiceLocale);
27
- IOC(I.I18nServiceInterface).setTranslator(t);
28
- }, [t, IOC, locale]);
29
-
30
- useEffect(() => {
31
- if (typeof window !== 'undefined') {
32
- BootstrapClient.main({
33
- root: window,
34
- pathname: window.location.pathname,
35
- IOC: IOC
36
- });
37
- }
38
- }, [IOC]);
39
-
40
- return (
41
- <IOCContext.Provider data-testid="BootstrapsProvider" value={IOC}>
42
- {props.children}
43
- </IOCContext.Provider>
44
- );
10
+ const IOC = useIOC();
11
+
12
+ const [, setIocMounted] = useState(false);
13
+
14
+ useStrictEffect(() => {
15
+ // clientIOC.register({
16
+ // appConfig: appConfig
17
+ // });
18
+ BootstrapClient.main({
19
+ root: window,
20
+ pathname: window.location.pathname,
21
+ IOC: IOC
22
+ }).then(() => {
23
+ setIocMounted(true);
24
+ });
25
+ }, []);
26
+
27
+ // useEffect(() => {
28
+ // const register = new ClientIOCRegister({
29
+ // appConfig: appConfig
30
+ // });
31
+
32
+ // // 启动前注册所有依赖
33
+ // // clientIOC.register(register);
34
+
35
+ // BootstrapClient.main({
36
+ // root: window,
37
+ // pathname: window.location.pathname,
38
+ // IOC: IOC,
39
+ // register: register
40
+ // }).then(() => {
41
+ // setIocMounted(true);
42
+ // });
43
+ // }, []);
44
+
45
+ // if (!register) {
46
+ // return <div data-testid="BootstrapsProviderLoading">Loading...</div>;
47
+ // }
48
+
49
+ // const IOC = clientIOC.create();
50
+ // const locale = useLocale();
51
+ // const router = useRouter();
52
+ // const t = useWarnTranslations();
53
+
54
+ // useEffect(() => {
55
+ // IOC(I.RouterServiceInterface).setLocale(locale);
56
+ // IOC(NavigateBridge).setUIBridge(router);
57
+ // }, [locale, router, IOC]);
58
+
59
+ // useEffect(() => {
60
+ // IOC(I.I18nServiceInterface).changeLanguage(locale as I18nServiceLocale);
61
+ // IOC(I.I18nServiceInterface).setTranslator(t);
62
+ // }, [t, IOC, locale]);
63
+
64
+ // useEffect(() => {
65
+ // if (typeof window !== 'undefined') {
66
+ // BootstrapClient.main({
67
+ // root: window,
68
+ // pathname: window.location.pathname,
69
+ // IOC: IOC
70
+ // });
71
+ // }
72
+ // }, [IOC]);
73
+
74
+ return props.children;
45
75
  }
@@ -0,0 +1,64 @@
1
+ 'use client';
2
+ import '@ant-design/v5-patch-for-react-19';
3
+ import { AntdRegistry } from '@ant-design/nextjs-registry';
4
+ import { AntdThemeProvider } from '@brain-toolkit/antd-theme-override/react';
5
+ import { ThemeProvider } from 'next-themes';
6
+ import { I } from '@config/IOCIdentifier';
7
+ import type { CommonThemeConfig } from '@config/theme';
8
+ import { useIOC } from '../hook/useIOC';
9
+
10
+ /**
11
+ * ClientRootProvider is a provider for the client components
12
+ *
13
+ * - ThemeProvider
14
+ * - AntdProvider
15
+ *
16
+ * TODO: 存在问题:
17
+ *
18
+ * 1. antd 样式存在闪烁问题, 目前没有解决, 可能是因为 cssinjs 的技术性问题
19
+ *
20
+ * 目前能将完美解决的就是完全使用客户端渲染,也就是引入 useMountedClient 当客户端渲染时才渲染, 这样就不会出现闪烁问题
21
+ * 但是他会导致国际化切换闪烁问题
22
+ *
23
+ * 可能需要等待 antd 官方解决这个问题
24
+ *
25
+ * @example
26
+ *
27
+ * ```tsx
28
+ * const mounted = useMountedClient();
29
+ *
30
+ * return mounted && children;
31
+ * ```
32
+ *
33
+ *
34
+ * @param themeConfig - The theme config
35
+ * @param children - The children components
36
+ * @returns
37
+ */
38
+ export function ClientRootProvider(props: {
39
+ themeConfig: CommonThemeConfig;
40
+ children: React.ReactNode;
41
+ }) {
42
+ const { themeConfig, children } = props;
43
+
44
+ const IOC = useIOC();
45
+
46
+ return (
47
+ <AntdThemeProvider
48
+ data-testid="ComboProvider"
49
+ theme={themeConfig.antdTheme}
50
+ staticApi={IOC(I.DialogHandler)}
51
+ >
52
+ <ThemeProvider
53
+ themes={themeConfig.supportedThemes as unknown as string[]}
54
+ attribute={themeConfig.domAttribute}
55
+ defaultTheme={themeConfig.defaultTheme}
56
+ enableSystem={themeConfig.enableSystem}
57
+ enableColorScheme={false}
58
+ storageKey={themeConfig.storageKey}
59
+ >
60
+ <AntdRegistry>{children}</AntdRegistry>
61
+ </ThemeProvider>
62
+ </AntdThemeProvider>
63
+ );
64
+ }
@@ -0,0 +1,42 @@
1
+ 'use client';
2
+
3
+ import { useMountedClient } from '@brain-toolkit/react-kit';
4
+
5
+ export interface ClinetRenderProviderProps {
6
+ children: React.ReactNode;
7
+ }
8
+
9
+ /**
10
+ * ClinetRenderProvider is a provider for the client components
11
+ *
12
+ *
13
+ * 当前组件仅用于需要客户端渲染的组件, 比如 adminLayout 等完全客户端渲染的组件
14
+ *
15
+ *
16
+ * @param children - The children components
17
+ * @returns
18
+ */
19
+ export function ClinetRenderProvider(props: ClinetRenderProviderProps) {
20
+ const { children } = props;
21
+
22
+ const mounted = useMountedClient();
23
+
24
+ return (
25
+ <>
26
+ {children}
27
+
28
+ {/* 为了防止语言切换时页面闪烁, 使用一个固定定位的div, 当客户端渲染时才渲染 */}
29
+ {!mounted && (
30
+ <div
31
+ role="status"
32
+ aria-label="Loading..."
33
+ aria-busy="true"
34
+ style={{
35
+ zIndex: '99999 !important'
36
+ }}
37
+ className="fixed inset-0 overflow-hidden cursor-wait no-scrollbar bg-primary pointer-events-none"
38
+ ></div>
39
+ )}
40
+ </>
41
+ );
42
+ }
@@ -0,0 +1,34 @@
1
+ 'use client';
2
+
3
+ import { useMemo } from 'react';
4
+ import { appConfig } from '@/core/globals';
5
+ import { clientIOC, IOCContext, IOCInstance } from '../context/IOCContext';
6
+
7
+ export function IOCProvider(props: { children: React.ReactNode }) {
8
+ /**
9
+ * 加载组件就立即注册
10
+ *
11
+ * 这样在渲染子组件时保证 IOC.get 正常工作
12
+ *
13
+ * 但是这样会导致注册时无法传递浏览器端的依赖, 比如 window.location.pathname
14
+ *
15
+ * - 如果有需要,可以将注册放在下面 useStrictEffect 中, 然后 IocMounted=true 时在渲染子节点
16
+ *
17
+ * **但是这样会有一个问题, 组件会重新挂载渲染,当切换语言时会闪烁**
18
+ *
19
+ * 因为页面初始化时有些组件可能已经使用了容器注入,这样就会丢失注册的依赖
20
+ *
21
+ * TODO: 这是一个需要解决的问题
22
+ */
23
+ useMemo(() => {
24
+ clientIOC.register({
25
+ appConfig: appConfig
26
+ });
27
+ }, []);
28
+
29
+ return (
30
+ <IOCContext.Provider data-testid="IOCProvider" value={IOCInstance}>
31
+ {props.children}
32
+ </IOCContext.Provider>
33
+ );
34
+ }
@@ -0,0 +1,17 @@
1
+ 'use client';
2
+
3
+ import { useEffect } from 'react';
4
+ import { NavigateBridge } from '@/base/cases/NavigateBridge';
5
+ import { useRouter } from '@/i18n/routing';
6
+ import { useIOC } from '../hook/useIOC';
7
+
8
+ export function AppBridge() {
9
+ const router = useRouter();
10
+ const navigateBridge = useIOC(NavigateBridge);
11
+
12
+ useEffect(() => {
13
+ navigateBridge.setUIBridge(router);
14
+ }, [router, navigateBridge]);
15
+
16
+ return null;
17
+ }
@@ -0,0 +1,112 @@
1
+ import { TeamOutlined } from '@ant-design/icons';
2
+ import { clsx } from 'clsx';
3
+ import { useLocale } from 'next-intl';
4
+ import { useMemo, type HTMLAttributes } from 'react';
5
+ import { AppBridge } from './AppBridge';
6
+ import { LanguageSwitcher } from './LanguageSwitcher';
7
+ import { LogoutButton } from './LogoutButton';
8
+ import { ThemeSwitcher } from './ThemeSwitcher';
9
+ import { LocaleLink } from '../components/LocaleLink';
10
+
11
+ export interface AppRoutePageTT {
12
+ title: string;
13
+ adminTitle: string;
14
+ }
15
+
16
+ export interface AppRoutePageProps extends HTMLAttributes<HTMLDivElement> {
17
+ showLogoutButton?: boolean;
18
+ showAdminButton?: boolean;
19
+ mainProps?: HTMLAttributes<HTMLElement>;
20
+ headerClassName?: string;
21
+ headerHref?: string;
22
+ tt: AppRoutePageTT;
23
+ }
24
+
25
+ /**
26
+ * App Route Page
27
+ *
28
+ * 主要用于 /src/app 目录下页面的基础布局,包含头部、主体内容等
29
+ *
30
+ * @description
31
+ * - /src/app/[locale]/page.tsx
32
+ * - /src/app/[locale]/login/page.tsx
33
+ *
34
+ */
35
+ export function AppRoutePage({
36
+ children,
37
+ showLogoutButton,
38
+ showAdminButton,
39
+ mainProps,
40
+ headerClassName,
41
+ tt,
42
+ headerHref = '/',
43
+ ...props
44
+ }: AppRoutePageProps) {
45
+ const locale = useLocale();
46
+ const adminTitle = tt.adminTitle;
47
+
48
+ const actions = useMemo(() => {
49
+ return [
50
+ showAdminButton && (
51
+ <LocaleLink
52
+ key="admin-button"
53
+ href="/admin"
54
+ title={adminTitle}
55
+ locale={locale}
56
+ className="text-text hover:text-text-hover cursor-pointer text-lg transition-colors"
57
+ >
58
+ <TeamOutlined className="text-lg text-text" />
59
+ </LocaleLink>
60
+ ),
61
+
62
+ <LanguageSwitcher key="language-switcher" />,
63
+
64
+ <ThemeSwitcher key="theme-switcher" />,
65
+
66
+ showLogoutButton && <LogoutButton key="logout-button" />
67
+ ].filter(Boolean);
68
+ }, [adminTitle, showAdminButton, showLogoutButton, locale]);
69
+
70
+ return (
71
+ <div
72
+ data-testid="AppRoutePage"
73
+ className="flex flex-col min-h-screen"
74
+ {...props}
75
+ >
76
+ <AppBridge />
77
+ <header
78
+ data-testid="BaseHeader"
79
+ className="h-14 bg-secondary border-b border-c-border sticky top-0 z-50"
80
+ >
81
+ <div
82
+ className={clsx(
83
+ 'flex items-center justify-between h-full px-4 mx-auto max-w-7xl',
84
+ headerClassName
85
+ )}
86
+ >
87
+ <div className="flex items-center">
88
+ <LocaleLink
89
+ data-testid="BaseHeaderLogo"
90
+ title={tt.title}
91
+ href={headerHref}
92
+ locale={locale}
93
+ className="flex items-center hover:opacity-80 transition-opacity"
94
+ >
95
+ <span
96
+ data-testid="base-header-app-name"
97
+ className="text-lg font-semibold text-text"
98
+ >
99
+ {tt.title}
100
+ </span>
101
+ </LocaleLink>
102
+ </div>
103
+ <div className="flex items-center gap-2 md:gap-4">{actions}</div>
104
+ </div>
105
+ </header>
106
+
107
+ <main className="flex flex-1 flex-col bg-primary" {...mainProps}>
108
+ {children}
109
+ </main>
110
+ </div>
111
+ );
112
+ }
@@ -2,22 +2,20 @@
2
2
 
3
3
  import { TranslationOutlined } from '@ant-design/icons';
4
4
  import { Dropdown } from 'antd';
5
+ import { useParams } from 'next/navigation';
5
6
  import { useLocale } from 'next-intl';
6
- import { useCallback, useMemo } from 'react';
7
- import type { I18nServiceLocale } from '@/base/port/I18nServiceInterface';
7
+ import { useCallback, useMemo, useTransition } from 'react';
8
8
  import { usePathname, useRouter } from '@/i18n/routing';
9
9
  import { i18nConfig } from '@config/i18n';
10
10
  import type { LocaleType } from '@config/i18n';
11
- import { I } from '@config/IOCIdentifier';
12
- import { useIOC } from '../hook/useIOC';
13
11
  import type { ItemType } from 'antd/es/menu/interface';
14
12
 
15
13
  export function LanguageSwitcher() {
16
- const i18nService = useIOC(I.I18nServiceInterface);
17
- const pathname = usePathname(); // current pathname, aware of i18n
18
-
19
- const router = useRouter(); // i18n-aware router instance
20
- const currentLocale = useLocale() as LocaleType; // currently active locale
14
+ const pathname = usePathname();
15
+ const router = useRouter();
16
+ const currentLocale = useLocale() as LocaleType;
17
+ const [isPending, startTransition] = useTransition();
18
+ const params = useParams();
21
19
 
22
20
  const options: ItemType[] = useMemo(() => {
23
21
  return i18nConfig.supportedLngs.map(
@@ -34,18 +32,16 @@ export function LanguageSwitcher() {
34
32
 
35
33
  const handleLanguageChange = useCallback(
36
34
  async (value: string) => {
37
- // Set a persistent cookie with the user's preferred locale (valid for 1 year)
38
- document.cookie = `NEXT_LOCALE=${value}; path=/; max-age=31536000; SameSite=Lax`;
39
- // Route to the same page in the selected locale
40
- router.replace(pathname, { locale: value });
35
+ if (isPending) return;
41
36
 
42
- try {
43
- await i18nService.changeLanguage(value as I18nServiceLocale);
44
- } catch (error) {
45
- console.error('Failed to change language:', error);
46
- }
37
+ startTransition(() => {
38
+ // @ts-expect-error -- TypeScript will validate that only known `params`
39
+ // are used in combination with a given `pathname`. Since the two will
40
+ // always match for the current route, we can skip runtime checks.
41
+ router.replace({ pathname, params }, { locale: value });
42
+ });
47
43
  },
48
- [i18nService, pathname, router]
44
+ [pathname, router, isPending, params]
49
45
  );
50
46
 
51
47
  const nextLocale = useMemo(() => {
@@ -14,11 +14,20 @@ import { useMountedClient } from '@brain-toolkit/react-kit';
14
14
  import { Dropdown } from 'antd';
15
15
  import { clsx } from 'clsx';
16
16
  import { useTheme } from 'next-themes';
17
- import { useMemo } from 'react';
17
+ import { useEffect, useMemo } from 'react';
18
+ import {
19
+ COMMON_THEME_DARK,
20
+ COMMON_THEME_DEFAULT,
21
+ COMMON_THEME_LIGHT,
22
+ COMMON_THEME_PINK
23
+ } from '@config/Identifier';
24
+ import { I } from '@config/IOCIdentifier';
18
25
  import { type SupportedTheme, themeConfig } from '@config/theme';
26
+ import { useIOC } from '../hook/useIOC';
27
+ import { useWarnTranslations } from '../hook/useWarnTranslations';
19
28
  import type { ItemType } from 'antd/es/menu/interface';
20
29
 
21
- const { supportedThemes } = themeConfig;
30
+ const { supportedThemes, storageKey } = themeConfig;
22
31
 
23
32
  const defaultTheme = supportedThemes[0] || 'system';
24
33
  const themesList = ['system', ...supportedThemes];
@@ -31,71 +40,76 @@ const colorMap: Record<
31
40
  normalColor: string;
32
41
  Icon: React.ElementType;
33
42
  SelectedIcon: React.ElementType;
34
- TriggerIcon: React.ElementType;
35
43
  }
36
44
  > = {
37
45
  system: {
38
- i18nkey: 'System',
46
+ i18nkey: COMMON_THEME_DEFAULT,
39
47
  selectedColor: 'text-text',
40
48
  normalColor: 'text-text-secondary',
41
49
  Icon: SettingOutlined,
42
- SelectedIcon: SettingFilled,
43
- TriggerIcon: SettingOutlined
50
+ SelectedIcon: SettingFilled
44
51
  },
45
52
  light: {
46
- i18nkey: 'Light',
53
+ i18nkey: COMMON_THEME_LIGHT,
47
54
  selectedColor: 'text-text',
48
55
  normalColor: 'text-text-secondary',
49
56
  Icon: SunOutlined,
50
- SelectedIcon: SunFilled,
51
- TriggerIcon: SunOutlined
57
+ SelectedIcon: SunFilled
52
58
  },
53
59
  dark: {
54
- i18nkey: 'Dark',
60
+ i18nkey: COMMON_THEME_DARK,
55
61
  selectedColor: 'text-[#9333ea]',
56
62
  normalColor: 'text-[#a855f7]',
57
63
  Icon: MoonOutlined,
58
- SelectedIcon: MoonFilled,
59
- TriggerIcon: MoonOutlined
64
+ SelectedIcon: MoonFilled
60
65
  },
61
66
  pink: {
62
- i18nkey: 'Pink',
67
+ i18nkey: COMMON_THEME_PINK,
63
68
  selectedColor: 'text-[#f472b6]',
64
69
  normalColor: 'text-[#ec4899]',
65
70
  Icon: HeartOutlined,
66
- SelectedIcon: HeartFilled,
67
- TriggerIcon: HeartOutlined
71
+ SelectedIcon: HeartFilled
68
72
  }
69
73
  };
70
74
 
71
75
  export function ThemeSwitcher() {
72
76
  const { theme: currentTheme, resolvedTheme, setTheme } = useTheme();
73
77
  const mounted = useMountedClient();
78
+ const cookieStorage = useIOC(I.CookieStorage);
79
+ const t = useWarnTranslations();
80
+
81
+ useEffect(() => {
82
+ if (resolvedTheme) {
83
+ cookieStorage.setItem(storageKey, resolvedTheme);
84
+ }
85
+ }, [resolvedTheme, cookieStorage]);
74
86
 
75
- const themeOptions = themesList.map((themeName) => {
76
- const { i18nkey, selectedColor, normalColor, Icon, SelectedIcon } =
77
- colorMap[themeName] || colorMap.light;
87
+ const themeOptions = useMemo(() => {
88
+ return themesList.map((themeName) => {
89
+ const { i18nkey, selectedColor, normalColor, Icon, SelectedIcon } =
90
+ colorMap[themeName] || colorMap.light;
78
91
 
79
- const isCurrentTheme =
80
- currentTheme === themeName ||
81
- (themeName === resolvedTheme && currentTheme === 'system');
92
+ const isCurrentTheme =
93
+ currentTheme === themeName ||
94
+ (themeName === resolvedTheme && currentTheme === 'system');
82
95
 
83
- return {
84
- key: themeName,
85
- value: themeName,
86
- label: (
87
- <div
88
- className={clsx(
89
- 'flex items-center gap-2',
90
- isCurrentTheme ? selectedColor : normalColor
91
- )}
92
- >
93
- {isCurrentTheme ? <SelectedIcon /> : <Icon />}
94
- <span>{i18nkey}</span>
95
- </div>
96
- )
97
- } as ItemType;
98
- });
96
+ return {
97
+ key: themeName,
98
+ value: themeName,
99
+ label: (
100
+ <div
101
+ className={clsx(
102
+ 'flex items-center gap-2',
103
+ isCurrentTheme ? selectedColor : normalColor
104
+ )}
105
+ >
106
+ {isCurrentTheme ? <SelectedIcon /> : <Icon />}
107
+ <span>{t(i18nkey)}</span>
108
+ </div>
109
+ )
110
+ } as ItemType;
111
+ });
112
+ }, [currentTheme, resolvedTheme, t]);
99
113
 
100
114
  const nextTheme = useMemo(() => {
101
115
  if (!currentTheme) {
@@ -106,26 +120,13 @@ export function ThemeSwitcher() {
106
120
  return supportedThemes[targetIndex % supportedThemes.length];
107
121
  }, [currentTheme]);
108
122
 
109
- const TriggerIcon = colorMap[currentTheme || defaultTheme].TriggerIcon;
110
-
111
- if (!mounted) {
112
- return (
113
- <span
114
- data-testid="ThemeSwitcher"
115
- className="text-text hover:text-text-hover cursor-pointer text-lg transition-colors"
116
- >
117
- <SettingOutlined />
118
- </span>
119
- );
120
- }
121
-
122
123
  return (
123
124
  <Dropdown
124
125
  data-testid="ThemeSwitcherDropdown"
125
126
  trigger={['hover']}
126
127
  menu={{
127
128
  items: themeOptions,
128
- selectedKeys: [currentTheme!],
129
+ selectedKeys: mounted ? [resolvedTheme!] : undefined,
129
130
  onClick: ({ key }) => {
130
131
  setTheme(key);
131
132
  }
@@ -136,7 +137,7 @@ export function ThemeSwitcher() {
136
137
  className="text-text hover:text-text-hover cursor-pointer text-lg transition-colors"
137
138
  onClick={() => setTheme(nextTheme)}
138
139
  >
139
- <TriggerIcon />
140
+ <SunOutlined />
140
141
  </span>
141
142
  </Dropdown>
142
143
  );