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:
imeepos
2026-01-16 11:56:38 +08:00
parent fb719c6ea2
commit ae120f24d3
9 changed files with 487 additions and 92 deletions

6
hooks/index.ts Normal file
View File

@@ -0,0 +1,6 @@
export { useActivates } from './use-activates'
export { useCategories } from './use-categories'
export { useTemplateActions } from './use-template-actions'
export { useTemplates, type TemplateDetail } from './use-templates'
export { useTemplateDetail, type TemplateDetail as TemplateDetailType } from './use-template-detail'
export { useTemplateGenerations, type TemplateGeneration } from './use-template-generations'

View File

@@ -1,17 +1,38 @@
import { OWNER_ID } from "@/lib/auth"
import { ApiError } from "@/lib/types"
import { root } from '@repo/core'
import { ActivityController, ListActivitiesResult } from "@repo/sdk"
import { useState } from "react"
import { ActivityController, type ListActivitiesInput, type ListActivitiesResult } from '@repo/sdk'
import { 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 useActivates = () => {
const [loading, setLoading] = useState<boolean>(false)
const [error, setError] = useState<ApiError | null>(null)
const [data, setData] = useState<ListActivitiesResult>()
const load = async () => {
const load = async (params?: ListActivitiesInput) => {
try {
setLoading(true)
const c = root.get(ActivityController)
const data = await c.list({ page: 1, limit: 10, isActive: true, orderBy: 'sortOrder', order: 'desc', ownerId: OWNER_ID })
const activity = root.get(ActivityController)
const { data, error } = await handleError(
async () =>
await activity.list({
page: 1,
limit: 10,
isActive: true,
orderBy: 'sortOrder',
order: 'desc',
ownerId: OWNER_ID,
...params,
}),
)
if (error) {
setError(error)
return
}
setData(data)
} catch (e) {
setError(e as ApiError)
@@ -24,6 +45,6 @@ export const useActivates = () => {
load,
loading,
error,
data
data,
}
}
}

52
hooks/use-categories.ts Normal file
View File

@@ -0,0 +1,52 @@
import { root } from '@repo/core'
import { CategoryController, type ListCategoriesInput, type ListCategoriesResult } from '@repo/sdk'
import { 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 useCategories = () => {
const [loading, setLoading] = useState<boolean>(false)
const [error, setError] = useState<ApiError | null>(null)
const [data, setData] = useState<ListCategoriesResult>()
const load = async (params?: ListCategoriesInput) => {
try {
setLoading(true)
const category = root.get(CategoryController)
const { data, error } = await handleError(
async () =>
await category.list({
page: 1,
limit: 1000,
isActive: true,
orderBy: 'sortOrder',
order: 'desc',
withChildren: true,
ownerId: OWNER_ID,
...params,
}),
)
if (error) {
setError(error)
return
}
setData(data)
} catch (e) {
setError(e as ApiError)
} finally {
setLoading(false)
}
}
return {
load,
loading,
error,
data,
}
}

10
hooks/use-error.ts Normal file
View File

@@ -0,0 +1,10 @@
import { type ApiError } from '@/lib/types'
export async function handleError<T>(cb: () => Promise<T>) {
try {
const data = await cb()
return { data, error: null }
} catch (e) {
return { data: null, error: e as ApiError }
}
}

View 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 }

View 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 }

109
hooks/use-templates.ts Normal file
View 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 }