Skip to content

fix: add hasToken to avoid loading repo to early #147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions ui/src/lib/auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ function useProvideAuth() {
}
};

/**
* This is not immediately set onrefresh.
*/
const isSignedIn = () => {
if (authToken) {
return true;
Expand All @@ -157,12 +160,20 @@ function useProvideAuth() {
}
};

/**
* This is set immediately on refresh.
*/
function hasToken() {
return localStorage.getItem("token") !== null;
}

return {
createApolloClient,
signIn,
signOut,
handleGoogle,
signUp,
isSignedIn,
hasToken,
};
}
29 changes: 23 additions & 6 deletions ui/src/pages/repo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { Header } from "../components/Header";
import { Sidebar } from "../components/Sidebar";
import { useLocalStorage } from "../hooks/useLocalStorage";
import { Stack, TextField } from "@mui/material";
import { useAuth } from "../lib/auth";

const DrawerWidth = 240;
const SIDEBAR_KEY = "sidebar";
Expand Down Expand Up @@ -160,6 +161,7 @@ function RepoImpl() {
const deleteClient = useStore(store, (state) => state.deleteClient);

const { loading, me } = useMe();
const { hasToken } = useAuth();
useEffect(() => {
if (me) {
setSessionId(`${me.id}_${id}`);
Expand All @@ -184,17 +186,32 @@ function RepoImpl() {
});
});
}
}, [provider]);
}, [addClient, deleteClient, provider]);

useEffect(() => {
resetState();
setRepo(id!);
// load the repo. It is actually not a queue, just an async thunk
loadRepo(client, id!);
if (!loading && me) {
setUser(me);
if (hasToken()) {
if (!loading && me) {
setUser(me);
// load the repo. It is actually not a queue, just an async thunk
loadRepo(client, id!);
}
} else {
// not signed in, just load the repo
loadRepo(client, id!);
}
}, [client, id, loadRepo, resetState, setRepo, me, loading, setUser]);
}, [
client,
id,
loadRepo,
resetState,
setRepo,
me,
loading,
setUser,
hasToken,
]);

// FIXME Removing queueL. This will cause Repo to be re-rendered a lot of
// times, particularly the delete pod action would cause syncstatus and repo
Expand Down