From e48a079f466ff09b9b1a758a3ee73722e724284b Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Sun, 17 Dec 2023 19:25:57 +0800 Subject: [PATCH] refactor: use MonitorListItem to replace MonitorHealthBar to show more info --- .../monitor/StatusPage/Services.tsx | 22 +++++++++---- src/server/trpc/routers/monitor.ts | 32 ++++++++++++++++++- src/types/index.ts | 2 +- src/types/monitor.ts | 6 ++++ 4 files changed, 53 insertions(+), 9 deletions(-) diff --git a/src/client/components/monitor/StatusPage/Services.tsx b/src/client/components/monitor/StatusPage/Services.tsx index 0fb25a4..85314d7 100644 --- a/src/client/components/monitor/StatusPage/Services.tsx +++ b/src/client/components/monitor/StatusPage/Services.tsx @@ -1,6 +1,8 @@ import { Empty } from 'antd'; import React from 'react'; -import { MonitorHealthBar } from '../MonitorHealthBar'; +import { trpc } from '../../../api/trpc'; +import { Loading } from '../../Loading'; +import { MonitorListItem } from '../MonitorListItem'; interface StatusPageServicesProps { workspaceId: string; @@ -10,17 +12,23 @@ export const StatusPageServices: React.FC = React.memo( (props) => { const { workspaceId, monitorList } = props; + const { data: list = [], isLoading } = trpc.monitor.getPublicInfo.useQuery({ + monitorIds: monitorList.map((item) => item.id), + }); + + if (isLoading) { + return ; + } + return (
- {monitorList.length > 0 ? ( - monitorList.map((item) => ( + {list.length > 0 ? ( + list.map((item) => (
-
)) diff --git a/src/server/trpc/routers/monitor.ts b/src/server/trpc/routers/monitor.ts index e5822d7..8cdf0f1 100644 --- a/src/server/trpc/routers/monitor.ts +++ b/src/server/trpc/routers/monitor.ts @@ -8,7 +8,10 @@ import { import { prisma } from '../../model/_client'; import { z } from 'zod'; import { monitorManager } from '../../model/monitor'; -import { MonitorInfoWithNotificationIds } from '../../../types'; +import { + MonitorInfoWithNotificationIds, + MonitorPublicInfoSchema, +} from '../../../types'; import dayjs from 'dayjs'; import { monitorEventSchema, @@ -77,6 +80,33 @@ export const monitorRouter = router({ return monitor; }), + getPublicInfo: publicProcedure + .meta({ + openapi: { + tags: [OPENAPI_TAG.MONITOR], + protect: false, + method: 'POST', + path: '/monitor/getPublicInfo', + }, + }) + .input( + z.object({ + monitorIds: z.array(z.string()), + }) + ) + .output(z.array(MonitorPublicInfoSchema)) + .query(async ({ input }) => { + const { monitorIds } = input; + const res = await prisma.monitor.findMany({ + where: { + id: { + in: monitorIds, + }, + }, + }); + + return res.map((item) => MonitorPublicInfoSchema.parse(item)); + }), upsert: workspaceOwnerProcedure .meta( buildMonitorOpenapi({ diff --git a/src/types/index.ts b/src/types/index.ts index eeddbda..49b7fc1 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,7 +1,7 @@ +export * as schemas from '../../prisma/zod/index'; export * from './server'; export * from './monitor'; export * from './utils'; -export * as schemas from '../../prisma/zod/index'; declare global { namespace PrismaJson { diff --git a/src/types/monitor.ts b/src/types/monitor.ts index e5ed36f..159b559 100644 --- a/src/types/monitor.ts +++ b/src/types/monitor.ts @@ -1,5 +1,6 @@ import type { Monitor } from '@prisma/client'; import { ExactType } from './utils'; +import { schemas } from '.'; export type MonitorInfo = ExactType< Monitor, @@ -11,3 +12,8 @@ export type MonitorInfo = ExactType< export type MonitorInfoWithNotificationIds = MonitorInfo & { notifications: { id: string }[]; }; + +export const MonitorPublicInfoSchema = schemas.MonitorModelSchema.pick({ + id: true, + name: true, +});