mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
256 lines
9.3 KiB
TypeScript
256 lines
9.3 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
|
import type { CreateScheduleJobRequest, UpdateScheduleJobRequest } from '@opencode-manager/shared/types'
|
|
import {
|
|
cancelRepoScheduleRun,
|
|
clearRepoScheduleRuns,
|
|
createRepoSchedule,
|
|
deleteRepoSchedule,
|
|
deleteRepoScheduleRun,
|
|
getRepoSchedule,
|
|
getRepoScheduleRun,
|
|
listAllScheduleRuns,
|
|
listAllSchedules,
|
|
listRepoScheduleRuns,
|
|
listUnreadScheduleRuns,
|
|
markAllScheduleRunsViewed,
|
|
markScheduleRunViewed,
|
|
runRepoSchedule,
|
|
updateRepoSchedule,
|
|
} from '@/api/schedules'
|
|
import { showToast } from '@/lib/toast'
|
|
import type { ListAllRunsParams, ScheduleJobWithRepo, ScheduleRunWithContext } from '@/api/schedules'
|
|
|
|
export const UNREAD_SCHEDULE_RUNS_QUERY_KEY = ['schedule-runs-unread'] as const
|
|
|
|
export function useAllSchedules() {
|
|
return useQuery({
|
|
queryKey: ['all-schedules'],
|
|
queryFn: async () => {
|
|
const response = await listAllSchedules()
|
|
return response.jobs as ScheduleJobWithRepo[]
|
|
},
|
|
refetchInterval: 10000,
|
|
})
|
|
}
|
|
|
|
export function useAllScheduleRuns(params: ListAllRunsParams, enabled: boolean = true) {
|
|
return useQuery({
|
|
queryKey: ['all-schedule-runs', params],
|
|
queryFn: async () => {
|
|
const response = await listAllScheduleRuns(params)
|
|
return response.runs as ScheduleRunWithContext[]
|
|
},
|
|
enabled,
|
|
refetchInterval: 5000,
|
|
})
|
|
}
|
|
|
|
export function useUnreadScheduleRuns() {
|
|
return useQuery({
|
|
queryKey: UNREAD_SCHEDULE_RUNS_QUERY_KEY,
|
|
queryFn: () => listUnreadScheduleRuns(),
|
|
refetchInterval: 10000,
|
|
refetchOnWindowFocus: true,
|
|
})
|
|
}
|
|
|
|
export function useMarkScheduleRunViewed() {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: (runId: number) => markScheduleRunViewed(runId),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: UNREAD_SCHEDULE_RUNS_QUERY_KEY })
|
|
queryClient.invalidateQueries({ queryKey: ['all-schedules'] })
|
|
queryClient.invalidateQueries({ queryKey: ['all-schedule-runs'] })
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useMarkAllScheduleRunsViewed() {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: () => markAllScheduleRunsViewed(),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: UNREAD_SCHEDULE_RUNS_QUERY_KEY })
|
|
queryClient.invalidateQueries({ queryKey: ['all-schedules'] })
|
|
queryClient.invalidateQueries({ queryKey: ['all-schedule-runs'] })
|
|
showToast.success('All reports marked as read')
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useRepoSchedule(repoId: number | undefined, jobId: number | null) {
|
|
return useQuery({
|
|
queryKey: ['repo-schedule', repoId, jobId],
|
|
queryFn: async () => {
|
|
const response = await getRepoSchedule(repoId!, jobId!)
|
|
return response.job
|
|
},
|
|
enabled: repoId !== undefined && jobId !== null,
|
|
refetchInterval: jobId !== null ? 5000 : false,
|
|
})
|
|
}
|
|
|
|
export function useRepoScheduleRuns(repoId: number | undefined, jobId: number | null, limit: number = 20) {
|
|
return useQuery({
|
|
queryKey: ['repo-schedule-runs', repoId, jobId, limit],
|
|
queryFn: async () => {
|
|
const response = await listRepoScheduleRuns(repoId!, jobId!, limit)
|
|
return response.runs
|
|
},
|
|
enabled: repoId !== undefined && jobId !== null,
|
|
refetchInterval: jobId !== null ? 5000 : false,
|
|
})
|
|
}
|
|
|
|
export function useRepoScheduleRun(repoId: number | undefined, jobId: number | null, runId: number | null) {
|
|
return useQuery({
|
|
queryKey: ['repo-schedule-run', repoId, jobId, runId],
|
|
queryFn: async () => {
|
|
const response = await getRepoScheduleRun(repoId!, jobId!, runId!)
|
|
return response.run
|
|
},
|
|
enabled: repoId !== undefined && jobId !== null && runId !== null,
|
|
refetchInterval: runId !== null ? 5000 : false,
|
|
})
|
|
}
|
|
|
|
export function useCreateRepoSchedule() {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: async ({ repoId, data }: { repoId: number; data: CreateScheduleJobRequest }) => {
|
|
const response = await createRepoSchedule(repoId, data)
|
|
return response.job
|
|
},
|
|
onSuccess: (_data, variables) => {
|
|
queryClient.invalidateQueries({ queryKey: ['repo-schedules', variables.repoId] })
|
|
queryClient.invalidateQueries({ queryKey: ['all-schedules'] })
|
|
showToast.success('Schedule created')
|
|
},
|
|
onError: (error: unknown) => {
|
|
showToast.error(`Failed to create schedule: ${error instanceof Error ? error.message : String(error)}`)
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useUpdateRepoSchedule() {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: async ({ repoId, jobId, data }: { repoId: number; jobId: number; data: UpdateScheduleJobRequest }) => {
|
|
const response = await updateRepoSchedule(repoId, jobId, data)
|
|
return response.job
|
|
},
|
|
onSuccess: (_data, variables) => {
|
|
queryClient.invalidateQueries({ queryKey: ['repo-schedules', variables.repoId] })
|
|
queryClient.invalidateQueries({ queryKey: ['repo-schedule', variables.repoId, variables.jobId] })
|
|
queryClient.invalidateQueries({ queryKey: ['all-schedules'] })
|
|
showToast.success('Schedule updated')
|
|
},
|
|
onError: (error: unknown) => {
|
|
showToast.error(`Failed to update schedule: ${error instanceof Error ? error.message : String(error)}`)
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useDeleteRepoSchedule() {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: ({ repoId, jobId }: { repoId: number; jobId: number }) => {
|
|
return deleteRepoSchedule(repoId, jobId)
|
|
},
|
|
onSuccess: (_data, variables) => {
|
|
queryClient.invalidateQueries({ queryKey: ['repo-schedules', variables.repoId] })
|
|
queryClient.invalidateQueries({ queryKey: ['all-schedules'] })
|
|
showToast.success('Schedule deleted')
|
|
},
|
|
onError: (error: unknown) => {
|
|
showToast.error(`Failed to delete schedule: ${error instanceof Error ? error.message : String(error)}`)
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useRunRepoSchedule() {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: async ({ repoId, jobId }: { repoId: number; jobId: number }) => {
|
|
const response = await runRepoSchedule(repoId, jobId)
|
|
return response.run
|
|
},
|
|
onSuccess: (run, variables) => {
|
|
queryClient.invalidateQueries({ queryKey: ['repo-schedules', variables.repoId] })
|
|
queryClient.invalidateQueries({ queryKey: ['repo-schedule-runs', variables.repoId, run.jobId] })
|
|
queryClient.invalidateQueries({ queryKey: ['repo-schedule', variables.repoId, run.jobId] })
|
|
queryClient.invalidateQueries({ queryKey: ['repo-schedule-run', variables.repoId, run.jobId, run.id] })
|
|
queryClient.invalidateQueries({ queryKey: ['all-schedules'] })
|
|
showToast.success(run.status === 'running' ? 'Schedule started' : 'Schedule run completed')
|
|
},
|
|
onError: (error: unknown) => {
|
|
showToast.error(`Failed to run schedule: ${error instanceof Error ? error.message : String(error)}`)
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useCancelRepoScheduleRun() {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: async ({ repoId, jobId, runId }: { repoId: number; jobId: number; runId: number }) => {
|
|
const response = await cancelRepoScheduleRun(repoId, jobId, runId)
|
|
return response.run
|
|
},
|
|
onSuccess: (run, variables) => {
|
|
queryClient.invalidateQueries({ queryKey: ['repo-schedules', variables.repoId] })
|
|
queryClient.invalidateQueries({ queryKey: ['repo-schedule-runs', variables.repoId, run.jobId] })
|
|
queryClient.invalidateQueries({ queryKey: ['repo-schedule', variables.repoId, run.jobId] })
|
|
queryClient.invalidateQueries({ queryKey: ['repo-schedule-run', variables.repoId, run.jobId, run.id] })
|
|
queryClient.invalidateQueries({ queryKey: ['all-schedules'] })
|
|
showToast.success('Schedule run cancelled')
|
|
},
|
|
onError: (error) => {
|
|
showToast.error(`Failed to cancel schedule run: ${error instanceof Error ? error.message : String(error)}`)
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useClearRepoScheduleRuns() {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: async ({ repoId, jobId }: { repoId: number; jobId: number }) => {
|
|
return clearRepoScheduleRuns(repoId, jobId)
|
|
},
|
|
onSuccess: (result, variables) => {
|
|
queryClient.invalidateQueries({ queryKey: ['repo-schedule-runs', variables.repoId, variables.jobId] })
|
|
queryClient.invalidateQueries({ queryKey: ['all-schedule-runs'] })
|
|
showToast.success(result.cleared > 0 ? `Cleared ${result.cleared} run${result.cleared === 1 ? '' : 's'}` : 'No runs to clear')
|
|
},
|
|
onError: (error: unknown) => {
|
|
showToast.error(`Failed to clear run history: ${error instanceof Error ? error.message : String(error)}`)
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useDeleteRepoScheduleRun() {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: ({ repoId, jobId, runId }: { repoId: number; jobId: number; runId: number }) => {
|
|
return deleteRepoScheduleRun(repoId, jobId, runId)
|
|
},
|
|
onSuccess: (_data, variables) => {
|
|
queryClient.invalidateQueries({ queryKey: ['repo-schedule-runs', variables.repoId, variables.jobId] })
|
|
queryClient.invalidateQueries({ queryKey: ['all-schedule-runs'] })
|
|
showToast.success('Run deleted')
|
|
},
|
|
onError: (error: unknown) => {
|
|
showToast.error(`Failed to delete run: ${error instanceof Error ? error.message : String(error)}`)
|
|
},
|
|
})
|
|
}
|