Skip to content

Commit 2ad9671

Browse files
authored
update service api (#29)
* update service api * update api.ts
1 parent 1cc36b9 commit 2ad9671

File tree

4 files changed

+46
-42
lines changed

4 files changed

+46
-42
lines changed

src/api/api.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { useMainStore } from "@/hooks/useMainStore";
2-
31
interface CommonResponse<T> {
42
success: boolean;
53
error: string;

src/components/service.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { ScrollArea } from "@/components/ui/scroll-area"
2929
import { useForm } from "react-hook-form"
3030
import { z } from "zod"
3131
import { zodResolver } from "@hookform/resolvers/zod"
32-
import { ModelService, ModelServiceResponse } from "@/types"
32+
import { ModelService } from "@/types"
3333
import { createService, updateService } from "@/api/service"
3434
import { Checkbox } from "@/components/ui/checkbox"
3535
import { Label } from "@/components/ui/label"
@@ -48,7 +48,7 @@ import { useTranslation } from "react-i18next";
4848

4949
interface ServiceCardProps {
5050
data?: ModelService;
51-
mutate: KeyedMutator<ModelServiceResponse>;
51+
mutate: KeyedMutator<ModelService[]>;
5252
}
5353

5454
const serviceFormSchema = z.object({

src/routes/service.tsx

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ import {
99
TableHeader,
1010
TableRow,
1111
} from "@/components/ui/table";
12-
import { ModelServiceResponse, ModelServiceResponseItem as Service } from "@/types";
12+
import { ModelService as Service } from "@/types";
1313
import { ColumnDef, flexRender, getCoreRowModel, useReactTable } from "@tanstack/react-table";
1414
import useSWR from "swr";
15-
import { conv } from "@/lib/utils";
1615
import { useEffect, useMemo } from "react";
1716
import { serviceTypes } from "@/types";
1817
import { ActionButtonGroup } from "@/components/action-button-group";
@@ -24,8 +23,8 @@ import { useTranslation } from "react-i18next";
2423

2524
export default function ServicePage() {
2625
const { t } = useTranslation();
27-
const { data, mutate, error, isLoading } = useSWR<ModelServiceResponse>(
28-
"/api/v1/service",
26+
const { data, mutate, error, isLoading } = useSWR<Service[]>(
27+
"/api/v1/service/list",
2928
swrFetcher
3029
);
3130

@@ -62,33 +61,33 @@ export default function ServicePage() {
6261
},
6362
{
6463
header: "ID",
65-
accessorKey: "service.id",
66-
accessorFn: (row) => row.service.id,
64+
accessorKey: "id",
65+
accessorFn: (row) => row.id,
6766
},
6867
{
6968
header: t("Name"),
70-
accessorFn: (row) => row.service.name,
71-
accessorKey: "service.name",
69+
accessorFn: (row) => row.name,
70+
accessorKey: "name",
7271
cell: ({ row }) => {
7372
const s = row.original;
74-
return <div className="max-w-24 whitespace-normal break-words">{s.service.name}</div>;
73+
return <div className="max-w-24 whitespace-normal break-words">{s.name}</div>;
7574
},
7675
},
7776
{
7877
header: t("Target"),
79-
accessorFn: (row) => row.service.target,
80-
accessorKey: "service.target",
78+
accessorFn: (row) => row.target,
79+
accessorKey: "target",
8180
cell: ({ row }) => {
8281
const s = row.original;
83-
return <div className="max-w-24 whitespace-normal break-words">{s.service.target}</div>;
82+
return <div className="max-w-24 whitespace-normal break-words">{s.target}</div>;
8483
},
8584
},
8685
{
8786
header: t("Coverage"),
88-
accessorKey: "service.cover",
89-
accessorFn: (row) => row.service.cover,
87+
accessorKey: "cover",
88+
accessorFn: (row) => row.cover,
9089
cell: ({ row }) => {
91-
const s = row.original.service;
90+
const s = row.original;
9291
return (
9392
<div className="max-w-48 whitespace-normal break-words">
9493
{(() => {
@@ -107,39 +106,39 @@ export default function ServicePage() {
107106
},
108107
{
109108
header: t("SpecificServers"),
110-
accessorKey: "service.skipServers",
111-
accessorFn: (row) => Object.keys(row.service.skip_servers ?? {}),
109+
accessorKey: "skipServers",
110+
accessorFn: (row) => Object.keys(row.skip_servers ?? {}),
112111
},
113112
{
114113
header: t("Type"),
115-
accessorKey: "service.type",
116-
accessorFn: (row) => row.service.type,
117-
cell: ({ row }) => serviceTypes[row.original.service.type] || "",
114+
accessorKey: "type",
115+
accessorFn: (row) => row.type,
116+
cell: ({ row }) => serviceTypes[row.original.type] || "",
118117
},
119118
{
120119
header: t("Interval"),
121-
accessorKey: "service.duration",
122-
accessorFn: (row) => row.service.duration,
120+
accessorKey: "duration",
121+
accessorFn: (row) => row.duration,
123122
},
124123
{
125124
header: t("NotifierGroupID"),
126-
accessorKey: "service.ngroup",
127-
accessorFn: (row) => row.service.notification_group_id,
125+
accessorKey: "ngroup",
126+
accessorFn: (row) => row.notification_group_id,
128127
},
129128
{
130129
header: t("Trigger"),
131-
accessorKey: "service.triggerTask",
132-
accessorFn: (row) => row.service.enable_trigger_task ?? false,
130+
accessorKey: "triggerTask",
131+
accessorFn: (row) => row.enable_trigger_task ?? false,
133132
},
134133
{
135134
header: t("TasksToTriggerOnAlert"),
136-
accessorKey: "service.failTriggerTasks",
137-
accessorFn: (row) => row.service.fail_trigger_tasks,
135+
accessorKey: "failTriggerTasks",
136+
accessorFn: (row) => row.fail_trigger_tasks,
138137
},
139138
{
140139
header: t("TasksToTriggerAfterRecovery"),
141-
accessorKey: "service.recoverTriggerTasks",
142-
accessorFn: (row) => row.service.recover_trigger_tasks,
140+
accessorKey: "recoverTriggerTasks",
141+
accessorFn: (row) => row.recover_trigger_tasks,
143142
},
144143
{
145144
id: "actions",
@@ -149,21 +148,21 @@ export default function ServicePage() {
149148
return (
150149
<ActionButtonGroup
151150
className="flex gap-2"
152-
delete={{ fn: deleteService, id: s.service.id, mutate: mutate }}
151+
delete={{ fn: deleteService, id: s.id, mutate: mutate }}
153152
>
154-
<ServiceCard mutate={mutate} data={s.service} />
153+
<ServiceCard mutate={mutate} data={s} />
155154
</ActionButtonGroup>
156155
);
157156
},
158157
},
159158
];
160159

161-
const dataArr = useMemo(() => {
162-
return conv.recordToArr(data?.services ?? {});
163-
}, [data?.services]);
160+
const dataCache = useMemo(() => {
161+
return data ?? [];
162+
}, [data]);
164163

165164
const table = useReactTable({
166-
data: dataArr,
165+
data: dataCache,
167166
columns,
168167
getCoreRowModel: getCoreRowModel(),
169168
});
@@ -178,7 +177,7 @@ export default function ServicePage() {
178177
className="flex-2 flex ml-auto gap-2"
179178
delete={{
180179
fn: deleteService,
181-
id: selectedRows.map((r) => r.original.service.id),
180+
id: selectedRows.map((r) => r.original.id),
182181
mutate: mutate,
183182
}}
184183
>

src/types/api.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ export interface GithubComNezhahqNezhaModelCommonResponseArrayModelServerGroupRe
6363
success: boolean;
6464
}
6565

66+
export interface GithubComNezhahqNezhaModelCommonResponseArrayModelService {
67+
data: ModelService[];
68+
error: string;
69+
success: boolean;
70+
}
71+
6672
export interface GithubComNezhahqNezhaModelCommonResponseArrayModelServiceInfos {
6773
data: ModelServiceInfos[];
6874
error: string;
@@ -555,7 +561,7 @@ export interface ModelServiceResponseItem {
555561
current_up: number;
556562
delay: number[];
557563
down: number[];
558-
service: ModelService;
564+
service_name: string;
559565
total_down: number;
560566
total_up: number;
561567
up: number[];
@@ -604,6 +610,7 @@ export interface ModelSettingResponse {
604610
jwt_secret_key: string;
605611
/** 系统语言,默认 zh_CN */
606612
language: string;
613+
listen_host: string;
607614
listen_port: number;
608615
/** 时区,默认为 Asia/Shanghai */
609616
location: string;

0 commit comments

Comments
 (0)