"use client"; import { useRef, useState, type KeyboardEvent } from "react"; import { Check, ChevronDown, X } from "lucide-react"; import { toast } from "sonner"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Separator } from "@/components/ui/separator"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; import { useSubspaceOptional } from "@/contexts/subspace-context"; import { useI18n } from "@/i18n/i18n-context"; /** * Inline subspace switcher rendered in KBHeader. Mirrors the UX of GitHub * repo / Linear workspace switchers and keeps a placeholder trigger after * a clear so the user can immediately pick another scope. */ export function SubspaceSwitcher() { const ctx = useSubspaceOptional(); const { t } = useI18n(); const [open, setOpen] = useState(false); const [inputValue, setInputValue] = useState(""); const inputRef = useRef(null); // No provider mounted (e.g. KB list page). if (!ctx) return null; const { subspace, history, setSubspace, clearSubspace, clearHistory } = ctx; const hasActive = subspace !== null; const handleSubmit = () => { const trimmed = inputValue.trim(); if (!trimmed || trimmed === subspace) { setInputValue(""); setOpen(false); return; } setSubspace(trimmed); setInputValue(""); setOpen(false); }; const handleKeyDown = (e: KeyboardEvent) => { if (e.key === "Enter") { e.preventDefault(); handleSubmit(); } }; const handlePick = (s: string) => { if (s !== subspace) setSubspace(s); setOpen(false); }; const handleClearHistory = () => { inputRef.current?.focus(); clearHistory(); }; const handleClear = () => { clearSubspace(); setOpen(false); toast.success(t("knowledgeBase.subspace.cleared")); }; return (
setInputValue(e.target.value)} onKeyDown={handleKeyDown} className="h-8 text-sm" />
{history.length > 0 && (
{t("knowledgeBase.subspace.recentlyUsed")}
{history.map((s) => { const active = hasActive && s === subspace; return ( ); })}
)} {hasActive && ( <> )}
); }