import 'dart:async'; import 'package:flutter/material.dart'; import '../domain/chat_account_usage.dart'; import '../domain/chat_usage_limit.dart'; import '../domain/chat_usage_warning.dart'; import 'chat_schedule_time.dart'; import 'chat_thread_controller.dart'; import 'chat_usage_warnings.dart'; /// Above the composer while the chat's harness account is nearly out of /// quota: "Claude Code 5-hour limit 84% used · resets in 1 h 20 m", in the /// error colours from 95%. It names the most constrained window and a second /// one also at a warning level. Dismissing it holds until a window reaches the /// next level or resets, in every chat of the account ([ChatUsageWarnings]). /// The usage-limit banner wins: this one hides while the account is out of /// quota. It also hands each new account report to [warnings], which /// announces a newly crossed level once. class ChatUsageWarningBanner extends StatefulWidget { ChatUsageWarningBanner({ super.key, required this.thread, ChatUsageWarnings? warnings, this.now = DateTime.now, }) : warnings = warnings ?? ChatUsageWarnings.shared; final ChatThreadController thread; final ChatUsageWarnings warnings; final DateTime Function() now; @override State createState() => _ChatUsageWarningBannerState(); } class _ChatUsageWarningBannerState extends State { ChatAccountUsage? _reported; /// Refreshes the countdown and drops a window once its reset passes. Timer? _tick; @override void initState() { super.initState(); unawaited(widget.warnings.load()); widget.thread.addListener(_report); _report(); } @override void didUpdateWidget(ChatUsageWarningBanner oldWidget) { super.didUpdateWidget(oldWidget); if (oldWidget.thread == widget.thread) return; oldWidget.thread.removeListener(_report); widget.thread.addListener(_report); _report(); } @override void dispose() { widget.thread.removeListener(_report); _tick?.cancel(); super.dispose(); } void _report() { final usage = widget.thread.accountUsage; if (usage == null || identical(usage, _reported)) return; _reported = usage; unawaited(widget.warnings.report(usage, now: widget.now())); } void _ticking(bool on) { if (on == (_tick != null)) return; _tick?.cancel(); _tick = on ? Timer.periodic(const Duration(seconds: 30), (_) { if (mounted) setState(() {}); }) : null; } @override Widget build(BuildContext context) { return ListenableBuilder( listenable: Listenable.merge([widget.thread, widget.warnings]), builder: (context, _) { final now = widget.now(); final thread = widget.thread; final limited = chatThreadUsageLimit(thread.accountUsage, thread.messages, now) != null; final warning = limited ? null : chatUsageWarning( thread.accountUsage, now, widget.warnings.dismissed, ); _ticking(warning != null); if (warning == null) return const SizedBox.shrink(); return _banner(context, warning, now); }, ); } String _resets(ChatQuotaWindow window, DateTime now) { if (window.resetsAt <= 0) return ''; final relative = chatQuotaResetsIn(window, now); if (relative != null) return ' · resets $relative'; final at = DateTime.fromMillisecondsSinceEpoch(window.resetsAt); return ' · resets ${chatScheduleTime(context, at, now: () => now)}'; } Widget _banner(BuildContext context, ChatUsageWarning warning, DateTime now) { final scheme = Theme.of(context).colorScheme; final text = Theme.of(context).textTheme; final background = warning.urgent ? scheme.errorContainer : scheme.secondaryContainer; final foreground = warning.urgent ? scheme.onErrorContainer : scheme.onSecondaryContainer; final second = warning.second; return Padding( padding: const EdgeInsetsDirectional.only(bottom: 8), child: Material( key: const Key('chat-usage-warning'), color: background, borderRadius: BorderRadius.circular(16), child: Padding( padding: const EdgeInsetsDirectional.fromSTEB(16, 4, 4, 4), child: Row( children: [ Icon( warning.urgent ? Icons.error_outline : Icons.data_usage, color: foreground, ), const SizedBox(width: 12), Expanded( child: Padding( padding: const EdgeInsets.symmetric(vertical: 6), child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( chatQuotaWarningTitle( warning.harness, warning.primary, ) + _resets(warning.primary, now), key: const Key('chat-usage-warning-status'), style: text.bodyMedium?.copyWith( color: foreground, fontWeight: FontWeight.w600, ), ), if (second != null) Text( '${second.label} limit ' '${second.usedPercent.round()}% used' '${_resets(second, now)}', key: const Key('chat-usage-warning-second'), style: text.bodySmall?.copyWith(color: foreground), ), ], ), ), ), IconButton( key: const Key('chat-usage-warning-dismiss'), tooltip: 'Dismiss', color: foreground, onPressed: () => widget.warnings.dismiss(warning), icon: const Icon(Icons.close), ), ], ), ), ), ); } }