Skip to content

chore(site): refactor workspace quota to use react-query instead of XState #9626

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 7 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions site/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -963,9 +963,9 @@ export const deleteGroup = async (groupId: string): Promise<void> => {
};

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

Expand Down
14 changes: 14 additions & 0 deletions site/src/api/queries/workspaceQuota.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useQuery } from "@tanstack/react-query";
import * as API from "api/api";

const getWorkspaceQuotaQueryKey = (username: string) => [
username,
"workspaceQuota",
];

export const useWorkspaceQuota = (username: string) => {
return useQuery({
queryKey: getWorkspaceQuotaQueryKey(username),
queryFn: () => API.getWorkspaceQuota(username),
});
};
11 changes: 5 additions & 6 deletions site/src/pages/WorkspacePage/WorkspacePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { ChooseOne, Cond } from "components/Conditionals/ChooseOne";
import { Loader } from "components/Loader/Loader";
import { FC } from "react";
import { useParams } from "react-router-dom";
import { quotaMachine } from "xServices/quotas/quotasXService";
import { workspaceMachine } from "xServices/workspace/workspaceXService";
import { WorkspaceReadyPage } from "./WorkspaceReadyPage";
import { RequirePermission } from "components/RequirePermission/RequirePermission";
import { ErrorAlert } from "components/Alert/ErrorAlert";
import { useOrganizationId } from "hooks";
import { isAxiosError } from "axios";
import { Margins } from "components/Margins/Margins";
import { useWorkspaceQuota } from "api/queries/workspaceQuota";

export const WorkspacePage: FC = () => {
const params = useParams() as {
Expand All @@ -28,9 +28,8 @@ export const WorkspacePage: FC = () => {
},
});
const { workspace, error } = workspaceState.context;
const [quotaState] = useMachine(quotaMachine, { context: { username } });
const { getQuotaError } = quotaState.context;
const pageError = error ?? getQuotaError;
const quotaQuery = useWorkspaceQuota(username);
const pageError = error ?? quotaQuery.error;

return (
<RequirePermission
Expand All @@ -48,12 +47,12 @@ export const WorkspacePage: FC = () => {
condition={
Boolean(workspace) &&
workspaceState.matches("ready") &&
quotaState.matches("success")
quotaQuery.isSuccess
}
>
<WorkspaceReadyPage
workspaceState={workspaceState}
quotaState={quotaState}
quota={quotaQuery.data}
workspaceSend={workspaceSend}
/>
</Cond>
Expand Down
7 changes: 3 additions & 4 deletions site/src/pages/WorkspacePage/WorkspaceReadyPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
getMaxDeadlineChange,
getMinDeadline,
} from "utils/schedule";
import { quotaMachine } from "xServices/quotas/quotasXService";
import { StateFrom } from "xstate";
import { DeleteDialog } from "components/Dialogs/DeleteDialog/DeleteDialog";
import { Workspace, WorkspaceErrors } from "./Workspace";
Expand All @@ -39,14 +38,14 @@ import { WorkspaceBuildLogsSection } from "./WorkspaceBuildLogsSection";

interface WorkspaceReadyPageProps {
workspaceState: StateFrom<typeof workspaceMachine>;
quotaState: StateFrom<typeof quotaMachine>;
workspaceSend: (event: WorkspaceEvent) => void;
quota?: TypesGen.WorkspaceQuota;
}

export const WorkspaceReadyPage = ({
workspaceState,
quotaState,
workspaceSend,
quota,
}: WorkspaceReadyPageProps): JSX.Element => {
const [_, bannerSend] = useActor(
workspaceState.children["scheduleBannerMachine"],
Expand Down Expand Up @@ -188,7 +187,7 @@ export const WorkspaceReadyPage = ({
buildInfo={buildInfo}
sshPrefix={sshPrefix}
template={template}
quota_budget={quotaState.context.quota?.budget}
quota_budget={quota?.budget}
templateWarnings={templateVersion?.warnings}
buildLogs={
shouldDisplayBuildLogs && (
Expand Down
63 changes: 0 additions & 63 deletions site/src/xServices/quotas/quotasXService.ts

This file was deleted.