From 20b16cf174a0772f8f103c45c013867a0b18414d Mon Sep 17 00:00:00 2001 From: Richard Shiue <71320345+richardshiue@users.noreply.github.com> Date: Mon, 30 Dec 2024 17:17:36 +0800 Subject: [PATCH] feat: add toast messages for ai chat interactions (#7086) --- .../message/ai_message_action_bar.dart | 48 +++++++++++++++++-- .../presentation/message/ai_metadata.dart | 1 + .../presentation/message/message_util.dart | 8 ++++ frontend/resources/translations/en.json | 4 +- 4 files changed, 55 insertions(+), 6 deletions(-) diff --git a/frontend/appflowy_flutter/lib/plugins/ai_chat/presentation/message/ai_message_action_bar.dart b/frontend/appflowy_flutter/lib/plugins/ai_chat/presentation/message/ai_message_action_bar.dart index 64183d0346..fe61398caa 100644 --- a/frontend/appflowy_flutter/lib/plugins/ai_chat/presentation/message/ai_message_action_bar.dart +++ b/frontend/appflowy_flutter/lib/plugins/ai_chat/presentation/message/ai_message_action_bar.dart @@ -274,7 +274,7 @@ class _SaveToPageButtonState extends State { onPressed: () async { final documentId = getOpenedDocumentId(); if (documentId != null) { - await onAddToExistingPage(documentId); + await onAddToExistingPage(context, documentId); await forceReloadAndUpdateSelection(documentId); } else { widget.onOverrideVisibility?.call(true); @@ -298,9 +298,8 @@ class _SaveToPageButtonState extends State { }, onAddToExistingPage: (documentId) async { popoverController.close(); - await onAddToExistingPage(documentId); - final view = - await ViewBackendService.getView(documentId).toNullable(); + final view = await onAddToExistingPage(context, documentId); + if (context.mounted) { openPageFromMessage(context, view); } @@ -309,12 +308,20 @@ class _SaveToPageButtonState extends State { ); } - Future onAddToExistingPage(String documentId) async { + Future onAddToExistingPage( + BuildContext context, + String documentId, + ) async { await ChatEditDocumentService.addMessageToPage( documentId, widget.textMessage, ); await Future.delayed(const Duration(milliseconds: 500)); + final view = await ViewBackendService.getView(documentId).toNullable(); + if (context.mounted) { + showSaveMessageSuccessToast(context, view); + } + return view; } void addMessageToNewPage(BuildContext context) async { @@ -327,12 +334,43 @@ class _SaveToPageButtonState extends State { chatView.parentViewId, [widget.textMessage], ); + if (context.mounted) { + showSaveMessageSuccessToast(context, newView); openPageFromMessage(context, newView); } } } + void showSaveMessageSuccessToast(BuildContext context, ViewPB? view) { + if (view == null) { + return; + } + showToastNotification( + context, + richMessage: TextSpan( + children: [ + TextSpan( + text: LocaleKeys.chat_addToNewPageSuccessToast.tr(), + style: Theme.of(context).textTheme.bodyMedium?.copyWith( + color: const Color(0xFFFFFFFF), + ), + ), + const TextSpan( + text: ' ', + ), + TextSpan( + text: view.nameOrDefault, + style: Theme.of(context).textTheme.bodyMedium?.copyWith( + color: const Color(0xFFFFFFFF), + fontWeight: FontWeight.w700, + ), + ), + ], + ), + ); + } + Future forceReloadAndUpdateSelection(String documentId) async { final bloc = DocumentBloc.findOpen(documentId); if (bloc == null) { diff --git a/frontend/appflowy_flutter/lib/plugins/ai_chat/presentation/message/ai_metadata.dart b/frontend/appflowy_flutter/lib/plugins/ai_chat/presentation/message/ai_metadata.dart index 1e06ff5f46..cc97610e8d 100644 --- a/frontend/appflowy_flutter/lib/plugins/ai_chat/presentation/message/ai_metadata.dart +++ b/frontend/appflowy_flutter/lib/plugins/ai_chat/presentation/message/ai_metadata.dart @@ -90,6 +90,7 @@ class _AIMessageMetadataState extends State { data == null) { return _MetadataButton( name: m.name, + onTap: () => widget.onSelectedMetadata?.call(m), ); } return BlocProvider( diff --git a/frontend/appflowy_flutter/lib/plugins/ai_chat/presentation/message/message_util.dart b/frontend/appflowy_flutter/lib/plugins/ai_chat/presentation/message/message_util.dart index 4673b9def1..97e5e84b54 100644 --- a/frontend/appflowy_flutter/lib/plugins/ai_chat/presentation/message/message_util.dart +++ b/frontend/appflowy_flutter/lib/plugins/ai_chat/presentation/message/message_util.dart @@ -1,8 +1,11 @@ +import 'package:appflowy/generated/locale_keys.g.dart'; import 'package:appflowy/mobile/application/mobile_router.dart'; import 'package:appflowy/startup/startup.dart'; import 'package:appflowy/workspace/application/tabs/tabs_bloc.dart'; import 'package:appflowy/workspace/application/view/view_ext.dart'; +import 'package:appflowy/workspace/presentation/widgets/dialogs.dart'; import 'package:appflowy_backend/protobuf/flowy-folder/protobuf.dart'; +import 'package:easy_localization/easy_localization.dart'; import 'package:flutter/widgets.dart'; import 'package:universal_platform/universal_platform.dart'; @@ -10,6 +13,11 @@ import 'package:universal_platform/universal_platform.dart'; /// on mobile void openPageFromMessage(BuildContext context, ViewPB? view) { if (view == null) { + showToastNotification( + context, + message: LocaleKeys.chat_openPagePreviewFailedToast.tr(), + type: ToastificationType.error, + ); return; } if (UniversalPlatform.isDesktop) { diff --git a/frontend/resources/translations/en.json b/frontend/resources/translations/en.json index 284d912d6f..ef8078a2a8 100644 --- a/frontend/resources/translations/en.json +++ b/frontend/resources/translations/en.json @@ -218,7 +218,9 @@ "addToPageButton": "Add to page", "addToPageTitle": "Add message to...", "addToNewPage": "Add to a new page", - "addToNewPageName": "Messages extracted from \"{}\"" + "addToNewPageName": "Messages extracted from \"{}\"", + "addToNewPageSuccessToast": "Message added to", + "openPagePreviewFailedToast": "Failed to open page" }, "trash": { "text": "Trash",