import { useQuery } from '@tanstack/react-query'; import { withBasePath } from '@/api/http-init'; import { keys } from '@/api/queryKeys'; import { GroupClientPageSchema, type GroupClientSelection } from '@/generated/zod'; import { HttpUtil } from '@/utils'; import { parseMsg } from '@/utils/zodValidate'; const PAGE_ENDPOINT = '/panel/api/clients/groups/clientPage'; export const GROUP_CLIENT_PAGE_SIZE = 100; export const MAX_GROUP_EXCLUSIONS = 1000; export type { GroupClientSelection }; export type GroupClientMode = 'add' | 'remove' | 'links'; export type GroupBulkScope = { group: string; scopeToken?: string }; type PageParams = { group: string; mode: GroupClientMode; page?: number; search?: string; freeze?: boolean; scopeSearch?: string; maxId?: number; }; export async function fetchGroupClients(params: PageParams, signal?: AbortSignal) { const response = await HttpUtil.get(PAGE_ENDPOINT, params, { signal, silent: true }); const result = parseMsg(response, GroupClientPageSchema, 'clients/groups/clientPage'); if (!result.success || !result.obj) throw new Error(result.msg); return result.obj; } export function useGroupClientPage(params: PageParams, enabled: boolean) { return useQuery({ queryKey: [...keys.clients.root(), 'groupClientPage', params], queryFn: ({ signal }) => fetchGroupClients(params, signal), enabled, gcTime: 0, retry: false, }); } export function groupSubscriptionExportPath(group: string) { return withBasePath( '/panel/api/clients/groups/subLinks/export?' + new URLSearchParams({ group }).toString(), ); }