/** Hooks for Authentik directory, access metadata, and messaging tabs. */ import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { fetchAuthentikAccessSummary, fetchAuthentikApplications, fetchAuthentikGroups, fetchAuthentikMessageStatus, fetchAuthentikUsers, sendAuthentikMessage, } from "../api/authentik"; export function useAuthentikUsers( serviceId: string, params: { search?: string; page?: number; page_size?: number }, ) { return useQuery({ queryKey: ["authentik", "users", serviceId, params], queryFn: () => fetchAuthentikUsers(serviceId, params), staleTime: 10_000, }); } export function useAuthentikAccessSummary( serviceId: string, params: { search?: string; page?: number; page_size?: number }, ) { return useQuery({ queryKey: ["authentik", "access-summary", serviceId, params], queryFn: () => fetchAuthentikAccessSummary(serviceId, params), staleTime: 10_000, }); } export function useAuthentikGroups(serviceId: string, limit = 100) { return useQuery({ queryKey: ["authentik", "groups", serviceId, limit], queryFn: () => fetchAuthentikGroups(serviceId, limit), staleTime: 30_000, }); } export function useAuthentikApplications(serviceId: string, limit = 100) { return useQuery({ queryKey: ["authentik", "applications", serviceId, limit], queryFn: () => fetchAuthentikApplications(serviceId, limit), staleTime: 30_000, }); } export function useSendAuthentikMessage(serviceId: string) { const queryClient = useQueryClient(); return useMutation({ mutationFn: (input: { recipient_emails: string[]; subject: string; html_body: string; }) => sendAuthentikMessage(serviceId, input), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["authentik", "message-status", serviceId], }); }, }); } export function useAuthentikMessageStatus(serviceId: string) { return useQuery({ queryKey: ["authentik", "message-status", serviceId], queryFn: () => fetchAuthentikMessageStatus(serviceId), refetchInterval: 5_000, staleTime: 0, }); }