import {sessionTitle} from '../lib/session-title'; import { tr } from '../lib/i18n'; import { useNative } from '../lib/native'; import { modelFamilyIcon } from '../lib/model-icons'; import { TaskTree } from './TaskTree'; /** Quiet navigation, with the same destinations at every window size. */ import { useEffect, useId, useLayoutEffect, useRef, useState, type RefObject } from 'react'; import { createPortal } from 'react-dom'; import { FloatingMenu } from '../components/FloatingMenu'; import { measureFrame } from '../lib/overlay-place'; import { QuitStrataDialog } from '../components/QuitStrata'; import { useAppMode } from '../lib/app-mode'; import { href, useLocation, type Route } from '../lib/router'; import { cx, relTime, relTimeShort } from '../lib/format'; import { useMedia } from '../lib/responsive'; import { recentChats, recentChatDescription } from '../lib/recent-chats'; import type { Session } from '../lib/types'; import { MOD, runCommand } from '../lib/commands'; import { bindingFor, bindingKeys, useBindings } from '../lib/shortcut-bindings'; import { Btn, Dialog, Icon, IconBtn, type IconName } from '../components/ui'; import { Brand } from '../components/Brand'; import { addWorkspace, deleteSession, go, isUnread, liveChatIds, pickWorkspace, renameSession, removeWorkspace, renameWorkspace, selectSession, setActiveWorkspace, setNavOpen, togglePin, toggleRail, setTheme, useStore } from '../lib/store'; export function Sidebar({ containerRef }: { containerRef: RefObject; }) { useBindings(); const location = useLocation(); const compact = useMedia('(max-width: 1279px)'); const rail = useStore(s => s.rail); const theme = useStore(s => s.theme); const systemDark = useMedia('(prefers-color-scheme: dark)'); const dark = theme === 'dark' || theme === 'system' && systemDark; const navOpen = useStore(s => s.navOpen); const collapsed = compact ? !navOpen : rail; const sessions = useStore(s => s.sessions); const library = useStore(s => s.library); const monitor = useStore(s => s.monitor); const workspaces = useStore(s => s.workspaces); const activeWorkspace = useStore(s => s.activeWorkspace); const activeSession = useStore(s => s.activeSession); // The chats that have a turn running now (the board reads the same list). The count is what makes this redraw when one starts or ends. const liveCount = useStore(s => s.liveChats); const working = new Set(liveCount ? liveChatIds() : []); const [query, setQuery] = useState(''); const [searchOpen, setSearchOpen] = useState(false); const searchInput = useRef(null); const searchTrigger = useRef(null); useEffect(() => { if (searchOpen) searchInput.current?.focus(); }, [searchOpen, collapsed]); function closeSearch() { setQuery(''); setSearchOpen(false); requestAnimationFrame(() => searchTrigger.current?.focus()); } const [recentOpen, setRecentOpen] = useState(false); const [includeArchived, setIncludeArchived] = useState(false); const [removeWorkspaceId, setRemoveWorkspaceId] = useState(null); const [menuId, setMenuId] = useState(null); const [renameId, setRenameId] = useState(null); const [renameWsId, setRenameWsId] = useState(null), [wsName, setWsName] = useState(''); const [name, setName] = useState(''); const [deleteId, setDeleteId] = useState(null); const [folders, setFolders] = useState>({}); const menuTrigger = useRef(null); const current = library?.variants.find(v => v.id === library.loaded); const native = useNative(), nativeCurrent = native?.instances.find(i => i.id === native.defaultId); const modelName = nativeCurrent?.label ?? (current?.local ? current.name : current?.family) ?? tr('No model loaded'); // The version from the catalog and the card it runs on ("Q2_0 · GPU 2"), not the file id. const nativeSize = nativeCurrent ? native?.catalog?.families.flatMap(f => f.sizes).find(s => s.id === nativeCurrent.model) : undefined; const nativeSecond = nativeCurrent ? [nativeSize?.model ?? nativeCurrent.model.toUpperCase(), nativeCurrent.gpus.length ? nativeCurrent.gpus.map(g => `GPU ${g + 1}`).join(' + ') : ''].filter(Boolean).join(' · ') : null; const modelQuant = nativeSecond ?? current?.quant ?? tr('View model versions'); const phase = nativeCurrent ? (nativeCurrent.status === 'ready' ? nativeCurrent.phase : nativeCurrent.status) : monitor?.phase ?? 'idle'; const status = phase === 'unavailable' ? 'Status unavailable' : phase === 'error' ? 'Error' : phase === 'loading' ? 'Loading' : !current && !nativeCurrent ? 'No model loaded' : phase === 'reading' ? 'Reading prompt' : phase === 'thinking' ? 'Thinking' : phase === 'generating' ? 'Generating' : 'Ready'; // The model is busy with a chat that is not the one in front of the person: the chip says which one (a title, or how many). const awayChats = [...working].filter(id => !(location.route === 'chat' && id === activeSession)); const awayTitle = awayChats.length === 1 ? sessions.find(s => s.id === awayChats[0]) : undefined; const workingLine = awayChats.length === 1 && awayTitle ? tr("Working · {0}", [sessionTitle(awayTitle)]) : awayChats.length > 1 ? tr("{0} chats working", [awayChats.length]) : null; const menuSession = sessions.find(s => s.id === menuId); const q = query.trim().toLowerCase(); const matching = sessions.filter(s => (!s.archived || q || includeArchived) && (!q || `${sessionTitle(s)} ${s.messages.map(m => m.content).join(' ')}`.toLowerCase().includes(q))).sort((a, b) => Number(!!b.pinned) - Number(!!a.pinned) || b.updatedAt.localeCompare(a.updatedAt)); function openMenu(id: string, trigger: HTMLElement) { menuTrigger.current = trigger; setMenuId(id); } function closeMenu() { setMenuId(null); menuTrigger.current?.focus(); } return <> {tr(menuId && menuSession ? : null)} setRenameId(null)} actions={<> setRenameId(null)}>{tr("Cancel")} { if (renameId) renameSession(renameId, name.trim()); setRenameId(null); }}>{tr("Save name")}}> setRenameWsId(null)} actions={<> setRenameWsId(null)}>{tr("Cancel")} { if (renameWsId) void renameWorkspace(renameWsId, wsName.trim()); setRenameWsId(null); }}>{tr("Save name")}}>

{tr("Only the name changes. The folder stays where it is.")}

setDeleteId(null)} actions={<> setDeleteId(null)}>{tr("Keep chat")} { if (deleteId) deleteSession(deleteId); setDeleteId(null); }}>{tr("Delete chat")}}>

