feat: 对接 templateDetail 页面后端接口
- 创建 hooks/use-error.ts 统一错误处理 - 创建 hooks/use-template-detail.ts 获取模板详情 - 创建 hooks/use-template-generations.ts 获取模板生成记录 - 更新 hooks 使用 handleError 统一错误处理 - 优化 SearchResultsGrid 组件,复用 TemplateGeneration 类型 - 删除不必要的类型转换和重复代码 Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
52
hooks/use-template-detail.ts
Normal file
52
hooks/use-template-detail.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { root } from '@repo/core'
|
||||
import { TemplateController, type TemplateDetail } from '@repo/sdk'
|
||||
import { useCallback, useState } from 'react'
|
||||
|
||||
import { type ApiError } from '@/lib/types'
|
||||
|
||||
import { handleError } from './use-error'
|
||||
|
||||
interface UseTemplateDetailParams {
|
||||
id: string
|
||||
}
|
||||
|
||||
export const useTemplateDetail = () => {
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [error, setError] = useState<ApiError | null>(null)
|
||||
const [data, setData] = useState<TemplateDetail | undefined>()
|
||||
|
||||
const execute = useCallback(async (params: UseTemplateDetailParams) => {
|
||||
setLoading(true)
|
||||
setError(null)
|
||||
|
||||
const template = root.get(TemplateController)
|
||||
const { data, error } = await handleError(async () => await template.get(params.id))
|
||||
|
||||
if (error) {
|
||||
setError(error)
|
||||
setLoading(false)
|
||||
return { data: undefined, error }
|
||||
}
|
||||
|
||||
setData(data)
|
||||
setLoading(false)
|
||||
return { data, error: null }
|
||||
}, [])
|
||||
|
||||
const refetch = useCallback(
|
||||
(params: UseTemplateDetailParams) => {
|
||||
return execute(params)
|
||||
},
|
||||
[execute],
|
||||
)
|
||||
|
||||
return {
|
||||
data,
|
||||
loading,
|
||||
error,
|
||||
execute,
|
||||
refetch,
|
||||
}
|
||||
}
|
||||
|
||||
export type { TemplateDetail }
|
||||
Reference in New Issue
Block a user