main
ts 30 lines 1.1 KB
Raw
1 import type { MessageOptions } from "naive-ui"
2 import type { MessageApiInjection, MessageReactive } from "naive-ui/es/message/src/MessageProvider"
3 import type { NotificationOptions } from "naive-ui/es/notification/src/NotificationEnvironment"
4 import type { NotificationApiInjection, NotificationReactive } from "naive-ui/es/notification/src/NotificationProvider"
5 import type { Writable } from "type-fest"
6
7 export type NotificationObject = Writable<NotificationOptions>
8
9 interface InitPayload {
10 message: MessageApiInjection
11 notification: NotificationApiInjection
12 }
13
14 let message: MessageApiInjection | null = null
15 let notification: NotificationApiInjection | null = null
16
17 export function useGlobalActions() {
18 return {
19 init: (payload: InitPayload): void => {
20 message = payload.message
21 notification = payload.notification
22 },
23 message: (content: string, options?: MessageOptions): MessageReactive | undefined => {
24 return message?.create(content, options || { type: "info" })
25 },
26 notification: (options: NotificationOptions): NotificationReactive | undefined => {
27 return notification?.create(options)
28 }
29 }
30 }