137113cbe6
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
235 lines
7.1 KiB
TypeScript
235 lines
7.1 KiB
TypeScript
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"
|
|
import { api } from "@/api/client"
|
|
import { useAuth } from "@/auth/AuthContext"
|
|
import type { CreateAccountInput, CreateTemplateInput, ApplyRequest, Project, Schedule, CreateChannelInput } from "@/api/types"
|
|
|
|
function requireProjectId(project: Project | null): string {
|
|
if (!project) throw new Error("no active project")
|
|
return project.id
|
|
}
|
|
|
|
export function useAccounts() {
|
|
const { project } = useAuth()
|
|
return useQuery({
|
|
queryKey: ["accounts", project?.id],
|
|
queryFn: () => api.listAccounts(project!.id),
|
|
enabled: !!project,
|
|
})
|
|
}
|
|
export function useCreateAccount() {
|
|
const { project } = useAuth()
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (input: CreateAccountInput) => {
|
|
const pid = requireProjectId(project)
|
|
return api.createAccount(pid, input)
|
|
},
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ["accounts", project?.id] }),
|
|
})
|
|
}
|
|
export function useDeleteAccount() {
|
|
const { project } = useAuth()
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (id: string) => {
|
|
const pid = requireProjectId(project)
|
|
return api.deleteAccount(pid, id)
|
|
},
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ["accounts", project?.id] }),
|
|
})
|
|
}
|
|
|
|
export function useTemplates() {
|
|
const { project } = useAuth()
|
|
return useQuery({
|
|
queryKey: ["templates", project?.id],
|
|
queryFn: () => api.listTemplates(project!.id),
|
|
enabled: !!project,
|
|
})
|
|
}
|
|
export function useCreateTemplate() {
|
|
const { project } = useAuth()
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (input: CreateTemplateInput) => {
|
|
const pid = requireProjectId(project)
|
|
return api.createTemplate(pid, input)
|
|
},
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ["templates", project?.id] }),
|
|
})
|
|
}
|
|
export function useUpdateTemplate() {
|
|
const { project } = useAuth()
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: ({ id, input }: { id: string; input: CreateTemplateInput }) => {
|
|
const pid = requireProjectId(project)
|
|
return api.updateTemplate(pid, id, input)
|
|
},
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ["templates", project?.id] }),
|
|
})
|
|
}
|
|
export function useDeleteTemplate() {
|
|
const { project } = useAuth()
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (id: string) => {
|
|
const pid = requireProjectId(project)
|
|
return api.deleteTemplate(pid, id)
|
|
},
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ["templates", project?.id] }),
|
|
})
|
|
}
|
|
|
|
export function useDomains() {
|
|
const { project } = useAuth()
|
|
return useQuery({
|
|
queryKey: ["domains", project?.id],
|
|
queryFn: () => api.listDomains(project!.id),
|
|
enabled: !!project,
|
|
})
|
|
}
|
|
export function useImportZones() {
|
|
const { project } = useAuth()
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (accountId: string) => {
|
|
const pid = requireProjectId(project)
|
|
return api.importZones(pid, accountId)
|
|
},
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ["domains", project?.id] }),
|
|
})
|
|
}
|
|
export function useSetDomainTemplate() {
|
|
const { project } = useAuth()
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: ({ id, templateId }: { id: string; templateId: string | null }) => {
|
|
const pid = requireProjectId(project)
|
|
return api.setDomainTemplate(pid, id, templateId)
|
|
},
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ["domains", project?.id] }),
|
|
})
|
|
}
|
|
export function useDeleteDomain() {
|
|
const { project } = useAuth()
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (id: string) => {
|
|
const pid = requireProjectId(project)
|
|
return api.deleteDomain(pid, id)
|
|
},
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ["domains", project?.id] }),
|
|
})
|
|
}
|
|
export function useCheckDomain(id: string, enabled = true) {
|
|
const { project } = useAuth()
|
|
return useQuery({
|
|
queryKey: ["check", project?.id, id],
|
|
queryFn: () => api.checkDomain(project!.id, id),
|
|
enabled: !!project && !!id && enabled,
|
|
})
|
|
}
|
|
export function useZoneRecords(id: string, enabled = true) {
|
|
const { project } = useAuth()
|
|
return useQuery({
|
|
queryKey: ["zoneRecords", project?.id, id],
|
|
queryFn: () => api.zoneRecords(project!.id, id),
|
|
enabled: !!project && !!id && enabled,
|
|
})
|
|
}
|
|
export function useCreateTemplateFromZone() {
|
|
const { project } = useAuth()
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (id: string) => {
|
|
const pid = requireProjectId(project)
|
|
return api.templateFromZone(pid, id)
|
|
},
|
|
onSuccess: (_data, id) => {
|
|
qc.invalidateQueries({ queryKey: ["domains", project?.id] })
|
|
qc.invalidateQueries({ queryKey: ["zoneRecords", project?.id, id] })
|
|
qc.invalidateQueries({ queryKey: ["check", project?.id, id] })
|
|
},
|
|
})
|
|
}
|
|
export function useApplyDomain(id: string) {
|
|
const { project } = useAuth()
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (body: ApplyRequest) => {
|
|
const pid = requireProjectId(project)
|
|
return api.applyDomain(pid, id, body)
|
|
},
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ["check", project?.id, id] }),
|
|
})
|
|
}
|
|
export function useDomainHistory(id: string) {
|
|
const { project } = useAuth()
|
|
return useQuery({
|
|
queryKey: ["domainHistory", project?.id, id],
|
|
queryFn: () => api.domainHistory(project!.id, id),
|
|
enabled: !!project && !!id,
|
|
})
|
|
}
|
|
|
|
export function useSchedule() {
|
|
const { project } = useAuth()
|
|
return useQuery({
|
|
queryKey: ["schedule", project?.id],
|
|
queryFn: () => api.getSchedule(project!.id),
|
|
enabled: !!project,
|
|
})
|
|
}
|
|
export function useUpdateSchedule() {
|
|
const { project } = useAuth()
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (input: Schedule) => {
|
|
const pid = requireProjectId(project)
|
|
return api.putSchedule(pid, input)
|
|
},
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ["schedule", project?.id] }),
|
|
})
|
|
}
|
|
|
|
export function useChannels() {
|
|
const { project } = useAuth()
|
|
return useQuery({
|
|
queryKey: ["channels", project?.id],
|
|
queryFn: () => api.listChannels(project!.id),
|
|
enabled: !!project,
|
|
})
|
|
}
|
|
export function useCreateChannel() {
|
|
const { project } = useAuth()
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (input: CreateChannelInput) => {
|
|
const pid = requireProjectId(project)
|
|
return api.createChannel(pid, input)
|
|
},
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ["channels", project?.id] }),
|
|
})
|
|
}
|
|
export function useDeleteChannel() {
|
|
const { project } = useAuth()
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (id: string) => {
|
|
const pid = requireProjectId(project)
|
|
return api.deleteChannel(pid, id)
|
|
},
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ["channels", project?.id] }),
|
|
})
|
|
}
|
|
export function useTestChannel() {
|
|
const { project } = useAuth()
|
|
return useMutation({
|
|
mutationFn: (id: string) => {
|
|
const pid = requireProjectId(project)
|
|
return api.testChannel(pid, id)
|
|
},
|
|
})
|
|
}
|