From 00e447ed416f08b9f1004ae3ab8cb84f1b371633 Mon Sep 17 00:00:00 2001 From: Jay Harris Date: Wed, 11 Sep 2024 00:53:00 +1200 Subject: [PATCH] [Brave News]: Refresh stale tabs --- .../browser/resources/shared/useFeedV2.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/components/brave_news/browser/resources/shared/useFeedV2.ts b/components/brave_news/browser/resources/shared/useFeedV2.ts index 656d61cd83bc..e496b0315613 100644 --- a/components/brave_news/browser/resources/shared/useFeedV2.ts +++ b/components/brave_news/browser/resources/shared/useFeedV2.ts @@ -185,6 +185,22 @@ export const useFeedV2 = (enabled: boolean) => { fetchFeed(feedView).then(setFeedV2) }, [feedView]) + // When we switch back to this tab, if the feed is stale refresh it. + useEffect(() => { + const handler = () => { + if (document.visibilityState !== 'visible') return + + if (feedV2 && isTooOld(feedV2)) { + refresh() + } + } + + document.addEventListener('visibilitychange', handler) + return () => { + document.removeEventListener('visibilitychange', handler) + } + }, [refresh, feedV2]) + // Updates are available if we've been told the latest hash, we have a feed // and the hashes don't match. const updatesAvailable = !!(hash && feedV2 && hash !== feedV2.sourceHash)