refactor: use MonitorListItem to replace MonitorHealthBar to show more info

This commit is contained in:
moonrailgun 2023-12-17 19:25:57 +08:00
parent dd3343f64b
commit e48a079f46
4 changed files with 53 additions and 9 deletions

View File

@ -1,6 +1,8 @@
import { Empty } from 'antd'; import { Empty } from 'antd';
import React from 'react'; import React from 'react';
import { MonitorHealthBar } from '../MonitorHealthBar'; import { trpc } from '../../../api/trpc';
import { Loading } from '../../Loading';
import { MonitorListItem } from '../MonitorListItem';
interface StatusPageServicesProps { interface StatusPageServicesProps {
workspaceId: string; workspaceId: string;
@ -10,17 +12,23 @@ export const StatusPageServices: React.FC<StatusPageServicesProps> = React.memo(
(props) => { (props) => {
const { workspaceId, monitorList } = props; const { workspaceId, monitorList } = props;
const { data: list = [], isLoading } = trpc.monitor.getPublicInfo.useQuery({
monitorIds: monitorList.map((item) => item.id),
});
if (isLoading) {
return <Loading />;
}
return ( return (
<div className="shadow-2xl p-2.5 flex flex-col gap-4"> <div className="shadow-2xl p-2.5 flex flex-col gap-4">
{monitorList.length > 0 ? ( {list.length > 0 ? (
monitorList.map((item) => ( list.map((item) => (
<div key={item.id} className="hover:bg-black hover:bg-opacity-20"> <div key={item.id} className="hover:bg-black hover:bg-opacity-20">
<MonitorHealthBar <MonitorListItem
workspaceId={workspaceId} workspaceId={workspaceId}
monitorId={item.id} monitorId={item.id}
count={40} monitorName={item.name}
size="large"
showCurrentStatus={true}
/> />
</div> </div>
)) ))

View File

@ -8,7 +8,10 @@ import {
import { prisma } from '../../model/_client'; import { prisma } from '../../model/_client';
import { z } from 'zod'; import { z } from 'zod';
import { monitorManager } from '../../model/monitor'; import { monitorManager } from '../../model/monitor';
import { MonitorInfoWithNotificationIds } from '../../../types'; import {
MonitorInfoWithNotificationIds,
MonitorPublicInfoSchema,
} from '../../../types';
import dayjs from 'dayjs'; import dayjs from 'dayjs';
import { import {
monitorEventSchema, monitorEventSchema,
@ -77,6 +80,33 @@ export const monitorRouter = router({
return monitor; 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 upsert: workspaceOwnerProcedure
.meta( .meta(
buildMonitorOpenapi({ buildMonitorOpenapi({

View File

@ -1,7 +1,7 @@
export * as schemas from '../../prisma/zod/index';
export * from './server'; export * from './server';
export * from './monitor'; export * from './monitor';
export * from './utils'; export * from './utils';
export * as schemas from '../../prisma/zod/index';
declare global { declare global {
namespace PrismaJson { namespace PrismaJson {

View File

@ -1,5 +1,6 @@
import type { Monitor } from '@prisma/client'; import type { Monitor } from '@prisma/client';
import { ExactType } from './utils'; import { ExactType } from './utils';
import { schemas } from '.';
export type MonitorInfo = ExactType< export type MonitorInfo = ExactType<
Monitor, Monitor,
@ -11,3 +12,8 @@ export type MonitorInfo = ExactType<
export type MonitorInfoWithNotificationIds = MonitorInfo & { export type MonitorInfoWithNotificationIds = MonitorInfo & {
notifications: { id: string }[]; notifications: { id: string }[];
}; };
export const MonitorPublicInfoSchema = schemas.MonitorModelSchema.pick({
id: true,
name: true,
});