Files
pipelines/app/lib/features/chat/conversation/chat_permission_card.dart
T

239 lines
7.5 KiB
Dart

import '../../../ui/theme/app_theme.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../ui/sessions/chat_adaptive.dart';
import '../ui/transcript/native_disclosure.dart';
import 'chat_card_styles.dart';
import 'chat_permission_card_model.dart';
/// One recorded permission request as a native card.
///
/// The headline is the request's action (or its recorded outcome), the status
/// line is the server's own note where present, and the once/always/reject
/// controls appear only while the request is still open. Every server string is
/// rendered as text, never parsed as markup.
class ChatPermissionCard extends StatelessWidget {
const ChatPermissionCard({
super.key,
required this.card,
required this.onDecision,
this.error,
});
final ChatPermissionCardData card;
final Future<void> Function(String decision) onDecision;
final String? error;
@override
Widget build(BuildContext context) =>
chatControlsStyleOf(context) == ChatControlsStyle.cupertino
? _cupertino(context)
: _material(context);
Widget _body(BuildContext context, {required bool cupertino}) {
final body = Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsetsDirectional.only(end: 12),
child: Icon(
cupertino
? CupertinoIcons.exclamationmark_shield
: Icons.shield_outlined,
color: chatCardWarningColor(context, cupertino),
),
),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
card.headline,
style: chatCardTitleStyle(context, cupertino),
),
Text(
card.statusLine,
style: chatCardSecondaryStyle(context, cupertino),
),
],
),
),
],
),
if (card.resourceText.isNotEmpty) ...[
const SizedBox(height: 12),
DecoratedBox(
decoration: BoxDecoration(
color: chatCardFillColor(context, cupertino),
borderRadius: BorderRadius.circular(8),
),
child: Padding(
padding: const EdgeInsets.all(8),
child: Text(
card.resourceText,
maxLines: chatApprovalMaxResourceLines,
overflow: TextOverflow.ellipsis,
),
),
),
if (card.resources.join('\n') != card.resourceText)
NativeDisclosure(
label: 'Show full request',
childBuilder: (context) =>
SelectableText(card.resources.join('\n')),
),
],
if (card.showRisk && card.risk.trim().isNotEmpty) ...[
const SizedBox(height: 12),
_risk(context, cupertino),
],
if (error != null && error!.trim().isNotEmpty) ...[
const SizedBox(height: 12),
Semantics(
liveRegion: true,
child: Text(
error!,
style: chatCardBodyStyle(
context,
cupertino,
)?.copyWith(color: chatCardErrorColor(context, cupertino)),
),
),
],
if (card.actionable) ...[
const SizedBox(height: 12),
_controls(context, cupertino),
],
],
);
return cupertino
? DefaultTextStyle.merge(
style: CupertinoTheme.of(context).textTheme.textStyle,
child: body,
)
: body;
}
Widget _risk(BuildContext context, bool cupertino) {
final tint = chatCardWarningColor(context, cupertino);
return DecoratedBox(
decoration: BoxDecoration(
color: chatCardWarningFillColor(context, cupertino),
borderRadius: BorderRadius.circular(8),
),
child: Padding(
padding: const EdgeInsets.all(12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Icon(
cupertino
? CupertinoIcons.exclamationmark_shield
: Icons.shield_outlined,
size: 16,
color: tint,
),
const SizedBox(width: 6),
Expanded(
child: Text(
chatRiskReviewLabel,
style: chatCardLabelStyle(
context,
cupertino,
)?.copyWith(color: tint),
),
),
],
),
const SizedBox(height: 4),
Text(card.risk, style: chatCardBodyStyle(context, cupertino)),
],
),
),
);
}
Widget _controls(BuildContext context, bool cupertino) {
final always = card.alwaysLabel;
if (cupertino) {
return Wrap(
spacing: 8,
runSpacing: 8,
children: [
CupertinoButton.filled(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
onPressed: () => onDecision(chatDecisionOnce),
child: const Text(chatApprovalOnceLabel),
),
CupertinoButton(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
onPressed: () => onDecision(chatDecisionAlways),
child: Text(always),
),
CupertinoButton(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
onPressed: () => onDecision(chatDecisionReject),
child: Text(
chatApprovalRejectLabel,
style: TextStyle(
color: CupertinoColors.systemRed.resolveFrom(context),
),
),
),
],
);
}
return Wrap(
spacing: 8,
runSpacing: 8,
children: [
FilledButton(
onPressed: () => onDecision(chatDecisionOnce),
child: const Text(chatApprovalOnceLabel),
),
TextButton(
onPressed: () => onDecision(chatDecisionAlways),
child: Text(always),
),
TextButton(
onPressed: () => onDecision(chatDecisionReject),
style: TextButton.styleFrom(
foregroundColor: Theme.of(context).colorScheme.error,
),
child: const Text(chatApprovalRejectLabel),
),
],
);
}
Widget _material(BuildContext context) => Card(
// Consecutive cards must read as separate surfaces; the row itself adds only
// the horizontal inset.
margin: const EdgeInsets.only(bottom: 12),
child: Padding(
padding: const EdgeInsets.all(16),
child: _body(context, cupertino: false),
),
);
Widget _cupertino(BuildContext context) => CupertinoListSection.insetGrouped(
backgroundColor: AppColors.groupedBackground,
// As the Material card: the transcript row owns the horizontal inset, so
// the card lines up with the message bubbles.
margin: const EdgeInsets.only(bottom: 12),
children: [
Padding(
padding: const EdgeInsets.all(16),
child: _body(context, cupertino: true),
),
],
);
}