API lib cleanup, 404 hosts WIP

This commit is contained in:
Jamie Curnow
2025-09-21 17:16:46 +10:00
parent 058f49ceea
commit 54e036276a
76 changed files with 921 additions and 544 deletions

View File

@@ -2,6 +2,7 @@ export * from "./useAccessLists";
export * from "./useAuditLog";
export * from "./useAuditLogs";
export * from "./useCertificates";
export * from "./useDeadHost";
export * from "./useDeadHosts";
export * from "./useHealth";
export * from "./useHostReport";

View File

@@ -0,0 +1,57 @@
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { createDeadHost, type DeadHost, getDeadHost, updateDeadHost } from "src/api/backend";
const fetchDeadHost = (id: number | "new") => {
if (id === "new") {
return Promise.resolve({
id: 0,
createdOn: "",
modifiedOn: "",
ownerUserId: 0,
domainNames: [],
certificateId: 0,
sslForced: false,
advancedConfig: "",
meta: {},
http2Support: false,
enabled: true,
hstsEnabled: false,
hstsSubdomains: false,
} as DeadHost);
}
return getDeadHost(id, ["owner"]);
};
const useDeadHost = (id: number | "new", options = {}) => {
return useQuery<DeadHost, Error>({
queryKey: ["dead-host", id],
queryFn: () => fetchDeadHost(id),
staleTime: 60 * 1000, // 1 minute
...options,
});
};
const useSetDeadHost = () => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (values: DeadHost) => (values.id ? updateDeadHost(values) : createDeadHost(values)),
onMutate: (values: DeadHost) => {
if (!values.id) {
return;
}
const previousObject = queryClient.getQueryData(["dead-host", values.id]);
queryClient.setQueryData(["dead-host", values.id], (old: DeadHost) => ({
...old,
...values,
}));
return () => queryClient.setQueryData(["dead-host", values.id], previousObject);
},
onError: (_, __, rollback: any) => rollback(),
onSuccess: async ({ id }: DeadHost) => {
queryClient.invalidateQueries({ queryKey: ["dead-host", id] });
queryClient.invalidateQueries({ queryKey: ["dead-hosts"] });
},
});
};
export { useDeadHost, useSetDeadHost };

View File

@@ -1,11 +1,11 @@
import { useQuery } from "@tanstack/react-query";
import { type DeadHost, type DeadHostExpansion, getDeadHosts } from "src/api/backend";
import { type DeadHost, getDeadHosts, type HostExpansion } from "src/api/backend";
const fetchDeadHosts = (expand?: DeadHostExpansion[]) => {
const fetchDeadHosts = (expand?: HostExpansion[]) => {
return getDeadHosts(expand);
};
const useDeadHosts = (expand?: DeadHostExpansion[], options = {}) => {
const useDeadHosts = (expand?: HostExpansion[], options = {}) => {
return useQuery<DeadHost[], Error>({
queryKey: ["dead-hosts", { expand }],
queryFn: () => fetchDeadHosts(expand),

View File

@@ -1,11 +1,11 @@
import { useQuery } from "@tanstack/react-query";
import { getRedirectionHosts, type RedirectionHost, type RedirectionHostExpansion } from "src/api/backend";
import { getRedirectionHosts, type HostExpansion, type RedirectionHost } from "src/api/backend";
const fetchRedirectionHosts = (expand?: RedirectionHostExpansion[]) => {
const fetchRedirectionHosts = (expand?: HostExpansion[]) => {
return getRedirectionHosts(expand);
};
const useRedirectionHosts = (expand?: RedirectionHostExpansion[], options = {}) => {
const useRedirectionHosts = (expand?: HostExpansion[], options = {}) => {
return useQuery<RedirectionHost[], Error>({
queryKey: ["redirection-hosts", { expand }],
queryFn: () => fetchRedirectionHosts(expand),

View File

@@ -1,11 +1,11 @@
import { useQuery } from "@tanstack/react-query";
import { getStreams, type Stream, type StreamExpansion } from "src/api/backend";
import { getStreams, type HostExpansion, type Stream } from "src/api/backend";
const fetchStreams = (expand?: StreamExpansion[]) => {
const fetchStreams = (expand?: HostExpansion[]) => {
return getStreams(expand);
};
const useStreams = (expand?: StreamExpansion[], options = {}) => {
const useStreams = (expand?: HostExpansion[], options = {}) => {
return useQuery<Stream[], Error>({
queryKey: ["streams", { expand }],
queryFn: () => fetchStreams(expand),

View File

@@ -15,7 +15,7 @@ const fetchUser = (id: number | string) => {
avatar: "",
} as User);
}
return getUser(id, { expand: "permissions" });
return getUser(id, ["permissions"]);
};
const useUser = (id: string | number, options = {}) => {