This commit is contained in:
Manav Rathi 2024-04-19 13:45:43 +05:30
parent 967ef2e3ea
commit 2c46be6ded
No known key found for this signature in database
2 changed files with 48 additions and 43 deletions

View file

@ -131,12 +131,6 @@ export default function Uploader(props: Props) {
const closeUploadProgress = () => setUploadProgressView(false);
const showUserNameInputDialog = () => setUserNameInputDialogView(true);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const setCollectionName = (collectionName: string) => {
isPendingDesktopUpload.current = true;
pendingDesktopUploadCollectionName.current = collectionName;
};
const handleChoiceModalClose = () => {
setChoiceModalView(false);
uploadRunning.current = false;
@ -186,13 +180,26 @@ export default function Uploader(props: Props) {
);
}
});
/* TODO(MR): This is the connection point, implement
watcher.init(
setElectronFiles,
setCollectionName,
props.syncWithRemote,
);
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const upload = (collectionName: string, filePaths: string[]) => {
isPendingDesktopUpload.current = true;
pendingDesktopUploadCollectionName.current = collectionName;
// TODO (MR):
// setElectronFiles(filePaths);
};
const requestSyncWithRemote = () => {
props.syncWithRemote().catch((e) => {
log.error(
"Ignoring error when syncing trash changes with remote",
e,
);
});
};
watcher.init(upload, requestSyncWithRemote);
}
}, [
publicCollectionGalleryContext.accessedThroughSharedURL,

View file

@ -57,11 +57,12 @@ class FolderWatcher {
*/
private upload: (collectionName: string, filePaths: string[]) => void;
/**
* A function to call when we want to sync with the backend.
* A function to call when we want to sync with the backend. It will
* initiate the sync but will not await its completion.
*
* This is passed as a param to {@link init}.
*/
private syncWithRemote: () => void;
private requestSyncWithRemote: () => void;
/** A helper function that debounces invocations of {@link runNextEvent}. */
private debouncedRunNextEvent: () => void;
@ -80,10 +81,10 @@ class FolderWatcher {
*/
init(
upload: (collectionName: string, filePaths: string[]) => void,
syncWithRemote: () => void,
requestSyncWithRemote: () => void,
) {
this.upload = upload;
this.syncWithRemote = syncWithRemote;
this.requestSyncWithRemote = requestSyncWithRemote;
this.registerListeners();
this.syncWithDisk();
}
@ -504,33 +505,30 @@ class FolderWatcher {
}
private async moveToTrash(syncedFiles: FolderWatch["syncedFiles"]) {
try {
const files = await getLocalFiles();
const toTrashFilesMap = new Map<number, FolderWatchSyncedFile>();
for (const file of syncedFiles) {
toTrashFilesMap.set(file.uploadedFileID, file);
}
const filesToTrash = files.filter((file) => {
if (toTrashFilesMap.has(file.id)) {
const fileToTrash = toTrashFilesMap.get(file.id);
if (fileToTrash.collectionID === file.collectionID) {
return true;
}
}
});
const groupFilesByCollectionId =
groupFilesBasedOnCollectionID(filesToTrash);
for (const [
collectionID,
filesToTrash,
] of groupFilesByCollectionId.entries()) {
await removeFromCollection(collectionID, filesToTrash);
}
this.syncWithRemote();
} catch (e) {
log.error("error while trashing by IDs", e);
const files = await getLocalFiles();
const toTrashFilesMap = new Map<number, FolderWatchSyncedFile>();
for (const file of syncedFiles) {
toTrashFilesMap.set(file.uploadedFileID, file);
}
const filesToTrash = files.filter((file) => {
if (toTrashFilesMap.has(file.id)) {
const fileToTrash = toTrashFilesMap.get(file.id);
if (fileToTrash.collectionID === file.collectionID) {
return true;
}
}
});
const groupFilesByCollectionId =
groupFilesBasedOnCollectionID(filesToTrash);
for (const [
collectionID,
filesToTrash,
] of groupFilesByCollectionId.entries()) {
await removeFromCollection(collectionID, filesToTrash);
}
this.requestSyncWithRemote();
}
}