mirror of
https://github.com/langgenius/dify.git
synced 2026-05-01 01:00:51 -04:00
fix(web): snippet usage count
This commit is contained in:
@@ -29,6 +29,7 @@ const mockToastError = vi.fn()
|
||||
const mockGetNodes = vi.fn()
|
||||
const mockSetNodes = vi.fn()
|
||||
const mockSetEdges = vi.fn()
|
||||
const mockIncrementSnippetUseCount = vi.fn()
|
||||
let mockEdges: unknown[] = [{ id: 'existing-edge', source: 'old', target: 'old-2' }]
|
||||
|
||||
vi.mock('@tanstack/react-query', () => ({
|
||||
@@ -66,6 +67,12 @@ vi.mock('@langgenius/dify-ui/toast', () => ({
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock('@/service/use-snippets', () => ({
|
||||
useIncrementSnippetUseCountMutation: () => ({
|
||||
mutate: mockIncrementSnippetUseCount,
|
||||
}),
|
||||
}))
|
||||
|
||||
describe('useInsertSnippet', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
@@ -136,6 +143,9 @@ describe('useInsertSnippet', () => {
|
||||
nodeId: nextNodes[1]!.id,
|
||||
})
|
||||
expect(mockHandleSyncWorkflowDraft).toHaveBeenCalledTimes(1)
|
||||
expect(mockIncrementSnippetUseCount).toHaveBeenCalledWith({
|
||||
params: { snippetId: 'snippet-1' },
|
||||
})
|
||||
})
|
||||
|
||||
it('should connect inserted snippet nodes to the requested edge position', async () => {
|
||||
@@ -238,6 +248,9 @@ describe('useInsertSnippet', () => {
|
||||
targetHandle: 'target',
|
||||
}),
|
||||
]))
|
||||
expect(mockIncrementSnippetUseCount).toHaveBeenCalledWith({
|
||||
params: { snippetId: 'snippet-1' },
|
||||
})
|
||||
})
|
||||
|
||||
it('should show error toast when fetching snippet workflow fails', async () => {
|
||||
@@ -250,6 +263,7 @@ describe('useInsertSnippet', () => {
|
||||
})
|
||||
|
||||
expect(mockToastError).toHaveBeenCalledWith('insert failed')
|
||||
expect(mockIncrementSnippetUseCount).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -5,6 +5,7 @@ import { useCallback } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { useStoreApi } from 'reactflow'
|
||||
import { consoleQuery } from '@/service/client'
|
||||
import { useIncrementSnippetUseCountMutation } from '@/service/use-snippets'
|
||||
import { CUSTOM_EDGE, ITERATION_CHILDREN_Z_INDEX, LOOP_CHILDREN_Z_INDEX, NODE_WIDTH_X_OFFSET, X_OFFSET } from '../../constants'
|
||||
import { useNodesSyncDraft, useWorkflowHistory, WorkflowHistoryEvent } from '../../hooks'
|
||||
import { BlockEnum } from '../../types'
|
||||
@@ -278,6 +279,7 @@ export const useInsertSnippet = () => {
|
||||
const store = useStoreApi()
|
||||
const { handleSyncWorkflowDraft } = useNodesSyncDraft()
|
||||
const { saveStateToHistory } = useWorkflowHistory()
|
||||
const { mutate: incrementSnippetUseCount } = useIncrementSnippetUseCountMutation()
|
||||
|
||||
const handleInsertSnippet = useCallback(async (snippetId: string, insertPayload?: SnippetInsertPayload) => {
|
||||
try {
|
||||
@@ -389,13 +391,16 @@ export const useInsertSnippet = () => {
|
||||
nodeId: remappedGraph.nodes[0]?.id,
|
||||
})
|
||||
handleSyncWorkflowDraft()
|
||||
incrementSnippetUseCount({
|
||||
params: { snippetId },
|
||||
})
|
||||
return true
|
||||
}
|
||||
catch (error) {
|
||||
toast.error(error instanceof Error ? error.message : t('createFailed', { ns: 'snippet' }))
|
||||
return false
|
||||
}
|
||||
}, [handleSyncWorkflowDraft, queryClient, saveStateToHistory, store, t])
|
||||
}, [handleSyncWorkflowDraft, incrementSnippetUseCount, queryClient, saveStateToHistory, store, t])
|
||||
|
||||
return {
|
||||
handleInsertSnippet,
|
||||
|
||||
@@ -201,6 +201,20 @@ export const useDeleteSnippetMutation = () => {
|
||||
})
|
||||
}
|
||||
|
||||
export const useIncrementSnippetUseCountMutation = () => {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
...consoleQuery.snippets.incrementUseCount.mutationOptions({
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: consoleQuery.snippets.key(),
|
||||
})
|
||||
},
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
||||
export const useExportSnippetMutation = () => {
|
||||
return useMutation<string, Error, { snippetId: string, include?: boolean }>({
|
||||
mutationFn: ({ snippetId, include = false }) => {
|
||||
|
||||
Reference in New Issue
Block a user