feat: 对接 generateVideo 页面后端接口
- 新增 uploadFile 工具函数用于图片上传 - 更新 useTemplateActions hook 使用 handleError 统一错误处理 - 实现 generateVideo 页面视频生成功能 - 根据 formSchema.startNodes 动态构建请求数据 - 支持图片和文本输入 - 添加 loading 状态和错误提示 - 生成成功后显示通知并返回 Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1,31 +1,33 @@
|
||||
import { OWNER_ID } from "@/lib/auth"
|
||||
import { ApiError } from "@/lib/types"
|
||||
import { root } from '@repo/core'
|
||||
import { TemplateController, type RunTemplateInput } from "@repo/sdk"
|
||||
import { useState } from "react"
|
||||
import { type RunTemplateInput, TemplateController } from '@repo/sdk'
|
||||
import { useCallback, useState } from 'react'
|
||||
|
||||
import { type ApiError } from '@/lib/types'
|
||||
import { handleError } from './use-error'
|
||||
|
||||
export const useTemplateActions = () => {
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [error, setError] = useState<ApiError | null>(null)
|
||||
|
||||
const runTemplate = async (params: RunTemplateInput): Promise<{ generationId?: string; error?: ApiError }> => {
|
||||
const runTemplate = useCallback(async (params: RunTemplateInput): Promise<{ generationId?: string; error?: ApiError }> => {
|
||||
setLoading(true)
|
||||
setError(null)
|
||||
|
||||
try {
|
||||
const template = root.get(TemplateController)
|
||||
const result = await template.run({
|
||||
templateId: params.templateId,
|
||||
data: params.data || {},
|
||||
})
|
||||
setLoading(false)
|
||||
return { generationId: result.generationId }
|
||||
} catch (e) {
|
||||
setError(e as ApiError)
|
||||
setLoading(false)
|
||||
return { error: e as ApiError }
|
||||
const template = root.get(TemplateController)
|
||||
const { data, error } = await handleError(async () => await template.run({
|
||||
templateId: params.templateId,
|
||||
data: params.data || {},
|
||||
}))
|
||||
|
||||
setLoading(false)
|
||||
|
||||
if (error) {
|
||||
setError(error)
|
||||
return { error }
|
||||
}
|
||||
}
|
||||
|
||||
return { generationId: data?.generationId, error: null }
|
||||
}, [])
|
||||
|
||||
return {
|
||||
loading,
|
||||
|
||||
Reference in New Issue
Block a user