feat: 可点击通知跳转对话

This commit is contained in:
CrescentLeaf
2025-11-01 01:11:44 +08:00
parent 51c6d1f0a6
commit dffa773acc

View File

@@ -119,30 +119,37 @@ export default function App() {
userProfileDialogRef.current!.open = true userProfileDialogRef.current!.open = true
} }
Notification.requestPermission() if ('Notification' in window) {
React.useEffect(() => { Notification.requestPermission()
interface OnMessageData { React.useEffect(() => {
chat: string interface OnMessageData {
msg: Message chat: string
} msg: Message
async function onMessage(_event: unknown) {
EventBus.emit('RecentsList.updateRecents')
const event = _event as OnMessageData
if (currentChatId != event.chat) {
const chat = await DataCaches.getChatInfo(event.chat)
const user = await DataCaches.getUserProfile(event.msg.user_id)
new Notification(`${user.nickname} (对话: ${chat.title})`, {
icon: getUrlForFileByHash(chat.avatar_file_hash),
body: event.msg.text,
})
} }
} async function onMessage(_event: unknown) {
Client.on('Client.onMessage', onMessage) EventBus.emit('RecentsList.updateRecents')
return () => {
Client.off('Client.onMessage', onMessage) const event = _event as OnMessageData
} if (currentChatId != event.chat) {
}, [currentChatId]) const chat = await DataCaches.getChatInfo(event.chat)
const user = await DataCaches.getUserProfile(event.msg.user_id)
const notification = new Notification(`${user.nickname} (对话: ${chat.title})`, {
icon: getUrlForFileByHash(chat.avatar_file_hash),
body: event.msg.text,
})
notification.addEventListener('click', () => {
setCurrentChatId(chat.id)
setIsShowChatFragment(true)
notification.close()
})
}
}
Client.on('Client.onMessage', onMessage)
return () => {
Client.off('Client.onMessage', onMessage)
}
}, [currentChatId])
}
return ( return (
<div style={{ <div style={{