import { client } from '@/ui/api/client' import { DualSidebar, DualSidebarItem } from '@/ui/components/dual-sidebar' import { NotificationList } from '@/ui/components/notification-list' import { TopNav } from '@/ui/components/top-nav' import { HomeIcon, NotificationFillIcon, UploadCloudIcon } from '@/ui/components/ui/icons' import { Toaster } from '@/ui/components/ui/sonner' import { UploadTasks } from '@/ui/components/upload-tasks' import { m } from '@/ui/paraglide/messages.js' import { getLocale, setLocale } from '@/ui/paraglide/runtime.js' import { useAuthStore } from '@/ui/stores/auth' import { useTeamContextStore } from '@/ui/stores/team-context' import { useUploadStore } from '@/ui/stores/upload' import { useUserMetadataStore } from '@/ui/stores/user-metadata' import { useQuery } from '@tanstack/react-query' import { createRootRouteWithContext, Outlet, useNavigate, useRouterState, } from '@tanstack/react-router' import { useEffect } from 'react' 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() const pathname = useRouterState({ select: (s) => s.location.pathname }) const navigate = useNavigate() const { teamId: storedTeamId, setTeamId } = useTeamContextStore() const showSidebar = user && (pathname.startsWith('/teams/') || pathname.startsWith('/projects/')) useEffect(() => { resolveTeamIdFromPath(pathname).then((resolvedTeamId) => { if (resolvedTeamId && resolvedTeamId !== storedTeamId) { setTeamId(resolvedTeamId) } }) }, [pathname, storedTeamId, setTeamId]) const { data: me } = useQuery({ queryKey: ['teams', storedTeamId, 'me'], queryFn: async () => { if (!storedTeamId) return null const res = await client.api.teams[':teamId'].me.$get({ param: { teamId: storedTeamId }, }) if (!res.ok) throw new Error('Failed to fetch me') return await res.json() }, enabled: !!storedTeamId, }) if (!showSidebar) { return } const unreadCount = me?.unreadNotificationCount ?? 0 const displayCount = unreadCount > 99 ? '99+' : unreadCount const badge = unreadCount > 0 ? ( {displayCount} ) : null return (
} label={m.dashboard()} onItemClick={() => { if (storedTeamId) { navigate({ to: '/teams/$teamId', params: { teamId: storedTeamId }, }) } }} /> } label={m.notifications()} badge={badge}> 0} className={uploading > 0 ? 'text-blue-500' : ''} /> } label={m.uploads()} >
) } import { QueryClient } from '@tanstack/react-query' interface MyRouterContext { queryClient: QueryClient } export const Route = createRootRouteWithContext()({ beforeLoad: async ({ location }) => { if (typeof window !== 'undefined') { const pathname = location.pathname const teamId = await resolveTeamIdFromPath(pathname) if (teamId) { const { fetchMetadata, getMetadata } = useUserMetadataStore.getState() try { await fetchMetadata(teamId) const savedLocale = getMetadata('locale') if ((savedLocale === 'en' || savedLocale === 'zh') && savedLocale !== getLocale()) { setLocale(savedLocale, { reload: false }) } } catch (e) { console.error('Failed to load user locale metadata:', e) } } document.documentElement.setAttribute('lang', getLocale()) } }, component: RootComponent, })