Fix: github stars on header always reloading
This commit is contained in:
parent
76c4d4afe7
commit
a2a3beb868
5 changed files with 49 additions and 48 deletions
|
|
@ -18,20 +18,9 @@ export default function Header(): JSX.Element {
|
||||||
const { dark, setDark } = useContext(darkContext);
|
const { dark, setDark } = useContext(darkContext);
|
||||||
const { notificationCenter } = useContext(alertContext);
|
const { notificationCenter } = useContext(alertContext);
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
const { logout, autoLogin, isAdmin, setIsAdmin } = useContext(AuthContext);
|
const { logout, autoLogin, isAdmin, stars } = useContext(AuthContext);
|
||||||
const navigate = useNavigate();
|
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 (
|
return (
|
||||||
<div className="header-arrangement">
|
<div className="header-arrangement">
|
||||||
<div className="header-start-display">
|
<div className="header-start-display">
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import { createContext, useEffect, useState } from "react";
|
import { createContext, useEffect, useState } from "react";
|
||||||
import Cookies from "universal-cookie";
|
import Cookies from "universal-cookie";
|
||||||
import { getLoggedUser } from "../controllers/API";
|
import { getLoggedUser, getRepoStars } from "../controllers/API";
|
||||||
import { Users } from "../types/api";
|
import { Users } from "../types/api";
|
||||||
import { AuthContextType } from "../types/contexts/auth";
|
import { AuthContextType } from "../types/contexts/auth";
|
||||||
|
|
||||||
|
|
@ -19,6 +19,8 @@ const initialValue: AuthContextType = {
|
||||||
authenticationErrorCount: 0,
|
authenticationErrorCount: 0,
|
||||||
autoLogin: false,
|
autoLogin: false,
|
||||||
setAutoLogin: () => {},
|
setAutoLogin: () => {},
|
||||||
|
stars: 0,
|
||||||
|
setStars: (stars) => 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
export const AuthContext = createContext<AuthContextType>(initialValue);
|
export const AuthContext = createContext<AuthContextType>(initialValue);
|
||||||
|
|
@ -30,6 +32,7 @@ export function AuthProvider({ children }): React.ReactElement {
|
||||||
const [isAdmin, setIsAdmin] = useState<boolean>(false);
|
const [isAdmin, setIsAdmin] = useState<boolean>(false);
|
||||||
const [userData, setUserData] = useState<Users | null>(null);
|
const [userData, setUserData] = useState<Users | null>(null);
|
||||||
const [autoLogin, setAutoLogin] = useState<boolean>(false);
|
const [autoLogin, setAutoLogin] = useState<boolean>(false);
|
||||||
|
const [stars, setStars] = useState<number>(0);
|
||||||
const cookies = new Cookies();
|
const cookies = new Cookies();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|
@ -37,6 +40,11 @@ export function AuthProvider({ children }): React.ReactElement {
|
||||||
if (storedAccessToken) {
|
if (storedAccessToken) {
|
||||||
setAccessToken(storedAccessToken);
|
setAccessToken(storedAccessToken);
|
||||||
}
|
}
|
||||||
|
async function fetchStars() {
|
||||||
|
const starsCount = await getRepoStars("logspace-ai", "langflow");
|
||||||
|
setStars(starsCount);
|
||||||
|
}
|
||||||
|
fetchStars();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|
@ -100,6 +108,8 @@ export function AuthProvider({ children }): React.ReactElement {
|
||||||
// !! to convert string to boolean
|
// !! to convert string to boolean
|
||||||
<AuthContext.Provider
|
<AuthContext.Provider
|
||||||
value={{
|
value={{
|
||||||
|
stars,
|
||||||
|
setStars,
|
||||||
isAdmin,
|
isAdmin,
|
||||||
setIsAdmin,
|
setIsAdmin,
|
||||||
isAuthenticated: !!accessToken,
|
isAuthenticated: !!accessToken,
|
||||||
|
|
|
||||||
|
|
@ -88,9 +88,9 @@ const Router = () => {
|
||||||
<Route
|
<Route
|
||||||
path="/admin"
|
path="/admin"
|
||||||
element={
|
element={
|
||||||
<ProtectedAdminRoute>
|
|
||||||
<AdminPage />
|
<AdminPage />
|
||||||
</ProtectedAdminRoute>
|
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,4 +15,6 @@ export type AuthContextType = {
|
||||||
authenticationErrorCount: number;
|
authenticationErrorCount: number;
|
||||||
autoLogin: boolean;
|
autoLogin: boolean;
|
||||||
setAutoLogin: (autoLogin: boolean) => void;
|
setAutoLogin: (autoLogin: boolean) => void;
|
||||||
|
stars: number;
|
||||||
|
setStars: (stars: number) => void;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue