diff --git a/src/client/components/monitor/MonitorInfo.tsx b/src/client/components/monitor/MonitorInfo.tsx index f9158ce..9364441 100644 --- a/src/client/components/monitor/MonitorInfo.tsx +++ b/src/client/components/monitor/MonitorInfo.tsx @@ -61,6 +61,10 @@ export const MonitorInfo: React.FC = React.memo((props) => { onSuccess: defaultSuccessHandler, onError: defaultErrorHandler, }); + const testNotifyScriptMutation = trpc.monitor.testNotifyScript.useMutation({ + onSuccess: defaultSuccessHandler, + onError: defaultErrorHandler, + }); const trpcUtils = trpc.useContext(); @@ -229,6 +233,15 @@ export const MonitorInfo: React.FC = React.memo((props) => { label: t('Show Badge'), onClick: () => setShowBadge(true), }, + { + key: 'testNotify', + label: t('Test Notify'), + onClick: () => + testNotifyScriptMutation.mutateAsync({ + workspaceId, + monitorId, + }), + }, { type: 'divider', }, diff --git a/src/server/trpc/routers/monitor.ts b/src/server/trpc/routers/monitor.ts index c869436..360bd68 100644 --- a/src/server/trpc/routers/monitor.ts +++ b/src/server/trpc/routers/monitor.ts @@ -33,6 +33,7 @@ import { monitorPublicInfoSchema, } from '../../model/_schema/monitor.js'; import { monitorPageManager } from '../../model/monitor/page/manager.js'; +import { token } from '../../model/notification/token/index.js'; export const monitorRouter = router({ all: workspaceProcedure @@ -204,6 +205,29 @@ export const monitorRouter = router({ usage: res.usage, }; }), + testNotifyScript: workspaceAdminProcedure + .input( + z.object({ + monitorId: z.string(), + }) + ) + .output(z.void()) + .mutation(async ({ input }) => { + const { monitorId } = input; + const runner = monitorManager.getRunner(monitorId); + if (!runner) { + throw new Error('This monitor is not running or not existed.'); + } + + if (runner.monitor.notifications.length === 0) { + throw new Error('This monitor not has any notifications.'); + } + + await runner.notify('Test title', [ + token.paragraph('Test content'), + token.paragraph(`Send from monitor: ${runner.monitor.name}`), + ]); + }), data: workspaceProcedure .meta( buildMonitorOpenapi({