{tr("This chat will be permanently deleted.")}

setRemoveWorkspaceId(null)} actions={<> setRemoveWorkspaceId(null)}>{tr("Keep workspace")} { if (removeWorkspaceId) void removeWorkspace(removeWorkspaceId).then(() => setRemoveWorkspaceId(null)); }}>{tr("Remove from sidebar")}}>

{tr("Files stay on your computer. Add this folder again to reopen its chats.")}

setRecentOpen(false)}>
setQuery(e.target.value)}/>{tr(matching.map(s => ))}{tr(!matching.length ?

{tr("No matching chats. Try another search.")}

: null)}
; } function RecentChatLink({ session, active, slot }: { session: Session; active: boolean; slot: number; }) { const binding = bindingFor('chat:slot:' + slot, { key: String(slot), mod: true, shift: false, alt: false }); const keys = bindingKeys(binding, MOD); const anchor = useRef(null); const tooltip = useRef(null); const id = useId(); const [open, setOpen] = useState(false); const [position, setPosition] = useState({ left: 0, top: 0 }); useLayoutEffect(() => { if (!open) return; const place = () => { const rect = anchor.current?.getBoundingClientRect(); if (!rect) return; const { scale, viewport } = measureFrame(); const height = tooltip.current?.getBoundingClientRect().height ?? 80 * scale; setPosition({ left: (rect.right + 12) / scale, top: Math.max(12, Math.min(rect.top, viewport.height - height - 12)) / scale }); }; place(); window.addEventListener('resize', place); window.addEventListener('scroll', place, true); return () => { window.removeEventListener('resize', place); window.removeEventListener('scroll', place, true); }; }, [open]); return <> { setOpen(false); selectSession(session.id); }} onMouseEnter={() => setOpen(true)} onMouseLeave={() => setOpen(false)} onFocus={() => setOpen(true)} onBlur={() => setOpen(false)} onKeyDown={event => { if (event.key === 'Escape' && open) { event.preventDefault(); event.stopPropagation(); setOpen(false); } }}> {open ? createPortal(, document.body) : null} ; } function NavLink({ route, label, icon, current, meta }: { route: Route; label: string; icon: IconName; current: string; meta?: string; }) { return {tr(label)}{tr(meta ? {tr(meta)} : null)}; } function MoreMenu({ current }: { current: string; }) { const [open, setOpen] = useState(false); const [quitOpen, setQuitOpen] = useState(false); const appMode = useAppMode(); const ref = useRef(null); return <>{tr(open ? { setOpen(false); ref.current?.focus(); }}>{tr(([{ route: 'serving', label: 'Serving', icon: 'link' }, { route: 'settings', label: 'Settings', icon: 'settings' }] as const).map(d => setOpen(false)}>{tr(d.label)}))}{appMode.canQuit ? : null} : null)} setQuitOpen(false)}/>; }