Quellcode durchsuchen

updated getCollectionLatestFile to use local files

Abhinav-grd vor 4 Jahren
Ursprung
Commit
08c993061c
2 geänderte Dateien mit 20 neuen und 16 gelöschten Zeilen
  1. 2 2
      src/pages/gallery/index.tsx
  2. 18 14
      src/services/collectionService.ts

+ 2 - 2
src/pages/gallery/index.tsx

@@ -119,7 +119,7 @@ export default function Gallery(props) {
             const encryptionKey = await getActualKey();
             const encryptionKey = await getActualKey();
             const collections = await fetchCollections(token, encryptionKey);
             const collections = await fetchCollections(token, encryptionKey);
             const data = await fetchData(token, collections);
             const data = await fetchData(token, collections);
-            const collectionLatestFile = await getCollectionLatestFile(collections, token);
+            const collectionLatestFile = await getCollectionLatestFile(collections, data);
             const favItemIds = await getFavItemIds(data);
             const favItemIds = await getFavItemIds(data);
             setCollections(collections);
             setCollections(collections);
             setData(data);
             setData(data);
@@ -296,7 +296,7 @@ export default function Gallery(props) {
                 showUploadModal={props.showUploadModal}
                 showUploadModal={props.showUploadModal}
                 collectionLatestFile={collectionLatestFile}
                 collectionLatestFile={collectionLatestFile}
                 refetchData={() => setReload(Math.random())}
                 refetchData={() => setReload(Math.random())}
-                
+
             />
             />
             {filteredData.length ? (
             {filteredData.length ? (
                 <Container>
                 <Container>

+ 18 - 14
src/services/collectionService.ts

@@ -108,21 +108,25 @@ export const fetchCollections = async (token: string, key: string) => {
     return collections;
     return collections;
 };
 };
 
 
-export const getCollectionLatestFile = async (
+export const getCollectionLatestFile = (
     collections: collection[],
     collections: collection[],
-    token
-): Promise<collectionLatestFile[]> => {
-    return Promise.all(
-        collections.map(async collection => {
-            const sinceTime: string = (Number(await localForage.getItem<string>(`${collection.id}-time`)) - 1).toString();
-            const files: file[] = await getFiles([collection], sinceTime, "1", token) || [];
-
-            return {
-                file: files[0],
-                collection,
-            }
-        }))
-};
+    files: file[]
+): collectionLatestFile[] => {
+    const latestFile = new Map<number, file>();
+    const collectionMap = new Map<number, collection>();
+
+    collections.forEach(collection => collectionMap.set(Number(collection.id), collection));
+    files.forEach(file => {
+        if (!latestFile.has(file.collectionID)) {
+            latestFile.set(file.collectionID, file)
+        }
+    });
+    let allCollectionLatestFile: collectionLatestFile[] = [];
+    for (const [collectionID, file] of latestFile) {
+        allCollectionLatestFile.push({ collection: collectionMap.get(collectionID), file });
+    }
+    return allCollectionLatestFile;
+}
 
 
 export const getFavItemIds = async (files: file[]): Promise<Set<number>> => {
 export const getFavItemIds = async (files: file[]): Promise<Set<number>> => {