|
1 | 1 | import {useCreateKnowledgeBaseMutation} from '@/shared/middleware/graphql'; |
2 | 2 | import {useEnvironmentStore} from '@/shared/stores/useEnvironmentStore'; |
| 3 | +import {getCookie} from '@/shared/util/cookie-utils'; |
3 | 4 | import {useQueryClient} from '@tanstack/react-query'; |
4 | 5 | import {ChangeEvent, useState} from 'react'; |
5 | 6 |
|
@@ -38,53 +39,74 @@ export default function useCreateKnowledgeBaseDialog({workspaceId}: UseCreateKno |
38 | 39 | setUploading(false); |
39 | 40 | }; |
40 | 41 |
|
41 | | - const startSimulatedUpload = () => { |
42 | | - setUploading(true); |
| 42 | + const uploadFile = async (knowledgeBaseId: string, file: File, index: number) => { |
| 43 | + setSelectedFiles((prev) => { |
| 44 | + const copy = [...prev]; |
| 45 | + copy[index] = {...copy[index], status: 'uploading'}; |
| 46 | + |
| 47 | + return copy; |
| 48 | + }); |
| 49 | + |
| 50 | + try { |
| 51 | + const formData = new FormData(); |
| 52 | + |
| 53 | + formData.append('file', file); |
| 54 | + |
| 55 | + const response = await fetch(`/api/automation/internal/knowledge-bases/${knowledgeBaseId}/documents`, { |
| 56 | + body: formData, |
| 57 | + headers: { |
| 58 | + 'X-XSRF-TOKEN': getCookie('XSRF-TOKEN') || '', |
| 59 | + }, |
| 60 | + method: 'POST', |
| 61 | + }); |
| 62 | + |
| 63 | + if (!response.ok) { |
| 64 | + throw new Error(`Upload failed: ${response.statusText}`); |
| 65 | + } |
43 | 66 |
|
44 | | - selectedFiles.forEach((_, index) => { |
45 | 67 | setSelectedFiles((prev) => { |
46 | 68 | const copy = [...prev]; |
47 | | - copy[index] = {...copy[index], status: 'uploading'}; |
| 69 | + copy[index] = { |
| 70 | + ...copy[index], |
| 71 | + status: 'completed', |
| 72 | + statusMessage: 'Uploaded successfully', |
| 73 | + }; |
48 | 74 |
|
49 | 75 | return copy; |
50 | 76 | }); |
| 77 | + } catch (error) { |
| 78 | + setSelectedFiles((prev) => { |
| 79 | + const copy = [...prev]; |
| 80 | + copy[index] = { |
| 81 | + ...copy[index], |
| 82 | + status: 'error', |
| 83 | + statusMessage: error instanceof Error ? error.message : 'Upload failed', |
| 84 | + }; |
51 | 85 |
|
52 | | - setTimeout(() => { |
53 | | - setSelectedFiles((prev) => { |
54 | | - const copy = [...prev]; |
55 | | - copy[index] = {...copy[index], status: 'processing', statusMessage: 'Processing document...'}; |
56 | | - |
57 | | - return copy; |
58 | | - }); |
59 | | - }, 1000); |
60 | | - |
61 | | - setTimeout(() => { |
62 | | - setSelectedFiles((prev) => { |
63 | | - const copy = [...prev]; |
64 | | - copy[index] = { |
65 | | - ...copy[index], |
66 | | - status: 'completed', |
67 | | - statusMessage: 'Document processed successfully', |
68 | | - }; |
69 | | - |
70 | | - return copy; |
71 | | - }); |
72 | | - |
73 | | - if (index === selectedFiles.length - 1) { |
74 | | - setTimeout(() => { |
75 | | - queryClient.invalidateQueries({queryKey: ['knowledgeBases']}); |
76 | | - setOpen(false); |
77 | | - resetForm(); |
78 | | - }, 500); |
79 | | - } |
80 | | - }, 3000); |
81 | | - }); |
| 86 | + return copy; |
| 87 | + }); |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + const uploadFiles = async (knowledgeBaseId: string, files: SelectedFileI[]) => { |
| 92 | + setUploading(true); |
| 93 | + |
| 94 | + await Promise.all(files.map((selectedFile, index) => uploadFile(knowledgeBaseId, selectedFile.file, index))); |
| 95 | + |
| 96 | + queryClient.invalidateQueries({queryKey: ['knowledgeBases']}); |
| 97 | + |
| 98 | + setTimeout(() => { |
| 99 | + setOpen(false); |
| 100 | + resetForm(); |
| 101 | + }, 500); |
82 | 102 | }; |
83 | 103 |
|
84 | 104 | const createMutation = useCreateKnowledgeBaseMutation({ |
85 | | - onSuccess: () => { |
86 | | - if (selectedFiles.length > 0) { |
87 | | - startSimulatedUpload(); |
| 105 | + onSuccess: (data) => { |
| 106 | + const knowledgeBaseId = data.createKnowledgeBase?.id; |
| 107 | + |
| 108 | + if (selectedFiles.length > 0 && knowledgeBaseId) { |
| 109 | + uploadFiles(knowledgeBaseId, selectedFiles); |
88 | 110 | } else { |
89 | 111 | queryClient.invalidateQueries({queryKey: ['knowledgeBases']}); |
90 | 112 | setOpen(false); |
|
0 commit comments