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:
109
hooks/use-templates.ts
Normal file
109
hooks/use-templates.ts
Normal file
@@ -0,0 +1,109 @@
|
||||
import { root } from '@repo/core'
|
||||
import { type ListTemplatesInput, type ListTemplatesResult, TemplateController, type TemplateDetail } 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 || ''
|
||||
|
||||
type ListTemplatesParams = Omit<ListTemplatesInput, 'ownerId'>
|
||||
|
||||
const DEFAULT_PARAMS = {
|
||||
limit: 20,
|
||||
sortBy: 'createdAt' as const,
|
||||
sortOrder: 'desc' as const,
|
||||
}
|
||||
|
||||
export const useTemplates = (initialParams?: ListTemplatesParams) => {
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [loadingMore, setLoadingMore] = useState(false)
|
||||
const [error, setError] = useState<ApiError | null>(null)
|
||||
const [data, setData] = useState<ListTemplatesResult>()
|
||||
const currentPageRef = useRef(1)
|
||||
const hasMoreRef = useRef(true)
|
||||
|
||||
const execute = useCallback(async (params?: ListTemplatesParams) => {
|
||||
setLoading(true)
|
||||
setError(null)
|
||||
currentPageRef.current = params?.page || 1
|
||||
|
||||
const template = root.get(TemplateController)
|
||||
const { data, error } = await handleError(
|
||||
async () =>
|
||||
await template.list({
|
||||
...DEFAULT_PARAMS,
|
||||
...initialParams,
|
||||
...params,
|
||||
ownerId: OWNER_ID,
|
||||
}),
|
||||
)
|
||||
|
||||
if (error) {
|
||||
setError(error)
|
||||
setLoading(false)
|
||||
return { data: undefined, error }
|
||||
}
|
||||
|
||||
const templates = data?.templates || []
|
||||
hasMoreRef.current = templates.length >= (params?.limit || DEFAULT_PARAMS.limit)
|
||||
setData(data)
|
||||
setLoading(false)
|
||||
return { data, error: null }
|
||||
}, [initialParams])
|
||||
|
||||
const loadMore = useCallback(async () => {
|
||||
if (loadingMore || loading || !hasMoreRef.current) return { data: undefined, error: null }
|
||||
|
||||
setLoadingMore(true)
|
||||
const nextPage = currentPageRef.current + 1
|
||||
|
||||
const template = root.get(TemplateController)
|
||||
const { data: newData, error } = await handleError(
|
||||
async () =>
|
||||
await template.list({
|
||||
...DEFAULT_PARAMS,
|
||||
...initialParams,
|
||||
page: nextPage,
|
||||
ownerId: OWNER_ID,
|
||||
}),
|
||||
)
|
||||
|
||||
if (error) {
|
||||
setLoadingMore(false)
|
||||
return { data: undefined, error }
|
||||
}
|
||||
|
||||
const newTemplates = newData?.templates || []
|
||||
hasMoreRef.current = newTemplates.length >= DEFAULT_PARAMS.limit
|
||||
currentPageRef.current = nextPage
|
||||
|
||||
setData((prev) => ({
|
||||
...newData,
|
||||
templates: [...(prev?.templates || []), ...newTemplates],
|
||||
}))
|
||||
setLoadingMore(false)
|
||||
return { data: newData, error: null }
|
||||
}, [loading, loadingMore, initialParams])
|
||||
|
||||
const refetch = useCallback(() => {
|
||||
hasMoreRef.current = true
|
||||
return execute()
|
||||
}, [execute])
|
||||
|
||||
return {
|
||||
data,
|
||||
templates: data?.templates || [],
|
||||
loading,
|
||||
loadingMore,
|
||||
error,
|
||||
execute,
|
||||
refetch,
|
||||
loadMore,
|
||||
hasMore: hasMoreRef.current,
|
||||
}
|
||||
}
|
||||
|
||||
export type { TemplateDetail }
|
||||
|
||||
Reference in New Issue
Block a user