mirror of
https://github.com/langgenius/dify.git
synced 2026-06-15 05:21:08 +08:00
Co-authored-by: JzoNg <jzongcode@gmail.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
71 lines
2.0 KiB
TypeScript
71 lines
2.0 KiB
TypeScript
import type { CreateSnippetDialogPayload } from '@/app/components/snippets/create-snippet-dialog'
|
|
import { toast } from '@langgenius/dify-ui/toast'
|
|
import { useState } from 'react'
|
|
import { useTranslation } from 'react-i18next'
|
|
import { useRouter } from '@/next/navigation'
|
|
import { consoleClient } from '@/service/client'
|
|
import { useCreateSnippetMutation } from '@/service/use-snippets'
|
|
|
|
export const useCreateSnippet = () => {
|
|
const { t } = useTranslation()
|
|
const { push } = useRouter()
|
|
const createSnippetMutation = useCreateSnippetMutation()
|
|
const [isCreateSnippetDialogOpen, setIsCreateSnippetDialogOpen] = useState(false)
|
|
const [isCreatingSnippet, setIsCreatingSnippet] = useState(false)
|
|
|
|
const handleOpenCreateSnippetDialog = () => {
|
|
setIsCreateSnippetDialogOpen(true)
|
|
}
|
|
|
|
const handleCloseCreateSnippetDialog = () => {
|
|
setIsCreateSnippetDialogOpen(false)
|
|
}
|
|
|
|
const handleCreateSnippet = async ({
|
|
name,
|
|
description,
|
|
graph,
|
|
input_fields,
|
|
}: CreateSnippetDialogPayload) => {
|
|
setIsCreatingSnippet(true)
|
|
|
|
try {
|
|
const createPayload = {
|
|
name,
|
|
description: description || undefined,
|
|
graph,
|
|
input_fields,
|
|
}
|
|
const snippet = await createSnippetMutation.mutateAsync({
|
|
body: createPayload,
|
|
})
|
|
await consoleClient.snippets.syncDraftWorkflow({
|
|
params: { snippetId: snippet.id },
|
|
body: {
|
|
graph: createPayload.graph,
|
|
input_fields: createPayload.input_fields,
|
|
},
|
|
})
|
|
|
|
toast.success(t('snippet.createSuccess', { ns: 'workflow' }))
|
|
handleCloseCreateSnippetDialog()
|
|
push(`/snippets/${snippet.id}/orchestrate`)
|
|
}
|
|
catch {
|
|
// The API client surfaces the response message. Avoid showing a second generic create-failed toast here.
|
|
}
|
|
finally {
|
|
setIsCreatingSnippet(false)
|
|
}
|
|
}
|
|
|
|
return {
|
|
createSnippetMutation,
|
|
handleCloseCreateSnippetDialog,
|
|
handleCreateSnippet,
|
|
handleOpenCreateSnippetDialog,
|
|
isCreateSnippetDialogOpen,
|
|
isCreatingSnippet,
|
|
}
|
|
}
|