SurfSense/surfsense_web/app/dashboard/[search_space_id]/client-layout.tsx

274 lines
8.5 KiB
TypeScript
Raw Normal View History

"use client";
2025-04-07 23:47:06 -07:00
2025-12-21 22:26:33 -08:00
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";
2025-12-23 01:16:25 -08:00
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { toast } from "sonner";
2025-12-19 00:45:29 -08:00
import { myAccessAtom } from "@/atoms/members/members-query.atoms";
2025-12-23 01:16:25 -08:00
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";
2025-08-02 22:46:15 -07:00
import { DashboardBreadcrumb } from "@/components/dashboard-breadcrumb";
import { LanguageSwitcher } from "@/components/LanguageSwitcher";
2025-07-27 10:05:37 -07:00
import { AppSidebarProvider } from "@/components/sidebar/AppSidebarProvider";
2025-11-14 00:42:19 +02:00
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { Separator } from "@/components/ui/separator";
2025-11-14 00:42:19 +02:00
import { SidebarInset, SidebarProvider, SidebarTrigger } from "@/components/ui/sidebar";
2025-04-07 23:47:06 -07:00
export function DashboardClientLayout({
2025-11-14 00:42:19 +02:00
children,
searchSpaceId,
navSecondary,
navMain,
2025-04-07 23:47:06 -07:00
}: {
2025-11-14 00:42:19 +02:00
children: React.ReactNode;
searchSpaceId: string;
navSecondary: any[];
navMain: any[];
2025-04-07 23:47:06 -07:00
}) {
2025-11-14 00:42:19 +02:00
const t = useTranslations("dashboard");
const router = useRouter();
const pathname = usePathname();
2025-12-21 22:26:33 -08:00
const { search_space_id } = useParams();
2025-11-14 00:42:19 +02:00
const setActiveSearchSpaceIdState = useSetAtom(activeSearchSpaceIdAtom);
2025-12-23 01:16:25 -08:00
const {
data: preferences = {},
isFetching: loading,
error,
refetch: refetchPreferences,
} = useAtomValue(llmPreferencesAtom);
const { data: globalConfigs = [], isFetching: globalConfigsLoading } =
useAtomValue(globalNewLLMConfigsAtom);
const { mutateAsync: updatePreferences } = useAtomValue(updateLLMPreferencesMutationAtom);
2025-12-11 13:42:33 +02:00
const isOnboardingComplete = useCallback(() => {
2025-12-23 01:16:25 -08:00
return !!(preferences.agent_llm_id && preferences.document_summary_llm_id);
}, [preferences]);
2025-12-11 13:42:33 +02:00
const { data: access = null, isLoading: accessLoading } = useAtomValue(myAccessAtom);
2025-11-14 00:42:19 +02:00
const [hasCheckedOnboarding, setHasCheckedOnboarding] = useState(false);
2025-12-23 01:16:25 -08:00
const [isAutoConfiguring, setIsAutoConfiguring] = useState(false);
const hasAttemptedAutoConfig = useRef(false);
2025-11-14 00:42:19 +02:00
// 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;
2025-11-14 00:42:19 +02:00
// 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]);
2025-11-14 00:42:19 +02:00
const translatedNavSecondary = useMemo(() => {
return navSecondary.map((item) => ({
...item,
title: item.title === "All Search Spaces" ? tNavMenu("all_search_spaces") : item.title,
}));
}, [navSecondary, tNavMenu]);
2025-11-14 00:42:19 +02:00
const [open, setOpen] = useState<boolean>(() => {
try {
const match = document.cookie.match(/(?:^|; )sidebar_state=([^;]+)/);
if (match) return match[1] === "true";
} catch {
// ignore
}
return true;
});
2025-11-14 00:42:19 +02:00
useEffect(() => {
// Skip check if already on onboarding page
if (isOnboardingPage) {
setHasCheckedOnboarding(true);
return;
}
2025-08-08 11:17:43 -07:00
2025-12-23 01:16:25 -08:00
// Wait for all data to load
if (
!loading &&
!accessLoading &&
!globalConfigsLoading &&
!hasCheckedOnboarding &&
!isAutoConfiguring
) {
2025-11-14 00:42:19 +02:00
const onboardingComplete = isOnboardingComplete();
2025-12-23 01:16:25 -08:00
// If onboarding is complete, nothing to do
if (onboardingComplete) {
setHasCheckedOnboarding(true);
return;
2025-11-14 00:42:19 +02:00
}
2025-12-23 01:16:25 -08:00
// 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`);
2025-11-14 00:42:19 +02:00
setHasCheckedOnboarding(true);
}
}, [
loading,
accessLoading,
2025-12-23 01:16:25 -08:00
globalConfigsLoading,
2025-11-14 00:42:19 +02:00
isOnboardingComplete,
isOnboardingPage,
isOwner,
2025-12-23 01:16:25 -08:00
isAutoConfiguring,
globalConfigs,
2025-11-14 00:42:19 +02:00
router,
searchSpaceId,
hasCheckedOnboarding,
2025-12-23 01:16:25 -08:00
updatePreferences,
refetchPreferences,
2025-11-14 00:42:19 +02:00
]);
2025-11-14 00:42:19 +02:00
// 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);
2025-12-21 22:26:33 -08:00
}, [search_space_id, setActiveSearchSpaceIdState]);
2025-12-23 01:16:25 -08:00
// Show loading screen while checking onboarding status or auto-configuring
if (
(!hasCheckedOnboarding &&
(loading || accessLoading || globalConfigsLoading) &&
!isOnboardingPage) ||
isAutoConfiguring
) {
2025-11-14 00:42:19 +02:00
return (
<div className="flex flex-col items-center justify-center min-h-screen space-y-4">
<Card className="w-[350px] bg-background/60 backdrop-blur-sm">
<CardHeader className="pb-2">
2025-12-23 01:16:25 -08:00
<CardTitle className="text-xl font-medium">
{isAutoConfiguring ? "Setting up AI..." : t("loading_config")}
</CardTitle>
<CardDescription>
{isAutoConfiguring
? "Auto-configuring with available settings"
: t("checking_llm_prefs")}
</CardDescription>
2025-11-14 00:42:19 +02:00
</CardHeader>
<CardContent className="flex justify-center py-6">
<Loader2 className="h-12 w-12 text-primary animate-spin" />
</CardContent>
</Card>
</div>
);
}
2025-11-14 00:42:19 +02:00
// Show error screen if there's an error loading preferences (but not on onboarding page)
if (error && !hasCheckedOnboarding && !isOnboardingPage) {
return (
<div className="flex flex-col items-center justify-center min-h-screen space-y-4">
<Card className="w-[400px] bg-background/60 backdrop-blur-sm border-destructive/20">
<CardHeader className="pb-2">
<CardTitle className="text-xl font-medium text-destructive">
{t("config_error")}
</CardTitle>
<CardDescription>{t("failed_load_llm_config")}</CardDescription>
</CardHeader>
<CardContent>
2025-12-14 22:32:13 -08:00
<p className="text-sm text-muted-foreground">
{error instanceof Error ? error.message : String(error)}
</p>
2025-11-14 00:42:19 +02:00
</CardContent>
</Card>
</div>
);
}
2025-11-14 00:42:19 +02:00
return (
<SidebarProvider
className="h-full bg-red-600 overflow-hidden"
open={open}
onOpenChange={setOpen}
>
{/* Use AppSidebarProvider which fetches user, search space, and recent chats */}
<AppSidebarProvider
searchSpaceId={searchSpaceId}
navSecondary={translatedNavSecondary}
navMain={translatedNavMain}
/>
<SidebarInset className="h-full ">
2025-12-21 22:26:33 -08:00
<main className="flex flex-col h-full">
2025-12-23 19:10:58 -08:00
<header className="sticky top-0 flex h-16 shrink-0 items-center gap-2 bg-background/95 backdrop-blur supports-backdrop-filter:bg-background/60 border-b">
2025-12-21 22:26:33 -08:00
<div className="flex items-center justify-between w-full gap-2 px-4">
<div className="flex items-center gap-2">
<SidebarTrigger className="-ml-1" />
<Separator orientation="vertical" className="h-6" />
<DashboardBreadcrumb />
</div>
<div className="flex items-center gap-2">
<LanguageSwitcher />
2025-11-11 04:02:04 +02:00
</div>
2025-12-21 22:26:33 -08:00
</div>
</header>
<div className="flex-1 overflow-hidden">{children}</div>
2025-11-11 04:02:04 +02:00
</main>
2025-07-27 10:05:37 -07:00
</SidebarInset>
</SidebarProvider>
);
}