From 2ad96714f34996d19281da3cf44c214e2cc2b723 Mon Sep 17 00:00:00 2001 From: UUBulb <35923940+uubulb@users.noreply.github.com> Date: Thu, 5 Dec 2024 21:00:48 +0800 Subject: [PATCH] update service api (#29) * update service api * update api.ts --- src/api/api.ts | 2 -- src/components/service.tsx | 4 +-- src/routes/service.tsx | 73 +++++++++++++++++++------------------- src/types/api.ts | 9 ++++- 4 files changed, 46 insertions(+), 42 deletions(-) diff --git a/src/api/api.ts b/src/api/api.ts index 357df1b..d093c94 100644 --- a/src/api/api.ts +++ b/src/api/api.ts @@ -1,5 +1,3 @@ -import { useMainStore } from "@/hooks/useMainStore"; - interface CommonResponse { success: boolean; error: string; diff --git a/src/components/service.tsx b/src/components/service.tsx index e260161..ad1a0f9 100644 --- a/src/components/service.tsx +++ b/src/components/service.tsx @@ -29,7 +29,7 @@ import { ScrollArea } from "@/components/ui/scroll-area" import { useForm } from "react-hook-form" import { z } from "zod" import { zodResolver } from "@hookform/resolvers/zod" -import { ModelService, ModelServiceResponse } from "@/types" +import { ModelService } from "@/types" import { createService, updateService } from "@/api/service" import { Checkbox } from "@/components/ui/checkbox" import { Label } from "@/components/ui/label" @@ -48,7 +48,7 @@ import { useTranslation } from "react-i18next"; interface ServiceCardProps { data?: ModelService; - mutate: KeyedMutator; + mutate: KeyedMutator; } const serviceFormSchema = z.object({ diff --git a/src/routes/service.tsx b/src/routes/service.tsx index b5c2b93..8b3e84c 100644 --- a/src/routes/service.tsx +++ b/src/routes/service.tsx @@ -9,10 +9,9 @@ import { TableHeader, TableRow, } from "@/components/ui/table"; -import { ModelServiceResponse, ModelServiceResponseItem as Service } from "@/types"; +import { ModelService as Service } from "@/types"; import { ColumnDef, flexRender, getCoreRowModel, useReactTable } from "@tanstack/react-table"; import useSWR from "swr"; -import { conv } from "@/lib/utils"; import { useEffect, useMemo } from "react"; import { serviceTypes } from "@/types"; import { ActionButtonGroup } from "@/components/action-button-group"; @@ -24,8 +23,8 @@ import { useTranslation } from "react-i18next"; export default function ServicePage() { const { t } = useTranslation(); - const { data, mutate, error, isLoading } = useSWR( - "/api/v1/service", + const { data, mutate, error, isLoading } = useSWR( + "/api/v1/service/list", swrFetcher ); @@ -62,33 +61,33 @@ export default function ServicePage() { }, { header: "ID", - accessorKey: "service.id", - accessorFn: (row) => row.service.id, + accessorKey: "id", + accessorFn: (row) => row.id, }, { header: t("Name"), - accessorFn: (row) => row.service.name, - accessorKey: "service.name", + accessorFn: (row) => row.name, + accessorKey: "name", cell: ({ row }) => { const s = row.original; - return
{s.service.name}
; + return
{s.name}
; }, }, { header: t("Target"), - accessorFn: (row) => row.service.target, - accessorKey: "service.target", + accessorFn: (row) => row.target, + accessorKey: "target", cell: ({ row }) => { const s = row.original; - return
{s.service.target}
; + return
{s.target}
; }, }, { header: t("Coverage"), - accessorKey: "service.cover", - accessorFn: (row) => row.service.cover, + accessorKey: "cover", + accessorFn: (row) => row.cover, cell: ({ row }) => { - const s = row.original.service; + const s = row.original; return (
{(() => { @@ -107,39 +106,39 @@ export default function ServicePage() { }, { header: t("SpecificServers"), - accessorKey: "service.skipServers", - accessorFn: (row) => Object.keys(row.service.skip_servers ?? {}), + accessorKey: "skipServers", + accessorFn: (row) => Object.keys(row.skip_servers ?? {}), }, { header: t("Type"), - accessorKey: "service.type", - accessorFn: (row) => row.service.type, - cell: ({ row }) => serviceTypes[row.original.service.type] || "", + accessorKey: "type", + accessorFn: (row) => row.type, + cell: ({ row }) => serviceTypes[row.original.type] || "", }, { header: t("Interval"), - accessorKey: "service.duration", - accessorFn: (row) => row.service.duration, + accessorKey: "duration", + accessorFn: (row) => row.duration, }, { header: t("NotifierGroupID"), - accessorKey: "service.ngroup", - accessorFn: (row) => row.service.notification_group_id, + accessorKey: "ngroup", + accessorFn: (row) => row.notification_group_id, }, { header: t("Trigger"), - accessorKey: "service.triggerTask", - accessorFn: (row) => row.service.enable_trigger_task ?? false, + accessorKey: "triggerTask", + accessorFn: (row) => row.enable_trigger_task ?? false, }, { header: t("TasksToTriggerOnAlert"), - accessorKey: "service.failTriggerTasks", - accessorFn: (row) => row.service.fail_trigger_tasks, + accessorKey: "failTriggerTasks", + accessorFn: (row) => row.fail_trigger_tasks, }, { header: t("TasksToTriggerAfterRecovery"), - accessorKey: "service.recoverTriggerTasks", - accessorFn: (row) => row.service.recover_trigger_tasks, + accessorKey: "recoverTriggerTasks", + accessorFn: (row) => row.recover_trigger_tasks, }, { id: "actions", @@ -149,21 +148,21 @@ export default function ServicePage() { return ( - + ); }, }, ]; - const dataArr = useMemo(() => { - return conv.recordToArr(data?.services ?? {}); - }, [data?.services]); + const dataCache = useMemo(() => { + return data ?? []; + }, [data]); const table = useReactTable({ - data: dataArr, + data: dataCache, columns, getCoreRowModel: getCoreRowModel(), }); @@ -178,7 +177,7 @@ export default function ServicePage() { className="flex-2 flex ml-auto gap-2" delete={{ fn: deleteService, - id: selectedRows.map((r) => r.original.service.id), + id: selectedRows.map((r) => r.original.id), mutate: mutate, }} > diff --git a/src/types/api.ts b/src/types/api.ts index a55f77a..ddfa861 100644 --- a/src/types/api.ts +++ b/src/types/api.ts @@ -63,6 +63,12 @@ export interface GithubComNezhahqNezhaModelCommonResponseArrayModelServerGroupRe success: boolean; } +export interface GithubComNezhahqNezhaModelCommonResponseArrayModelService { + data: ModelService[]; + error: string; + success: boolean; +} + export interface GithubComNezhahqNezhaModelCommonResponseArrayModelServiceInfos { data: ModelServiceInfos[]; error: string; @@ -555,7 +561,7 @@ export interface ModelServiceResponseItem { current_up: number; delay: number[]; down: number[]; - service: ModelService; + service_name: string; total_down: number; total_up: number; up: number[]; @@ -604,6 +610,7 @@ export interface ModelSettingResponse { jwt_secret_key: string; /** 系统语言,默认 zh_CN */ language: string; + listen_host: string; listen_port: number; /** 时区,默认为 Asia/Shanghai */ location: string;