Skip to content

Commit 1ffe316

Browse files
authored
add hasToken to avoid loading repo to early (#147)
1 parent 70b5ffa commit 1ffe316

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

ui/src/lib/auth.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ function useProvideAuth() {
149149
}
150150
};
151151

152+
/**
153+
* This is not immediately set onrefresh.
154+
*/
152155
const isSignedIn = () => {
153156
if (authToken) {
154157
return true;
@@ -157,12 +160,20 @@ function useProvideAuth() {
157160
}
158161
};
159162

163+
/**
164+
* This is set immediately on refresh.
165+
*/
166+
function hasToken() {
167+
return localStorage.getItem("token") !== null;
168+
}
169+
160170
return {
161171
createApolloClient,
162172
signIn,
163173
signOut,
164174
handleGoogle,
165175
signUp,
166176
isSignedIn,
177+
hasToken,
167178
};
168179
}

ui/src/pages/repo.tsx

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { Header } from "../components/Header";
1818
import { Sidebar } from "../components/Sidebar";
1919
import { useLocalStorage } from "../hooks/useLocalStorage";
2020
import { Stack, TextField } from "@mui/material";
21+
import { useAuth } from "../lib/auth";
2122

2223
const DrawerWidth = 240;
2324
const SIDEBAR_KEY = "sidebar";
@@ -160,6 +161,7 @@ function RepoImpl() {
160161
const deleteClient = useStore(store, (state) => state.deleteClient);
161162

162163
const { loading, me } = useMe();
164+
const { hasToken } = useAuth();
163165
useEffect(() => {
164166
if (me) {
165167
setSessionId(`${me.id}_${id}`);
@@ -184,17 +186,32 @@ function RepoImpl() {
184186
});
185187
});
186188
}
187-
}, [provider]);
189+
}, [addClient, deleteClient, provider]);
188190

189191
useEffect(() => {
190192
resetState();
191193
setRepo(id!);
192-
// load the repo. It is actually not a queue, just an async thunk
193-
loadRepo(client, id!);
194-
if (!loading && me) {
195-
setUser(me);
194+
if (hasToken()) {
195+
if (!loading && me) {
196+
setUser(me);
197+
// load the repo. It is actually not a queue, just an async thunk
198+
loadRepo(client, id!);
199+
}
200+
} else {
201+
// not signed in, just load the repo
202+
loadRepo(client, id!);
196203
}
197-
}, [client, id, loadRepo, resetState, setRepo, me, loading, setUser]);
204+
}, [
205+
client,
206+
id,
207+
loadRepo,
208+
resetState,
209+
setRepo,
210+
me,
211+
loading,
212+
setUser,
213+
hasToken,
214+
]);
198215

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

0 commit comments

Comments
 (0)