Skip to content

Commit

Permalink
update service api (#29)
Browse files Browse the repository at this point in the history
* update service api

* update api.ts
  • Loading branch information
uubulb authored Dec 5, 2024
1 parent 1cc36b9 commit 2ad9671
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 42 deletions.
2 changes: 0 additions & 2 deletions src/api/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { useMainStore } from "@/hooks/useMainStore";

interface CommonResponse<T> {
success: boolean;
error: string;
Expand Down
4 changes: 2 additions & 2 deletions src/components/service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -48,7 +48,7 @@ import { useTranslation } from "react-i18next";

interface ServiceCardProps {
data?: ModelService;
mutate: KeyedMutator<ModelServiceResponse>;
mutate: KeyedMutator<ModelService[]>;
}

const serviceFormSchema = z.object({
Expand Down
73 changes: 36 additions & 37 deletions src/routes/service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -24,8 +23,8 @@ import { useTranslation } from "react-i18next";

export default function ServicePage() {
const { t } = useTranslation();
const { data, mutate, error, isLoading } = useSWR<ModelServiceResponse>(
"/api/v1/service",
const { data, mutate, error, isLoading } = useSWR<Service[]>(
"/api/v1/service/list",
swrFetcher
);

Expand Down Expand Up @@ -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 <div className="max-w-24 whitespace-normal break-words">{s.service.name}</div>;
return <div className="max-w-24 whitespace-normal break-words">{s.name}</div>;
},
},
{
header: t("Target"),
accessorFn: (row) => row.service.target,
accessorKey: "service.target",
accessorFn: (row) => row.target,
accessorKey: "target",
cell: ({ row }) => {
const s = row.original;
return <div className="max-w-24 whitespace-normal break-words">{s.service.target}</div>;
return <div className="max-w-24 whitespace-normal break-words">{s.target}</div>;
},
},
{
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 (
<div className="max-w-48 whitespace-normal break-words">
{(() => {
Expand All @@ -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",
Expand All @@ -149,21 +148,21 @@ export default function ServicePage() {
return (
<ActionButtonGroup
className="flex gap-2"
delete={{ fn: deleteService, id: s.service.id, mutate: mutate }}
delete={{ fn: deleteService, id: s.id, mutate: mutate }}
>
<ServiceCard mutate={mutate} data={s.service} />
<ServiceCard mutate={mutate} data={s} />
</ActionButtonGroup>
);
},
},
];

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(),
});
Expand All @@ -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,
}}
>
Expand Down
9 changes: 8 additions & 1 deletion src/types/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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[];
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 2ad9671

Please sign in to comment.