Skip to content

fix: stop metadata queries from short-circuiting #10312

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 2 commits into from
Oct 17, 2023
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
25 changes: 17 additions & 8 deletions site/src/api/queries/appearance.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
import { QueryClient } from "react-query";
import { QueryClient, type UseQueryOptions } from "react-query";
import * as API from "api/api";
import { AppearanceConfig } from "api/typesGenerated";
import { type AppearanceConfig } from "api/typesGenerated";
import { getMetadataAsJSON } from "utils/metadata";

export const appearance = () => {
const initialAppearanceData = getMetadataAsJSON<AppearanceConfig>("appearance");
const appearanceConfigKey = ["appearance"] as const;

export const appearance = (queryClient: QueryClient) => {
return {
queryKey: ["appearance"],
queryFn: async () =>
getMetadataAsJSON<AppearanceConfig>("appearance") ?? API.getAppearance(),
};
queryKey: appearanceConfigKey,
queryFn: async () => {
const cachedData = queryClient.getQueryData(appearanceConfigKey);
if (cachedData === undefined && initialAppearanceData !== undefined) {
return initialAppearanceData;
}

return API.getAppearance();
},
} satisfies UseQueryOptions<AppearanceConfig>;
};

export const updateAppearance = (queryClient: QueryClient) => {
return {
mutationFn: API.updateAppearance,
onSuccess: (newConfig: AppearanceConfig) => {
queryClient.setQueryData(["appearance"], newConfig);
queryClient.setQueryData(appearanceConfigKey, newConfig);
},
};
};
22 changes: 16 additions & 6 deletions site/src/api/queries/buildInfo.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import { QueryClient, type UseQueryOptions } from "react-query";
import { type BuildInfoResponse } from "api/typesGenerated";
import * as API from "api/api";
import { BuildInfoResponse } from "api/typesGenerated";
import { getMetadataAsJSON } from "utils/metadata";

export const buildInfo = () => {
const initialBuildInfoData = getMetadataAsJSON<BuildInfoResponse>("build-info");
const buildInfoKey = ["buildInfo"] as const;

export const buildInfo = (queryClient: QueryClient) => {
return {
queryKey: ["buildInfo"],
queryFn: async () =>
getMetadataAsJSON<BuildInfoResponse>("build-info") ?? API.getBuildInfo(),
};
queryKey: buildInfoKey,
queryFn: async () => {
const cachedData = queryClient.getQueryData(buildInfoKey);
if (cachedData === undefined && initialBuildInfoData !== undefined) {
return initialBuildInfoData;
}

return API.getBuildInfo();
},
} satisfies UseQueryOptions<BuildInfoResponse>;
};
22 changes: 16 additions & 6 deletions site/src/api/queries/experiments.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import * as API from "api/api";
import { Experiments } from "api/typesGenerated";
import { getMetadataAsJSON } from "utils/metadata";
import { type Experiments } from "api/typesGenerated";
import { QueryClient, type UseQueryOptions } from "react-query";

