mirror of
https://github.com/jxxghp/MoviePilot-Frontend.git
synced 2026-07-17 02:23:05 +08:00
feat: add agent assistant notification bubble functionality
- Introduced a new utility for managing agent assistant notifications. - Created functions to emit and listen for notification bubbles using custom events. - Defined types for notification payloads to ensure type safety.
This commit is contained in:
56
src/utils/agentAssistantBubble.ts
Normal file
56
src/utils/agentAssistantBubble.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import type { SystemNotification } from '@/api/types'
|
||||
|
||||
const AGENT_ASSISTANT_BUBBLE_EVENT = 'agentAssistantBubble'
|
||||
|
||||
export interface AgentAssistantNotificationBubblePayload {
|
||||
id: string
|
||||
title?: string
|
||||
text?: string
|
||||
type?: string
|
||||
mtype?: string
|
||||
source?: string
|
||||
date?: string
|
||||
reg_time?: string
|
||||
}
|
||||
|
||||
interface AgentAssistantBubbleEvent extends CustomEvent<AgentAssistantNotificationBubblePayload> {}
|
||||
|
||||
function createNotificationBubbleId(notification: SystemNotification) {
|
||||
if (notification.id) return `notification-${notification.id}`
|
||||
|
||||
return `notification-${Date.now()}-${Math.random().toString(16).slice(2)}`
|
||||
}
|
||||
|
||||
// 通知中心和智能助手入口没有父子关系,通过全局事件传递实时通知气泡数据。
|
||||
export function emitAgentAssistantNotificationBubble(notification: SystemNotification) {
|
||||
if (typeof window === 'undefined') return
|
||||
|
||||
window.dispatchEvent(
|
||||
new CustomEvent<AgentAssistantNotificationBubblePayload>(AGENT_ASSISTANT_BUBBLE_EVENT, {
|
||||
detail: {
|
||||
id: createNotificationBubbleId(notification),
|
||||
title: notification.title,
|
||||
text: notification.text,
|
||||
type: notification.type,
|
||||
mtype: notification.mtype,
|
||||
source: notification.source,
|
||||
date: notification.date,
|
||||
reg_time: notification.reg_time,
|
||||
},
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
export function onAgentAssistantNotificationBubble(
|
||||
callback: (payload: AgentAssistantNotificationBubblePayload) => void,
|
||||
) {
|
||||
if (typeof window === 'undefined') return () => {}
|
||||
|
||||
const handler = (event: Event) => {
|
||||
callback((event as AgentAssistantBubbleEvent).detail)
|
||||
}
|
||||
|
||||
window.addEventListener(AGENT_ASSISTANT_BUBBLE_EVENT, handler)
|
||||
|
||||
return () => window.removeEventListener(AGENT_ASSISTANT_BUBBLE_EVENT, handler)
|
||||
}
|
||||
Reference in New Issue
Block a user