// Copyright (c) 2026 Doforu // Copyright (c) 2026 Cloudflare, Inc. // Licensed under the Apache 2.0 license found in the LICENSE-APACHE file or at: // https://opensource.org/licenses/Apache-2.0 // Modifications Copyright (c) 2026 Doforu, distributed under the AGPL-3.0-only (see LICENSE). import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import api, { type CreateDomainResponse, type DnsRecord } from "~/services/api"; import type { Domain } from "~/types"; import { queryKeys } from "./keys"; export function useDomains() { return useQuery({ queryKey: queryKeys.domains.all, queryFn: () => api.domains.list() as Promise, }); } export function useCreateDomain() { const qc = useQueryClient(); return useMutation }>({ mutationFn: (data) => api.domains.create(data) as Promise, onSettled: () => { qc.invalidateQueries({ queryKey: queryKeys.domains.all }); }, }); } export function useDeleteDomain() { const qc = useQueryClient(); return useMutation({ mutationFn: (id: string) => api.domains.delete(id), onSuccess: () => { qc.invalidateQueries({ queryKey: queryKeys.domains.all }); qc.invalidateQueries({ queryKey: queryKeys.mailboxes.all }); qc.invalidateQueries({ queryKey: queryKeys.config }); }, }); } export function useSetCatchAll() { const qc = useQueryClient(); return useMutation({ mutationFn: ({ domainId, catchAllMailbox }: { domainId: string; catchAllMailbox: string | null }) => api.domains.setCatchAll(domainId, catchAllMailbox), onSuccess: () => { qc.invalidateQueries({ queryKey: queryKeys.domains.all }); }, }); } export function useUpdateDomainApiKey() { const qc = useQueryClient(); return useMutation({ mutationFn: ({ domainId, apiKey }) => api.domains.updateApiKey(domainId, apiKey) as Promise, onSuccess: () => { qc.invalidateQueries({ queryKey: queryKeys.domains.all }); }, }); }