import 'dart:async'; import 'package:flutter/material.dart'; import '../features/chat/chat_runtime.dart'; import '../features/chat/domain/chat_sessions.dart'; import '../features/chat/ui/sessions/chat_create_fields.dart'; import '../ui/adaptive/app_popups.dart'; import 'open_in_chat.dart'; /// Shows the common destination picker used by runs, pipelines and artifacts. /// The draft is always placed in the composer; no option sends it. Future showSendToChatPicker({ required BuildContext context, required ChatRuntime runtime, required ValueChanged selectChat, required String directory, required String title, required String draft, }) async { if (runtime.sessions.loading && runtime.sessions.sessions.isEmpty) { unawaited(runtime.sessions.refresh()); } final choice = await showAppDetails<_ChatChoice>( context, maxWidth: 600, builder: (context) => _SendToChatSheet(runtime: runtime, title: title), ); if (!context.mounted || choice == null) return; if (choice.newChat) { await openReferenceInChat( context: context, runtime: runtime, selectChat: selectChat, directory: directory, title: title, draft: draft, ); return; } final id = choice.id; if (id == null || id.isEmpty) return; await runtime.appendDraft(id, draft); selectChat(id); runtime.composerFocus.request(id); } class _ChatChoice { const _ChatChoice.newChat() : newChat = true, id = null; const _ChatChoice.existing(this.id) : newChat = false; final bool newChat; final String? id; } class _SendToChatSheet extends StatefulWidget { const _SendToChatSheet({required this.runtime, required this.title}); final ChatRuntime runtime; final String title; @override State<_SendToChatSheet> createState() => _SendToChatSheetState(); } class _SendToChatSheetState extends State<_SendToChatSheet> { final _search = TextEditingController(); String _query = ''; @override void initState() { super.initState(); _search.addListener(() => setState(() => _query = _search.text)); widget.runtime.sessions.addListener(_changed); } void _changed() { if (mounted) setState(() {}); } @override void dispose() { widget.runtime.sessions.removeListener(_changed); _search.dispose(); super.dispose(); } List> get _rows { final rows = filterChatSessions(widget.runtime.sessions.sessions, _query) .where((row) { final id = '${row['id'] ?? ''}'; return id.isNotEmpty && row['closed_at'] == null && row['closed'] != true; }) .toList(); final active = widget.runtime.sessions.activeSessionId; rows.sort((a, b) { int priority(Map row) { final id = '${row['id'] ?? ''}'; if (id == active) return 0; if (widget.runtime.openedForWork.contains(id)) return 1; return 2; } final byPriority = priority(a).compareTo(priority(b)); if (byPriority != 0) return byPriority; return chatSessionMillis( b['updated_at'], ).compareTo(chatSessionMillis(a['updated_at'])); }); return rows; } @override Widget build(BuildContext context) { final sessions = _rows; return Padding( padding: const EdgeInsets.fromLTRB(20, 0, 20, 20), child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Text('Send to chat', style: Theme.of(context).textTheme.titleLarge), const SizedBox(height: 12), TextField( controller: _search, decoration: const InputDecoration( labelText: 'Search chats', prefixIcon: Icon(Icons.search), border: OutlineInputBorder(), ), ), const SizedBox(height: 8), ConstrainedBox( constraints: BoxConstraints( maxHeight: MediaQuery.sizeOf(context).height * .55, ), child: ListView( // As tall as its rows up to the cap, then it scrolls: a short // list leaves no empty space, a long one fits phone sheets and // desktop dialogs. shrinkWrap: true, children: [ ListTile( leading: const Icon(Icons.add_comment_outlined), title: const Text('New chat'), subtitle: const Text('Use the saved Open in chat settings'), onTap: () => Navigator.of(context).pop(const _ChatChoice.newChat()), ), if (sessions.isEmpty) ListTile( leading: const Icon(Icons.forum_outlined), title: Text( widget.runtime.sessions.loading ? 'Loading chats…' : _query.isEmpty ? 'No existing chats' : 'No chats match this search', ), ), for (final row in sessions) ListTile( leading: const Icon(Icons.chat_bubble_outline), title: Text(chatSessionTitle(row)), subtitle: Text(_chatSubtitle(row)), onTap: () => Navigator.of( context, ).pop(_ChatChoice.existing('${row['id']}')), ), ], ), ), ], ), ); } } /// The chat's folder and harness, leaving out whichever is unknown. String _chatSubtitle(Map row) { final folder = '${row['directory'] ?? ''}'.trim(); final backend = '${row['backend'] ?? row['harness'] ?? ''}'.trim(); final harness = backend.isEmpty ? '' : chatBackendLabel(backend).trim(); return [ folder.isEmpty ? 'No folder' : folder, if (harness.isNotEmpty) harness, ].join(' · '); }