export const experiments = () => {
const initialExperimentsData = getMetadataAsJSON<Experiments>("experiments");
const experimentsKey = ["experiments"] as const;

export const experiments = (queryClient: QueryClient) => {
return {
queryKey: ["experiments"],
queryFn: async () =>
getMetadataAsJSON<Experiments>("experiments") ?? API.getExperiments(),
};
queryKey: experimentsKey,
queryFn: async () => {
const cachedData = queryClient.getQueryData(experimentsKey);
if (cachedData === undefined && initialExperimentsData !== undefined) {
return initialExperimentsData;
}

return API.getExperiments();
},
} satisfies UseQueryOptions<Experiments>;
};
23 changes: 16 additions & 7 deletions site/src/api/queries/users.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { QueryClient, QueryOptions } from "react-query";
import { QueryClient, type UseQueryOptions } from "react-query";
import * as API from "api/api";
import {
AuthorizationRequest,
Expand All @@ -11,7 +11,7 @@ import {
import { getMetadataAsJSON } from "utils/metadata";
import { getAuthorizationKey } from "./authCheck";

export const users = (req: UsersRequest): QueryOptions<GetUsersResponse> => {
export const users = (req: UsersRequest): UseQueryOptions<GetUsersResponse> => {
return {
queryKey: ["users", req],
queryFn: ({ signal }) => API.getUsers(req, signal),
Expand Down Expand Up @@ -89,12 +89,21 @@ export const authMethods = () => {
};
};

export const me = () => {
const initialMeData = getMetadataAsJSON<User>("user");
const meKey = ["me"] as const;

export const me = (queryClient: QueryClient) => {
return {
queryKey: ["me"],
queryFn: async () =>
getMetadataAsJSON<User>("user") ?? API.getAuthenticatedUser(),
};
queryKey: meKey,
queryFn: async () => {
const cachedData = queryClient.getQueryData(meKey);
if (cachedData === undefined && initialMeData !== undefined) {
return initialMeData;
}

return API.getAuthenticatedUser();
},
} satisfies UseQueryOptions<User>;
};

export const hasFirstUser = () => {
Expand Down
5 changes: 3 additions & 2 deletions site/src/components/AuthProvider/AuthProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ type AuthContextValue = {
const AuthContext = createContext<AuthContextValue | undefined>(undefined);

export const AuthProvider: FC<PropsWithChildren> = ({ children }) => {
const meOptions = me();
const queryClient = useQueryClient();
const meOptions = me(queryClient);

const userQuery = useQuery(meOptions);
const authMethodsQuery = useQuery(authMethods());
const hasFirstUserQuery = useQuery(hasFirstUser());
Expand All @@ -54,7 +56,6 @@ export const AuthProvider: FC<PropsWithChildren> = ({ children }) => {
enabled: userQuery.data !== undefined,
});

const queryClient = useQueryClient();
const loginMutation = useMutation(
login({ checks: permissionsToCheck }, queryClient),
);
Expand Down
12 changes: 7 additions & 5 deletions site/src/components/Dashboard/DashboardProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useQuery } from "react-query";
import { useQuery, useQueryClient } from "react-query";
import { buildInfo } from "api/queries/buildInfo";
import { experiments } from "api/queries/experiments";
import { entitlements } from "api/queries/entitlements";
Expand Down Expand Up @@ -30,19 +30,21 @@ interface Appearance {
interface DashboardProviderValue {
buildInfo: BuildInfoResponse;
entitlements: Entitlements;
appearance: Appearance;
experiments: Experiments;
appearance: Appearance;
}

export const DashboardProviderContext = createContext<
DashboardProviderValue | undefined
>(undefined);

export const DashboardProvider: FC<PropsWithChildren> = ({ children }) => {
const buildInfoQuery = useQuery(buildInfo());
const queryClient = useQueryClient();
const buildInfoQuery = useQuery(buildInfo(queryClient));
const entitlementsQuery = useQuery(entitlements());
const experimentsQuery = useQuery(experiments());
const appearanceQuery = useQuery(appearance());
const experimentsQuery = useQuery(experiments(queryClient));
const appearanceQuery = useQuery(appearance(queryClient));

const isLoading =
!buildInfoQuery.data ||
!entitlementsQuery.data ||
Expand Down
3 changes: 2 additions & 1 deletion site/src/theme/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { getMetadataAsJSON } from "utils/metadata";
// so you can just set this to true.
export const experimentalTheme =
typeof document !== "undefined" &&
getMetadataAsJSON("experiments")?.includes("dashboard_theme");
(getMetadataAsJSON<string[]>("experiments")?.includes("dashboard_theme") ??
false);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugly, but had to do this to make sure that the code is always type boolean and not type any

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I hate this lol. but we can fix it later.


export const colors = {
white: "hsl(0, 0%, 100%)",
Expand Down
24 changes: 15 additions & 9 deletions site/src/utils/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- It can be any
export const getMetadataAsJSON = <T extends Record<string, any>>(
export const getMetadataAsJSON = <T extends NonNullable<unknown>>(
property: string,
): T | undefined => {
const appearance = document.querySelector(`meta[property=${property}]`);

if (appearance) {
const rawContent = appearance.getAttribute("content");
try {
return JSON.parse(rawContent as string);
} catch (ex) {
// In development the metadata is always going to be empty throwing this
// error
if (process.env.NODE_ENV === "production") {
console.warn(`Failed to parse ${property} metadata`);

if (rawContent) {
try {
return JSON.parse(rawContent);
} catch (err) {
// In development, the metadata is always going to be empty; error is
// only a concern for production
if (process.env.NODE_ENV === "production") {
console.warn(`Failed to parse ${property} metadata. Error message:`);
console.warn(err);
}
}
}
}

return undefined;
};