Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
36 changes: 36 additions & 0 deletions site/src/pages/WorkspacesPage/BatchStopConfirmation.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { FC } from "react";
import type { Workspace } from "#/api/typesGenerated";
import { ConfirmDialog } from "#/components/Dialogs/ConfirmDialog/ConfirmDialog";

type BatchStopConfirmationProps = {
workspacesToStop: readonly Workspace[];
open: boolean;
isLoading: boolean;
onClose: () => void;
onConfirm: () => void;
};

export const BatchStopConfirmation: FC<BatchStopConfirmationProps> = ({
workspacesToStop,
open,
onClose,
onConfirm,
isLoading,
}) => {
const workspaceCount = `${workspacesToStop.length} ${
workspacesToStop.length === 1 ? "workspace" : "workspaces"
}`;

return (
<ConfirmDialog
type="delete"
open={open}
onClose={onClose}
title={`Stop ${workspaceCount}`}
confirmLoading={isLoading}
confirmText="Stop"
onConfirm={onConfirm}
description={`Are you sure you want to stop ${workspaceCount}? This will terminate all running processes and disconnect any active sessions.`}
/>
);
};
6 changes: 6 additions & 0 deletions site/src/pages/WorkspacesPage/WorkspacesPage.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,9 @@ export const StopsOnlySelectedWorkspaces: Story = {
await openBulkActions(canvas, user);
await user.click(await body.findByRole("menuitem", { name: /stop/i }));

const dialog = await body.findByRole("dialog");
await user.click(within(dialog).getByRole("button", { name: "Stop" }));

await waitFor(() => expect(API.stopWorkspace).toHaveBeenCalledTimes(2));
expect(API.stopWorkspace).toHaveBeenCalledWith("1");
expect(API.stopWorkspace).toHaveBeenCalledWith("2");
Expand Down Expand Up @@ -532,6 +535,9 @@ export const StopIgnoresAlreadyStoppedWorkspaces: Story = {
expect(stopItem).not.toHaveAttribute("data-disabled");
await user.click(stopItem);

const dialog = await body.findByRole("dialog");
await user.click(within(dialog).getByRole("button", { name: "Stop" }));

await waitFor(() => expect(API.stopWorkspace).toHaveBeenCalledTimes(1));
expect(API.stopWorkspace).toHaveBeenCalledWith("2");
},
Expand Down
23 changes: 21 additions & 2 deletions site/src/pages/WorkspacesPage/WorkspacesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { useOrganizationsFilterMenu } from "#/modules/tableFiltering/options";
import { ACTIVE_BUILD_STATUSES } from "#/modules/workspaces/status";
import { pageTitle } from "#/utils/page";
import { BatchDeleteConfirmation } from "./BatchDeleteConfirmation";
import { BatchStopConfirmation } from "./BatchStopConfirmation";
import { BatchUpdateModalForm } from "./BatchUpdateModalForm";
import { useBatchActions } from "./batchActions";
import { useStatusFilterMenu, useTemplateFilterMenu } from "./filter/menus";
Expand Down Expand Up @@ -52,7 +53,7 @@ function useSafeSearchParams() {
>;
}

type BatchAction = "delete" | "update";
type BatchAction = "delete" | "stop" | "update";

const WorkspacesPage: FC = () => {
const queryClient = useQueryClient();
Expand Down Expand Up @@ -163,6 +164,13 @@ const WorkspacesPage: FC = () => {
const checkedWorkspaces =
data?.workspaces.filter((w) => checkedWorkspaceIds.has(w.id)) ?? [];

// Bulk stop only affects running workspaces, so the confirmation dialog and
// the mutation should both operate on that subset to avoid over-reporting
// how many workspaces will actually be stopped.
const workspacesToStop = checkedWorkspaces.filter(
(w) => w.latest_build.status === "running",
);

return (
<>
<title>{pageTitle("Workspaces")}</title>
Expand Down Expand Up @@ -197,7 +205,7 @@ const WorkspacesPage: FC = () => {
isRunningBatchAction={batchActions.isProcessing}
onBatchDeleteTransition={() => setActiveBatchAction("delete")}
onBatchStartTransition={() => batchActions.start(checkedWorkspaces)}
onBatchStopTransition={() => batchActions.stop(checkedWorkspaces)}
onBatchStopTransition={() => setActiveBatchAction("stop")}
onBatchUpdateTransition={() => {
// Just because batch-updating can be really dangerous
// action for running workspaces, we're going to invalidate
Expand Down Expand Up @@ -241,6 +249,17 @@ const WorkspacesPage: FC = () => {
}}
/>

<BatchStopConfirmation
isLoading={batchActions.isProcessing}
workspacesToStop={workspacesToStop}
open={activeBatchAction === "stop"}
onClose={() => setActiveBatchAction(undefined)}
onConfirm={async () => {
await batchActions.stop(workspacesToStop);
setActiveBatchAction(undefined);
}}
/>

<BatchUpdateModalForm
open={activeBatchAction === "update"}
workspacesToUpdate={checkedWorkspaces}
Expand Down
Loading