feat: add retry functionality to use-template-actions hook
Add retry function to handle failed template operations by storing last params in ref and allowing retry without re-passing parameters. Includes comprehensive test coverage. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { root } from '@repo/core'
|
||||
import { type RunTemplateInput, TemplateController } from '@repo/sdk'
|
||||
import { useCallback, useState } from 'react'
|
||||
import { useCallback, useRef, useState } from 'react'
|
||||
|
||||
import { type ApiError } from '@/lib/types'
|
||||
import { handleError } from './use-error'
|
||||
@@ -8,10 +8,12 @@ import { handleError } from './use-error'
|
||||
export const useTemplateActions = () => {
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [error, setError] = useState<ApiError | null>(null)
|
||||
const lastParamsRef = useRef<RunTemplateInput | null>(null)
|
||||
|
||||
const runTemplate = useCallback(async (params: RunTemplateInput): Promise<{ generationId?: string; error?: ApiError }> => {
|
||||
setLoading(true)
|
||||
setError(null)
|
||||
lastParamsRef.current = params
|
||||
|
||||
const template = root.get(TemplateController)
|
||||
const { data, error } = await handleError(async () => await template.run({
|
||||
@@ -29,9 +31,17 @@ export const useTemplateActions = () => {
|
||||
return { generationId: data?.generationId }
|
||||
}, [])
|
||||
|
||||
const retry = useCallback(async (): Promise<{ generationId?: string; error?: ApiError }> => {
|
||||
if (!lastParamsRef.current) {
|
||||
return { error: { message: 'No previous operation to retry' } as ApiError }
|
||||
}
|
||||
return runTemplate(lastParamsRef.current)
|
||||
}, [runTemplate])
|
||||
|
||||
return {
|
||||
loading,
|
||||
error,
|
||||
runTemplate,
|
||||
retry,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user