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:
108
hooks/use-template-generations.ts
Normal file
108
hooks/use-template-generations.ts
Normal file
@@ -0,0 +1,108 @@
|
||||
import { root } from '@repo/core'
|
||||
import {
|
||||
TemplateGenerationController,
|
||||
type ListTemplateGenerationsInput,
|
||||
type ListTemplateGenerationsResult,
|
||||
type TemplateGeneration,
|
||||
} from '@repo/sdk'
|
||||
import { useCallback, useRef, useState } from 'react'
|
||||
|
||||
import { type ApiError } from '@/lib/types'
|
||||
|
||||
import { handleError } from './use-error'
|
||||
|
||||
const OWNER_ID = process.env.EXPO_PUBLIC_OWNER_ID || ''
|
||||
|
||||
export const useTemplateGenerations = () => {
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [loadingMore, setLoadingMore] = useState(false)
|
||||
const [error, setError] = useState<ApiError | null>(null)
|
||||
const [data, setData] = useState<ListTemplateGenerationsResult | undefined>()
|
||||
const currentPageRef = useRef(1)
|
||||
const hasMoreRef = useRef(true)
|
||||
|
||||
const execute = useCallback(async (params?: ListTemplateGenerationsInput) => {
|
||||
setLoading(true)
|
||||
setError(null)
|
||||
currentPageRef.current = params?.page || 1
|
||||
|
||||
const templateGeneration = root.get(TemplateGenerationController)
|
||||
const { data, error } = await handleError(
|
||||
async () =>
|
||||
await templateGeneration.list({
|
||||
page: params?.page || 1,
|
||||
limit: params?.limit || 20,
|
||||
...params,
|
||||
}),
|
||||
)
|
||||
if (error) {
|
||||
setError(error)
|
||||
setLoading(false)
|
||||
return { data: undefined, error }
|
||||
}
|
||||
|
||||
const generations = data?.data || []
|
||||
hasMoreRef.current = generations.length >= (params?.limit || 20)
|
||||
setData(data)
|
||||
setLoading(false)
|
||||
return { data, error: null }
|
||||
}, [])
|
||||
|
||||
const loadMore = useCallback(
|
||||
async (params?: Omit<ListTemplateGenerationsInput, 'page'>) => {
|
||||
if (loadingMore || loading || !hasMoreRef.current) return { data: undefined, error: null }
|
||||
|
||||
setLoadingMore(true)
|
||||
const nextPage = currentPageRef.current + 1
|
||||
|
||||
const templateGeneration = root.get(TemplateGenerationController)
|
||||
const { data: newData, error } = await handleError(
|
||||
async () =>
|
||||
await templateGeneration.list({
|
||||
page: nextPage,
|
||||
limit: params?.limit || 20,
|
||||
...params,
|
||||
}),
|
||||
)
|
||||
|
||||
if (error) {
|
||||
setLoadingMore(false)
|
||||
return { data: undefined, error }
|
||||
}
|
||||
|
||||
const newGenerations = newData?.data || []
|
||||
hasMoreRef.current = newGenerations.length >= (params?.limit || 20)
|
||||
currentPageRef.current = nextPage
|
||||
|
||||
setData((prev) => ({
|
||||
...newData,
|
||||
data: [...(prev?.data || []), ...newGenerations],
|
||||
}))
|
||||
setLoadingMore(false)
|
||||
return { data: newData, error: null }
|
||||
},
|
||||
[loading, loadingMore],
|
||||
)
|
||||
|
||||
const refetch = useCallback(
|
||||
(params?: ListTemplateGenerationsInput) => {
|
||||
hasMoreRef.current = true
|
||||
return execute(params)
|
||||
},
|
||||
[execute],
|
||||
)
|
||||
|
||||
return {
|
||||
data,
|
||||
generations: data?.data || [],
|
||||
loading,
|
||||
loadingMore,
|
||||
error,
|
||||
execute,
|
||||
refetch,
|
||||
loadMore,
|
||||
hasMore: hasMoreRef.current,
|
||||
}
|
||||
}
|
||||
|
||||
export type { TemplateGeneration }
|
||||
Reference in New Issue
Block a user