mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
Add cron-based scheduling with prompt templates integration (#164)
This commit is contained in:
1 parent
cbd0ee2958
commit
1e863c5fc5
63 files changed
+7797
-64
No files matched your search
@@ -0,0 +1,62 @@
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import type { CreatePromptTemplateRequest, UpdatePromptTemplateRequest } from '@opencode-manager/shared/types'
|
||||
import {
|
||||
listPromptTemplates,
|
||||
createPromptTemplate,
|
||||
updatePromptTemplate,
|
||||
deletePromptTemplate,
|
||||
} from '@/api/prompt-templates'
|
||||
import { showToast } from '@/lib/toast'
|
||||
|
||||
export function usePromptTemplates() {
|
||||
return useQuery({
|
||||
queryKey: ['prompt-templates'],
|
||||
queryFn: async () => {
|
||||
const response = await listPromptTemplates()
|
||||
return response.templates
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function useCreatePromptTemplate() {
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: (data: CreatePromptTemplateRequest) => createPromptTemplate(data).then(r => r.template),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['prompt-templates'] })
|
||||
showToast.success('Template created')
|
||||
},
|
||||
onError: (error) => {
|
||||
showToast.error(`Failed to create template: ${error instanceof Error ? error.message : String(error)}`)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function useUpdatePromptTemplate() {
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: ({ id, data }: { id: number; data: UpdatePromptTemplateRequest }) =>
|
||||
updatePromptTemplate(id, data).then(r => r.template),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['prompt-templates'] })
|
||||
showToast.success('Template updated')
|
||||
},
|
||||
onError: (error) => {
|
||||
showToast.error(`Failed to update template: ${error instanceof Error ? error.message : String(error)}`)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function useDeletePromptTemplate() {
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: (id: number) => deletePromptTemplate(id),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['prompt-templates'] })
|
||||
showToast.success('Template deleted')
|
||||
},
|
||||
onError: (error) => {
|
||||
showToast.error(`Failed to delete template: ${error instanceof Error ? error.message : String(error)}`)
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import type { CreateScheduleJobRequest, UpdateScheduleJobRequest } from '@opencode-manager/shared/types'
|
||||
import {
|
||||
cancelRepoScheduleRun,
|
||||
createRepoSchedule,
|
||||
deleteRepoSchedule,
|
||||
getRepoSchedule,
|
||||
getRepoScheduleRun,
|
||||
listRepoScheduleRuns,
|
||||
listRepoSchedules,
|
||||
runRepoSchedule,
|
||||
updateRepoSchedule,
|
||||
} from '@/api/schedules'
|
||||
import { showToast } from '@/lib/toast'
|
||||
|
||||
export function useRepoSchedules(repoId: number | undefined) {
|
||||
return useQuery({
|
||||
queryKey: ['repo-schedules', repoId],
|
||||
queryFn: async () => {
|
||||
const response = await listRepoSchedules(repoId!)
|
||||
return response.jobs
|
||||
},
|
||||
enabled: repoId !== undefined,
|
||||
refetchInterval: 5000,
|
||||
})
|
||||
}
|
||||
|
||||
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(repoId: number | undefined) {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (data: CreateScheduleJobRequest) => {
|
||||
const response = await createRepoSchedule(repoId!, data)
|
||||
return response.job
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['repo-schedules', repoId] })
|
||||
showToast.success('Schedule created')
|
||||
},
|
||||
onError: (error) => {
|
||||
showToast.error(`Failed to create schedule: ${error instanceof Error ? error.message : String(error)}`)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function useUpdateRepoSchedule(repoId: number | undefined) {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({ jobId, data }: { jobId: number; data: UpdateScheduleJobRequest }) => {
|
||||
const response = await updateRepoSchedule(repoId!, jobId, data)
|
||||
return response.job
|
||||
},
|
||||
onSuccess: (_, variables) => {
|
||||
queryClient.invalidateQueries({ queryKey: ['repo-schedules', repoId] })
|
||||
queryClient.invalidateQueries({ queryKey: ['repo-schedule', repoId, variables.jobId] })
|
||||
showToast.success('Schedule updated')
|
||||
},
|
||||
onError: (error) => {
|
||||
showToast.error(`Failed to update schedule: ${error instanceof Error ? error.message : String(error)}`)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function useDeleteRepoSchedule(repoId: number | undefined) {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (jobId: number) => deleteRepoSchedule(repoId!, jobId),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['repo-schedules', repoId] })
|
||||
showToast.success('Schedule deleted')
|
||||
},
|
||||
onError: (error) => {
|
||||
showToast.error(`Failed to delete schedule: ${error instanceof Error ? error.message : String(error)}`)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function useRunRepoSchedule(repoId: number | undefined) {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (jobId: number) => {
|
||||
const response = await runRepoSchedule(repoId!, jobId)
|
||||
return response.run
|
||||
},
|
||||
onSuccess: (run) => {
|
||||
queryClient.invalidateQueries({ queryKey: ['repo-schedules', repoId] })
|
||||
queryClient.invalidateQueries({ queryKey: ['repo-schedule-runs', repoId, run.jobId] })
|
||||
queryClient.invalidateQueries({ queryKey: ['repo-schedule', repoId, run.jobId] })
|
||||
queryClient.invalidateQueries({ queryKey: ['repo-schedule-run', repoId, run.jobId, run.id] })
|
||||
showToast.success(run.status === 'running' ? 'Schedule started' : 'Schedule run completed')
|
||||
},
|
||||
onError: (error) => {
|
||||
showToast.error(`Failed to run schedule: ${error instanceof Error ? error.message : String(error)}`)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function useCancelRepoScheduleRun(repoId: number | undefined) {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({ jobId, runId }: { jobId: number; runId: number }) => {
|
||||
const response = await cancelRepoScheduleRun(repoId!, jobId, runId)
|
||||
return response.run
|
||||
},
|
||||
onSuccess: (run) => {
|
||||
queryClient.invalidateQueries({ queryKey: ['repo-schedules', repoId] })
|
||||
queryClient.invalidateQueries({ queryKey: ['repo-schedule-runs', repoId, run.jobId] })
|
||||
queryClient.invalidateQueries({ queryKey: ['repo-schedule', repoId, run.jobId] })
|
||||
queryClient.invalidateQueries({ queryKey: ['repo-schedule-run', repoId, run.jobId, run.id] })
|
||||
showToast.success('Schedule run cancelled')
|
||||
},
|
||||
onError: (error) => {
|
||||
showToast.error(`Failed to cancel schedule run: ${error instanceof Error ? error.message : String(error)}`)
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user