Skip to content

Commit

Permalink
shifting query client clear() for unauthenticated users
Browse files Browse the repository at this point in the history
  • Loading branch information
selfcontained committed Sep 21, 2023
1 parent c02d883 commit 28e8e4c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 20 deletions.
6 changes: 1 addition & 5 deletions components/dashboard/src/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { SSOLoginForm } from "./login/SSOLoginForm";
import { useAuthProviders } from "./data/auth-providers/auth-provider-query";
import { SetupPending } from "./login/SetupPending";
import { useNeedsSetup } from "./dedicated-setup/use-needs-setup";
import { useQueryClient } from "@tanstack/react-query";

export function markLoggedIn() {
document.cookie = GitpodCookie.generateCookie(window.location.hostname);
Expand All @@ -36,7 +35,6 @@ type LoginProps = {
};
export const Login: FC<LoginProps> = ({ onLoggedIn }) => {
const { setUser } = useContext(UserContext);
const client = useQueryClient();

const urlHash = useMemo(() => getURLHash(), []);

Expand Down Expand Up @@ -68,11 +66,9 @@ export const Login: FC<LoginProps> = ({ onLoggedIn }) => {
const updateUser = useCallback(async () => {
await getGitpodService().reconnect();
const user = await getGitpodService().server.getLoggedInUser();
// Clear the query cache on login to reset it
client.clear();
setUser(user);
markLoggedIn();
}, [client, setUser]);
}, [setUser]);

const authorizeSuccessful = useCallback(async () => {
updateUser().catch(console.error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

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

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

// User needs to Login
if (caughtError.code === ErrorCodes.NOT_AUTHENTICATED) {
console.log("clearing query cache for unauthenticated user");
client.clear();
// Before we show a Login screen, check to see if we need to redirect to www site
// Redirects if it's the root, no user, and no gp cookie present (has logged in recently)
if (isGitpodIo() && window.location.pathname === "/" && window.location.hash === "") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@ import { getGitpodService } from "../../service/service";
export const useSuggestedRepositories = () => {
const { data: org } = useCurrentOrg();

return useQuery(["suggested-repositories", { orgId: org?.id }], async () => {
if (!org) {
throw new Error("No org selected");
}
return useQuery(
["suggested-repositories", { orgId: org?.id }],
async () => {
if (!org) {
throw new Error("No org selected");
}

return await getGitpodService().server.getSuggestedRepositories(org.id);
});
return await getGitpodService().server.getSuggestedRepositories(org.id);
},
{
// Keeps data in cache for 24h - will still refresh though
cacheTime: 1000 * 60 * 60 * 24,
},
);
};
6 changes: 6 additions & 0 deletions components/dashboard/src/data/setup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ export function isNoPersistence(queryKey: QueryKey): boolean {

export const setupQueryClientProvider = () => {
const client = new QueryClient({
defaultOptions: {
queries: {
// Default stale time to help avoid re-fetching data too frequently
staleTime: 1000 * 5, // 5 seconds
},
},
queryCache: new QueryCache({
// log any errors our queries throw
onError: (error) => {
Expand Down
9 changes: 1 addition & 8 deletions components/dashboard/src/menu/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { isGitpodIo } from "../utils";
import OrganizationSelector from "./OrganizationSelector";
import { getAdminTabs } from "../admin/admin.routes";
import classNames from "classnames";
import { useQueryClient } from "@tanstack/react-query";

interface Entry {
title: string;
Expand Down Expand Up @@ -168,8 +167,6 @@ type UserMenuProps = {
onFeedback?: () => void;
};
const UserMenu: FC<UserMenuProps> = ({ user, className, withAdminLink, withFeedbackLink, onFeedback }) => {
const client = useQueryClient();

const extraSection = useMemo(() => {
const items: ContextMenuEntry[] = [];

Expand Down Expand Up @@ -222,13 +219,9 @@ const UserMenu: FC<UserMenuProps> = ({ user, className, withAdminLink, withFeedb
{
title: "Log out",
href: gitpodHostUrl.asApiLogout().toString(),
// Clear query cache on logout
onClick: () => {
client.clear();
},
},
];
}, [client, extraSection, user]);
}, [extraSection, user]);

return (
<div
Expand Down

0 comments on commit 28e8e4c

Please sign in to comment.