Skip to content
Merged

4768 #4772

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {useCreateKnowledgeBaseMutation} from '@/shared/middleware/graphql';
import {useEnvironmentStore} from '@/shared/stores/useEnvironmentStore';
import {getCookie} from '@/shared/util/cookie-utils';
import {useQueryClient} from '@tanstack/react-query';
import {ChangeEvent, useState} from 'react';

Expand Down Expand Up @@ -38,53 +39,74 @@ export default function useCreateKnowledgeBaseDialog({workspaceId}: UseCreateKno
setUploading(false);
};

const startSimulatedUpload = () => {
setUploading(true);
const uploadFile = async (knowledgeBaseId: string, file: File, index: number) => {
setSelectedFiles((prev) => {
const copy = [...prev];
copy[index] = {...copy[index], status: 'uploading'};

return copy;
});

try {
const formData = new FormData();

formData.append('file', file);

const response = await fetch(`/api/automation/internal/knowledge-bases/${knowledgeBaseId}/documents`, {
body: formData,
headers: {
'X-XSRF-TOKEN': getCookie('XSRF-TOKEN') || '',
},
method: 'POST',
});

if (!response.ok) {
throw new Error(`Upload failed: ${response.statusText}`);
}

selectedFiles.forEach((_, index) => {
setSelectedFiles((prev) => {
const copy = [...prev];
copy[index] = {...copy[index], status: 'uploading'};
copy[index] = {
...copy[index],
status: 'completed',
statusMessage: 'Uploaded successfully',
};

return copy;
});
} catch (error) {
setSelectedFiles((prev) => {
const copy = [...prev];
copy[index] = {
...copy[index],
status: 'error',
statusMessage: error instanceof Error ? error.message : 'Upload failed',
};

setTimeout(() => {
setSelectedFiles((prev) => {
const copy = [...prev];
copy[index] = {...copy[index], status: 'processing', statusMessage: 'Processing document...'};

return copy;
});
}, 1000);

setTimeout(() => {
setSelectedFiles((prev) => {
const copy = [...prev];
copy[index] = {
...copy[index],
status: 'completed',
statusMessage: 'Document processed successfully',
};

return copy;
});

if (index === selectedFiles.length - 1) {
setTimeout(() => {
queryClient.invalidateQueries({queryKey: ['knowledgeBases']});
setOpen(false);
resetForm();
}, 500);
}
}, 3000);
});
return copy;
});
}
};

const uploadFiles = async (knowledgeBaseId: string, files: SelectedFileI[]) => {
setUploading(true);

await Promise.all(files.map((selectedFile, index) => uploadFile(knowledgeBaseId, selectedFile.file, index)));

queryClient.invalidateQueries({queryKey: ['knowledgeBases']});

setTimeout(() => {
setOpen(false);
resetForm();
}, 500);
};

const createMutation = useCreateKnowledgeBaseMutation({
onSuccess: () => {
if (selectedFiles.length > 0) {
startSimulatedUpload();
onSuccess: (data) => {
const knowledgeBaseId = data.createKnowledgeBase?.id;

if (selectedFiles.length > 0 && knowledgeBaseId) {
uploadFiles(knowledgeBaseId, selectedFiles);
} else {
queryClient.invalidateQueries({queryKey: ['knowledgeBases']});
setOpen(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,17 @@ describe('PropertyInput', async () => {
});

it('uses minute precision (step=60) for time inputs so the field is easily clearable', () => {
const {container} = render(<PropertyInput aria-label="Time" label="Time" name="time" type="time" />);
render(<PropertyInput aria-label="Time" label="Time" name="time" type="time" />);

const input = container.querySelector('input[type="time"]');
const input = screen.getByLabelText(/time/i);

expect(input).toHaveAttribute('step', '60');
});

it('keeps step=1 for non-time inputs', () => {
const {container} = render(<PropertyInput aria-label="Date" label="Date" name="date" type="date" />);
render(<PropertyInput aria-label="Date" label="Date" name="date" type="date" />);

const input = container.querySelector('input[type="date"]');
const input = screen.getByLabelText(/date/i);

expect(input).toHaveAttribute('step', '1');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,10 +569,10 @@ export const useProperty = ({

let resolvedValue: unknown;

if (isNumericalInput) {
resolvedValue = parseFloat(valueToSave as string);
} else if (valueToSave === '' && isDateOrTimeControlType) {
if (valueToSave === '' && (isNumericalInput || isDateOrTimeControlType)) {
resolvedValue = null;
} else if (isNumericalInput) {
resolvedValue = parseFloat(valueToSave as string);
} else {
resolvedValue = valueToSave;
}
Expand Down
Loading