🐛 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:
parent
28927ddc78
commit
1a65530bf9
1 changed files with 9 additions and 1 deletions
|
|
@ -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 }));
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue