From 2f9a4c3d4ff57bed647a9658ff8c5cdba6b69fdc Mon Sep 17 00:00:00 2001 From: imperosol Date: Wed, 6 May 2026 21:35:15 +0200 Subject: [PATCH] feat: use a queue in user pictures localstorage --- sas/static/bundled/sas/user/pictures-index.ts | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/sas/static/bundled/sas/user/pictures-index.ts b/sas/static/bundled/sas/user/pictures-index.ts index abbdc2c8..de08f858 100644 --- a/sas/static/bundled/sas/user/pictures-index.ts +++ b/sas/static/bundled/sas/user/pictures-index.ts @@ -22,35 +22,35 @@ document.addEventListener("alpine:init", () => { albums: [] as Album[], async fetchPictures(): Promise { - const localStorageKey = `user${config.userId}Pictures`; - const localStorageInvalidationKey = `user${config.userId}PicturesNumber`; - const lastCachedNumber = localStorage.getItem(localStorageInvalidationKey); + // Check the cache before hitting the API. + const localStorageKey = "userPictures"; + const cacheContent: { userId: number; pictures: PictureSchema[] }[] = JSON.parse( + localStorage.getItem(localStorageKey) || "[]", + ); + const userPictures = cacheContent.find((obj) => obj.userId === config.userId); if ( - lastCachedNumber !== null && - Number.parseInt(lastCachedNumber, 10) === config.nbPictures + userPictures !== undefined && + userPictures.pictures.length === config.nbPictures ) { - return JSON.parse(localStorage.getItem(localStorageKey)); + // The cached value is considered valid + // if it contains the right amount of pictures. + // This amount is known because it is given in the template. + return userPictures.pictures; } const pictures = await paginated(picturesFetchPictures, { // biome-ignore lint/style/useNamingConvention: from python api query: { users_identified: [config.userId] }, } as PicturesFetchPicturesData); + + cacheContent.push({ userId: config.userId, pictures: pictures }); try { - localStorage.setItem(localStorageInvalidationKey, config.nbPictures.toString()); - localStorage.setItem(localStorageKey, JSON.stringify(pictures)); + // cache only the pictures of the last 4 visited profiles + localStorage.setItem(localStorageKey, JSON.stringify(cacheContent.slice(-4))); } catch { - // an exception is raised if the localstorage is entirely filled - // so just delete all cached user pictures. + // an exception is raised if the localstorage is entirely filled. + // To be as safe as possible, delete the cached pictures. // A cache hit is not worth the page breaking. - Object.keys(localStorage) - .filter( - (key) => - key.startsWith("user") && - (key.endsWith("Pictures") || key.endsWith("PicturesNumber")), - ) - .forEach((key) => { - localStorage.removeItem(key); - }); + localStorage.removeItem(localStorageKey); } return pictures; },