37 lines
963 B
TypeScript
37 lines
963 B
TypeScript
import { root } from '@repo/core'
|
|
import { TemplateController } from '@repo/sdk'
|
|
import { useCallback, useState } from 'react'
|
|
|
|
import { type ApiError } from '@/lib/types'
|
|
import { handleError } from './use-error'
|
|
|
|
export const useRerunGeneration = () => {
|
|
const [loading, setLoading] = useState(false)
|
|
const [error, setError] = useState<ApiError | null>(null)
|
|
|
|
const rerun = useCallback(async (generationId: string): Promise<{ generationId?: string; error?: ApiError }> => {
|
|
setLoading(true)
|
|
setError(null)
|
|
|
|
const template = root.get(TemplateController)
|
|
const { data, error } = await handleError(async () => await template.rerun({
|
|
generationId,
|
|
}))
|
|
|
|
setLoading(false)
|
|
|
|
if (error) {
|
|
setError(error)
|
|
return { error }
|
|
}
|
|
|
|
return { generationId: data?.generationId }
|
|
}, [])
|
|
|
|
return {
|
|
rerun,
|
|
loading,
|
|
error,
|
|
}
|
|
}
|