From ec46a562883f92ad3ce22978d76e808437166991 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Thu, 28 Mar 2024 16:55:08 -0300 Subject: [PATCH] Refactor useDarkStore in darkStore.tsx --- src/frontend/src/stores/darkStore.tsx | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/frontend/src/stores/darkStore.tsx b/src/frontend/src/stores/darkStore.tsx index cc58944cc..f3ccc2e5c 100644 --- a/src/frontend/src/stores/darkStore.tsx +++ b/src/frontend/src/stores/darkStore.tsx @@ -4,7 +4,7 @@ import { DarkStoreType } from "../types/zustand/dark"; const startedStars = Number(window.localStorage.getItem("githubStars")) ?? 0; -export const useDarkStore = create((set) => ({ +export const useDarkStore = create((set, get) => ({ dark: JSON.parse(window.localStorage.getItem("isDark")!) ?? false, stars: startedStars, version: "", @@ -15,14 +15,23 @@ export const useDarkStore = create((set) => ({ }); }, refreshStars: () => { - if (window.localStorage.getItem("githubStars") !== null) { - set(() => ({ stars: startedStars })); - return; + let lastUpdated = window.localStorage.getItem("githubStarsLastUpdated"); + let diff = 0; + // check if lastUpdated actually exists + if (lastUpdated !== null) { + diff = Math.abs(new Date().getTime() - new Date(lastUpdated).getTime()); } - getRepoStars("logspace-ai", "langflow").then((res) => { - window.localStorage.setItem("githubStars", res.toString()); - set(() => ({ stars: res })); - }); + // if lastUpdated is null or the difference is greater than 2 hours + if (lastUpdated === null || diff > 7200000) { + getRepoStars("logspace-ai", "langflow").then((res) => { + window.localStorage.setItem("githubStars", res.toString()); + window.localStorage.setItem( + "githubStarsLastUpdated", + new Date().toString() + ); + set(() => ({ stars: res, lastUpdated: new Date() })); + }); + } }, }));