65 lines
1.8 KiB
TypeScript
65 lines
1.8 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
|
import { api } from './client'
|
|
|
|
export interface AlertItem {
|
|
id: number
|
|
device_id: number
|
|
device_ip: string | null
|
|
device_hostname: string | null
|
|
object_id: number | null
|
|
object_name: string | null
|
|
status: 'open' | 'acknowledged' | 'resolved'
|
|
message: string
|
|
created_at: string
|
|
acknowledged_at: string | null
|
|
acknowledged_by_username: string | null
|
|
resolved_at: string | null
|
|
}
|
|
|
|
export const useAlerts = (params?: {
|
|
status?: string
|
|
object_id?: number
|
|
device_id?: number
|
|
limit?: number
|
|
skip?: number
|
|
refetchInterval?: number
|
|
}) => {
|
|
const { refetchInterval, ...queryParams } = params ?? {}
|
|
return useQuery({
|
|
queryKey: ['alerts', queryParams],
|
|
queryFn: () =>
|
|
api.get<AlertItem[]>('/api/v1/alerts', { params: queryParams }).then((r) => r.data),
|
|
refetchInterval: refetchInterval ?? false,
|
|
})
|
|
}
|
|
|
|
export const useAcknowledgeAlert = () => {
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (id: number) =>
|
|
api.post<AlertItem>(`/api/v1/alerts/${id}/acknowledge`).then((r) => r.data),
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ['alerts'] }),
|
|
})
|
|
}
|
|
|
|
export const useResolveAlert = () => {
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (id: number) =>
|
|
api.post<AlertItem>(`/api/v1/alerts/${id}/resolve`).then((r) => r.data),
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ['alerts'] }),
|
|
})
|
|
}
|
|
|
|
export const useAcknowledgeByObject = () => {
|
|
const qc = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (objectId: number) =>
|
|
api
|
|
.post<AlertItem[]>(`/api/v1/alerts/acknowledge-by-object`, null, {
|
|
params: { object_id: objectId },
|
|
})
|
|
.then((r) => r.data),
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ['alerts'] }),
|
|
})
|
|
}
|