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.
This commit is contained in:
suyao 2025-05-21 23:12:03 +08:00 committed by 亢奋猫
parent c49a262df6
commit 4f046a5510
3 changed files with 29 additions and 19 deletions

View File

@ -39,7 +39,8 @@ export const NotificationProvider: React.FC<{ children: React.ReactNode }> = ({
return new Promise<void>((resolve) => { return new Promise<void>((resolve) => {
api.open({ api.open({
message: notification.title, message: notification.title,
description: notification.message, description:
notification.message.length > 50 ? notification.message.slice(0, 47) + '...' : notification.message,
duration: 3, duration: 3,
placement: 'topRight', placement: 'topRight',
type: typeMap[notification.type] || 'info', type: typeMap[notification.type] || 'info',

View File

@ -36,6 +36,7 @@ import {
} from '@renderer/utils/messageUtils/create' } from '@renderer/utils/messageUtils/create'
import { getMainTextContent } from '@renderer/utils/messageUtils/find' import { getMainTextContent } from '@renderer/utils/messageUtils/find'
import { getTopicQueue, waitForTopicQueue } from '@renderer/utils/queue' import { getTopicQueue, waitForTopicQueue } from '@renderer/utils/queue'
import { isOnHomePage } from '@renderer/utils/window'
import { t } from 'i18next' import { t } from 'i18next'
import { throttle } from 'lodash' import { throttle } from 'lodash'
@ -589,15 +590,17 @@ const fetchAndProcessAssistantResponseImpl = async (
status: error.status || error.code, status: error.status || error.code,
requestId: error.request_id requestId: error.request_id
} }
await notificationService.send({ if (!isOnHomePage()) {
id: uuid(), await notificationService.send({
type: 'error', id: uuid(),
title: t('notification.assistant'), type: 'error',
message: serializableError.message, title: t('notification.assistant'),
silent: false, message: serializableError.message,
timestamp: Date.now(), silent: false,
source: 'assistant' timestamp: Date.now(),
}) source: 'assistant'
})
}
if (lastBlockId) { if (lastBlockId) {
// 更改上一个block的状态为ERROR // 更改上一个block的状态为ERROR
@ -646,15 +649,17 @@ const fetchAndProcessAssistantResponseImpl = async (
} }
const content = getMainTextContent(finalAssistantMsg) const content = getMainTextContent(finalAssistantMsg)
await notificationService.send({ if (!isOnHomePage()) {
id: uuid(), await notificationService.send({
type: 'success', id: uuid(),
title: t('notification.assistant'), type: 'success',
message: content.length > 50 ? content.slice(0, 47) + '...' : content, title: t('notification.assistant'),
silent: false, message: content.length > 50 ? content.slice(0, 47) + '...' : content,
timestamp: Date.now(), silent: false,
source: 'assistant' timestamp: Date.now(),
}) source: 'assistant'
})
}
// 更新topic的name // 更新topic的name
autoRenameTopic(assistant, topicId) autoRenameTopic(assistant, topicId)

View File

@ -1,3 +1,7 @@
export const isFocused = () => { export const isFocused = () => {
return document.hasFocus() return document.hasFocus()
} }
export const isOnHomePage = () => {
return window.location.hash === '#/' || window.location.hash === '#' || window.location.hash === ''
}