-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathSessionItem.tsx
More file actions
369 lines (351 loc) · 14.4 KB
/
SessionItem.tsx
File metadata and controls
369 lines (351 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import { useState, useEffect, useCallback, useRef, type RefObject } from "react";
import type { SessionItem as SessionItemType } from "../utils/project-grouping.js";
interface SessionItemProps {
session: SessionItemType;
isActive: boolean;
isArchived?: boolean;
sessionName: string | undefined;
permCount: number;
isRecentlyRenamed: boolean;
onSelect: (id: string) => void;
onStartRename: (id: string, currentName: string) => void;
onArchive: (e: React.MouseEvent, id: string) => void;
onUnarchive: (e: React.MouseEvent, id: string) => void;
onDelete: (e: React.MouseEvent, id: string) => void;
onClearRecentlyRenamed: (id: string) => void;
editingSessionId: string | null;
editingName: string;
setEditingName: (name: string) => void;
onConfirmRename: () => void;
onCancelRename: () => void;
editInputRef: RefObject<HTMLInputElement | null>;
}
type DerivedStatus = "awaiting" | "running" | "reconnecting" | "idle" | "exited";
function deriveStatus(s: SessionItemType, permCount: number): DerivedStatus {
if (permCount > 0) return "awaiting";
if ((s.status === "running" || s.status === "compacting") && s.isConnected) return "running";
if (s.isReconnecting) return "reconnecting";
if (s.isConnected) return "idle";
return "exited";
}
function StatusDot({ status }: { status: DerivedStatus }) {
switch (status) {
case "running":
return (
<span className="relative shrink-0 w-2 h-2">
<span className="absolute inset-0 rounded-full bg-cc-success animate-[pulse-dot_1.5s_ease-in-out_infinite]" />
<span className="w-2 h-2 rounded-full bg-cc-success block" />
</span>
);
case "awaiting":
return (
<span className="relative shrink-0 w-2 h-2">
<span className="w-2 h-2 rounded-full bg-cc-warning block animate-[ring-pulse_1.5s_ease-out_infinite]" />
</span>
);
case "reconnecting":
return (
<span className="relative shrink-0 w-2 h-2">
<span className="w-2 h-2 rounded-full border border-cc-warning/40 border-t-cc-warning block animate-spin" />
</span>
);
case "idle":
return <span className="w-2 h-2 rounded-full bg-cc-muted/40 shrink-0" />;
case "exited":
return <span className="w-2 h-2 rounded-full border border-cc-muted/25 shrink-0" />;
}
}
function BackendBadge({ type }: { type: "claude" | "codex" }) {
if (type === "codex") {
return (
<span className="text-[9px] font-semibold px-1.5 py-0.5 rounded bg-sky-500/15 text-sky-600 dark:text-sky-300 leading-none">
CX
</span>
);
}
return (
<span className="text-[9px] font-semibold px-1.5 py-0.5 rounded bg-cc-success/15 text-cc-success leading-none">
CC
</span>
);
}
export function SessionItem({
session: s,
isActive,
isArchived: archived,
sessionName,
permCount,
isRecentlyRenamed,
onSelect,
onStartRename,
onArchive,
onUnarchive,
onDelete,
onClearRecentlyRenamed,
editingSessionId,
editingName,
setEditingName,
onConfirmRename,
onCancelRename,
editInputRef,
}: SessionItemProps) {
const shortId = s.id.slice(0, 8);
const label = sessionName || s.model || shortId;
const isEditing = editingSessionId === s.id;
const [menuOpen, setMenuOpen] = useState(false);
const menuRef = useRef<HTMLDivElement>(null);
const menuBtnRef = useRef<HTMLButtonElement>(null);
const derivedStatus = archived ? ("exited" as DerivedStatus) : deriveStatus(s, permCount);
// Show the full cwd path below the session name
const cwdTail = s.cwd || "";
// Close menu on click outside or Escape; arrow-key navigation between menu items
useEffect(() => {
if (!menuOpen) return;
function handleClickOutside(e: MouseEvent | TouchEvent) {
if (
menuRef.current && !menuRef.current.contains(e.target as Node) &&
menuBtnRef.current && !menuBtnRef.current.contains(e.target as Node)
) {
setMenuOpen(false);
menuBtnRef.current?.focus();
}
}
function handleKeyDown(e: KeyboardEvent) {
if (e.key === "Escape") {
setMenuOpen(false);
menuBtnRef.current?.focus();
return;
}
if (e.key === "Tab") {
setMenuOpen(false);
// Let native Tab move focus to the next element in sequence
return;
}
// Arrow key navigation within menu — only when focus is inside menu
if (
(e.key === "ArrowDown" || e.key === "ArrowUp") &&
(menuRef.current?.contains(document.activeElement) || menuBtnRef.current === document.activeElement)
) {
e.preventDefault();
const items = menuRef.current?.querySelectorAll<HTMLElement>("[role='menuitem']");
if (!items || items.length === 0) return;
const focused = document.activeElement as HTMLElement;
const idx = Array.from(items).indexOf(focused);
if (e.key === "ArrowDown") {
items[idx < items.length - 1 ? idx + 1 : 0].focus();
} else {
items[idx > 0 ? idx - 1 : items.length - 1].focus();
}
}
}
document.addEventListener("mousedown", handleClickOutside);
document.addEventListener("touchstart", handleClickOutside);
document.addEventListener("keydown", handleKeyDown);
return () => {
document.removeEventListener("mousedown", handleClickOutside);
document.removeEventListener("touchstart", handleClickOutside);
document.removeEventListener("keydown", handleKeyDown);
};
}, [menuOpen]);
// Focus the first menu item when the menu opens
useEffect(() => {
if (menuOpen) {
requestAnimationFrame(() => {
menuRef.current?.querySelector<HTMLElement>("[role='menuitem']")?.focus();
});
}
}, [menuOpen]);
const handleMenuAction = useCallback((action: () => void) => {
setMenuOpen(false);
action();
}, []);
return (
<div className="relative group">
<button
onClick={() => onSelect(s.id)}
onDoubleClick={(e) => {
e.preventDefault();
onStartRename(s.id, label);
}}
onKeyDown={(e) => {
if (e.key === "F2" && !isEditing) {
e.preventDefault();
onStartRename(s.id, label);
}
}}
className={`w-full flex items-center gap-2 py-1.5 pl-2.5 pr-12 min-h-[44px] rounded-lg transition-all duration-100 cursor-pointer relative border ${
isActive
? "bg-cc-active border-cc-primary/30"
: "border-transparent hover:bg-cc-hover hover:border-cc-border/60"
}`}
>
{/* Left accent edge for active state */}
<span
aria-hidden
className={`absolute left-0 top-1/2 -translate-y-1/2 w-[3px] rounded-full transition-all duration-150 ${
isActive ? "h-5 bg-cc-primary" : "h-0 bg-transparent"
}`}
/>
{/* Status dot */}
{!isEditing && (
<StatusDot status={derivedStatus} />
)}
{/* Session name / edit input */}
{isEditing ? (
<input
ref={editInputRef}
value={editingName}
onChange={(e) => setEditingName(e.target.value)}
onKeyDown={(e) => {
if (e.key === "Enter") {
e.preventDefault();
onConfirmRename();
} else if (e.key === "Escape") {
e.preventDefault();
onCancelRename();
}
e.stopPropagation();
}}
onBlur={onConfirmRename}
onClick={(e) => e.stopPropagation()}
onDoubleClick={(e) => e.stopPropagation()}
className="text-[12.5px] font-medium flex-1 min-w-0 text-cc-fg bg-transparent border border-cc-border rounded-md px-2 py-1 outline-none focus:border-cc-primary/50 focus:ring-1 focus:ring-cc-primary/20"
/>
) : (
<div className="flex-1 min-w-0">
<span
className={`text-[12.5px] font-medium truncate block leading-snug tracking-tight ${
isActive ? "text-cc-fg" : "text-cc-fg/90"
} ${isRecentlyRenamed ? "animate-name-appear" : ""}`}
onAnimationEnd={() => onClearRecentlyRenamed(s.id)}
>
{label}
</span>
{cwdTail && (
<span className="text-[10px] text-cc-muted/75 truncate block leading-tight mt-px">
{cwdTail}
</span>
)}
</div>
)}
{/* Badges: backend type + Docker + Cron */}
{!isEditing && (
<span className="flex items-center gap-1 shrink-0">
<BackendBadge type={s.backendType} />
{s.isContainerized && (
<span className="flex items-center px-1 py-0.5 rounded bg-blue-400/10" title="Docker">
<img src="/logo-docker.svg" alt="Docker logo" className="w-3 h-3" />
</span>
)}
{s.cronJobId && (
<span className="flex items-center px-1 py-0.5 rounded bg-cc-primary/10" title="Scheduled">
<svg viewBox="0 0 16 16" fill="currentColor" className="w-2.5 h-2.5 text-cc-primary">
<path d="M8 2a6 6 0 100 12A6 6 0 008 2zM0 8a8 8 0 1116 0A8 8 0 010 8zm9-3a1 1 0 10-2 0v3a1 1 0 00.293.707l2 2a1 1 0 001.414-1.414L9 7.586V5z" />
</svg>
</span>
)}
</span>
)}
</button>
{/* Archive button — hover reveal (desktop), always visible (mobile) */}
{!archived && !isEditing && !menuOpen && (
<button
onClick={(e) => {
e.stopPropagation();
onArchive(e, s.id);
}}
className="absolute right-7 top-1/2 -translate-y-1/2 p-1 rounded-md opacity-0 pointer-events-none sm:group-hover:opacity-100 sm:group-hover:pointer-events-auto hover:bg-cc-border text-cc-muted hover:text-cc-fg transition-all cursor-pointer"
title="Archive"
aria-label="Archive session"
>
<svg viewBox="0 0 16 16" fill="currentColor" className="w-3 h-3">
<path d="M2 4a1 1 0 011-1h10a1 1 0 011 1v1H2V4zm1 2h10v6a1 1 0 01-1 1H4a1 1 0 01-1-1V6zm3 2a.5.5 0 000 1h4a.5.5 0 000-1H6z" />
</svg>
</button>
)}
{/* Three-dot menu button */}
<button
ref={menuBtnRef}
onClick={(e) => {
e.stopPropagation();
setMenuOpen(!menuOpen);
}}
className="absolute right-1 top-1/2 -translate-y-1/2 p-1 rounded-md opacity-100 pointer-events-auto sm:opacity-0 sm:pointer-events-none sm:group-hover:opacity-100 sm:group-hover:pointer-events-auto hover:bg-cc-border text-cc-muted hover:text-cc-fg transition-all cursor-pointer"
title="Session actions"
aria-label="Session actions"
aria-haspopup="true"
aria-expanded={menuOpen}
>
<svg viewBox="0 0 16 16" fill="currentColor" className="w-3.5 h-3.5">
<circle cx="8" cy="3" r="1.5" />
<circle cx="8" cy="8" r="1.5" />
<circle cx="8" cy="13" r="1.5" />
</svg>
</button>
{/* Context menu */}
{menuOpen && (
<div
ref={menuRef}
role="menu"
aria-label="Session actions"
className="absolute right-0 top-full mt-1 w-40 py-1 bg-cc-card border border-cc-border/80 rounded-lg shadow-xl z-10 animate-[menu-appear_150ms_ease-out]"
>
{!archived && (
<button
role="menuitem"
tabIndex={-1}
onClick={() => handleMenuAction(() => onStartRename(s.id, label))}
className="w-full px-3 py-1.5 text-[12px] text-left text-cc-fg hover:bg-cc-hover transition-colors cursor-pointer flex items-center gap-2"
>
<svg viewBox="0 0 16 16" fill="currentColor" className="w-3 h-3 text-cc-muted">
<path d="M12.146.854a.5.5 0 00-.707 0L3.714 8.579a.5.5 0 00-.138.242l-.777 3.11a.5.5 0 00.607.607l3.11-.777a.5.5 0 00.242-.138L14.573 3.854a.5.5 0 000-.708L12.146.854z" />
</svg>
Rename
</button>
)}
{archived ? (
<>
<button
role="menuitem"
tabIndex={-1}
onClick={(e) => handleMenuAction(() => onUnarchive(e, s.id))}
className="w-full px-3 py-1.5 text-[12px] text-left text-cc-fg hover:bg-cc-hover transition-colors cursor-pointer flex items-center gap-2"
>
<svg viewBox="0 0 16 16" fill="currentColor" className="w-3 h-3 text-cc-muted">
<path d="M8 4a.5.5 0 01.5.5v3.793l1.854-1.853a.5.5 0 01.707.707l-2.828 2.828a.5.5 0 01-.707 0L4.697 7.147a.5.5 0 01.707-.707L7.5 8.293V4.5A.5.5 0 018 4z" />
<path d="M2 12.5A1.5 1.5 0 003.5 14h9a1.5 1.5 0 001.5-1.5v-2a.5.5 0 00-1 0v2a.5.5 0 01-.5.5h-9a.5.5 0 01-.5-.5v-2a.5.5 0 00-1 0v2z" />
</svg>
Restore
</button>
<div className="my-1 mx-2 border-t border-cc-border/50" />
<button
role="menuitem"
tabIndex={-1}
onClick={(e) => handleMenuAction(() => onDelete(e, s.id))}
className="w-full px-3 py-1.5 text-[12px] text-left text-cc-error hover:bg-cc-error/5 transition-colors cursor-pointer flex items-center gap-2"
>
<svg viewBox="0 0 16 16" fill="currentColor" className="w-3 h-3">
<path d="M5.5 5.5A.5.5 0 016 6v6a.5.5 0 01-1 0V6a.5.5 0 01.5-.5zm2.5 0a.5.5 0 01.5.5v6a.5.5 0 01-1 0V6a.5.5 0 01.5-.5zm3 .5a.5.5 0 00-1 0v6a.5.5 0 001 0V6z" />
<path fillRule="evenodd" d="M14.5 3a1 1 0 01-1 1H13v9a2 2 0 01-2 2H5a2 2 0 01-2-2V4h-.5a1 1 0 010-2H6a1 1 0 011-1h2a1 1 0 011 1h3.5a1 1 0 011 1zM4.118 4L4 4.059V13a1 1 0 001 1h6a1 1 0 001-1V4.059L11.882 4H4.118zM6 2h4v1H6V2z" clipRule="evenodd" />
</svg>
Delete
</button>
</>
) : (
<button
role="menuitem"
tabIndex={-1}
onClick={(e) => handleMenuAction(() => onArchive(e, s.id))}
className="w-full px-3 py-1.5 text-[12px] text-left text-cc-fg hover:bg-cc-hover transition-colors cursor-pointer flex items-center gap-2"
>
<svg viewBox="0 0 16 16" fill="currentColor" className="w-3 h-3 text-cc-muted">
<path d="M2 4a1 1 0 011-1h10a1 1 0 011 1v1H2V4zm1 2h10v6a1 1 0 01-1 1H4a1 1 0 01-1-1V6zm3 2a.5.5 0 000 1h4a.5.5 0 000-1H6z" />
</svg>
Archive
</button>
)}
</div>
)}
</div>
);
}