From abc647dbf25372ee3b9c401fdc654254e8f7a2de Mon Sep 17 00:00:00 2001 From: hailin Date: Tue, 22 Apr 2025 19:33:04 +0800 Subject: [PATCH] . --- app/[locale]/setup/page.tsx | 30 ++++++++++++++++--- .../items/all/sidebar-display-item.tsx | 26 ++++++++++++---- components/utility/global-state.tsx | 27 +++++++++++++++-- components/utility/workspace-switcher.tsx | 26 ++++++++++++++-- components/workspace/delete-workspace.tsx | 2 +- 5 files changed, 97 insertions(+), 14 deletions(-) diff --git a/app/[locale]/setup/page.tsx b/app/[locale]/setup/page.tsx index 830c175..322b7eb 100644 --- a/app/[locale]/setup/page.tsx +++ b/app/[locale]/setup/page.tsx @@ -37,7 +37,25 @@ export default function SetupPage() { } = useContext(ChatbotUIContext) const router = useRouter() + + + const pathname = usePathname() // 获取当前路径 + const pathSegments = pathname.split("/").filter(Boolean) + const locales = i18nConfig.locales + const defaultLocale = i18nConfig.defaultLocale + + let locale: (typeof locales)[number] = defaultLocale + + const segment = pathSegments[0] as (typeof locales)[number] + + if (locales.includes(segment)) { + locale = segment + } + const homePath = locale === defaultLocale ? "/" : `/${locale}` + + + // 提取当前路径中的 locale 部分 const locale = pathname.split("/")[1] || "en" // 获取路径中的 locale 部分,如果没有则默认为 "en" @@ -76,7 +94,8 @@ export default function SetupPage() { if (!session) { // 强制跳转到带有 locale 的 login 页面 - return router.push(`/${locale}/login`) + return router.push(`${homePath}/login`) + // return router.push(`/${locale}/login`) } else { const user = session.user @@ -103,7 +122,8 @@ export default function SetupPage() { const homeWorkspaceId = await getHomeWorkspaceByUserId( session.user.id ) - return router.push(`/${locale}/${homeWorkspaceId}/chat`) + return router.push(`${homePath}/${homeWorkspaceId}/chat`) + // return router.push(`/${locale}/${homeWorkspaceId}/chat`) } } })() @@ -124,7 +144,8 @@ export default function SetupPage() { const handleSaveSetupSetting = async () => { const session = (await supabase.auth.getSession()).data.session if (!session) { - return router.push(`/${locale}/login`) + // return router.push(`/${locale}/login`) + return (`${homePath}/login`) } const user = session.user @@ -162,7 +183,8 @@ export default function SetupPage() { setSelectedWorkspace(homeWorkspace!) setWorkspaces(workspaces) - return router.push(`/${locale}/${homeWorkspace?.id}/chat`) + return router.push(`${homePath}/${homeWorkspace?.id}/chat`) + // return router.push(`/${locale}/${homeWorkspace?.id}/chat`) } const renderStep = (stepNum: number) => { diff --git a/components/sidebar/items/all/sidebar-display-item.tsx b/components/sidebar/items/all/sidebar-display-item.tsx index c03bca9..792ddd7 100644 --- a/components/sidebar/items/all/sidebar-display-item.tsx +++ b/components/sidebar/items/all/sidebar-display-item.tsx @@ -8,7 +8,7 @@ import { FC, useContext, useRef, useState } from "react" import { SidebarUpdateItem } from "./sidebar-update-item" import { usePathname } from "next/navigation" - +import i18nConfig from "@/i18nConfig" interface SidebarItemProps { item: DataItemType @@ -30,10 +30,25 @@ export const SidebarItem: FC = ({ const { selectedWorkspace, setChats, setSelectedAssistant } = useContext(ChatbotUIContext) - const pathname = usePathname() // 获取当前路径 - // 提取当前路径中的 locale 部分 - const locale = pathname.split("/")[1] || "en" + + const pathname = usePathname() // 获取当前路径 + const pathSegments = pathname.split("/").filter(Boolean) + const locales = i18nConfig.locales + const defaultLocale = i18nConfig.defaultLocale + + let locale: (typeof locales)[number] = defaultLocale + + const segment = pathSegments[0] as (typeof locales)[number] + + if (locales.includes(segment)) { + locale = segment + } + const homePath = locale === defaultLocale ? "/" : `/${locale}` + + + + const router = useRouter() @@ -68,7 +83,8 @@ export const SidebarItem: FC = ({ setChats(prevState => [createdChat, ...prevState]) setSelectedAssistant(assistant) - return router.push(`/${locale}/${selectedWorkspace.id}/chat/${createdChat.id}`) + return router.push(`${homePath}/${selectedWorkspace.id}/chat/${createdChat.id}`) + // return router.push(`/${locale}/${selectedWorkspace.id}/chat/${createdChat.id}`) }, tools: async (item: any) => {}, models: async (item: any) => {} diff --git a/components/utility/global-state.tsx b/components/utility/global-state.tsx index cd0ab6b..ed211bc 100644 --- a/components/utility/global-state.tsx +++ b/components/utility/global-state.tsx @@ -30,13 +30,35 @@ import { VALID_ENV_KEYS } from "@/types/valid-keys" import { useRouter } from "next/navigation" import { FC, useEffect, useState } from "react" +import i18nConfig from "@/i18nConfig" + interface GlobalStateProps { children: React.ReactNode } export const GlobalState: FC = ({ children }) => { const router = useRouter() + + + + const pathname = usePathname() // 获取当前路径 + const pathSegments = pathname.split("/").filter(Boolean) + const locales = i18nConfig.locales + const defaultLocale = i18nConfig.defaultLocale + + let locale: (typeof locales)[number] = defaultLocale + + const segment = pathSegments[0] as (typeof locales)[number] + + if (locales.includes(segment)) { + locale = segment + } + const homePath = locale === defaultLocale ? "/" : `/${locale}` + + + + // PROFILE STORE const [profile, setProfile] = useState | null>(null) @@ -166,10 +188,11 @@ export const GlobalState: FC = ({ children }) => { if (!profile.has_onboarded) { // 提取当前路径中的 locale 部分 - const locale = pathname.split("/")[1] || "en" // 获取路径中的 locale 部分,如果没有则默认为 "en" + // const locale = pathname.split("/")[1] || "en" // 获取路径中的 locale 部分,如果没有则默认为 "en" // 强制跳转到带有 locale 的 setup 页面 - return router.push(`/${locale}/setup`) + return router.push(`${homePath}/setup`) + // return router.push(`/${locale}/setup`) } const workspaces = await getWorkspacesByUserId(user.id) diff --git a/components/utility/workspace-switcher.tsx b/components/utility/workspace-switcher.tsx index 94d78d7..bbbda3d 100644 --- a/components/utility/workspace-switcher.tsx +++ b/components/utility/workspace-switcher.tsx @@ -19,14 +19,35 @@ import { Input } from "../ui/input" import { useTranslation } from 'react-i18next' import { usePathname } from "next/navigation" +import i18nConfig from "@/i18nConfig" interface WorkspaceSwitcherProps {} export const WorkspaceSwitcher: FC = ({}) => { const { t } = useTranslation() + // const pathname = usePathname() // 获取当前路径 + // const locale = pathname.split("/")[1] || "en" + + + + const pathname = usePathname() // 获取当前路径 - const locale = pathname.split("/")[1] || "en" + const pathSegments = pathname.split("/").filter(Boolean) + const locales = i18nConfig.locales + const defaultLocale = i18nConfig.defaultLocale + + let locale: (typeof locales)[number] = defaultLocale + + const segment = pathSegments[0] as (typeof locales)[number] + + if (locales.includes(segment)) { + locale = segment + } + const homePath = locale === defaultLocale ? "/" : `/${locale}` + + + useHotkey(";", () => setOpen(prevState => !prevState)) @@ -76,7 +97,8 @@ export const WorkspaceSwitcher: FC = ({}) => { setSelectedWorkspace(createdWorkspace) setOpen(false) - return router.push(`/${locale}/${createdWorkspace.id}/chat`) + return router.push(`${homePath}/${createdWorkspace.id}/chat`) + // return router.push(`/${locale}/${createdWorkspace.id}/chat`) } const getWorkspaceName = (workspaceId: string) => { diff --git a/components/workspace/delete-workspace.tsx b/components/workspace/delete-workspace.tsx index a272b96..5e3c134 100644 --- a/components/workspace/delete-workspace.tsx +++ b/components/workspace/delete-workspace.tsx @@ -50,7 +50,7 @@ export const DeleteWorkspace: FC = ({ setSelectedWorkspace(defaultWorkspace) - + const pathname = usePathname() // 获取当前路径 const pathSegments = pathname.split("/").filter(Boolean) const locales = i18nConfig.locales const defaultLocale = i18nConfig.defaultLocale