optimize code
This commit is contained in:
parent
e64f1c9ad7
commit
ac2257771f
|
@ -12,7 +12,6 @@ import {
|
||||||
isSharedFile,
|
isSharedFile,
|
||||||
sortFiles,
|
sortFiles,
|
||||||
groupFilesBasedOnCollectionID,
|
groupFilesBasedOnCollectionID,
|
||||||
groupFilesBasedOnID,
|
|
||||||
} from 'utils/file';
|
} from 'utils/file';
|
||||||
import {
|
import {
|
||||||
Collection,
|
Collection,
|
||||||
|
@ -496,31 +495,58 @@ const encryptWithNewCollectionKey = async (
|
||||||
};
|
};
|
||||||
export const removeFromCollection = async (
|
export const removeFromCollection = async (
|
||||||
collectionID: number,
|
collectionID: number,
|
||||||
files: EnteFile[]
|
toRemoveFiles: EnteFile[]
|
||||||
) => {
|
) => {
|
||||||
try {
|
try {
|
||||||
|
const allFiles = await getLocalFiles();
|
||||||
|
|
||||||
|
const toRemoveFilesIds = new Set(toRemoveFiles.map((f) => f.id));
|
||||||
|
const toRemoveFilesCopiesInOtherCollections = allFiles.filter((f) => {
|
||||||
|
if (f.collectionID === collectionID) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return toRemoveFilesIds.has(f.id);
|
||||||
|
});
|
||||||
|
const groupiedFiles = groupFilesBasedOnCollectionID(
|
||||||
|
toRemoveFilesCopiesInOtherCollections
|
||||||
|
);
|
||||||
|
|
||||||
|
const collections = await getLocalCollections();
|
||||||
|
const collectionsMap = new Map(collections.map((c) => [c.id, c]));
|
||||||
|
|
||||||
|
for (const [toMoveCollectionID, files] of groupiedFiles.entries()) {
|
||||||
|
const toMoveFiles = files.filter((f) => {
|
||||||
|
if (toRemoveFilesIds.has(f.id)) {
|
||||||
|
toRemoveFilesIds.delete(f.id);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
if (toMoveFiles.length === 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
await moveToCollection(
|
||||||
|
collectionsMap[toMoveCollectionID],
|
||||||
|
collectionID,
|
||||||
|
toMoveFiles
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const leftFiles = toRemoveFiles.filter((f) =>
|
||||||
|
toRemoveFilesIds.has(f.id)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (leftFiles.length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
let uncategorizedCollection = await getUncategorizedCollection();
|
let uncategorizedCollection = await getUncategorizedCollection();
|
||||||
if (!uncategorizedCollection) {
|
if (!uncategorizedCollection) {
|
||||||
uncategorizedCollection = await createUnCategorizedCollection();
|
uncategorizedCollection = await createUnCategorizedCollection();
|
||||||
}
|
}
|
||||||
const allFiles = await getLocalFiles();
|
await moveToCollection(
|
||||||
const groupiedFiles = groupFilesBasedOnID(allFiles);
|
uncategorizedCollection,
|
||||||
for (const file of files) {
|
collectionID,
|
||||||
if (groupiedFiles[file.id].length === 1) {
|
leftFiles
|
||||||
await moveToCollection(uncategorizedCollection, collectionID, [
|
);
|
||||||
file,
|
|
||||||
]);
|
|
||||||
} else {
|
|
||||||
const differentCollectionFile = groupiedFiles[file.id].find(
|
|
||||||
(f) => f.collectionID !== collectionID
|
|
||||||
);
|
|
||||||
const collections = await getLocalCollections();
|
|
||||||
const collection = collections.find(
|
|
||||||
(c) => c.id === differentCollectionFile?.collectionID
|
|
||||||
);
|
|
||||||
await moveToCollection(collection, collectionID, [file]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logError(e, 'remove from collection failed ');
|
logError(e, 'remove from collection failed ');
|
||||||
throw e;
|
throw e;
|
||||||
|
|
|
@ -592,15 +592,3 @@ export const copyFileToClipboard = async (fileUrl: string) => {
|
||||||
.write([new ClipboardItem({ 'image/png': blobPromise })])
|
.write([new ClipboardItem({ 'image/png': blobPromise })])
|
||||||
.catch((e) => logError(e, 'failed to copy to clipboard'));
|
.catch((e) => logError(e, 'failed to copy to clipboard'));
|
||||||
};
|
};
|
||||||
|
|
||||||
export const groupFilesBasedOnID = (files: EnteFile[]) => {
|
|
||||||
const groupedFiles = files.reduce((acc, file) => {
|
|
||||||
if (!acc[file.id]) {
|
|
||||||
acc[file.id] = [];
|
|
||||||
}
|
|
||||||
acc[file.id].push(file);
|
|
||||||
return acc;
|
|
||||||
}, {} as { [key: number]: EnteFile[] });
|
|
||||||
|
|
||||||
return groupedFiles;
|
|
||||||
};
|
|
||||||
|
|
Loading…
Reference in a new issue