🐛 fix(darkStore.tsx): initialize stars variable with the value from localStorage to persist stars count on page reload

✨ feat(darkStore.tsx): add support for storing and retrieving stars count from localStorage to avoid unnecessary API calls
This commit is contained in:
cristhianzl 2024-02-26 14:33:21 -03:00
commit 1a65530bf9

View file

@ -2,9 +2,11 @@ import { create } from "zustand";
import { getRepoStars, getVersion } from "../controllers/API"; import { getRepoStars, getVersion } from "../controllers/API";
import { DarkStoreType } from "../types/zustand/dark"; import { DarkStoreType } from "../types/zustand/dark";
const startedStars = Number(window.localStorage.getItem("githubStars")) ?? 0;
export const useDarkStore = create<DarkStoreType>((set) => ({ export const useDarkStore = create<DarkStoreType>((set) => ({
dark: JSON.parse(window.localStorage.getItem("isDark")!) ?? false, dark: JSON.parse(window.localStorage.getItem("isDark")!) ?? false,
stars: 0, stars: startedStars,
version: "", version: "",
setDark: (dark) => set(() => ({ dark: dark })), setDark: (dark) => set(() => ({ dark: dark })),
refreshVersion: () => { refreshVersion: () => {
@ -13,7 +15,13 @@ export const useDarkStore = create<DarkStoreType>((set) => ({
}); });
}, },
refreshStars: () => { refreshStars: () => {
if (window.localStorage.getItem("githubStars") !== null) {
set(() => ({ stars: startedStars }));
return;
}
getRepoStars("logspace-ai", "langflow").then((res) => { getRepoStars("logspace-ai", "langflow").then((res) => {
window.localStorage.setItem("githubStars", res.toString());
set(() => ({ stars: res })); set(() => ({ stars: res }));
}); });
}, },