Skip to content

Commit 3b088a5

Browse files
chore(site): refactor deployment values service to react-query (#9669)
1 parent 225cf8a commit 3b088a5

File tree

10 files changed

+39
-126
lines changed

10 files changed

+39
-126
lines changed

site/src/api/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,7 @@ export type DeploymentConfig = {
10061006
readonly options: DeploymentOption[];
10071007
};
10081008

1009-
export const getDeploymentValues = async (): Promise<DeploymentConfig> => {
1009+
export const getDeploymentConfig = async (): Promise<DeploymentConfig> => {
10101010
const response = await axios.get(`/api/v2/deployment/config`);
10111011
return response.data;
10121012
};

site/src/api/queries/deployment.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import * as API from "api/api";
2+
3+
export const deploymentConfig = () => {
4+
return {
5+
queryKey: ["deployment", "config"],
6+
queryFn: API.getDeploymentConfig,
7+
};
8+
};
9+
10+
export const deploymentDAUs = () => {
11+
return {
12+
queryKey: ["deployment", "daus"],
13+
queryFn: () => API.getDeploymentDAUs(),
14+
};
15+
};

site/src/components/DeploySettingsLayout/DeploySettingsLayout.tsx

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,16 @@ import { Margins } from "components/Margins/Margins";
33
import { Stack } from "components/Stack/Stack";
44
import { Sidebar } from "./Sidebar";
55
import { createContext, Suspense, useContext, FC } from "react";
6-
import { useMachine } from "@xstate/react";
76
import { Loader } from "components/Loader/Loader";
8-
import { DAUsResponse } from "api/typesGenerated";
9-
import { deploymentConfigMachine } from "xServices/deploymentConfig/deploymentConfigMachine";
107
import { RequirePermission } from "components/RequirePermission/RequirePermission";
118
import { usePermissions } from "hooks/usePermissions";
129
import { Outlet } from "react-router-dom";
1310
import { DeploymentConfig } from "api/api";
11+
import { useQuery } from "@tanstack/react-query";
12+
import { deploymentConfig } from "api/queries/deployment";
1413

1514
type DeploySettingsContextValue = {
1615
deploymentValues: DeploymentConfig;
17-
getDeploymentValuesError: unknown;
18-
deploymentDAUs?: DAUsResponse;
19-
getDeploymentDAUsError: unknown;
2016
};
2117

2218
const DeploySettingsContext = createContext<
@@ -34,14 +30,8 @@ export const useDeploySettings = (): DeploySettingsContextValue => {
3430
};
3531

3632
export const DeploySettingsLayout: FC = () => {
37-
const [state] = useMachine(deploymentConfigMachine);
33+
const deploymentConfigQuery = useQuery(deploymentConfig());
3834
const styles = useStyles();
39-
const {
40-
deploymentValues,
41-
deploymentDAUs,
42-
getDeploymentValuesError,
43-
getDeploymentDAUsError,
44-
} = state.context;
4535
const permissions = usePermissions();
4636

4737
return (
@@ -50,13 +40,10 @@ export const DeploySettingsLayout: FC = () => {
5040
<Stack className={styles.wrapper} direction="row" spacing={6}>
5141
<Sidebar />
5242
<main className={styles.content}>
53-
{deploymentValues ? (
43+
{deploymentConfigQuery.data ? (
5444
<DeploySettingsContext.Provider
5545
value={{
56-
deploymentValues,
57-
getDeploymentValuesError,
58-
deploymentDAUs,
59-
getDeploymentDAUsError,
46+
deploymentValues: deploymentConfigQuery.data,
6047
}}
6148
>
6249
<Suspense fallback={<Loader />}>

site/src/pages/DeploySettingsPage/GeneralSettingsPage/GeneralSettingsPage.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ import { FC } from "react";
33
import { Helmet } from "react-helmet-async";
44
import { pageTitle } from "utils/page";
55
import { GeneralSettingsPageView } from "./GeneralSettingsPageView";
6+
import { useQuery } from "@tanstack/react-query";
7+
import { deploymentDAUs } from "api/queries/deployment";
68

79
const GeneralSettingsPage: FC = () => {
8-
const { deploymentValues, deploymentDAUs, getDeploymentDAUsError } =
9-
useDeploySettings();
10+
const { deploymentValues } = useDeploySettings();
11+
const deploymentDAUsQuery = useQuery(deploymentDAUs());
1012

1113
return (
1214
<>
@@ -15,8 +17,8 @@ const GeneralSettingsPage: FC = () => {
1517
</Helmet>
1618
<GeneralSettingsPageView
1719
deploymentOptions={deploymentValues.options}
18-
deploymentDAUs={deploymentDAUs}
19-
getDeploymentDAUsError={getDeploymentDAUsError}
20+
deploymentDAUs={deploymentDAUsQuery.data}
21+
deploymentDAUsError={deploymentDAUsQuery.error}
2022
/>
2123
</>
2224
);

site/src/pages/DeploySettingsPage/GeneralSettingsPage/GeneralSettingsPageView.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export const NoDAUs: Story = {
5959
export const DAUError: Story = {
6060
args: {
6161
deploymentDAUs: undefined,
62-
getDeploymentDAUsError: mockApiError({
62+
deploymentDAUsError: mockApiError({
6363
message: "Error fetching DAUs.",
6464
}),
6565
},

site/src/pages/DeploySettingsPage/GeneralSettingsPage/GeneralSettingsPageView.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ import { DeploymentOption } from "api/api";
1313
export type GeneralSettingsPageViewProps = {
1414
deploymentOptions: DeploymentOption[];
1515
deploymentDAUs?: DAUsResponse;
16-
getDeploymentDAUsError: unknown;
16+
deploymentDAUsError: unknown;
1717
};
1818
export const GeneralSettingsPageView = ({
1919
deploymentOptions,
2020
deploymentDAUs,
21-
getDeploymentDAUsError,
21+
deploymentDAUsError,
2222
}: GeneralSettingsPageViewProps): JSX.Element => {
2323
return (
2424
<>
@@ -28,8 +28,8 @@ export const GeneralSettingsPageView = ({
2828
docsHref={docs("/admin/configure")}
2929
/>
3030
<Stack spacing={4}>
31-
{Boolean(getDeploymentDAUsError) && (
32-
<ErrorAlert error={getDeploymentDAUsError} />
31+
{Boolean(deploymentDAUsError) && (
32+
<ErrorAlert error={deploymentDAUsError} />
3333
)}
3434
{deploymentDAUs && (
3535
<Box height={200} sx={{ mb: 3 }}>

site/src/pages/UsersPage/UsersPage.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ import { UsersPageView } from "./UsersPageView";
1818
import { useStatusFilterMenu } from "./UsersFilter";
1919
import { useFilter } from "components/Filter/filter";
2020
import { useDashboard } from "components/Dashboard/DashboardProvider";
21-
import { deploymentConfigMachine } from "xServices/deploymentConfig/deploymentConfigMachine";
2221
import { useQuery } from "@tanstack/react-query";
2322
import { getAuthMethods } from "api/api";
2423
import { roles } from "api/queries/roles";
24+
import { deploymentConfig } from "api/queries/deployment";
2525

2626
export const Language = {
2727
suspendDialogTitle: "Suspend user",
@@ -62,14 +62,12 @@ export const UsersPage: FC<{ children?: ReactNode }> = () => {
6262
paginationRef,
6363
count,
6464
} = usersState.context;
65-
6665
const { updateUsers: canEditUsers, viewDeploymentValues } = usePermissions();
6766
const rolesQuery = useQuery({ ...roles(), enabled: canEditUsers });
68-
69-
// Ideally this only runs if 'canViewDeployment' is true.
70-
// TODO: Prevent api call if the user does not have the perms.
71-
const [state] = useMachine(deploymentConfigMachine);
72-
const { deploymentValues } = state.context;
67+
const { data: deploymentValues } = useQuery({
68+
...deploymentConfig(),
69+
enabled: viewDeploymentValues,
70+
});
7371
// Indicates if oidc roles are synced from the oidc idp.
7472
// Assign 'false' if unknown.
7573
const oidcRoleSyncEnabled =

site/src/pages/WorkspacePage/WorkspacePage.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const renderWorkspacePage = async () => {
3838
jest.spyOn(api, "getTemplate").mockResolvedValueOnce(MockTemplate);
3939
jest.spyOn(api, "getTemplateVersionRichParameters").mockResolvedValueOnce([]);
4040
jest
41-
.spyOn(api, "getDeploymentValues")
41+
.spyOn(api, "getDeploymentConfig")
4242
.mockResolvedValueOnce(MockDeploymentConfig);
4343
jest
4444
.spyOn(api, "watchWorkspaceAgentLogs")

site/src/xServices/deploymentConfig/deploymentConfigMachine.ts

Lines changed: 0 additions & 89 deletions
This file was deleted.

site/src/xServices/workspace/workspaceXService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ async function loadInitialWorkspaceData({
781781
(permissions as Permissions)?.viewDeploymentValues,
782782
);
783783
const deploymentValues = canViewDeploymentValues
784-
? (await API.getDeploymentValues())?.config
784+
? (await API.getDeploymentConfig())?.config
785785
: undefined;
786786
return {
787787
workspace,

0 commit comments

Comments
 (0)