1import { HostType } from "@marzneshin/modules/hosts";
2import { useQuery } from "@tanstack/react-query";
3import { fetch } from "@marzneshin/common/utils";
4import {
5 FetchEntityReturn,
6 UseEntityQueryProps,
7 EntitySidebarQueryKeyType
8} from "@marzneshin/libs/entity-table/hooks";
9
10export async function fetchHosts({ queryKey }: EntitySidebarQueryKeyType): FetchEntityReturn<HostType> {
11 const pagination = queryKey[3];
12 const primaryFilter = queryKey[4];
13 const filters = queryKey[6].filters;
14 return fetch(queryKey[1] !== undefined ? `/inbounds/${queryKey[1]}/hosts` : `/inbounds/hosts`, {
15 query: {
16 ...pagination,
17 ...filters,
18 remark: primaryFilter,
19 descending: queryKey[5].desc,
20 order_by: queryKey[5].sortBy,
21 }
22 }).then((result) => {
23 return {
24 entities: result.items,
25 pageCount: result.pages
26 };
27 });
28}
29
30export const HostsQueryFetchKey = "hosts";
31
32export const useHostsQuery = ({
33 page, size, sortBy = "created_at", desc = false, filters = {}, inboundId
34}: UseEntityQueryProps & { inboundId?: number }) => {
35 return useQuery({
36 queryKey: ["inbounds", inboundId, HostsQueryFetchKey, { page, size }, filters?.username ?? "", { sortBy, desc }, { filters }],
37 queryFn: fetchHosts,
38 initialData: { entities: [], pageCount: 0 }
39 })
40}