"use client"; import { useState } from "react"; import { usePathname, useRouter } from "next/navigation"; import { Check, ChevronDown, Plus, Settings2 } from "lucide-react"; import { Button } from "@/components/ui/button"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { ConnectInstanceDialog } from "@/components/instance/connect-instance-dialog"; import { InstanceConnectivityDot } from "@/components/instance/instance-connectivity-dot"; import { useInstanceConnectivity, useInstances, useSwitchInstance, } from "@/hooks/use-instances"; import { useI18n } from "@/i18n/i18n-context"; import { cn } from "@/lib/utils"; /** * Map the current path to a safe destination after switching the active * instance. Pages that address a specific resource by name (knowledge base * detail, memory store detail, ...) would render a "not found" state under * the new instance, so we fall back to the corresponding list view. Pages * that are instance-scoped but not resource-scoped (e.g. the chat homepage * or the LLM provider list) stay where they are. */ function resolveSwitchTarget(pathname: string | null): string | null { if (!pathname) return null; const kbDetail = pathname.match(/^\/knowledge-base\/[^/]+(?:\/.*)?$/); if (kbDetail) return "/knowledge-base"; const memoryDetail = pathname.match(/^\/memory\/[^/]+(?:\/.*)?$/); if (memoryDetail) return "/memory"; return null; } export function InstanceSwitcher() { const router = useRouter(); const pathname = usePathname(); const { t } = useI18n(); const [connectOpen, setConnectOpen] = useState(false); const { data, isLoading } = useInstances(); const { data: connectivity } = useInstanceConnectivity(); const switchMutation = useSwitchInstance(); const activeName = data?.activeInstance ?? null; const instances = data?.instances ?? []; async function handleSwitch(name: string) { if (name === activeName || switchMutation.isPending) return; try { await switchMutation.mutateAsync(name); // Hard reload (not router.refresh) so every client-side state owned // by the dashboard rebuilds against the new instance: // - ChatConfigProvider's mounts / provider / model selection // - QaChatPanel local drafts and unsent input // - any in-memory hook state keyed off the previous instance // router.refresh only re-fetches RSC payloads; it leaves the React // tree (and thus all of the above) intact, which is what made the // dashboard appear to "not actually switch" in production builds. // Mirrors the standard Discord / GitHub / Notion workspace-switch UX. // // When the current page is scoped to a resource that does not exist // under the new instance (e.g. /knowledge-base/), redirect to // the corresponding list view to avoid the "knowledge base not found" // dead-end. Other pages keep the current pathname. const target = resolveSwitchTarget(pathname) ?? window.location.pathname; window.location.assign(target); } catch { // Error toast is handled inside the hook. } } return ( <> {t("instance.switcher.switchProject")} {instances.length === 0 ? (
{t("instance.switcher.noProjects")}
) : ( instances.map((item) => { const isActive = item.name === activeName; return ( { event.preventDefault(); handleSwitch(item.name); }} className="flex items-center gap-2 pl-2 pr-2" > {item.name} {item.region && ( {item.region} )} ); }) )} { event.preventDefault(); setConnectOpen(true); }} className="gap-2" > {t("instance.switcher.createProject")} { event.preventDefault(); router.push("/projects"); }} className="gap-2" > {t("instance.switcher.manageProjects")}
router.refresh()} /> ); }