Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(web): preserve dataset uploads after returning to source step (#43080)

Lucien657624154db2
diff --git a/web/app/components/datasets/create/file-uploader/__tests__/index.spec.tsx b/web/app/components/datasets/create/file-uploader/__tests__/index.spec.tsx
index ffcf4d582b2..2e9ddc33cc7 100644
--- a/web/app/components/datasets/create/file-uploader/__tests__/index.spec.tsx
+++ b/web/app/components/datasets/create/file-uploader/__tests__/index.spec.tsx
@@ -1,5 +1,5 @@
import type { CustomFile as File, FileItem } from '@/models/datasets'
-import { fireEvent, screen } from '@testing-library/react'
+import { screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { beforeEach, describe, expect, it, vi } from 'vite-plus/test'
import { consoleQuery } from '@/service/console'
@@ -180,25 +180,29 @@ describe('FileUploader', () => {
expect(onPreview).toHaveBeenCalledWith(fileItem.file)
})
- it('should handle file remove click', () => {
+ it('keeps other uploaded files when removing one after mounting with an existing list', async () => {
+ const user = userEvent.setup()
const onFileListUpdate = vi.fn()
- const fileItem = createMockFileItem()
+ const firstFile = createMockFileItem({
+ fileID: 'first',
+ file: createMockFile({ name: 'first.pdf', id: 'first-id' }),
+ })
+ const secondFile = createMockFileItem({
+ fileID: 'second',
+ file: createMockFile({ name: 'second.pdf', id: 'second-id' }),
+ })
- const { container } = render(
+ render(
<FileUploader
{...defaultProps}
- fileList={[fileItem]}
+ fileList={[firstFile, secondFile]}
onFileListUpdate={onFileListUpdate}
/>,
)
- // Find the delete button (the span with cursor-pointer containing the icon)
- const deleteButtons = container.querySelectorAll('[class*="cursor-pointer"]')
- // Get the last one which should be the delete button (not the browse label)
- const deleteButton = deleteButtons[deleteButtons.length - 1]
- if (deleteButton) fireEvent.click(deleteButton)
+ await user.click(screen.getByRole('button', { name: 'common.operation.remove first.pdf' }))
- expect(onFileListUpdate).toHaveBeenCalled()
+ expect(onFileListUpdate).toHaveBeenCalledWith([secondFile])
})
it('should handle browse button click', () => {
diff --git a/web/app/components/datasets/create/file-uploader/hooks/__tests__/use-file-upload.spec.tsx b/web/app/components/datasets/create/file-uploader/hooks/__tests__/use-file-upload.spec.tsx
index 406d3a4f4ba..570969458b0 100644
--- a/web/app/components/datasets/create/file-uploader/hooks/__tests__/use-file-upload.spec.tsx
+++ b/web/app/components/datasets/create/file-uploader/hooks/__tests__/use-file-upload.spec.tsx
@@ -312,6 +312,33 @@ describe('useFileUpload', () => {
})
describe('fileChangeHandle', () => {
+ it('keeps previously uploaded files when adding another file after remount', () => {
+ const existingFile: FileItem = {
+ fileID: 'existing',
+ file: { id: 'existing-id', name: 'existing.pdf', size: 1024 } as CustomFile,
+ progress: PROGRESS_COMPLETE,
+ }
+ const prepareFileList = vi.fn()
+ const { result } = renderHook(
+ () => useFileUpload({ ...defaultOptions, fileList: [existingFile], prepareFileList }),
+ { wrapper: createWrapper() },
+ )
+
+ const newFile = new File(['content'], 'new.pdf', { type: 'application/pdf' })
+ const event = {
+ target: { files: [newFile] },
+ } as unknown as React.ChangeEvent<HTMLInputElement>
+
+ act(() => {
+ result.current.fileChangeHandle(event)
+ })
+
+ expect(prepareFileList).toHaveBeenCalledWith([
+ existingFile,
+ expect.objectContaining({ file: newFile }),
+ ])
+ })
+
it('should handle valid files', async () => {
mockUpload.mockResolvedValue({ id: 'uploaded-id' })
diff --git a/web/app/components/datasets/create/file-uploader/hooks/use-file-upload.ts b/web/app/components/datasets/create/file-uploader/hooks/use-file-upload.ts
index 161041945be..dbdff6af6da 100644
--- a/web/app/components/datasets/create/file-uploader/hooks/use-file-upload.ts
+++ b/web/app/components/datasets/create/file-uploader/hooks/use-file-upload.ts
@@ -81,7 +81,7 @@ export const useFileUpload = ({
const dropRef = useRef<HTMLDivElement>(null)
const dragRef = useRef<HTMLDivElement>(null)
const fileUploaderRef = useRef<HTMLInputElement>(null)
- const fileListRef = useRef<FileItem[]>([])
+ const fileListRef = useRef<FileItem[]>(fileList)
const hideUpload = !supportBatchUpload && fileList.length > 0