Skip to content

Commit b33cb0e

Browse files
chore(site): refactor workspace quota to use react-query instead of XState (#9626)
1 parent 64bc317 commit b33cb0e

File tree

7 files changed

+32
-81
lines changed

7 files changed

+32
-81
lines changed

site/src/api/api.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -961,9 +961,11 @@ export const deleteGroup = async (groupId: string): Promise<void> => {
961961
};
962962

963963
export const getWorkspaceQuota = async (
964-
userID: string,
964+
username: string,
965965
): Promise<TypesGen.WorkspaceQuota> => {
966-
const response = await axios.get(`/api/v2/workspace-quota/${userID}`);
966+
const response = await axios.get(
967+
`/api/v2/workspace-quota/${encodeURIComponent(username)}`,
968+
);
967969
return response.data;
968970
};
969971

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

site/src/pages/WorkspacePage/Workspace.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export interface WorkspaceProps {
6868
buildInfo?: TypesGen.BuildInfoResponse;
6969
sshPrefix?: string;
7070
template?: TypesGen.Template;
71-
quota_budget?: number;
71+
quotaBudget?: number;
7272
handleBuildRetry: () => void;
7373
buildLogs?: React.ReactNode;
7474
}
@@ -101,7 +101,7 @@ export const Workspace: FC<React.PropsWithChildren<WorkspaceProps>> = ({
101101
buildInfo,
102102
sshPrefix,
103103
template,
104-
quota_budget,
104+
quotaBudget,
105105
handleBuildRetry,
106106
templateWarnings,
107107
buildLogs,
@@ -183,7 +183,7 @@ export const Workspace: FC<React.PropsWithChildren<WorkspaceProps>> = ({
183183

184184
<WorkspaceStats
185185
workspace={workspace}
186-
quota_budget={quota_budget}
186+
quotaBudget={quotaBudget}
187187
handleUpdate={handleUpdate}
188188
canUpdateWorkspace={canUpdateWorkspace}
189189
maxDeadlineDecrease={scheduleProps.maxDeadlineDecrease}

site/src/pages/WorkspacePage/WorkspacePage.tsx

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ 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 { workspaceQuota } from "api/queries/workspaceQuota";
14+
import { useQuery } from "@tanstack/react-query";
1415

1516
export const WorkspacePage: FC = () => {
1617
const params = useParams() as {
@@ -28,9 +29,8 @@ export const WorkspacePage: FC = () => {
2829
},
2930
});
3031
const { workspace, error } = workspaceState.context;
31-
const [quotaState] = useMachine(quotaMachine, { context: { username } });
32-
const { getQuotaError } = quotaState.context;
33-
const pageError = error ?? getQuotaError;
32+
const quotaQuery = useQuery(workspaceQuota(username));
33+
const pageError = error ?? quotaQuery.error;
3434

3535
return (
3636
<RequirePermission
@@ -48,12 +48,12 @@ export const WorkspacePage: FC = () => {
4848
condition={
4949
Boolean(workspace) &&
5050
workspaceState.matches("ready") &&
51-
quotaState.matches("success")
51+
quotaQuery.isSuccess
5252
}
5353
>
5454
<WorkspaceReadyPage
5555
workspaceState={workspaceState}
56-
quotaState={quotaState}
56+
quota={quotaQuery.data}
5757
workspaceSend={workspaceSend}
5858
/>
5959
</Cond>

site/src/pages/WorkspacePage/WorkspaceReadyPage.tsx

+3-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
getMaxDeadlineChange,
1212
getMinDeadline,
1313
} from "utils/schedule";
14-
import { quotaMachine } from "xServices/quotas/quotasXService";
1514
import { StateFrom } from "xstate";
1615
import { DeleteDialog } from "components/Dialogs/DeleteDialog/DeleteDialog";
1716
import { Workspace, WorkspaceErrors } from "./Workspace";
@@ -38,14 +37,14 @@ import { WorkspaceBuildLogsSection } from "./WorkspaceBuildLogsSection";
3837

3938
interface WorkspaceReadyPageProps {
4039
workspaceState: StateFrom<typeof workspaceMachine>;
41-
quotaState: StateFrom<typeof quotaMachine>;
4240
workspaceSend: (event: WorkspaceEvent) => void;
41+
quota?: TypesGen.WorkspaceQuota;
4342
}
4443

4544
export const WorkspaceReadyPage = ({
4645
workspaceState,
47-
quotaState,
4846
workspaceSend,
47+
quota,
4948
}: WorkspaceReadyPageProps): JSX.Element => {
5049
const [_, bannerSend] = useActor(
5150
workspaceState.children["scheduleBannerMachine"],
@@ -186,7 +185,7 @@ export const WorkspaceReadyPage = ({
186185
buildInfo={buildInfo}
187186
sshPrefix={sshPrefix}
188187
template={template}
189-
quota_budget={quotaState.context.quota?.budget}
188+
quotaBudget={quota?.budget}
190189
templateWarnings={templateVersion?.warnings}
191190
buildLogs={
192191
shouldDisplayBuildLogs && (

site/src/pages/WorkspacePage/WorkspaceStats.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ export interface WorkspaceStatsProps {
3939
maxDeadlineIncrease: number;
4040
maxDeadlineDecrease: number;
4141
canUpdateWorkspace: boolean;
42-
quota_budget?: number;
42+
quotaBudget?: number;
4343
onDeadlinePlus: (hours: number) => void;
4444
onDeadlineMinus: (hours: number) => void;
4545
handleUpdate: () => void;
4646
}
4747

4848
export const WorkspaceStats: FC<WorkspaceStatsProps> = ({
4949
workspace,
50-
quota_budget,
50+
quotaBudget,
5151
maxDeadlineDecrease,
5252
maxDeadlineIncrease,
5353
canUpdateWorkspace,
@@ -169,7 +169,7 @@ export const WorkspaceStats: FC<WorkspaceStatsProps> = ({
169169
className={styles.statsItem}
170170
label={Language.costLabel}
171171
value={`${workspace.latest_build.daily_cost} ${
172-
quota_budget ? `/ ${quota_budget}` : ""
172+
quotaBudget ? `/ ${quotaBudget}` : ""
173173
}`}
174174
/>
175175
)}

site/src/xServices/quotas/quotasXService.ts

-63
This file was deleted.

0 commit comments

Comments
 (0)