Refactor uploadFile to support multiple file uploads and update related types and handlers

This commit is contained in:
2025-07-12 14:25:03 +02:00
parent 51c6f62c44
commit ff4d1de2b7
4 changed files with 105 additions and 56 deletions

View File

@@ -4,7 +4,9 @@ import {
API_BASE_URL,
isLookupResponse,
isSaveFileResponse,
isUploadFilesResponse,
type SaveFileResponse,
type UploadFilesResponse,
} from '@/types/api';
/**
@@ -203,19 +205,19 @@ export const moveFile = async (
* @param workspaceName - The name of the workspace
* @param directoryPath - The directory path where files should be uploaded
* @param files - Multiple files to upload
* @returns {Promise<SaveFileResponse>} A promise that resolves to the upload file response
* @returns {Promise<UploadFilesResponse>} A promise that resolves to the upload file response
* @throws {Error} If the API call fails or returns an invalid response
*/
export const uploadFile = async (
workspaceName: string,
directoryPath: string,
files: FileList
): Promise<SaveFileResponse> => {
): Promise<UploadFilesResponse> => {
const formData = new FormData();
// Add all files to the form data
Array.from(files).forEach((file) => {
formData.append('file', file);
formData.append('files', file);
});
const response = await apiCall(
@@ -228,7 +230,7 @@ export const uploadFile = async (
}
);
const data: unknown = await response.json();
if (!isSaveFileResponse(data)) {
if (!isUploadFilesResponse(data)) {
throw new Error('Invalid upload file response received from API');
}
return data;

View File

@@ -122,7 +122,6 @@ export const useFileOperations = (): UseFileOperationsResult => {
if (!currentWorkspace) return false;
try {
// Use unified upload API that handles both single and multiple files
await uploadFile(currentWorkspace.name, targetPath || '', files);
notifications.show({

View File

@@ -98,6 +98,24 @@ export function isSaveFileResponse(obj: unknown): obj is SaveFileResponse {
);
}
export interface UploadFilesResponse {
filePaths: string[];
}
export function isUploadFilesResponse(
obj: unknown
): obj is UploadFilesResponse {
return (
typeof obj === 'object' &&
obj !== null &&
'filePaths' in obj &&
Array.isArray((obj as UploadFilesResponse).filePaths) &&
(obj as UploadFilesResponse).filePaths.every(
(path) => typeof path === 'string'
)
);
}
export interface UpdateLastOpenedFileRequest {
filePath: string;
}