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