From 6e2e5bdbc28a97daad493e78a89e2e2a07a016a9 Mon Sep 17 00:00:00 2001 From: yiling Date: Fri, 26 Jun 2026 11:18:09 +0800 Subject: [PATCH] refactor(webui): extract shared resolveTeamIdFromPath function for root route and component --- packages/webui/routes/__root.tsx | 40 ++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/packages/webui/routes/__root.tsx b/packages/webui/routes/__root.tsx index f68f77af..75cf9236 100644 --- a/packages/webui/routes/__root.tsx +++ b/packages/webui/routes/__root.tsx @@ -20,6 +20,21 @@ import { getLocale, setLocale } from '@/ui/src/paraglide/runtime.js' import { m } from '@/ui/src/paraglide/messages.js' import { useUserMetadataStore } from '@/ui/stores/user-metadata' +async function resolveTeamIdFromPath(pathname: string): Promise { + const teamIdMatch = pathname.match(/^\/teams\/([^/]+)/) + const teamId = teamIdMatch ? teamIdMatch[1] : null + if (teamId) return teamId + + const projectIdMatch = pathname.match(/^\/projects\/([^/]+)/) + const projectId = projectIdMatch ? projectIdMatch[1] : null + if (projectId) { + const { ensureTeamIdForProject } = useTeamContextStore.getState() + return await ensureTeamIdForProject(projectId) + } + + return null +} + function RootComponent() { const user = useAuthStore((state) => state.user) const { uploading } = useUploadStore() @@ -32,23 +47,15 @@ function RootComponent() { return } - // Extract teamId from pathname if possible - // Matches /teams/:teamId - const teamIdMatch = pathname.match(/^\/teams\/([^/]+)/) - const teamId = teamIdMatch ? teamIdMatch[1] : null - - const projectIdMatch = pathname.match(/^\/projects\/([^/]+)/) - const projectId = projectIdMatch ? projectIdMatch[1] : null - - const { teamId: storedTeamId, setTeamId, ensureTeamIdForProject } = useTeamContextStore() + const { teamId: storedTeamId, setTeamId } = useTeamContextStore() useEffect(() => { - if (teamId) { - setTeamId(teamId) - } else if (projectId) { - ensureTeamIdForProject(projectId) - } - }, [teamId, projectId, setTeamId, ensureTeamIdForProject]) + resolveTeamIdFromPath(pathname).then((resolvedTeamId) => { + if (resolvedTeamId && resolvedTeamId !== storedTeamId) { + setTeamId(resolvedTeamId) + } + }) + }, [pathname, storedTeamId, setTeamId]) const { data: me } = useQuery({ queryKey: ['teams', storedTeamId, 'me'], @@ -123,8 +130,7 @@ export const Route = createRootRouteWithContext()({ beforeLoad: async () => { if (typeof window !== 'undefined') { const pathname = window.location.pathname - const teamIdMatch = pathname.match(/^\/teams\/([^/]+)/) - const teamId = teamIdMatch ? teamIdMatch[1] : null + const teamId = await resolveTeamIdFromPath(pathname) if (teamId) { const { fetchMetadata, getMetadata } = useUserMetadataStore.getState()