"use client"; import Tooltip from "@/shared/ui/Tooltip"; import { useEffect, useRef, useState } from "react"; import Link from "next/link"; import dynamic from "next/dynamic"; import { Check, ChevronDown, Folder, FolderOpen, Plus } from "lucide-react"; import { useTranslation } from "react-i18next"; import { useLingerExpand } from "@/hooks/use-linger-expand"; import ToolbarLabel from "@/components/chat/home/ToolbarLabel"; import { saveWorkspace, type ChatWorkspaceRegistration } from "@/lib/workspaces-api"; const ServerFolderPicker = dynamic(() => import("./ServerFolderPicker").then(module => module.ServerFolderPicker), { ssr: false }); /** * Which workspace this conversation belongs to. * * Unlike the other composer settings, picking a workspace is not a preference * that takes effect next turn: it re-navigates the app (a full page load, * which drops the draft) and moves an existing conversation's storage. That is * why ``readOnly`` * turns it into a plain label once the conversation has messages. Re-binding a * thread mid-conversation is a management action; it belongs on the workspace * settings page, where the consequences are visible, rather than one stray * click away from a half-written message. */ export function WorkspacePill({ workspaces, workspaceId, onSelect, disabled = false, readOnly = false, collapsible = false, error = "", label: pickerLabel = "Conversation workspace", }: { workspaces: ChatWorkspaceRegistration[]; workspaceId: string; onSelect: (workspaceId: string) => void; disabled?: boolean; collapsible?: boolean; /** Freeze the binding as a label: this conversation already has messages. */ readOnly?: boolean; error?: string; label?: string; }) { const { t } = useTranslation(); const [creating, setCreating] = useState(false); const [browsing, setBrowsing] = useState(false); const [name, setName] = useState(""); const [saving, setSaving] = useState(false); const [createError, setCreateError] = useState(""); const [created, setCreated] = useState([]); const [open, setOpen] = useState(false); const { expanded, triggerProps } = useLingerExpand(open, 1200, !collapsible); const menuRef = useRef(null); const buttonRef = useRef(null); useEffect(() => { if (!open) return; const close = (event: MouseEvent) => { const target = event.target as Node; if ( !menuRef.current?.contains(target) && !buttonRef.current?.contains(target) ) { setOpen(false); } }; const closeOnEscape = (event: KeyboardEvent) => { if (event.key === "Escape") setOpen(false); }; document.addEventListener("mousedown", close); document.addEventListener("keydown", closeOnEscape); return () => { document.removeEventListener("mousedown", close); document.removeEventListener("keydown", closeOnEscape); }; }, [open]); const choices = [...workspaces, ...created.filter(row => !workspaces.some(item => item.workspace_id === row.workspace_id))]; const active = choices.find((row) => row.workspace_id === workspaceId); // An archived workspace keeps its conversations; it just stops being offered // for new ones. Still listed while it is the current binding, or the menu // would deny the workspace the pill is naming. const selectable = choices.filter( (row) => row.kind === "workspace" && (!row.archived || row.workspace_id === workspaceId), ); const label = active ? active.display_name : workspaceId ? t("Workspace unavailable") : t("Default workspace"); if (readOnly) { return ( {workspaceId ? ( ) : ( )} {label} {label} ); } return (
{error ? ( {error} ) : null} {open && !disabled && (
{selectable.length === 0 ? (

{t( "Create a workspace to keep conversations and learning materials together.", )}

) : ( selectable.map((row) => ( )) )}
{workspaceId ? ( ) : null} {creating ? (
{ event.preventDefault(); event.stopPropagation(); if (!name.trim() || saving) return; setSaving(true); setCreateError(""); try { const row = await saveWorkspace({ name: name.trim() }); setCreated(items => [...items, row]); onSelect(row.workspace_id); setCreating(false); setName(""); setOpen(false); } catch (error) { setCreateError(error instanceof Error ? error.message : String(error)); } finally { setSaving(false); } }}> {createError &&

{createError}

}
) : null}
setOpen(false)} className="min-w-0 flex-1 rounded-md px-1 py-1.5 text-[11.5px] transition-colors hover:bg-[var(--muted)] hover:text-[var(--foreground)] focus-visible:outline focus-visible:outline-2 focus-visible:outline-[var(--ring)]" > {t("Manage workspaces")} {!creating && ( )}
)} {browsing && setBrowsing(false)} onSelect={async folder => { const existing = choices.find(item => item.kind === "workspace" && item.path === folder.path); const row = existing ? existing.archived ? await saveWorkspace({ archived: false }, existing.workspace_id) : existing : await saveWorkspace({ name: folder.name.slice(0, 100), path: folder.path }); setCreated(items => [...items.filter(item => item.workspace_id !== row.workspace_id), row]); onSelect(row.workspace_id); }} />}
); }