From e4b5750df88325ada77330cc363a1739f83753ab Mon Sep 17 00:00:00 2001 From: lethemanh Date: Mon, 3 Mar 2025 11:49:43 +0700 Subject: [PATCH] :bug: Fix bug unable to recreate document or folder --- .../app/components/uploads/file-tree-utils.ts | 13 ++++++++ .../features/drive/hooks/use-drive-upload.tsx | 5 ++- .../features/files/hooks/use-upload-zones.ts | 3 +- .../files/services/file-upload-service.ts | 33 +++++++++++++++++-- .../body/drive/modals/create/create-link.tsx | 1 + 5 files changed, 50 insertions(+), 5 deletions(-) diff --git a/tdrive/frontend/src/app/components/uploads/file-tree-utils.ts b/tdrive/frontend/src/app/components/uploads/file-tree-utils.ts index 5d2903a6..e7c3ace1 100644 --- a/tdrive/frontend/src/app/components/uploads/file-tree-utils.ts +++ b/tdrive/frontend/src/app/components/uploads/file-tree-utils.ts @@ -263,3 +263,16 @@ export const getFilesTree = ( } }); }; + +export const getFileIdsInTree = (tree: FileTreeObject['tree']): string[] => { + const fileIds: string[] = []; + for (const directory of Object.keys(tree)) { + if (tree[directory].root) { + fileIds.push(tree[directory].root as string); + } else { + getFileIdsInTree(tree[directory] as FileTreeObject['tree']); + } + } + + return fileIds; +} diff --git a/tdrive/frontend/src/app/features/drive/hooks/use-drive-upload.tsx b/tdrive/frontend/src/app/features/drive/hooks/use-drive-upload.tsx index 6f2e300e..2839883e 100644 --- a/tdrive/frontend/src/app/features/drive/hooks/use-drive-upload.tsx +++ b/tdrive/frontend/src/app/features/drive/hooks/use-drive-upload.tsx @@ -1,4 +1,4 @@ -import { FileTreeObject } from '@components/uploads/file-tree-utils'; +import { FileTreeObject, getFileIdsInTree } from '@components/uploads/file-tree-utils'; import FileUploadService from '@features/files/services/file-upload-service'; import { ToasterService } from '@features/global/services/toaster-service'; import { DriveApiClient } from '../api-client/api-client'; @@ -16,6 +16,7 @@ export const useDriveUpload = () => { const uploadVersion = async (file: File, context: { companyId: string; id: string }) => { return new Promise(r => { + FileUploadService.resetStates([file.name]); FileUploadService.upload([{ root: file.name, file }], { context: { companyId: context.companyId, @@ -49,6 +50,7 @@ export const useDriveUpload = () => { context: { companyId: string; parentId: string }, ) => { logger.debug('Start creating directories and file upload ...'); + FileUploadService.resetStates(getFileIdsInTree(tree.tree)); await FileUploadService.createDirectories(tree, context); await refresh(context.parentId, true); }; @@ -68,6 +70,7 @@ export const useDriveUpload = () => { `Unexpected response status code: ${request.status} from ${JSON.stringify(url)}`, ); const file = new File([request.response], name); + FileUploadService.resetStates([file.name]); FileUploadService.upload([{ root: file.name, file }], { context: { companyId: context.companyId, diff --git a/tdrive/frontend/src/app/features/files/hooks/use-upload-zones.ts b/tdrive/frontend/src/app/features/files/hooks/use-upload-zones.ts index 658b1427..04eb2422 100644 --- a/tdrive/frontend/src/app/features/files/hooks/use-upload-zones.ts +++ b/tdrive/frontend/src/app/features/files/hooks/use-upload-zones.ts @@ -27,8 +27,9 @@ export const useUploadZones = (zoneId: string) => { list: File[], options?: { uploader?: (file: File, context: any) => Promise; context?: any }, ) => { + FileUploadService.resetStates(list.map((file) => file.name)); const newFiles = await FileUploadService.upload( - list.map((file, index) => { + list.map((file) => { return { root: file.name, file, diff --git a/tdrive/frontend/src/app/features/files/services/file-upload-service.ts b/tdrive/frontend/src/app/features/files/services/file-upload-service.ts index cc2a54c2..444fedb0 100644 --- a/tdrive/frontend/src/app/features/files/services/file-upload-service.ts +++ b/tdrive/frontend/src/app/features/files/services/file-upload-service.ts @@ -12,8 +12,6 @@ import FileUploadAPIClient from '../api/file-upload-api-client'; import { isPendingFileStatusPending } from '../utils/pending-files'; import { FileTreeObject } from 'components/uploads/file-tree-utils'; import { DriveApiClient } from 'features/drive/api-client/api-client'; -import { ToasterService } from 'app/features/global/services/toaster-service'; -import Languages from 'app/features/global/services/languages-service'; import { DriveItem, DriveItemVersion } from 'app/features/drive/types'; export enum Events { @@ -63,6 +61,9 @@ class FileUploadService { * @private */ async _waitWhilePaused(id?: string) { + logger.debug('===== _waitWhilePaused ======'); + logger.debug('rootStates: ', this.rootStates); + logger.debug('status: ', this.uploadStatus) while (this.uploadStatus === UploadStateEnum.Paused || (id && this.rootStates.paused[id])) { if (this.uploadStatus === UploadStateEnum.Cancelled || (id && this.rootStates.cancelled[id])) return; @@ -305,10 +306,14 @@ class FileUploadService { callback?: (file: { root: string; file: FileType | null }, context: any) => void; }, ): Promise { + logger.debug('===== upload ====='); + logger.debug('uploadStatus: ', this.uploadStatus); // reset the upload status when creating a new document if (fileList.length === 1 && fileList[0].root === fileList[0].file.name) { - this.pauseOrResume(); + if (this.uploadStatus === UploadStateEnum.Paused) { + this.uploadStatus = UploadStateEnum.Progress; + } } // if we're uploading one file directly, do the size calc first @@ -746,6 +751,28 @@ class FileUploadService { this.groupIds = {}; this.notify(); } + + public resetStates(ids: string[]) { + this.logger.debug('===== START resetStates ====='); + this.logger.debug('groupedPendingFiles: ', this.groupedPendingFiles); + this.logger.debug('groupIds: ', this.groupIds); + this.logger.debug('rootStates: ', this.rootStates); + + this.uploadStatus = UploadStateEnum.Progress; + for (const id of ids) { + delete this.groupedPendingFiles[id]; + delete this.groupIds[id]; + delete this.rootStates.paused[id]; + delete this.rootStates.completed[id]; + delete this.rootStates.failed[id]; + delete this.rootStates.cancelled[id]; + } + + this.logger.debug('===== END resetStates ====='); + this.logger.debug('groupedPendingFiles: ', this.groupedPendingFiles); + this.logger.debug('groupIds: ', this.groupIds); + this.logger.debug('rootStates: ', this.rootStates); + } } export default new FileUploadService(); diff --git a/tdrive/frontend/src/app/views/client/body/drive/modals/create/create-link.tsx b/tdrive/frontend/src/app/views/client/body/drive/modals/create/create-link.tsx index f2168527..7ef44d71 100644 --- a/tdrive/frontend/src/app/views/client/body/drive/modals/create/create-link.tsx +++ b/tdrive/frontend/src/app/views/client/body/drive/modals/create/create-link.tsx @@ -21,6 +21,7 @@ export const CreateLink = () => { type: 'text/uri-list', }); + FileUploadService.resetStates([file.name]); await FileUploadService.upload([{ root: file.name, file }], { context: { parentId: state.parent_id,