| 1 | import React, { useEffect, useRef } from "react"; |
| 2 | import { Folder, FolderOpen, Globe2, LoaderCircle, LogOut, Monitor, Plus, Settings, Sparkles } from "lucide-react"; |
| 3 | import { InlineSelect } from "./inline-select"; |
| 4 | import { SessionRow, sortSessions } from "./session-row"; |
| 5 | |
| 6 | export const workspaceTools = [ |
| 7 | {id:"browser", name:"浏览器", icon:Globe2}, |
| 8 | {id:"claude", name:"Claude.ai", icon:Sparkles}, |
| 9 | {id:"desktop", name:"Claude Desktop", icon:Monitor}, |
| 10 | ]; |
| 11 | |
| 12 | export function CodeNavigation({catalog, project, sessions, current, creating, onProject, onSession, onNew, onAction, formatTime}) { |
| 13 | if (!catalog) return <p className="nav-note" role="status">正在读取项目…</p>; |
| 14 | if (!project) return <p className="nav-note">请先在电脑 Cage 打开项目。</p>; |
| 15 | return <section className="code-navigation" aria-label="Claude Code 项目和会话"> |
| 16 | <div className="project-field"> |
| 17 | <InlineSelect label="选择项目" icon={<FolderOpen size={16} />} value={project.path} |
| 18 | disabled={creating} onChange={onProject} options={catalog.projects.map(p => ({ |
| 19 | value:p.path, label:p.name, optionLabel:p.name + " · " + p.path, |
| 20 | }))} /> |
| 21 | </div> |
| 22 | <div className="row-heading"><h2>会话</h2><button type="button" className="nav-new" onClick={onNew} disabled={creating || !catalog.ready}> |
| 23 | {creating ? <LoaderCircle size={15} className="spin" /> : <Plus size={15} />}新建会话</button></div> |
| 24 | {sessions.length ? <ul className="session-list">{sessions.map(s => <SessionRow key={s.id} session={s} current={current === s.id} |
| 25 | disabled={creating} onSelect={() => onSession(s.id)} onAction={onAction} detail={formatTime(s.updated_at)}/>)}</ul> |
| 26 | : <p className="nav-note">{catalog.ready ? "这个项目还没有会话。" : "请先在电脑上启动 Cage。"}</p>} |
| 27 | </section>; |
| 28 | } |
| 29 | |
| 30 | export function ProjectTree({catalog, projectPath, sessionID, expanded, toggle, creating, onNew, onSession, onAction}) { |
| 31 | const projects = catalog?.projects || []; |
| 32 | return <section className="project-tree" aria-label="Claude Code 项目和会话"> |
| 33 | <h2>Claude Code</h2> |
| 34 | {catalog?.shared_sessions === false && <p className="nav-note">当前启动配置使用独立移动会话。共享电脑终端需要使用 claude 启动命令。</p>} |
| 35 | {!catalog ? <p className="nav-note" role="status">正在读取项目…</p> : |
| 36 | !projects.length ? <p className="nav-note">请在电脑 Cage 打开项目。</p> : projects.map((project, index) => { |
| 37 | const path = project.path; |
| 38 | const open = expanded.get(path) ?? (path === projectPath || index === 0); |
| 39 | const name = projects.some(other => other !== project && other.name === project.name) ? path : project.name; |
| 40 | const sessions = catalog.sessions.filter(s => s.workspace === path) |
| 41 | .sort(sortSessions); |
| 42 | return <div className="tree-project" key={path}> |
| 43 | <div className="tree-row"> |
| 44 | <button type="button" className="tree-folder" title={path} aria-label={"项目 " + name} |
| 45 | aria-expanded={open} onClick={() => toggle(path, !open)}> |
| 46 | {open ? <FolderOpen size={17} /> : <Folder size={17} />} |
| 47 | <span>{name}</span> |
| 48 | </button> |
| 49 | <button type="button" className="icon-button tree-new" aria-label={"在 " + name + " 新建会话"} |
| 50 | title={"在 " + name + " 新建会话"} disabled={creating || !catalog.ready} onClick={() => onNew(project)}> |
| 51 | <Plus size={16} /> |
| 52 | </button> |
| 53 | </div> |
| 54 | {open && <ul className="tree-sessions" aria-label={name + "的会话"}> |
| 55 | {sessions.length ? sessions.map(s => <SessionRow key={s.id} session={s} current={s.id === sessionID} disabled={creating} |
| 56 | onSelect={() => onSession(path, s.id)} onAction={onAction}/>) : <li className="tree-empty">暂无会话</li>} |
| 57 | </ul>} |
| 58 | </div>; |
| 59 | })} |
| 60 | </section>; |
| 61 | } |
| 62 | |
| 63 | export function WorkspaceDrawer({open, active, name, onClose, onTool, onExit, onSettings, nativePhone, children}) { |
| 64 | const panel = useRef(null); |
| 65 | useEffect(() => { |
| 66 | const dialog = panel.current; |
| 67 | let frame; |
| 68 | let timer; |
| 69 | if (open) { |
| 70 | if (!dialog.open) dialog.showModal(); |
| 71 | frame = requestAnimationFrame(() => { frame = requestAnimationFrame(() => { dialog.dataset.visible = "true"; }); }); |
| 72 | } else if (dialog.open) { |
| 73 | delete dialog.dataset.visible; |
| 74 | if (matchMedia("(prefers-reduced-motion: reduce)").matches) dialog.close(); |
| 75 | else timer = setTimeout(() => dialog.close(), 240); |
| 76 | } |
| 77 | return () => { cancelAnimationFrame(frame); clearTimeout(timer); }; |
| 78 | }, [open]); |
| 79 | return <dialog className="workspace-drawer" ref={panel} aria-label="工作区侧栏" |
| 80 | onCancel={event => {event.preventDefault(); onClose();}} |
| 81 | onClick={event => { |
| 82 | if (event.target !== panel.current) return; |
| 83 | const rect = panel.current.getBoundingClientRect(); |
| 84 | if (event.clientX < rect.left || event.clientX >= rect.right || event.clientY < rect.top || event.clientY >= rect.bottom) onClose(); |
| 85 | }}> |
| 86 | <header className="drawer-heading"><Monitor size={18} /><h2>{name || "远程工作台"}</h2></header> |
| 87 | <div className="drawer-content"> |
| 88 | <nav className="workspace-tools" aria-label="远程工具">{workspaceTools.map(({id, name:toolName, icon:Icon}) => |
| 89 | <button key={id} type="button" className="tool-link" aria-current={id === active ? "page" : undefined} onClick={() => onTool(id)}> |
| 90 | <Icon size={19} /><span>{toolName}</span> |
| 91 | </button>)}</nav> |
| 92 | {children} |
| 93 | </div> |
| 94 | <footer className="drawer-footer"><button className="drawer-exit" type="button" onClick={onSettings}><Settings size={18}/>设置</button> |
| 95 | <a className="drawer-exit" href={nativePhone ? "cage://home" : "/"} |
| 96 | onClick={event => {if (onExit()) event.preventDefault();}}><LogOut size={18}/>退出</a></footer> |
| 97 | </dialog>; |
| 98 | } |