Files
pipelines/app/lib/features/chat/data/chat_recency.dart
T
alan 12c7642e35 Chat quick switcher MRU, Closed section and markers; Alt+N, Alt+J and run tab keys
Ctrl+K lists open chats most recently used first (tracked on open and pane
focus, persisted per server) and preselects the previous chat, so Ctrl+K,
Enter returns to it. Rows mark needs you / working / unread; a query also
lists matching closed chats under Closed, and choosing one reopens it.
Alt+N (Option+N) starts a new chat and Alt+J (Option+J) opens the next chat
waiting for you, unread or failed, cycling; the terminal keeps both. In run
detail, bare 1-4 switch tabs when not typing. Cheatsheet entries added.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 00:49:36 +00:00

102 lines
2.8 KiB
Dart

import 'dart:async';
import 'package:shared_preferences/shared_preferences.dart';
import '../../../core/servers/server_scope.dart';
/// Where the chats' most-recently-used order is kept on this device.
abstract interface class ChatRecencyStore {
Future<List<String>> read();
Future<void> write(List<String> ids);
}
/// The chats this device used, most recent first, for the quick switcher's
/// order with no query. A chat counts as used when it is opened or its pane
/// takes focus ([touch]). The list is read once, lazily, and written back on
/// every change; a chat touched before the read finished stays in front.
class ChatRecency {
ChatRecency({ChatRecencyStore? store, this.limit = 200})
: _store = store ?? MemoryChatRecencyStore();
final ChatRecencyStore _store;
/// How many chats are remembered.
final int limit;
List<String> _ids = const [];
Future<void>? _loading;
/// The remembered chats, most recent first.
List<String> get ids => _ids;
/// Reads the stored order once; later calls return the same read.
Future<void> load() => _loading ??= _read();
Future<void> _read() async {
List<String> stored;
try {
stored = await _store.read();
} catch (_) {
stored = const [];
}
_ids = _merged(_ids, stored);
}
/// [id] was opened or focused: it moves to the front.
void touch(String id) {
if (id.isEmpty || (_ids.isNotEmpty && _ids.first == id)) return;
_ids = _merged([id], _ids);
unawaited(load().then((_) => _write()));
}
Future<void> _write() async {
try {
await _store.write(_ids);
} catch (_) {
// The order is a convenience; the switcher still lists every chat.
}
}
List<String> _merged(List<String> front, List<String> rest) {
final seen = <String>{};
return [
for (final id in [...front, ...rest])
if (id.isNotEmpty && seen.add(id)) id,
].take(limit).toList(growable: false);
}
}
class SharedPreferencesChatRecencyStore implements ChatRecencyStore {
const SharedPreferencesChatRecencyStore({this.serverScope = ''});
final String serverScope;
String get _key => scopedServerKey('pipelines.chat.recency.v1', serverScope);
@override
Future<List<String>> read() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getStringList(_key) ?? const [];
}
@override
Future<void> write(List<String> ids) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setStringList(_key, ids);
}
}
/// An in-memory store for tests and hosts without preferences.
class MemoryChatRecencyStore implements ChatRecencyStore {
MemoryChatRecencyStore([List<String>? ids]) : ids = ids ?? [];
List<String> ids;
@override
Future<List<String>> read() async => List.of(ids);
@override
Future<void> write(List<String> ids) async => this.ids = List.of(ids);
}