| 1 | import { shortcut } from "./platform.mjs"; |
| 2 | import React, { useEffect, useRef, useState } from "react"; |
| 3 | import { |
| 4 | CircleAlert, |
| 5 | ExternalLink, |
| 6 | Folder, |
| 7 | FolderOpen, |
| 8 | Globe2, |
| 9 | Home, |
| 10 | Monitor, |
| 11 | Network, |
| 12 | MessageCircle, |
| 13 | PanelLeftClose, |
| 14 | Plus, |
| 15 | RefreshCw, |
| 16 | Settings, |
| 17 | Server, |
| 18 | SquareTerminal, |
| 19 | } from "lucide-react"; |
| 20 | import { EnvironmentShield } from "./environment-shield"; |
| 21 | import logo from "./logo.png"; |
| 22 | import { projectName } from "./workspace-state.mjs"; |
| 23 | import { WorkspaceTabs } from "./workspace-tabs"; |
| 24 | import { projectSessionRows, sessionKey } from "./session-order.mjs"; |
| 25 | import { AccountMenu } from "./account"; |
| 26 | import { useSSHNavigation } from "./remote-hosts"; |
| 27 | import { GuestSystemIcon } from "./guest-system-icon"; |
| 28 | |
| 29 | export function WorkspaceSidebar({ |
| 30 | workspace, |
| 31 | guestSystem, |
| 32 | defaultWorkspace, |
| 33 | projects, |
| 34 | tabs, |
| 35 | sessions = [], |
| 36 | sessionError, |
| 37 | sessionRefreshing, |
| 38 | onSessionRefresh, |
| 39 | onSessionOpen, |
| 40 | openingSession, |
| 41 | sessionOpenError, |
| 42 | onSessionArchive, |
| 43 | onSessionPin, |
| 44 | onSessionRename, |
| 45 | saved, |
| 46 | active, |
| 47 | home, |
| 48 | busy, |
| 49 | ready, |
| 50 | running, |
| 51 | runtimeLabel, |
| 52 | network, |
| 53 | onHome, |
| 54 | onOpenProject, |
| 55 | onProjectSelect, |
| 56 | onSelect, |
| 57 | onClose, |
| 58 | onRename, |
| 59 | onPin, |
| 60 | onSessionReorder, |
| 61 | splitDrag, |
| 62 | onNewTerminal, |
| 63 | terminalMenuAt, |
| 64 | onBrowser, |
| 65 | onTailscale, |
| 66 | downloadAction, |
| 67 | onClaudeWeb, |
| 68 | onClaudeDesktop, |
| 69 | desktopOpen, |
| 70 | onEditor, |
| 71 | onSettings, |
| 72 | onAccountConfirm, |
| 73 | onAccountStatus, |
| 74 | onSafety, |
| 75 | onCollapse, |
| 76 | updateAction, |
| 77 | }) { |
| 78 | const ssh = useSSHNavigation(); |
| 79 | const [collapsed, setCollapsed] = useState({}); |
| 80 | const sessionBaseline = useRef(new Map()); |
| 81 | const activeWorkspace = tabs.find((tab) => tab.id === active)?.workspace; |
| 82 | useEffect(() => { |
| 83 | if (activeWorkspace) |
| 84 | setCollapsed((previous) => ({ ...previous, [activeWorkspace]: false })); |
| 85 | }, [active, activeWorkspace]); |
| 86 | if (ssh?.expanded) return null; |
| 87 | const healthy = ready && network.mode === "open"; |
| 88 | return ( |
| 89 | <aside className="workspace-sidebar" aria-label="项目与终端"> |
| 90 | <div className="sidebar-top"> |
| 91 | <span className="sidebar-brand"> |
| 92 | <img className="brand-logo" src={logo} alt="" /> |
| 93 | Cage |
| 94 | <GuestSystemIcon system={guestSystem} /> |
| 95 | </span> |
| 96 | <button |
| 97 | className="icon-button" |
| 98 | onClick={onCollapse} |
| 99 | aria-label="收起侧栏" |
| 100 | title="收起侧栏" |
| 101 | > |
| 102 | <PanelLeftClose size={17} /> |
| 103 | </button> |
| 104 | </div> |
| 105 | <nav className="sidebar-navigation" aria-label="主导航"> |
| 106 | <button |
| 107 | className={`sidebar-link ${home ? "selected" : ""}`} |
| 108 | onClick={onHome} |
| 109 | > |
| 110 | <Home size={17} /> |
| 111 | 工作台 |
| 112 | </button> |
| 113 | <button |
| 114 | className={`sidebar-link ${!desktopOpen && !home && tabs.some((tab) => tab.id === active && tab.kind !== "terminal") ? "selected" : ""}`} |
| 115 | onClick={onBrowser} |
| 116 | disabled={!ready || busy} |
| 117 | aria-label="打开浏览器" |
| 118 | title="打开浏览器" |
| 119 | > |
| 120 | <Globe2 size={17} /> |
| 121 | 浏览器 |
| 122 | </button> |
| 123 | <button className="sidebar-link" onClick={onTailscale} aria-label="打开 Tailscale"><Network size={17} />Tailscale</button> |
| 124 | {ssh && <button className="sidebar-link ssh-navigation" onClick={ssh.open} aria-label="SSH 主机"><Server size={17} />SSH</button>} |
| 125 | <div className="claude-shortcuts"> |
| 126 | <button className="sidebar-link" onClick={onClaudeWeb} disabled={!ready || busy} title="在 Cage 浏览器打开 claude.ai"><MessageCircle size={17} />claude.ai</button> |
| 127 | <button className={`sidebar-link ${desktopOpen ? "selected" : ""}`} onClick={onClaudeDesktop} disabled={!running || busy} title="安装或打开 Claude Desktop"><Monitor size={17} />Claude Desktop</button> |
| 128 | </div> |
| 129 | {downloadAction} |
| 130 | </nav> |
| 131 | <div className="sidebar-scroll"> |
| 132 | <div className="sidebar-section-heading"> |
| 133 | <span>项目</span> |
| 134 | <button |
| 135 | className="icon-button" |
| 136 | onClick={() => onOpenProject()} |
| 137 | aria-label="添加项目" |
| 138 | title="添加项目" |
| 139 | disabled={busy} |
| 140 | > |
| 141 | <Plus size={15} /> |
| 142 | </button> |
| 143 | </div> |
| 144 | {sessionError && ( |
| 145 | <div className="sidebar-session-error"> |
| 146 | <div role="alert"> |
| 147 | <div className="sidebar-session-error-heading"> |
| 148 | <CircleAlert size={16} aria-hidden="true" /> |
| 149 | <strong>会话列表暂未更新</strong> |
| 150 | </div> |
| 151 | <p>{sessionError}</p> |
| 152 | </div> |
| 153 | <button |
| 154 | onClick={onSessionRefresh} |
| 155 | disabled={sessionRefreshing || busy || !running} |
| 156 | aria-busy={sessionRefreshing || undefined} |
| 157 | > |
| 158 | <RefreshCw size={14} aria-hidden="true" /> |
| 159 | {sessionRefreshing ? "正在读取…" : "重新读取会话"} |
| 160 | </button> |
| 161 | </div> |
| 162 | )} |
| 163 | {projects.map((path) => { |
| 164 | const project = projectName(path, defaultWorkspace); |
| 165 | const rows = projectSessionRows(tabs.filter(tab => tab.kind === "terminal" && tab.workspace === path), |
| 166 | sessions.filter(session => session.workspace === path), saved, saved.sessionOrder?.[path], sessionBaseline.current); |
| 167 | const rowMetadata = { ...saved }; |
| 168 | for (const row of rows) rowMetadata[row.id] = { ...saved[row.id], pinned: row.sessionPinned }; |
| 169 | const expanded = !collapsed[path]; |
| 170 | return ( |
| 171 | <section |
| 172 | className="sidebar-project" |
| 173 | key={path} |
| 174 | aria-label={`项目 ${project}`} |
| 175 | > |
| 176 | <div |
| 177 | className={`project-heading ${workspace === path ? "current-project" : ""}`} |
| 178 | > |
| 179 | <button |
| 180 | className="project-disclosure" |
| 181 | title={path} |
| 182 | aria-expanded={expanded} |
| 183 | onClick={() => { |
| 184 | setCollapsed((previous) => ({ |
| 185 | ...previous, |
| 186 | [path]: expanded, |
| 187 | })); |
| 188 | onProjectSelect(path); |
| 189 | }} |
| 190 | > |
| 191 | {expanded ? <FolderOpen size={16} /> : <Folder size={16} />} |
| 192 | <span>{project}</span> |
| 193 | </button> |
| 194 | <button |
| 195 | className="icon-button" |
| 196 | aria-label={`打开 ${project} 的编辑器`} |
| 197 | title="在 code-server 中打开项目目录" |
| 198 | disabled={!ready || busy} |
| 199 | onClick={() => onEditor(path)} |
| 200 | > |
| 201 | <ExternalLink size={15} /> |
| 202 | </button> |
| 203 | <button |
| 204 | className="icon-button" |
| 205 | aria-label={`在 ${project} 新建终端`} |
| 206 | title="新建终端" |
| 207 | aria-haspopup="menu" |
| 208 | data-terminal-location={path} |
| 209 | disabled={!running || busy} |
| 210 | onClick={(event) => { |
| 211 | setCollapsed((previous) => ({ |
| 212 | ...previous, |
| 213 | [path]: false, |
| 214 | })); |
| 215 | onNewTerminal(path, event.currentTarget); |
| 216 | }} |
| 217 | > |
| 218 | <SquareTerminal size={15} /> |
| 219 | </button> |
| 220 | </div> |
| 221 | {terminalMenuAt(path)} |
| 222 | <div |
| 223 | className={`project-children ${expanded ? "expanded" : ""}`} |
| 224 | inert={!expanded} |
| 225 | > |
| 226 | <div> |
| 227 | {!!rows.length && ( |
| 228 | <WorkspaceTabs |
| 229 | tabs={rows} |
| 230 | saved={rowMetadata} |
| 231 | openingSession={openingSession} |
| 232 | sessionOpenError={sessionOpenError} |
| 233 | active={home || desktopOpen ? "" : active} |
| 234 | label={`${project} 的终端`} |
| 235 | busy={busy} |
| 236 | ready={ready} |
| 237 | onSelect={(id, ...args) => { |
| 238 | const row = rows.find(row => row.id === id); |
| 239 | if (row?.historySession) onSessionOpen(row.historySession); |
| 240 | else onSelect(id, ...args); |
| 241 | }} |
| 242 | onClose={(row, anchor) => row.historySession ? onSessionArchive(row.historySession, anchor) : onClose(row, anchor)} |
| 243 | onRename={row => row.historySession ? onSessionRename(row.historySession) : onRename(row)} |
| 244 | onPin={id => { |
| 245 | const row = rows.find(row => row.id === id); |
| 246 | if (row?.historySession) onSessionPin(row.historySession); |
| 247 | else onPin(id); |
| 248 | }} |
| 249 | onReorder={(from, to) => { |
| 250 | const targetIndex = rows.findIndex(row => row.id === to); |
| 251 | const order = rows.map(row => row.id).filter(id => id !== from); |
| 252 | order.splice(targetIndex, 0, from); |
| 253 | onSessionReorder(path, order.map(id => sessionKey(rows.find(row => row.id === id)))); |
| 254 | }} |
| 255 | splitDrag={splitDrag} |
| 256 | /> |
| 257 | )} |
| 258 | {!rows.length && !running && ( |
| 259 | <p className="sidebar-empty">启动后可新建终端</p> |
| 260 | )} |
| 261 | </div> |
| 262 | </div> |
| 263 | </section> |
| 264 | ); |
| 265 | })} |
| 266 | {!projects.length && ( |
| 267 | <p className="sidebar-empty">打开本地文件夹开始工作</p> |
| 268 | )} |
| 269 | </div> |
| 270 | <div className="sidebar-bottom"> |
| 271 | <button className="sidebar-status" onClick={onSafety}> |
| 272 | <i className={`status-dot ${network.mode}`} /> |
| 273 | <span>{healthy ? "环境运行正常" : runtimeLabel}</span> |
| 274 | <EnvironmentShield size={17} healthy={healthy} /> |
| 275 | </button> |
| 276 | <div className="sidebar-footer-actions"> |
| 277 | <AccountMenu onSettings={onSettings} onConfirm={onAccountConfirm} onStatus={onAccountStatus} busy={busy} /> |
| 278 | <button className="icon-button" aria-label="设置" title={shortcut("设置 ⌘,")} onClick={() => onSettings("keyboard")}><Settings size={17} /></button> |
| 279 | {updateAction} |
| 280 | </div> |
| 281 | </div> |
| 282 | </aside> |
| 283 | ); |
| 284 | } |