-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclosed_sidebar_view.dart
More file actions
539 lines (502 loc) · 17 KB
/
Copy pathclosed_sidebar_view.dart
File metadata and controls
539 lines (502 loc) · 17 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
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_riverpod/legacy.dart';
import 'package:phosphoricons_flutter/phosphoricons_flutter.dart';
import '../../store/models.dart';
import '../../store/store.dart';
import '../../status/status_event.dart';
import '../../status/status_providers.dart';
/// Whether the sidebar shows the Active tree or the Closed list (SPEC-29).
final sidebarClosedProvider = StateProvider<bool>((_) => false);
/// How the closed list is grouped.
enum ClosedGroupBy { repo, branch, age, harness }
extension on ClosedGroupBy {
String get label => switch (this) {
ClosedGroupBy.repo => 'Repo',
ClosedGroupBy.branch => 'Branch',
ClosedGroupBy.age => 'Age',
ClosedGroupBy.harness => 'Harness',
};
}
/// The Closed view that replaces the repo tree when the sidebar is in
/// closed mode. Loads closed sessions on demand and groups them by the
/// chosen dimension; each row restores back to the active list.
class ClosedSidebarView extends ConsumerStatefulWidget {
const ClosedSidebarView({super.key});
@override
ConsumerState<ClosedSidebarView> createState() => _ClosedSidebarViewState();
}
class _ClosedSidebarViewState extends ConsumerState<ClosedSidebarView> {
ClosedGroupBy _by = ClosedGroupBy.repo;
late Future<List<Session>> _future = _load();
Future<List<Session>> _load() =>
ref.read(storeControllerProvider.notifier).listClosedSessions();
// Guard for the live-sync listener below: the set of active session ids the
// last snapshot carried, so routine activity/status churn doesn't trigger a
// reload — only an actual close/reopen (which adds/removes an id) does.
//
// Seeded in [initState] rather than left null, for the same reason the mobile
// ClosedScreen seeds it: the listener only fires on a *change*, so an unseeded
// baseline swallows the FIRST one — close a session from a chat pane with this
// view open and the list stayed stale until a manual toggle. Seeding it *empty*
// is not enough either, or the next snapshot always looks like a change and
// reloads for nothing, so it starts from what the store already holds.
late Set<String> _lastActiveIds;
@override
void initState() {
super.initState();
_lastActiveIds = {
for (final s in ref.read(sessionsProvider).sessions) s.id,
};
}
void _refresh() {
if (!mounted) return;
setState(() {
_future = _load();
});
}
Future<void> _reopen(String id) async {
final status = ref.status;
try {
await ref.read(storeControllerProvider.notifier).reopenSession(id);
if (!mounted) return;
// Refresh immediately for snappy local feedback. The sessionsProvider
// listener in build() also fires once the server re-broadcasts the active
// set (the cross-pane path), so a local restore reloads twice — harmless
// and intentional: the listener alone isn't guaranteed same-frame.
_refresh();
} catch (e) {
status.failure(
'Could not reopen the session',
error: e,
source: StatusSources.session,
sessionId: id,
);
}
}
@override
Widget build(BuildContext context) {
// Live-sync: closing/reopening a session elsewhere (e.g. from a chat
// pane) changes the ACTIVE session set the server broadcasts. Reload the
// closed list on that transition so it stays fresh without the user
// toggling the view. Keyed on the id SET (order-independent, no sort/join)
// so this stays allocation-light under frequent broadcasts from many live
// sessions and only fires on a real add/remove, not activity/status churn.
ref.listen<SessionsState>(sessionsProvider, (_, next) {
final ids = {for (final s in next.sessions) s.id};
final prev = _lastActiveIds;
if (ids.length != prev.length || !ids.containsAll(prev)) {
_refresh();
}
_lastActiveIds = ids;
});
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
_controls(context),
const Divider(height: 1),
Expanded(
child: FutureBuilder<List<Session>>(
future: _future,
builder: (context, snap) {
if (snap.connectionState == ConnectionState.waiting &&
!snap.hasData) {
return const Center(
child: Padding(
padding: EdgeInsets.all(24),
child: SizedBox(
width: 18,
height: 18,
child: CircularProgressIndicator(strokeWidth: 2),
),
),
);
}
if (snap.hasError) return _error(context);
final sessions = snap.data ?? const <Session>[];
if (sessions.isEmpty) return _empty(context);
return _grouped(context, sessions);
},
),
),
],
);
}
Widget _controls(BuildContext context) {
final cs = Theme.of(context).colorScheme;
return Padding(
padding: const EdgeInsets.fromLTRB(12, 8, 8, 8),
child: Row(
children: [
Icon(PhosphorIconsLight.moon, size: 15, color: cs.outline),
const SizedBox(width: 8),
Expanded(
child: Text(
'CLOSED',
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: Theme.of(context).textTheme.labelSmall?.copyWith(
color: cs.outline,
fontWeight: FontWeight.w700,
letterSpacing: 0.9,
),
),
),
const SizedBox(width: 8),
_GroupByMenu(value: _by, onChanged: (v) => setState(() => _by = v)),
],
),
);
}
Widget _empty(BuildContext context) {
final cs = Theme.of(context).colorScheme;
return Center(
child: Padding(
padding: const EdgeInsets.all(28),
child: Text(
'No closed sessions.',
style: TextStyle(color: cs.outline, fontSize: 12.5),
),
),
);
}
Widget _error(BuildContext context) {
final cs = Theme.of(context).colorScheme;
return Center(
child: Padding(
padding: const EdgeInsets.all(24),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Icon(PhosphorIconsLight.warningCircle, size: 20, color: cs.error),
const SizedBox(height: 8),
Text(
"Couldn't load closed sessions.",
textAlign: TextAlign.center,
style: TextStyle(color: cs.onSurfaceVariant, fontSize: 12.5),
),
const SizedBox(height: 10),
TextButton(onPressed: _refresh, child: const Text('Retry')),
],
),
),
);
}
Widget _grouped(BuildContext context, List<Session> sessions) {
final groups = _group(sessions, _by, ref);
return ListView.builder(
padding: const EdgeInsets.only(bottom: 12, top: 4),
itemCount: groups.length,
itemBuilder: (context, i) {
final g = groups[i];
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
_GroupHeader(
title: g.title,
subtitle: g.subtitle,
count: g.items.length,
),
for (final s in g.items)
_ClosedRow(
session: s,
groupBy: _by,
onReopen: () => _reopen(s.id),
),
],
);
},
);
}
}
// ---------- grouping ----------
class _Group {
_Group(this.title, this.subtitle);
final String title;
final String? subtitle;
final List<Session> items = [];
}
List<_Group> _group(List<Session> sessions, ClosedGroupBy by, WidgetRef ref) {
final repos = ref.read(reposProvider);
String repoName(String pid) => repos.byId(pid)?.name ?? pid;
({String key, String title, String? sub}) keyOf(Session s) => switch (by) {
ClosedGroupBy.repo => (
key: s.projectId,
title: repoName(s.projectId),
sub: null,
),
ClosedGroupBy.branch => (
key: '${s.projectId}/${s.branch ?? '∅'}',
title: s.branch ?? 'detached',
sub: repoName(s.projectId),
),
ClosedGroupBy.age => (
key: _ageBucket(s).$1,
title: _ageBucket(s).$2,
sub: null,
),
ClosedGroupBy.harness => (
key: s.agent,
title: s.agent == 'pi' ? 'Pi' : (s.agent == 'codex' ? 'Codex' : s.agent),
sub: null,
),
};
final map = <String, _Group>{};
final order = <String>[];
for (final s in sessions) {
final k = keyOf(s);
final g = map.putIfAbsent(k.key, () {
order.add(k.key);
return _Group(k.title, k.sub);
});
g.items.add(s);
}
// Age groups get a fixed chronological order; others keep first-seen (the
// server already sorts newest-first), which reads well for repo/branch/harness.
if (by == ClosedGroupBy.age) {
const rank = {'today': 0, 'week': 1, 'month': 2, 'older': 3};
order.sort((a, b) => (rank[a] ?? 9).compareTo(rank[b] ?? 9));
}
for (final g in map.values) {
g.items.sort((a, b) => b.lastActivityAt.compareTo(a.lastActivityAt));
}
return [for (final k in order) map[k]!];
}
/// (bucketKey, label) for a session's last-activity age.
(String, String) _ageBucket(Session s) {
final now = DateTime.now();
final at = DateTime.fromMillisecondsSinceEpoch(s.lastActivityAt);
final days = now.difference(at).inDays;
if (at.year == now.year && at.month == now.month && at.day == now.day) {
return ('today', 'Today');
}
if (days < 7) return ('week', 'This week');
if (days < 30) return ('month', 'Last 30 days');
return ('older', 'Older');
}
String _relativeAge(int ms) {
if (ms == 0) return '';
final d = DateTime.now().difference(DateTime.fromMillisecondsSinceEpoch(ms));
if (d.inDays >= 30) return '${(d.inDays / 30).floor()}mo';
if (d.inDays >= 7) return '${(d.inDays / 7).floor()}w';
if (d.inDays >= 1) return '${d.inDays}d';
if (d.inHours >= 1) return '${d.inHours}h';
return 'now';
}
// ---------- rows ----------
class _GroupHeader extends StatelessWidget {
const _GroupHeader({required this.title, this.subtitle, required this.count});
final String title;
final String? subtitle;
final int count;
@override
Widget build(BuildContext context) {
final cs = Theme.of(context).colorScheme;
final theme = Theme.of(context);
return Padding(
padding: const EdgeInsets.fromLTRB(14, 12, 12, 4),
child: Row(
children: [
Flexible(
child: Text(
title,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: theme.textTheme.labelMedium?.copyWith(
fontWeight: FontWeight.w700,
color: cs.onSurface,
),
),
),
if (subtitle != null) ...[
const SizedBox(width: 6),
Flexible(
child: Text(
subtitle!,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: theme.textTheme.labelSmall?.copyWith(color: cs.outline),
),
),
],
const SizedBox(width: 8),
Container(
padding: const EdgeInsets.symmetric(horizontal: 7, vertical: 1),
decoration: BoxDecoration(
color: cs.surfaceContainerHighest,
borderRadius: BorderRadius.circular(999),
),
child: Text(
'$count',
style: theme.textTheme.labelSmall?.copyWith(color: cs.outline),
),
),
],
),
);
}
}
class _ClosedRow extends StatefulWidget {
const _ClosedRow({
required this.session,
required this.groupBy,
required this.onReopen,
});
final Session session;
final ClosedGroupBy groupBy;
final VoidCallback onReopen;
@override
State<_ClosedRow> createState() => _ClosedRowState();
}
class _ClosedRowState extends State<_ClosedRow> {
bool _hover = false;
@override
Widget build(BuildContext context) {
final s = widget.session;
final cs = Theme.of(context).colorScheme;
final theme = Theme.of(context);
final harnessColor = s.agent == 'codex'
? const Color(0xFF7AA2F7)
: cs.primary;
final showBranch =
widget.groupBy != ClosedGroupBy.branch && s.branch != null;
final showHarness = widget.groupBy != ClosedGroupBy.harness;
return MouseRegion(
onEnter: (_) => setState(() => _hover = true),
onExit: (_) => setState(() => _hover = false),
child: Container(
margin: const EdgeInsets.fromLTRB(8, 1, 8, 1),
padding: const EdgeInsets.fromLTRB(10, 7, 6, 7),
decoration: BoxDecoration(
color: _hover ? cs.surfaceContainerHigh : Colors.transparent,
borderRadius: BorderRadius.circular(9),
),
child: Row(
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
s.title,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: theme.textTheme.bodyMedium?.copyWith(fontSize: 12.5),
),
const SizedBox(height: 3),
Wrap(
spacing: 5,
runSpacing: 3,
crossAxisAlignment: WrapCrossAlignment.center,
children: [
if (showHarness)
_Chip(
s.agent == 'pi'
? 'Pi'
: (s.agent == 'codex' ? 'Codex' : s.agent),
color: harnessColor,
),
if (showBranch) _Chip(s.branch!, mono: true),
if (s.orphaned)
_Chip('worktree removed', color: cs.error),
],
),
],
),
),
const SizedBox(width: 6),
Text(
_relativeAge(s.lastActivityAt),
style: theme.textTheme.labelSmall?.copyWith(color: cs.outline),
),
// Always present (not hover-gated) so keyboard-only users can focus
// and restore. Dimmed until the row is hovered/focused.
IconButton(
tooltip: 'Reopen',
iconSize: 15,
visualDensity: VisualDensity.compact,
constraints: const BoxConstraints(minWidth: 26, minHeight: 26),
icon: const Icon(PhosphorIconsLight.arrowCounterClockwise),
color: _hover ? cs.primary : cs.outline,
onPressed: widget.onReopen,
),
],
),
),
);
}
}
class _Chip extends StatelessWidget {
const _Chip(this.text, {this.color, this.mono = false});
final String text;
final Color? color;
final bool mono;
@override
Widget build(BuildContext context) {
final cs = Theme.of(context).colorScheme;
final c = color ?? cs.outline;
return Container(
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 1),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(6),
border: Border.all(color: c.withValues(alpha: 0.4)),
),
child: Text(
text,
style: TextStyle(
fontSize: 10,
height: 1.3,
color: c,
fontFamily: mono ? 'SF Mono' : null,
fontFamilyFallback: mono ? const ['monospace'] : null,
),
),
);
}
}
class _GroupByMenu extends StatelessWidget {
const _GroupByMenu({required this.value, required this.onChanged});
final ClosedGroupBy value;
final ValueChanged<ClosedGroupBy> onChanged;
@override
Widget build(BuildContext context) {
final cs = Theme.of(context).colorScheme;
return PopupMenuButton<ClosedGroupBy>(
tooltip: 'Group by',
initialValue: value,
onSelected: onChanged,
position: PopupMenuPosition.under,
itemBuilder: (context) => [
for (final v in ClosedGroupBy.values)
PopupMenuItem(
value: v,
height: 36,
child: Text(v.label, style: Theme.of(context).textTheme.bodyMedium),
),
],
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
decoration: BoxDecoration(
color: cs.surfaceContainerLow,
borderRadius: BorderRadius.circular(7),
border: Border.all(color: cs.outlineVariant),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Text(
'Group: ${value.label}',
style: TextStyle(fontSize: 11.5, color: cs.onSurfaceVariant),
),
const SizedBox(width: 3),
Icon(
PhosphorIconsLight.caretDown,
size: 12,
color: cs.onSurfaceVariant,
),
],
),
),
);
}
}