mirror of
https://github.com/NginxProxyManager/nginx-proxy-manager.git
synced 2025-11-04 17:35:15 +00:00
Streams modal
This commit is contained in:
@@ -9,6 +9,7 @@ export * from "./useHealth";
|
||||
export * from "./useHostReport";
|
||||
export * from "./useProxyHosts";
|
||||
export * from "./useRedirectionHosts";
|
||||
export * from "./useStream";
|
||||
export * from "./useStreams";
|
||||
export * from "./useTheme";
|
||||
export * from "./useUser";
|
||||
|
||||
54
frontend/src/hooks/useStream.ts
Normal file
54
frontend/src/hooks/useStream.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { createStream, getStream, type Stream, updateStream } from "src/api/backend";
|
||||
|
||||
const fetchStream = (id: number | "new") => {
|
||||
if (id === "new") {
|
||||
return Promise.resolve({
|
||||
id: 0,
|
||||
createdOn: "",
|
||||
modifiedOn: "",
|
||||
ownerUserId: 0,
|
||||
tcpForwarding: true,
|
||||
udpForwarding: false,
|
||||
meta: {},
|
||||
enabled: true,
|
||||
certificateId: 0,
|
||||
} as Stream);
|
||||
}
|
||||
return getStream(id, ["owner"]);
|
||||
};
|
||||
|
||||
const useStream = (id: number | "new", options = {}) => {
|
||||
return useQuery<Stream, Error>({
|
||||
queryKey: ["stream", id],
|
||||
queryFn: () => fetchStream(id),
|
||||
staleTime: 60 * 1000, // 1 minute
|
||||
...options,
|
||||
});
|
||||
};
|
||||
|
||||
const useSetStream = () => {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: (values: Stream) => (values.id ? updateStream(values) : createStream(values)),
|
||||
onMutate: (values: Stream) => {
|
||||
if (!values.id) {
|
||||
return;
|
||||
}
|
||||
const previousObject = queryClient.getQueryData(["stream", values.id]);
|
||||
queryClient.setQueryData(["stream", values.id], (old: Stream) => ({
|
||||
...old,
|
||||
...values,
|
||||
}));
|
||||
return () => queryClient.setQueryData(["stream", values.id], previousObject);
|
||||
},
|
||||
onError: (_, __, rollback: any) => rollback(),
|
||||
onSuccess: async ({ id }: Stream) => {
|
||||
queryClient.invalidateQueries({ queryKey: ["stream", id] });
|
||||
queryClient.invalidateQueries({ queryKey: ["streams"] });
|
||||
queryClient.invalidateQueries({ queryKey: ["audit-logs"] });
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export { useStream, useSetStream };
|
||||
Reference in New Issue
Block a user