import 'package:flutter/widgets.dart'; import '../../../../core/api_gateway.dart'; import '../../data/chat_attachment_images.dart'; import '../../domain/chat_attachment_image.dart'; import 'chat_attachment_thumbnail.dart'; /// Whether a file part draws as a thumbnail: a raster image whose bytes the /// client can reach (inline, the server's attachment store or a fresh upload). bool chatPartIsThumbnail(JsonMap part) => chatPartImageMime(part) != null && chatImageSourceOf('${part['uri'] ?? ''}'.trim()) != null; /// The operator's attached images above their message, end-aligned like the /// turn itself, wrapping onto further lines on a narrow pane. class ChatAttachmentGallery extends StatelessWidget { const ChatAttachmentGallery({ super.key, required this.parts, required this.images, required this.cupertino, }); /// File parts for which [chatPartIsThumbnail] holds. final List parts; final ChatAttachmentImages images; final bool cupertino; @override Widget build(BuildContext context) => Wrap( key: const Key('chat-attachment-gallery'), alignment: WrapAlignment.end, spacing: 8, runSpacing: 8, children: [ for (final (index, part) in parts.indexed) ChatAttachmentThumbnail( key: ValueKey('chat-attachment-image-$index'), name: chatAttachmentPartName(part), uri: '${part['uri'] ?? ''}'.trim(), images: images, cupertino: cupertino, ), ], ); } /// The web's attachment name rule: its own name, else the URI basename, else /// `file`. A `data:` URI has no useful basename, so it reads `image`. String chatAttachmentPartName(JsonMap part) { final name = '${part['name'] ?? ''}'.trim(); if (name.isNotEmpty) return name; final uri = '${part['uri'] ?? ''}'.trim(); if (uri.startsWith('data:')) return 'image'; final basename = uri.split('/').last.trim(); return basename.isEmpty ? 'file' : basename; }