"use client"; import { useAtomValue, useSetAtom } from "jotai"; import { Loader2 } from "lucide-react"; import { useParams, usePathname, useRouter } from "next/navigation"; import { useTranslations } from "next-intl"; import type React from "react"; import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { toast } from "sonner"; import { myAccessAtom } from "@/atoms/members/members-query.atoms"; import { updateLLMPreferencesMutationAtom } from "@/atoms/new-llm-config/new-llm-config-mutation.atoms"; import { globalNewLLMConfigsAtom, llmPreferencesAtom, } from "@/atoms/new-llm-config/new-llm-config-query.atoms"; import { activeSearchSpaceIdAtom } from "@/atoms/search-spaces/search-space-query.atoms"; import { DashboardBreadcrumb } from "@/components/dashboard-breadcrumb"; import { LanguageSwitcher } from "@/components/LanguageSwitcher"; import { AppSidebarProvider } from "@/components/sidebar/AppSidebarProvider"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Separator } from "@/components/ui/separator"; import { SidebarInset, SidebarProvider, SidebarTrigger } from "@/components/ui/sidebar"; export function DashboardClientLayout({ children, searchSpaceId, navSecondary, navMain, }: { children: React.ReactNode; searchSpaceId: string; navSecondary: any[]; navMain: any[]; }) { const t = useTranslations("dashboard"); const router = useRouter(); const pathname = usePathname(); const { search_space_id } = useParams(); const setActiveSearchSpaceIdState = useSetAtom(activeSearchSpaceIdAtom); const { data: preferences = {}, isFetching: loading, error, refetch: refetchPreferences, } = useAtomValue(llmPreferencesAtom); const { data: globalConfigs = [], isFetching: globalConfigsLoading } = useAtomValue(globalNewLLMConfigsAtom); const { mutateAsync: updatePreferences } = useAtomValue(updateLLMPreferencesMutationAtom); const isOnboardingComplete = useCallback(() => { return !!(preferences.agent_llm_id && preferences.document_summary_llm_id); }, [preferences]); const { data: access = null, isLoading: accessLoading } = useAtomValue(myAccessAtom); const [hasCheckedOnboarding, setHasCheckedOnboarding] = useState(false); const [isAutoConfiguring, setIsAutoConfiguring] = useState(false); const hasAttemptedAutoConfig = useRef(false); // Skip onboarding check if we're already on the onboarding page const isOnboardingPage = pathname?.includes("/onboard"); // Only owners should see onboarding - invited members use existing config const isOwner = access?.is_owner ?? false; // Translate navigation items const tNavMenu = useTranslations("nav_menu"); const translatedNavMain = useMemo(() => { return navMain.map((item) => ({ ...item, title: tNavMenu(item.title.toLowerCase().replace(/ /g, "_")), items: item.items?.map((subItem: any) => ({ ...subItem, title: tNavMenu(subItem.title.toLowerCase().replace(/ /g, "_")), })), })); }, [navMain, tNavMenu]); const translatedNavSecondary = useMemo(() => { return navSecondary.map((item) => ({ ...item, title: item.title === "All Search Spaces" ? tNavMenu("all_search_spaces") : item.title, })); }, [navSecondary, tNavMenu]); const [open, setOpen] = useState(() => { try { const match = document.cookie.match(/(?:^|; )sidebar_state=([^;]+)/); if (match) return match[1] === "true"; } catch { // ignore } return true; }); useEffect(() => { // Skip check if already on onboarding page if (isOnboardingPage) { setHasCheckedOnboarding(true); return; } // Wait for all data to load if ( !loading && !accessLoading && !globalConfigsLoading && !hasCheckedOnboarding && !isAutoConfiguring ) { const onboardingComplete = isOnboardingComplete(); // If onboarding is complete, nothing to do if (onboardingComplete) { setHasCheckedOnboarding(true); return; } // Only handle onboarding for owners if (!isOwner) { setHasCheckedOnboarding(true); return; } // If global configs available, auto-configure without going to onboard page if (globalConfigs.length > 0 && !hasAttemptedAutoConfig.current) { hasAttemptedAutoConfig.current = true; setIsAutoConfiguring(true); const autoConfigureWithGlobal = async () => { try { const firstGlobalConfig = globalConfigs[0]; await updatePreferences({ search_space_id: Number(searchSpaceId), data: { agent_llm_id: firstGlobalConfig.id, document_summary_llm_id: firstGlobalConfig.id, }, }); await refetchPreferences(); toast.success("AI configured automatically!", { description: `Using ${firstGlobalConfig.name}. Customize in Settings.`, }); setHasCheckedOnboarding(true); } catch (error) { console.error("Auto-configuration failed:", error); // Fall back to onboard page router.push(`/dashboard/${searchSpaceId}/onboard`); } finally { setIsAutoConfiguring(false); } }; autoConfigureWithGlobal(); return; } // No global configs - redirect to onboard page router.push(`/dashboard/${searchSpaceId}/onboard`); setHasCheckedOnboarding(true); } }, [ loading, accessLoading, globalConfigsLoading, isOnboardingComplete, isOnboardingPage, isOwner, isAutoConfiguring, globalConfigs, router, searchSpaceId, hasCheckedOnboarding, updatePreferences, refetchPreferences, ]); // Synchronize active search space and chat IDs with URL useEffect(() => { const activeSeacrhSpaceId = typeof search_space_id === "string" ? search_space_id : Array.isArray(search_space_id) && search_space_id.length > 0 ? search_space_id[0] : ""; if (!activeSeacrhSpaceId) return; setActiveSearchSpaceIdState(activeSeacrhSpaceId); }, [search_space_id, setActiveSearchSpaceIdState]); // Show loading screen while checking onboarding status or auto-configuring if ( (!hasCheckedOnboarding && (loading || accessLoading || globalConfigsLoading) && !isOnboardingPage) || isAutoConfiguring ) { return (
{isAutoConfiguring ? "Setting up AI..." : t("loading_config")} {isAutoConfiguring ? "Auto-configuring with available settings" : t("checking_llm_prefs")}
); } // Show error screen if there's an error loading preferences (but not on onboarding page) if (error && !hasCheckedOnboarding && !isOnboardingPage) { return (
{t("config_error")} {t("failed_load_llm_config")}

{error instanceof Error ? error.message : String(error)}

); } return ( {/* Use AppSidebarProvider which fetches user, search space, and recent chats */}
{children}
); }