From e2a49aec71f0f5ec89285f8ee31f7754e89da28c Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Mon, 19 Aug 2024 15:10:20 +0800 Subject: [PATCH] Trim old pages from the cache --- .../library_items/useLibraryItems.tsx | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/packages/web/lib/networking/library_items/useLibraryItems.tsx b/packages/web/lib/networking/library_items/useLibraryItems.tsx index 3dcdfc9cb..517fe0ba9 100644 --- a/packages/web/lib/networking/library_items/useLibraryItems.tsx +++ b/packages/web/lib/networking/library_items/useLibraryItems.tsx @@ -252,10 +252,6 @@ export function useGetLibraryItems( // First check if the previous page had detected a modification // if it had we keep fetching until we find a - console.log( - 'GOING TO USE cached page: ', - cached.pages[idx - 1].pageInfo.wasUnchanged - ) if ( idx > 0 && idx < cached.pages.length && @@ -279,11 +275,6 @@ export function useGetLibraryItems( query: fullQuery, includeContent: false, })) as LibraryItemsData - - console.log( - 'cached.pageParams.indexOf(pageParam)', - cached.pageParams.indexOf(pageParam) - ) let wasUnchanged = false if (cached && cached.pageParams.indexOf(pageParam) > -1) { const idx = cached.pageParams.indexOf(pageParam) @@ -317,6 +308,28 @@ export function useGetLibraryItems( ? lastPage?.pageInfo?.endCursor : undefined }, + select: (data) => { + const now = new Date() + + // Filter pages based on the lastUpdated condition + const filteredPages = data.pages.slice(0, 5).concat( + data.pages.slice(5).filter((page, index) => { + if (page.pageInfo?.lastUpdated) { + const lastUpdatedDate = new Date(page.pageInfo.lastUpdated) + const diffMinutes = + (now.getTime() - lastUpdatedDate.getTime()) / (1000 * 60) + console.log(`page: ${index} age: ${diffMinutes}`) + return diffMinutes <= 10 + } + return true + }) + ) + + return { + ...data, + pages: filteredPages, + } + }, }) }