main
ts 38 lines 1.15 KB
Raw
1 import { setToastImpl } from "@shuffleio/shuffle-mcps"
2 import { useMessage } from "naive-ui"
3 import { onMounted } from "vue"
4
5 /**
6 * Bridges `@shuffleio/shuffle-mcps`'s internal toast facade onto Naive
7 * UI's message API so toasts the package emits (auth saved, test
8 * connection results, errors, etc.) surface in CoPilot's UI instead of
9 * the package's silent console.warn fallback.
10 *
11 * Must run inside the `<n-message-provider>` tree, hence its home here
12 * in `GlobalListener` rather than in `App.vue`.
13 */
14
15 const VARIANT_TO_METHOD = {
16 destructive: "error",
17 error: "error",
18 success: "success",
19 warning: "warning"
20 } as const
21
22 export function useShuffleToastBridge() {
23 const message = useMessage()
24
25 onMounted(() => {
26 setToastImpl((arg, opts) => {
27 const obj = typeof arg === "string" ? null : arg
28 const title = obj ? obj.title : arg
29 const description = obj?.description ?? opts?.description
30 const text = [title, description].filter(Boolean).join("")
31 if (!text) return
32
33 const variant = obj?.variant ?? opts?.variant
34 const method = VARIANT_TO_METHOD[variant as keyof typeof VARIANT_TO_METHOD] ?? "info"
35 message[method](text)
36 })
37 })
38 }