Skip to content

Commit 1499bb5

Browse files
committed
chore(site): refactor workspace quota to use react-query instead of XState
1 parent 1a1c230 commit 1499bb5

File tree

5 files changed

+24
-75
lines changed

5 files changed

+24
-75
lines changed

site/src/api/api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -963,9 +963,9 @@ export const deleteGroup = async (groupId: string): Promise<void> => {
963963
};
964964

965965
export const getWorkspaceQuota = async (
966-
userID: string,
966+
username: string,
967967
): Promise<TypesGen.WorkspaceQuota> => {
968-
const response = await axios.get(`/api/v2/workspace-quota/${userID}`);
968+
const response = await axios.get(`/api/v2/workspace-quota/${username}`);
969969
return response.data;
970970
};
971971

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { useQuery } from "@tanstack/react-query";
2+
import * as API from "api/api";
3+
4+
const getWorkspaceQuotaQueryKey = (username: string) => [
5+
username,
6+
"workspaceQuota",
7+
];
8+
9+
export const useWorkspaceQuota = (username: string) => {
10+
return useQuery({
11+
queryKey: getWorkspaceQuotaQueryKey(username),
12+
queryFn: () => API.getWorkspaceQuota(username),
13+
});
14+
};

site/src/pages/WorkspacePage/WorkspacePage.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import { ChooseOne, Cond } from "components/Conditionals/ChooseOne";
33
import { Loader } from "components/Loader/Loader";
44
import { FC } from "react";
55
import { useParams } from "react-router-dom";
6-
import { quotaMachine } from "xServices/quotas/quotasXService";
76
import { workspaceMachine } from "xServices/workspace/workspaceXService";
87
import { WorkspaceReadyPage } from "./WorkspaceReadyPage";
98
import { RequirePermission } from "components/RequirePermission/RequirePermission";
109
import { ErrorAlert } from "components/Alert/ErrorAlert";
1110
import { useOrganizationId } from "hooks";
1211
import { isAxiosError } from "axios";
1312
import { Margins } from "components/Margins/Margins";
13+
import { useWorkspaceQuota } from "api/queries/workspaceQuota";
1414

1515
export const WorkspacePage: FC = () => {
1616
const params = useParams() as {
@@ -28,9 +28,8 @@ export const WorkspacePage: FC = () => {
2828
},
2929
});
3030
const { workspace, error } = workspaceState.context;
31-
const [quotaState] = useMachine(quotaMachine, { context: { username } });
32-
const { getQuotaError } = quotaState.context;
33-
const pageError = error ?? getQuotaError;
31+
const quotaQuery = useWorkspaceQuota(username);
32+
const pageError = error ?? quotaQuery.error;
3433

3534
return (
3635
<RequirePermission
@@ -48,12 +47,12 @@ export const WorkspacePage: FC = () => {
4847
condition={
4948
Boolean(workspace) &&
5049
workspaceState.matches("ready") &&
51-
quotaState.matches("success")
50+
quotaQuery.isSuccess
5251
}
5352
>
5453
<WorkspaceReadyPage
5554
workspaceState={workspaceState}
56-
quotaState={quotaState}
55+
quota={quotaQuery.data}
5756
workspaceSend={workspaceSend}
5857
/>
5958
</Cond>

site/src/pages/WorkspacePage/WorkspaceReadyPage.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
getMaxDeadlineChange,
1313
getMinDeadline,
1414
} from "utils/schedule";
15-
import { quotaMachine } from "xServices/quotas/quotasXService";
1615
import { StateFrom } from "xstate";
1716
import { DeleteDialog } from "components/Dialogs/DeleteDialog/DeleteDialog";
1817
import { Workspace, WorkspaceErrors } from "./Workspace";
@@ -39,14 +38,14 @@ import { WorkspaceBuildLogsSection } from "./WorkspaceBuildLogsSection";
3938

4039
interface WorkspaceReadyPageProps {
4140
workspaceState: StateFrom<typeof workspaceMachine>;
42-
quotaState: StateFrom<typeof quotaMachine>;
4341
workspaceSend: (event: WorkspaceEvent) => void;
42+
quota?: TypesGen.WorkspaceQuota;
4443
}
4544

4645
export const WorkspaceReadyPage = ({
4746
workspaceState,
48-
quotaState,
4947
workspaceSend,
48+
quota,
5049
}: WorkspaceReadyPageProps): JSX.Element => {
5150
const [_, bannerSend] = useActor(
5251
workspaceState.children["scheduleBannerMachine"],
@@ -188,7 +187,7 @@ export const WorkspaceReadyPage = ({
188187
buildInfo={buildInfo}
189188
sshPrefix={sshPrefix}
190189
template={template}
191-
quota_budget={quotaState.context.quota?.budget}
190+
quota_budget={quota?.budget}
192191
templateWarnings={templateVersion?.warnings}
193192
buildLogs={
194193
shouldDisplayBuildLogs && (

site/src/xServices/quotas/quotasXService.ts

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

0 commit comments

Comments
 (0)