From 4f046a551048dddcb04447c75484bdedc1ed45c5 Mon Sep 17 00:00:00 2001 From: suyao Date: Wed, 21 May 2025 23:12:03 +0800 Subject: [PATCH] refactor: enhance notification handling based on page context - Updated NotificationProvider to truncate long messages for better display. - Modified message sending logic in messageThunk to prevent notifications on the home page, improving user experience. --- .../src/context/NotificationProvider.tsx | 3 +- src/renderer/src/store/thunk/messageThunk.ts | 41 +++++++++++-------- src/renderer/src/utils/window.ts | 4 ++ 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/renderer/src/context/NotificationProvider.tsx b/src/renderer/src/context/NotificationProvider.tsx index fca0ef818e..c6de09b722 100644 --- a/src/renderer/src/context/NotificationProvider.tsx +++ b/src/renderer/src/context/NotificationProvider.tsx @@ -39,7 +39,8 @@ export const NotificationProvider: React.FC<{ children: React.ReactNode }> = ({ return new Promise((resolve) => { api.open({ message: notification.title, - description: notification.message, + description: + notification.message.length > 50 ? notification.message.slice(0, 47) + '...' : notification.message, duration: 3, placement: 'topRight', type: typeMap[notification.type] || 'info', diff --git a/src/renderer/src/store/thunk/messageThunk.ts b/src/renderer/src/store/thunk/messageThunk.ts index 1e81d123e2..29281fb155 100644 --- a/src/renderer/src/store/thunk/messageThunk.ts +++ b/src/renderer/src/store/thunk/messageThunk.ts @@ -36,6 +36,7 @@ import { } from '@renderer/utils/messageUtils/create' import { getMainTextContent } from '@renderer/utils/messageUtils/find' import { getTopicQueue, waitForTopicQueue } from '@renderer/utils/queue' +import { isOnHomePage } from '@renderer/utils/window' import { t } from 'i18next' import { throttle } from 'lodash' @@ -589,15 +590,17 @@ const fetchAndProcessAssistantResponseImpl = async ( status: error.status || error.code, requestId: error.request_id } - await notificationService.send({ - id: uuid(), - type: 'error', - title: t('notification.assistant'), - message: serializableError.message, - silent: false, - timestamp: Date.now(), - source: 'assistant' - }) + if (!isOnHomePage()) { + await notificationService.send({ + id: uuid(), + type: 'error', + title: t('notification.assistant'), + message: serializableError.message, + silent: false, + timestamp: Date.now(), + source: 'assistant' + }) + } if (lastBlockId) { // 更改上一个block的状态为ERROR @@ -646,15 +649,17 @@ const fetchAndProcessAssistantResponseImpl = async ( } const content = getMainTextContent(finalAssistantMsg) - await notificationService.send({ - id: uuid(), - type: 'success', - title: t('notification.assistant'), - message: content.length > 50 ? content.slice(0, 47) + '...' : content, - silent: false, - timestamp: Date.now(), - source: 'assistant' - }) + if (!isOnHomePage()) { + await notificationService.send({ + id: uuid(), + type: 'success', + title: t('notification.assistant'), + message: content.length > 50 ? content.slice(0, 47) + '...' : content, + silent: false, + timestamp: Date.now(), + source: 'assistant' + }) + } // 更新topic的name autoRenameTopic(assistant, topicId) diff --git a/src/renderer/src/utils/window.ts b/src/renderer/src/utils/window.ts index 3f67aca4b3..ed50c38820 100644 --- a/src/renderer/src/utils/window.ts +++ b/src/renderer/src/utils/window.ts @@ -1,3 +1,7 @@ export const isFocused = () => { return document.hasFocus() } + +export const isOnHomePage = () => { + return window.location.hash === '#/' || window.location.hash === '#' || window.location.hash === '' +}