From d9ab97cc34d0a819aebe53364d25d61b9231c7bf Mon Sep 17 00:00:00 2001 From: Abhinav Date: Tue, 26 Jul 2022 13:50:54 +0530 Subject: [PATCH 1/3] dont proceed if user missing --- src/services/collectionService.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/services/collectionService.ts b/src/services/collectionService.ts index 78e2f2766..d7b41b685 100644 --- a/src/services/collectionService.ts +++ b/src/services/collectionService.ts @@ -774,6 +774,10 @@ export function getCollectionSummaries( files: EnteFile[], archivedCollections: Set ): CollectionSummaries { + const user: User = getData(LS_KEYS.USER); + if (!user) { + return; + } const collectionSummaries: CollectionSummaries = new Map(); const collectionLatestFiles = getCollectionLatestFiles( files, @@ -781,7 +785,6 @@ export function getCollectionSummaries( ); const collectionFilesCount = getCollectionsFileCount(files); const uniqueFileCount = new Set(files.map((file) => file.id)).size; - const user: User = getData(LS_KEYS.USER); for (const collection of collections) { if (collectionFilesCount.get(collection.id)) { From f11c1d6e5194e3ffd23cd96f51e1434df8191c06 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Tue, 26 Jul 2022 14:02:33 +0530 Subject: [PATCH 2/3] move getUser to top caller and add null check there itself --- src/pages/gallery/index.tsx | 13 +++++++++---- src/services/collectionService.ts | 5 +---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/pages/gallery/index.tsx b/src/pages/gallery/index.tsx index f81714da7..395ea2e61 100644 --- a/src/pages/gallery/index.tsx +++ b/src/pages/gallery/index.tsx @@ -99,6 +99,8 @@ import { NotificationAttributes } from 'types/Notification'; import { ITEM_TYPE, TimeStampListItem } from 'components/PhotoList'; import UploadInputs from 'components/UploadSelectorInputs'; import useFileInput from 'hooks/useFileInput'; +import { User } from 'types/user'; +import { getData, LS_KEYS } from 'utils/storage/localStorage'; export const DeadCenter = styled('div')` flex: 1; @@ -260,7 +262,11 @@ export default function Gallery() { }, []); useEffect(() => { - setDerivativeState(collections, files); + const user: User = getData(LS_KEYS.USER); + if (!user || !files || !collections) { + return; + } + setDerivativeState(user, collections, files); }, [collections, files]); useEffect( @@ -375,18 +381,17 @@ export default function Gallery() { }; const setDerivativeState = async ( + user: User, collections: Collection[], files: EnteFile[] ) => { - if (!collections || !files) { - return; - } const favItemIds = await getFavItemIds(files); setFavItemIds(favItemIds); const archivedCollections = getArchivedCollections(collections); setArchivedCollections(archivedCollections); const collectionSummaries = getCollectionSummaries( + user, collections, files, archivedCollections diff --git a/src/services/collectionService.ts b/src/services/collectionService.ts index d7b41b685..1f69e357a 100644 --- a/src/services/collectionService.ts +++ b/src/services/collectionService.ts @@ -770,14 +770,11 @@ function compareCollectionsLatestFile(first: EnteFile, second: EnteFile) { } export function getCollectionSummaries( + user: User, collections: Collection[], files: EnteFile[], archivedCollections: Set ): CollectionSummaries { - const user: User = getData(LS_KEYS.USER); - if (!user) { - return; - } const collectionSummaries: CollectionSummaries = new Map(); const collectionLatestFiles = getCollectionLatestFiles( files, From f8c905e5ac22b7b4e12a4d77b6942e9105a7eb20 Mon Sep 17 00:00:00 2001 From: Abhinav Date: Tue, 26 Jul 2022 14:06:54 +0530 Subject: [PATCH 3/3] make user a state --- src/pages/gallery/index.tsx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/pages/gallery/index.tsx b/src/pages/gallery/index.tsx index 395ea2e61..35e3e0cea 100644 --- a/src/pages/gallery/index.tsx +++ b/src/pages/gallery/index.tsx @@ -134,6 +134,7 @@ export const GalleryContext = createContext( export default function Gallery() { const router = useRouter(); + const [user, setUser] = useState(null); const [collections, setCollections] = useState(null); const [files, setFiles] = useState(null); @@ -246,10 +247,12 @@ export default function Gallery() { setPlanModalView(true); } setIsFirstLogin(false); + const user = getData(LS_KEYS.USER); const files = mergeMetadata(await getLocalFiles()); const collections = await getLocalCollections(); const trash = await getLocalTrash(); files.push(...getTrashedFiles(trash)); + setUser(user); setFiles(sortFiles(files)); setCollections(collections); await syncWithRemote(true); @@ -262,7 +265,6 @@ export default function Gallery() { }, []); useEffect(() => { - const user: User = getData(LS_KEYS.USER); if (!user || !files || !collections) { return; }