Open History to see the last commit
1import { useCallback, useEffect, useMemo, useState } from "react";
2import { DEFAULT_MODEL } from "../model-logic";
3import { readNativeApi } from "../session-logic";
4import { useStore } from "../store";
5import type { Project } from "../types";
6
7
8function formatRelativeTime(iso: string): string {
9 const diff = Date.now() - new Date(iso).getTime();
10 const minutes = Math.floor(diff / 60_000);
11 if (minutes < 1) return "just now";
12 if (minutes < 60) return `${minutes}m ago`;
13 const hours = Math.floor(minutes / 60);
14 if (hours < 24) return `${hours}h ago`;
15 return `${Math.floor(hours / 24)}d ago`;
16}
17
18function threadStatusLabel(
19 status: "connecting" | "ready" | "running" | "error" | "closed" | undefined,
20): "Working" | "Connecting" | null {
21 if (status === "running") return "Working";
22 if (status === "connecting") return "Connecting";
23 return null;
24}
25
26export default function Sidebar() {
27 const { state, dispatch } = useStore();
28 const api = useMemo(() => readNativeApi(), []);
29 const [addingProject, setAddingProject] = useState(false);
30 const [newCwd, setNewCwd] = useState("");
31 const [isPickingFolder, setIsPickingFolder] = useState(false);
32
33 const handleAddProject = () => {
34 const cwd = newCwd.trim();
35 if (!cwd) return;
36 const name = cwd.split("/").filter(Boolean).pop() ?? "project";
37 const project: Project = {
38 id: crypto.randomUUID(),
39 name,
40 cwd,
41 model: DEFAULT_MODEL,
42 expanded: true,
43 };
44 dispatch({ type: "ADD_PROJECT", project });
45 setNewCwd("");
46 setAddingProject(false);
47 };
48
49 const handleNewThread = useCallback(
50 (projectId: string) => {
51 dispatch({
52 type: "ADD_THREAD",
53 thread: {
54 id: crypto.randomUUID(),
55 projectId,
56 title: "New thread",
57 model:
58 state.projects.find((p) => p.id === projectId)?.model ??
59 DEFAULT_MODEL,
60 session: null,
61 messages: [],
62 events: [],
63 error: null,
64 createdAt: new Date().toISOString(),
65 },
66 });
67 },
68 [dispatch, state.projects],
69 );
70
71 const handlePickFolder = async () => {
72 if (!api || isPickingFolder) return;
73 setIsPickingFolder(true);
74 try {
75 const pickedPath = await api.dialogs.pickFolder();
76 if (!pickedPath) return;
77 setNewCwd(pickedPath);
78 } finally {
79 setIsPickingFolder(false);
80 }
81 };
82
83 useEffect(() => {
84 const onWindowKeyDown = (event: KeyboardEvent) => {
85 const isNewThreadShortcut =
86 event.metaKey &&
87 event.shiftKey &&
88 !event.ctrlKey &&
89 !event.altKey &&
90 event.key.toLowerCase() === "o";
91 if (!isNewThreadShortcut) return;
92
93 const target = event.target;
94 if (target instanceof HTMLElement) {
95 const tagName = target.tagName;
96 if (
97 target.isContentEditable ||
98 tagName === "INPUT" ||
99 tagName === "TEXTAREA" ||
100 tagName === "SELECT"
101 ) {
102 return;
103 }
104 }
105
106 const activeThread = state.threads.find(
107 (t) => t.id === state.activeThreadId,
108 );
109 const projectId = activeThread?.projectId ?? state.projects[0]?.id;
110 if (!projectId) return;
111
112 event.preventDefault();
113 handleNewThread(projectId);
114 };
115
116 window.addEventListener("keydown", onWindowKeyDown);
117 return () => {
118 window.removeEventListener("keydown", onWindowKeyDown);
119 };
120 }, [handleNewThread, state.activeThreadId, state.projects, state.threads]);
121
122 return (
123 <aside className="sidebar flex h-full w-[260px] shrink-0 flex-col border-r border-white/8 bg-[#141414]">
124 {/* Drag region / traffic light space */}
125 <div className="drag-region h-[52px] shrink-0" />
126 {/* Branding */}
127 <div className="flex items-center gap-2 px-4 pb-4">
128 <div className="flex h-7 w-7 items-center justify-center rounded-lg bg-white text-xs font-bold text-[#0c0c0c]">
129 CT
130 </div>
131 <span className="text-sm font-semibold tracking-tight text-[#e0e0e0]">CodeThing</span>
132 </div>
133
134 {/* New thread (global) */}
135 <div className="px-3 pb-3">
136 <button
137 type="button"
138 className="flex w-full items-center gap-2 rounded-lg border border-white/8 bg-white/4 px-3 py-2 text-xs text-[#a0a0a0]/80 transition-colors duration-150 hover:bg-white/[0.07]"
139 onClick={() => {
140 if (state.projects.length === 0) {
141 setAddingProject(true);
142 return;
143 }
144 const firstProject = state.projects[0];
145 if (firstProject) handleNewThread(firstProject.id);
146 }}
147 >
148 <span className="text-white">+</span>
149 New thread
150 </button>
151 </div>
152
153 {/* Project list */}
154 <nav className="flex-1 overflow-y-auto px-2">
155 {state.projects.map((project) => {
156 const threads = state.threads
157 .filter((t) => t.projectId === project.id)
158 .sort((a, b) => {
159 const byDate =
160 new Date(b.createdAt).getTime() -
161 new Date(a.createdAt).getTime();
162 if (byDate !== 0) return byDate;
163 return b.id.localeCompare(a.id);
164 });
165 return (
166 <div key={project.id} className="mb-1">
167 {/* Project header */}
168 <button
169 type="button"
170 className="flex w-full items-center gap-2 rounded-md px-2 py-1.5 text-left transition-colors duration-150 hover:bg-white/5"
171 onClick={() => dispatch({ type: "TOGGLE_PROJECT", projectId: project.id })}
172 >
173 <span className="text-[10px] text-[#a0a0a0]/50">
174 {project.expanded ? "▼" : "▶"}
175 </span>
176 <span className="flex-1 truncate text-xs font-medium text-[#e0e0e0]/90">
177 {project.name}
178 </span>
179 <span className="text-[10px] text-[#a0a0a0]/40">{threads.length}</span>
180 </button>
181
182 {/* Threads */}
183 {project.expanded && (
184 <div className="ml-2 border-l border-white/6 pl-2">
185 {threads.map((thread) => {
186 const isActive = state.activeThreadId === thread.id;
187 const threadStatus = threadStatusLabel(
188 thread.session?.status,
189 );
190 return (
191 <button
192 key={thread.id}
193 type="button"
194 className={`flex w-full items-center gap-2 rounded-md px-2 py-1.5 text-left transition-colors duration-150 ${
195 isActive ? "bg-white/10 text-white" : "text-[#a0a0a0]/70 hover:bg-white/4"
196 }`}
197 onClick={() =>
198 dispatch({
199 type: "SET_ACTIVE_THREAD",
200 threadId: thread.id,
201 })
202 }
203 >
204 <div className="flex min-w-0 flex-1 items-center gap-1.5">
205 {threadStatus && (
206 <span className="inline-flex items-center gap-1 text-[10px] text-[#7ed8ff]/80">
207 <span className="h-1.5 w-1.5 animate-pulse rounded-full bg-sky-300/80" />
208 <span className="hidden xl:inline">
209 {threadStatus}
210 </span>
211 </span>
212 )}
213 <span className="min-w-0 flex-1 truncate text-xs">
214 {thread.title}
215 </span>
216 </div>
217 <span className="shrink-0 text-[10px] text-[#a0a0a0]/30">
218 {formatRelativeTime(thread.createdAt)}
219 </span>
220 </button>
221 );
222 })}
223
224 {/* New thread within project */}
225 <button
226 type="button"
227 className="flex w-full items-center gap-1 px-2 py-1 text-[10px] text-[#a0a0a0]/40 transition-colors duration-150 hover:text-[#a0a0a0]/60"
228 onClick={() => handleNewThread(project.id)}
229 >
230 <span>+</span> New thread
231 </button>
232 </div>
233 )}
234 </div>
235 );
236 })}
237
238 {state.projects.length === 0 && !addingProject && (
239 <div className="px-2 pt-4 text-center text-xs text-[#a0a0a0]/40">
240 No projects yet.
241 <br />
242 Add one to get started.
243 </div>
244 )}
245 </nav>
246
247 {/* Add project form */}
248 {addingProject ? (
249 <div className="border-t border-white/8 p-3">
250 <p className="mb-2 text-[10px] font-medium uppercase tracking-wider text-[#a0a0a0]/50">
251 Add project
252 </p>
253 <input
254 className="mb-2 w-full rounded-md border border-white/10 bg-white/4 px-2 py-1.5 font-mono text-xs text-[#e0e0e0] placeholder:text-[#a0a0a0]/30 focus:border-white/30 focus:outline-none"
255 placeholder="/path/to/project"
256 value={newCwd}
257 onChange={(e) => setNewCwd(e.target.value)}
258 onKeyDown={(e) => {
259 if (e.key === "Enter") handleAddProject();
260 if (e.key === "Escape") setAddingProject(false);
261 }}
262 />
263 {api && (
264 <button
265 type="button"
266 className="mb-2 flex w-full items-center justify-center rounded-md border border-white/10 px-2 py-1.5 text-xs text-[#a0a0a0]/70 transition-colors duration-150 hover:bg-white/4 disabled:cursor-not-allowed disabled:opacity-60"
267 onClick={() => void handlePickFolder()}
268 disabled={isPickingFolder}
269 >
270 {isPickingFolder ? "Picking folder..." : "Browse for folder"}
271 </button>
272 )}
273 <div className="flex gap-2">
274 <button
275 type="button"
276 className="flex-1 rounded-md bg-white px-2 py-1 text-xs font-medium text-[#0c0c0c] transition-colors duration-150 hover:bg-white/90"
277 onClick={handleAddProject}
278 >
279 Add
280 </button>
281 <button
282 type="button"
283 className="flex-1 rounded-md border border-white/10 px-2 py-1 text-xs text-[#a0a0a0]/60 transition-colors duration-150 hover:bg-white/4"
284 onClick={() => setAddingProject(false)}
285 >
286 Cancel
287 </button>
288 </div>
289 </div>
290 ) : (
291 <div className="border-t border-white/8 p-3">
292 <button
293 type="button"
294 className="flex w-full items-center justify-center gap-1 rounded-md border border-dashed border-white/12 py-2 text-xs text-[#a0a0a0]/50 transition-colors duration-150 hover:border-white/20 hover:text-[#a0a0a0]/70"
295 onClick={() => setAddingProject(true)}
296 >
297 + Add project
298 </button>
299 </div>
300 )}
301 </aside>
302 );
303}