Fix: github stars on header always reloading

This commit is contained in:
igorrCarvalho 2023-08-21 19:54:20 -03:00
commit a2a3beb868
5 changed files with 49 additions and 48 deletions

View file

@ -18,20 +18,9 @@ export default function Header(): JSX.Element {
const { dark, setDark } = useContext(darkContext);
const { notificationCenter } = useContext(alertContext);
const location = useLocation();
const { logout, autoLogin, isAdmin, setIsAdmin } = useContext(AuthContext);
const { logout, autoLogin, isAdmin, stars } = useContext(AuthContext);
const navigate = useNavigate();
const [stars, setStars] = useState(null);
// Get and set numbers of stars on header
useEffect(() => {
async function fetchStars() {
const starsCount = await getRepoStars("logspace-ai", "langflow");
setStars(starsCount);
}
fetchStars();
}, []);
return (
<div className="header-arrangement">
<div className="header-start-display">

View file

@ -1,6 +1,6 @@
import { createContext, useEffect, useState } from "react";
import Cookies from "universal-cookie";
import { getLoggedUser } from "../controllers/API";
import { getLoggedUser, getRepoStars } from "../controllers/API";
import { Users } from "../types/api";
import { AuthContextType } from "../types/contexts/auth";
@ -19,6 +19,8 @@ const initialValue: AuthContextType = {
authenticationErrorCount: 0,
autoLogin: false,
setAutoLogin: () => {},
stars: 0,
setStars: (stars) => 0,
};
export const AuthContext = createContext<AuthContextType>(initialValue);
@ -30,6 +32,7 @@ export function AuthProvider({ children }): React.ReactElement {
const [isAdmin, setIsAdmin] = useState<boolean>(false);
const [userData, setUserData] = useState<Users | null>(null);
const [autoLogin, setAutoLogin] = useState<boolean>(false);
const [stars, setStars] = useState<number>(0);
const cookies = new Cookies();
useEffect(() => {
@ -37,6 +40,11 @@ export function AuthProvider({ children }): React.ReactElement {
if (storedAccessToken) {
setAccessToken(storedAccessToken);
}
async function fetchStars() {
const starsCount = await getRepoStars("logspace-ai", "langflow");
setStars(starsCount);
}
fetchStars();
}, []);
useEffect(() => {
@ -100,6 +108,8 @@ export function AuthProvider({ children }): React.ReactElement {
// !! to convert string to boolean
<AuthContext.Provider
value={{
stars,
setStars,
isAdmin,
setIsAdmin,
isAuthenticated: !!accessToken,

View file

@ -88,9 +88,9 @@ const Router = () => {
<Route
path="/admin"
element={
<ProtectedAdminRoute>
<AdminPage />
</ProtectedAdminRoute>
}
/>

View file

@ -15,4 +15,6 @@ export type AuthContextType = {
authenticationErrorCount: number;
autoLogin: boolean;
setAutoLogin: (autoLogin: boolean) => void;
stars: number;
setStars: (stars: number) => void;
};