Skip to content

feat(site): add stop and start batch actions #10565

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 9 commits into from
Nov 8, 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
Prev Previous commit
Next Next commit
Add bulk actions
  • Loading branch information
BrunoQuaresma committed Nov 7, 2023
commit 894629d18805880ec12f3b875e70e9c7c3157353
50 changes: 46 additions & 4 deletions site/src/pages/WorkspacesPage/WorkspacesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@ import { useTemplateFilterMenu, useStatusFilterMenu } from "./filter/menus";
import { useSearchParams } from "react-router-dom";
import { useFilter } from "components/Filter/filter";
import { useUserFilterMenu } from "components/Filter/UserFilter";
import { deleteWorkspace, getWorkspaces } from "api/api";
import {
deleteWorkspace,
getWorkspaces,
startWorkspace,
stopWorkspace,
} from "api/api";
import { ConfirmDialog } from "components/Dialogs/ConfirmDialog/ConfirmDialog";
import Box from "@mui/material/Box";
import { MONOSPACE_FONT_FAMILY } from "theme/constants";
import TextField from "@mui/material/TextField";
import { displayError } from "components/GlobalSnackbar/utils";
import { getErrorMessage } from "api/errors";
import { useEffectEvent } from "hooks/hookPolyfills";
import { useQuery } from "react-query";
import { useMutation, useQuery } from "react-query";
import { templates } from "api/queries/templates";

function useSafeSearchParams() {
Expand Down Expand Up @@ -99,6 +104,30 @@ const WorkspacesPage: FC = () => {
entitlements.features["workspace_batch_actions"].enabled;
const permissions = usePermissions();

// Batch mutations
const startAllMutation = useMutation({
mutationFn: async (workspaces: Workspace[]) => {
return Promise.all(
workspaces.map((w) =>
startWorkspace(w.id, w.latest_build.template_version_id),
),
);
},
onSuccess: async () => {
await refetch();
setCheckedWorkspaces([]);
},
});
const stopAllMutation = useMutation({
mutationFn: async (workspaces: Workspace[]) => {
return Promise.all(workspaces.map((w) => stopWorkspace(w.id)));
},
onSuccess: async () => {
await refetch();
Copy link
Member

Choose a reason for hiding this comment

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

Does this need to be awaited? I'm wondering if we could just refetch without await, so the state can be cleared out faster

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, it is needed because we only want to remove the loading state from this action when the new info is on the screen.

setCheckedWorkspaces([]);
},
});

// We want to uncheck the selected workspaces always when the url changes
// because of filtering or pagination
useEffect(() => {
Expand Down Expand Up @@ -129,11 +158,24 @@ const WorkspacesPage: FC = () => {
onUpdateWorkspace={(workspace) => {
updateWorkspace.mutate(workspace);
}}
isRunningBatchAction={
isDeletingAll ||
startAllMutation.isLoading ||
stopAllMutation.isLoading
}
onDeleteAll={() => {
setIsDeletingAll(true);
}}
onStartAll={() => {}}
onStopAll={() => {}}
onStartAll={async () => {
await startAllMutation.mutateAsync(checkedWorkspaces);
await refetch();
setCheckedWorkspaces([]);
}}
onStopAll={async () => {
await stopAllMutation.mutateAsync(checkedWorkspaces);
await refetch();
setCheckedWorkspaces([]);
}}
/>

<BatchDeleteConfirmation
Expand Down
10 changes: 7 additions & 3 deletions site/src/pages/WorkspacesPage/WorkspacesPageView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
TableToolbar,
} from "components/TableToolbar/TableToolbar";
import Box from "@mui/material/Box";
import Button from "@mui/material/Button";
import DeleteOutlined from "@mui/icons-material/DeleteOutlined";
import { WorkspacesButton } from "./WorkspacesButton";
import { UseQueryResult } from "react-query";
Expand All @@ -30,6 +29,7 @@ import {
} from "components/MoreMenu/MoreMenu";
import KeyboardArrowDownOutlined from "@mui/icons-material/KeyboardArrowDownOutlined";
import Divider from "@mui/material/Divider";
import LoadingButton from "@mui/lab/LoadingButton";

export const Language = {
pageTitle: "Workspaces",
Expand All @@ -55,6 +55,7 @@ export interface WorkspacesPageViewProps {
onPageChange: (page: number) => void;
onUpdateWorkspace: (workspace: Workspace) => void;
onCheckChange: (checkedWorkspaces: Workspace[]) => void;
isRunningBatchAction: boolean;
onDeleteAll: () => void;
onStartAll: () => void;
onStopAll: () => void;
Expand All @@ -79,6 +80,7 @@ export const WorkspacesPageView = ({
onDeleteAll,
onStopAll,
onStartAll,
isRunningBatchAction,
canCheckWorkspaces,
templates,
templatesFetchStatus,
Expand Down Expand Up @@ -144,14 +146,16 @@ export const WorkspacesPageView = ({

<MoreMenu>
<MoreMenuTrigger>
<Button
<LoadingButton
loading={isRunningBatchAction}
loadingPosition="end"
variant="text"
size="small"
css={{ borderRadius: 9999, marginLeft: "auto" }}
endIcon={<KeyboardArrowDownOutlined />}
>
Actions
</Button>
</LoadingButton>
</MoreMenuTrigger>
<MoreMenuContent>
<MoreMenuItem
Expand Down