-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathActivityIndicator.tsx
More file actions
556 lines (509 loc) · 17.7 KB
/
ActivityIndicator.tsx
File metadata and controls
556 lines (509 loc) · 17.7 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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
import { Show, For, createSignal, createEffect, onCleanup } from "solid-js";
import {
useActivityIndicator,
type ActivityTask,
type TaskHistoryEntry,
type TaskSource,
} from "@/context/ActivityIndicatorContext";
import { Icon } from "./ui/Icon";
import { LoadingSpinner, ProgressBar } from "@/components/ui";
// ============================================================================
// Source Icons
// ============================================================================
function getSourceIcon(source: TaskSource) {
switch (source) {
case "lsp":
return <Icon name="code" size={12} />;
case "git":
return <Icon name="code-branch" size={12} />;
case "build":
return <Icon name="terminal" size={12} />;
case "format":
return <Icon name="code" size={12} />;
case "remote":
return <Icon name="server" size={12} />;
case "extension":
return <Icon name="box" size={12} />;
case "auto-update":
return <Icon name="download" size={12} />;
case "repl":
return <Icon name="play" size={12} />;
case "debug":
return <Icon name="bolt" size={12} />;
case "mcp":
return <Icon name="server" size={12} />;
case "system":
return <Icon name="gear" size={12} />;
default:
return <Icon name="spinner" size={12} />;
}
}
// ============================================================================
// Duration Formatter
// ============================================================================
function formatDuration(ms: number | undefined): string {
if (ms === undefined || ms === null || isNaN(ms)) return "-";
if (ms < 1000) return `${ms}ms`;
const seconds = Math.floor(ms / 1000);
if (seconds < 60) return `${seconds}s`;
const minutes = Math.floor(seconds / 60);
const remainingSeconds = seconds % 60;
return `${minutes}m ${remainingSeconds}s`;
}
function formatRelativeTime(timestamp: number): string {
const diff = Date.now() - timestamp;
if (diff < 60000) return "just now";
if (diff < 3600000) return `${Math.floor(diff / 60000)}m ago`;
if (diff < 86400000) return `${Math.floor(diff / 3600000)}h ago`;
return `${Math.floor(diff / 86400000)}d ago`;
}
// ============================================================================
// Task Item Component
// ============================================================================
interface TaskItemProps {
task: ActivityTask;
onCancel?: (taskId: string) => void;
compact?: boolean;
}
function TaskItem(props: TaskItemProps) {
return (
<div
class="flex items-center gap-2 px-2 py-1.5 rounded hover:bg-white/5 transition-colors group"
style={{ "min-height": "32px" }}
>
{/* Source icon with spinner overlay for running tasks */}
<div class="relative flex-shrink-0">
<span style={{ color: "var(--text-weak)" }}>
{getSourceIcon(props.task.source)}
</span>
<Show when={props.task.status === "running"}>
<div class="absolute inset-0 flex items-center justify-center">
<LoadingSpinner
size={14}
style={{ color: "var(--accent)" }}
/>
</div>
</Show>
</div>
{/* Task info */}
<div class="flex-1 min-w-0">
<div class="flex items-center gap-2">
<span
class="text-xs font-medium truncate"
style={{ color: "var(--text-primary)" }}
>
{props.task.title}
</span>
<Show when={props.task.progress !== undefined}>
<span
class="text-[10px] tabular-nums"
style={{ color: "var(--text-weak)" }}
>
{Math.round(props.task.progress!)}%
</span>
</Show>
</div>
<Show when={props.task.message && !props.compact}>
<p
class="text-[10px] truncate"
style={{ color: "var(--text-weaker)" }}
>
{props.task.message}
</p>
</Show>
{/* Progress bar */}
<Show when={props.task.progress !== undefined}>
<div
class="h-1 rounded-full mt-1 overflow-hidden"
style={{ background: "var(--surface-raised)" }}
>
<div
class="h-full rounded-full transition-all duration-300"
style={{
background: "var(--accent)",
width: `${props.task.progress}%`,
}}
/>
</div>
</Show>
</div>
{/* Cancel button */}
<Show when={props.task.cancellable && props.onCancel}>
<button
class="p-1 rounded opacity-0 group-hover:opacity-100 transition-opacity hover:bg-white/10"
style={{ color: "var(--text-weak)" }}
onClick={(e) => {
e.stopPropagation();
props.onCancel?.(props.task.id);
}}
title="Cancel task"
>
<Icon name="xmark" size={14} />
</button>
</Show>
</div>
);
}
// ============================================================================
// History Item Component
// ============================================================================
interface HistoryItemProps {
entry: TaskHistoryEntry;
}
function HistoryItem(props: HistoryItemProps) {
const statusIcon = () => {
switch (props.entry.status) {
case "completed":
return <Icon name="check" size={12} style={{ color: "var(--success)" }} />;
case "failed":
return <Icon name="circle-exclamation" size={12} style={{ color: "var(--error)" }} />;
case "cancelled":
return <Icon name="xmark" size={12} style={{ color: "var(--warning)" }} />;
}
};
return (
<div
class="flex items-center gap-2 px-2 py-1.5 rounded hover:bg-white/5 transition-colors"
title={props.entry.error || undefined}
>
{/* Status icon */}
<div class="flex-shrink-0">{statusIcon()}</div>
{/* Source icon */}
<span style={{ color: "var(--text-weaker)" }}>
{getSourceIcon(props.entry.source)}
</span>
{/* Info */}
<div class="flex-1 min-w-0">
<span
class="text-xs truncate block"
style={{ color: "var(--text-weak)" }}
>
{props.entry.title}
</span>
</div>
{/* Duration */}
<span
class="text-[10px] tabular-nums flex-shrink-0"
style={{ color: "var(--text-weaker)" }}
>
{formatDuration(props.entry.duration)}
</span>
{/* Time ago */}
<span
class="text-[10px] flex-shrink-0"
style={{ color: "var(--text-weaker)" }}
>
{formatRelativeTime(props.entry.completedAt)}
</span>
</div>
);
}
// ============================================================================
// Activity Indicator (Status Bar)
// ============================================================================
export function ActivityIndicator() {
const activity = useActivityIndicator();
const [showPopup, setShowPopup] = createSignal(false);
const [activeTab, setActiveTab] = createSignal<"active" | "history">("active");
let popupRef: HTMLDivElement | undefined;
let triggerRef: HTMLButtonElement | undefined;
// Close popup when clicking outside
const handleClickOutside = (e: MouseEvent) => {
if (
popupRef &&
triggerRef &&
!popupRef.contains(e.target as Node) &&
!triggerRef.contains(e.target as Node)
) {
setShowPopup(false);
}
};
createEffect(() => {
if (showPopup()) {
document.addEventListener("mousedown", handleClickOutside);
} else {
document.removeEventListener("mousedown", handleClickOutside);
}
});
onCleanup(() => {
document.removeEventListener("mousedown", handleClickOutside);
});
// Auto-switch to active tab when tasks start
createEffect(() => {
if (activity.hasActiveTasks() && activeTab() === "history") {
setActiveTab("active");
}
});
const primaryTask = () => activity.primaryTask();
const activeTasks = () => activity.activeTasks();
const history = () => activity.state.history;
// Don't render if no active tasks and no history
const shouldRender = () => activity.hasActiveTasks() || history().length > 0;
return (
<Show when={shouldRender()}>
<div class="relative">
{/* Trigger Button */}
<button
ref={triggerRef}
class="flex items-center gap-1.5 px-2 py-0.5 rounded hover:bg-white/5 transition-colors"
style={{
color: activity.hasActiveTasks()
? "var(--accent)"
: "var(--text-weak)",
}}
onClick={() => setShowPopup(!showPopup())}
title={
activity.hasActiveTasks()
? `${activity.activeTaskCount()} active task${activity.activeTaskCount() > 1 ? "s" : ""}`
: "View task history"
}
>
{/* Spinner or history icon - uses VS Code codicon-spin (1.5s, steps(30)) */}
<Show
when={activity.hasActiveTasks()}
fallback={<Icon name="clock" size={14} />}
>
<LoadingSpinner size={14} />
</Show>
{/* Primary task label */}
<Show when={primaryTask()}>
<span class="text-xs max-w-32 truncate">
{primaryTask()!.title}
<Show when={primaryTask()!.progress !== undefined}>
<span class="ml-1 tabular-nums">
({Math.round(primaryTask()!.progress!)}%)
</span>
</Show>
</span>
</Show>
{/* Additional task count */}
<Show when={activity.activeTaskCount() > 1}>
<span
class="text-[10px] px-1 rounded"
style={{
background: "var(--surface-raised)",
color: "var(--text-weak)",
}}
>
+{activity.activeTaskCount() - 1}
</span>
</Show>
{/* Expand indicator */}
<Show when={showPopup()} fallback={<Icon name="chevron-up" size={12} />}>
<Icon name="chevron-down" size={12} />
</Show>
</button>
{/* Popup Panel */}
<Show when={showPopup()}>
<div
ref={popupRef}
class="absolute bottom-full right-0 mb-1 w-80 rounded-lg shadow-xl overflow-hidden z-50"
style={{
background: "var(--surface-base)",
border: "1px solid var(--border-weak)",
}}
>
{/* Header with tabs */}
<div
class="flex items-center gap-1 px-2 py-1.5"
style={{
background: "var(--surface-raised)",
"border-bottom": "1px solid var(--border-weak)",
}}
>
<button
class="px-2 py-1 rounded text-xs font-medium transition-colors"
style={{
background:
activeTab() === "active"
? "var(--accent)"
: "transparent",
color:
activeTab() === "active" ? "white" : "var(--text-weak)",
}}
onClick={() => setActiveTab("active")}
>
Active
<Show when={activity.activeTaskCount() > 0}>
<span class="ml-1 px-1 rounded-full text-[10px]" style={{
background: activeTab() === "active" ? "white/20" : "var(--surface-base)",
}}>
{activity.activeTaskCount()}
</span>
</Show>
</button>
<button
class="px-2 py-1 rounded text-xs font-medium transition-colors"
style={{
background:
activeTab() === "history"
? "var(--accent)"
: "transparent",
color:
activeTab() === "history" ? "white" : "var(--text-weak)",
}}
onClick={() => setActiveTab("history")}
>
History
<Show when={history().length > 0}>
<span class="ml-1 px-1 rounded-full text-[10px]" style={{
background: activeTab() === "history" ? "white/20" : "var(--surface-base)",
}}>
{history().length}
</span>
</Show>
</button>
<div class="flex-1" />
{/* Clear history button */}
<Show when={activeTab() === "history" && history().length > 0}>
<button
class="p-1 rounded hover:bg-white/10 transition-colors"
style={{ color: "var(--text-weak)" }}
onClick={() => activity.clearHistory()}
title="Clear history"
>
<Icon name="trash" size={14} />
</button>
</Show>
</div>
{/* Content */}
<div class="max-h-64 overflow-y-auto">
<Show when={activeTab() === "active"}>
<Show
when={activeTasks().length > 0}
fallback={
<div
class="px-3 py-6 text-center text-xs"
style={{ color: "var(--text-weaker)" }}
>
No active tasks
</div>
}
>
<div class="p-1">
<For each={activeTasks()}>
{(task) => (
<TaskItem
task={task}
onCancel={(id) => activity.cancelTask(id)}
/>
)}
</For>
</div>
</Show>
</Show>
<Show when={activeTab() === "history"}>
<Show
when={history().length > 0}
fallback={
<div
class="px-3 py-6 text-center text-xs"
style={{ color: "var(--text-weaker)" }}
>
No task history
</div>
}
>
<div class="p-1">
<For each={history()}>
{(entry) => <HistoryItem entry={entry} />}
</For>
</div>
</Show>
</Show>
</div>
{/* Footer with summary */}
<Show when={activeTasks().length > 0}>
<div
class="px-3 py-2 flex items-center justify-between"
style={{
background: "var(--surface-raised)",
"border-top": "1px solid var(--border-weak)",
}}
>
<span
class="text-[10px]"
style={{ color: "var(--text-weaker)" }}
>
{activeTasks().filter((t) => t.cancellable).length > 0 && (
<button
class="hover:underline"
style={{ color: "var(--text-weak)" }}
onClick={() => activity.cancelAllCancellable()}
>
Cancel all
</button>
)}
</span>
</div>
</Show>
</div>
</Show>
</div>
</Show>
);
}
// ============================================================================
// Minimal Activity Indicator (for tight spaces)
// ============================================================================
export function ActivityIndicatorMinimal() {
const activity = useActivityIndicator();
return (
<Show when={activity.hasActiveTasks()}>
<div
class="flex items-center gap-1"
title={activity.primaryTask()?.title}
>
{/* VS Code codicon-spin: 1.5s cycle with steps(30) for CPU efficiency */}
<LoadingSpinner
size={12}
style={{ color: "var(--accent)" }}
/>
<Show when={activity.activeTaskCount() > 1}>
<span
class="text-[10px] tabular-nums"
style={{ color: "var(--text-weak)" }}
>
{activity.activeTaskCount()}
</span>
</Show>
</div>
</Show>
);
}
// ============================================================================
// Activity Progress Bar (for determinate progress display)
// Uses VS Code spec: 2px height, 4s infinite animation, GPU-optimized transforms
// Long-running mode activates after 10s (throttle to steps(100))
// ============================================================================
interface ActivityProgressBarProps {
source?: TaskSource;
}
export function ActivityProgressBar(props: ActivityProgressBarProps) {
const activity = useActivityIndicator();
const progress = () => {
if (props.source) {
return activity.getSourceProgress(props.source);
}
const primary = activity.primaryTask();
return primary?.progress;
};
const isActive = () => {
if (props.source) {
return activity.isSourceBusy(props.source);
}
return activity.hasActiveTasks();
};
// Use VS Code-spec ProgressBar component with:
// - 2px height horizontal bar
// - 4-second infinite animation with GPU transforms
// - Long-running mode after 10s (steps(100) for CPU efficiency)
// - Discrete mode for determinate progress (100ms transitions)
return (
<ProgressBar
mode={progress() !== undefined ? "discrete" : "infinite"}
value={progress() ?? 0}
visible={isActive()}
/>
);
}