Skip to content

Commit b02c94d

Browse files
Fix react-query cache persistence (#18762)
* adjust when we clear query cache * adjusting icon/color * remove log statement * strip off protocol * shifting query client clear() for unauthenticated users
1 parent d7ba4db commit b02c94d

File tree

7 files changed

+64
-54
lines changed

7 files changed

+64
-54
lines changed

components/dashboard/src/Login.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export const Login: FC<LoginProps> = ({ onLoggedIn }) => {
6565

6666
const updateUser = useCallback(async () => {
6767
await getGitpodService().reconnect();
68-
const [user] = await Promise.all([getGitpodService().server.getLoggedInUser()]);
68+
const user = await getGitpodService().server.getLoggedInUser();
6969
setUser(user);
7070
markLoggedIn();
7171
}, [setUser]);

components/dashboard/src/components/RepositoryFinder.tsx

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { ReactComponent as RepositoryIcon } from "../icons/RepositoryWithColor.s
1212
import { useSuggestedRepositories } from "../data/git-providers/suggested-repositories-query";
1313
import { useFeatureFlag } from "../data/featureflag-query";
1414
import { SuggestedRepository } from "@gitpod/gitpod-protocol";
15-
import classNames from "classnames";
1615
import { MiddleDot } from "./typography/MiddleDot";
1716

1817
// TODO: Remove this once we've fully enabled `includeProjectsOnCreateWorkspace`
@@ -157,14 +156,7 @@ export default function RepositoryFinder(props: RepositoryFinderProps) {
157156
searchPlaceholder="Paste repository URL or type to find suggestions"
158157
>
159158
<DropDown2SelectedElement
160-
// TODO: clean this up - have to use different icons to keep the right shade of gray when there's no project
161-
icon={
162-
selectedSuggestion?.projectId ? (
163-
<RepositoryIcon className={classNames("w-8 h-8 text-orange-500")} />
164-
) : (
165-
RepositorySVG
166-
)
167-
}
159+
icon={RepositorySVG}
168160
htmlTitle={displayContextUrl(props.selectedContextURL) || "Repository"}
169161
title={
170162
<div className="truncate w-80">
@@ -178,7 +170,7 @@ export default function RepositoryFinder(props: RepositoryFinderProps) {
178170
subtitle={
179171
// Only show the url if we have a project or repo name, otherwise it's redundant w/ the title
180172
selectedSuggestion?.projectName || selectedSuggestion?.repositoryName
181-
? selectedSuggestion?.url
173+
? displayContextUrl(selectedSuggestion?.url)
182174
: undefined
183175
}
184176
loading={props.loading || isLoading}
@@ -199,9 +191,7 @@ const SuggestedRepositoryOption: FC<SuggestedRepositoryOptionProps> = ({ repo })
199191
aria-label={`${repo.projectId ? "Project" : "Repo"}: ${repo.url}`}
200192
>
201193
<span className={"pr-2"}>
202-
<RepositoryIcon
203-
className={classNames("w-5 h-5 text-orange-500", !repo.projectId && "filter-grayscale")}
204-
/>
194+
<RepositoryIcon className={"w-5 h-5 text-gray-400"} />
205195
</span>
206196

207197
{name && (

components/dashboard/src/components/error-boundaries/QueryErrorBoundary.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
import { ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
8-
import { QueryErrorResetBoundary } from "@tanstack/react-query";
8+
import { QueryErrorResetBoundary, useQueryClient } from "@tanstack/react-query";
99
import { FC } from "react";
1010
import { ErrorBoundary, FallbackProps } from "react-error-boundary";
1111
import { hasLoggedInBefore, Login } from "../../Login";
@@ -28,11 +28,14 @@ export const QueryErrorBoundary: FC = ({ children }) => {
2828

2929
// This handles any expected errors, i.e. errors w/ a code that an api call produced
3030
const ExpectedQueryErrorsFallback: FC<FallbackProps> = ({ error, resetErrorBoundary }) => {
31+
const client = useQueryClient();
3132
// adjust typing, as we may have caught an api error here w/ a code property
3233
const caughtError = error as CaughtError;
3334

3435
// User needs to Login
3536
if (caughtError.code === ErrorCodes.NOT_AUTHENTICATED) {
37+
console.log("clearing query cache for unauthenticated user");
38+
client.clear();
3639
// Before we show a Login screen, check to see if we need to redirect to www site
3740
// Redirects if it's the root, no user, and no gp cookie present (has logged in recently)
3841
if (isGitpodIo() && window.location.pathname === "/" && window.location.hash === "") {

components/dashboard/src/data/git-providers/suggested-repositories-query.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,18 @@ import { getGitpodService } from "../../service/service";
1111
export const useSuggestedRepositories = () => {
1212
const { data: org } = useCurrentOrg();
1313

14-
return useQuery(["suggested-repositories", { orgId: org?.id }], async () => {
15-
if (!org) {
16-
throw new Error("No org selected");
17-
}
14+
return useQuery(
15+
["suggested-repositories", { orgId: org?.id }],
16+
async () => {
17+
if (!org) {
18+
throw new Error("No org selected");
19+
}
1820

19-
return await getGitpodService().server.getSuggestedRepositories(org.id);
20-
});
21+
return await getGitpodService().server.getSuggestedRepositories(org.id);
22+
},
23+
{
24+
// Keeps data in cache for 24h - will still refresh though
25+
cacheTime: 1000 * 60 * 60 * 24,
26+
},
27+
);
2128
};

components/dashboard/src/data/setup.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ export function isNoPersistence(queryKey: QueryKey): boolean {
3030

3131
export const setupQueryClientProvider = () => {
3232
const client = new QueryClient({
33+
defaultOptions: {
34+
queries: {
35+
// Default stale time to help avoid re-fetching data too frequently
36+
staleTime: 1000 * 5, // 5 seconds
37+
},
38+
},
3339
queryCache: new QueryCache({
3440
// log any errors our queries throw
3541
onError: (error) => {

components/dashboard/src/menu/Menu.tsx

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,38 @@ const UserMenu: FC<UserMenuProps> = ({ user, className, withAdminLink, withFeedb
191191
return items;
192192
}, [onFeedback, user?.rolesOrPermissions, withAdminLink, withFeedbackLink]);
193193

194+
const menuEntries = useMemo(() => {
195+
return [
196+
{
197+
title: (user && (User.getPrimaryEmail(user) || user?.name)) || "User",
198+
customFontStyle: "text-gray-400",
199+
separator: true,
200+
},
201+
{
202+
title: "User Settings",
203+
link: "/user/settings",
204+
},
205+
{
206+
title: "Docs",
207+
href: "https://www.gitpod.io/docs/",
208+
target: "_blank",
209+
rel: "noreferrer",
210+
},
211+
{
212+
title: "Help",
213+
href: "https://www.gitpod.io/support/",
214+
target: "_blank",
215+
rel: "noreferrer",
216+
separator: true,
217+
},
218+
...extraSection,
219+
{
220+
title: "Log out",
221+
href: gitpodHostUrl.asApiLogout().toString(),
222+
},
223+
];
224+
}, [extraSection, user]);
225+
194226
return (
195227
<div
196228
className={classNames(
@@ -199,37 +231,7 @@ const UserMenu: FC<UserMenuProps> = ({ user, className, withAdminLink, withFeedb
199231
)}
200232
data-analytics='{"label":"Account"}'
201233
>
202-
<ContextMenu
203-
menuEntries={[
204-
{
205-
title: (user && (User.getPrimaryEmail(user) || user?.name)) || "User",
206-
customFontStyle: "text-gray-400",
207-
separator: true,
208-
},
209-
{
210-
title: "User Settings",
211-
link: "/user/settings",
212-
},
213-
{
214-
title: "Docs",
215-
href: "https://www.gitpod.io/docs/",
216-
target: "_blank",
217-
rel: "noreferrer",
218-
},
219-
{
220-
title: "Help",
221-
href: "https://www.gitpod.io/support/",
222-
target: "_blank",
223-
rel: "noreferrer",
224-
separator: true,
225-
},
226-
...extraSection,
227-
{
228-
title: "Log out",
229-
href: gitpodHostUrl.asApiLogout().toString(),
230-
},
231-
]}
232-
>
234+
<ContextMenu menuEntries={menuEntries}>
233235
<img className="rounded-full w-8 h-8" src={user?.avatarUrl || ""} alt={user?.name || "Anonymous"} />
234236
</ContextMenu>
235237
</div>

components/dashboard/src/user-context.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ const UserContextProvider: React.FC = ({ children }) => {
2929
const doSetUser = useCallback(
3030
(updatedUser: User) => {
3131
updateErrorUserDetails(updatedUser);
32-
if (user?.id !== updatedUser.id) {
32+
// If user has changed clear cache
33+
// Ignore the case where user hasn't been set yet - initial load
34+
if (user && user?.id !== updatedUser.id) {
3335
client.clear();
3436
}
3537
setUser(updatedUser);
@@ -53,7 +55,7 @@ const UserContextProvider: React.FC = ({ children }) => {
5355
}, frequencyMs);
5456
}
5557
},
56-
[user?.id, setUser, client],
58+
[user, client],
5759
);
5860

5961
// Wrap value in useMemo to avoid unnecessary re-renders

0 commit comments

Comments
 (0)