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

Skip to content

Commit a328d20

Browse files
chore(site): remove workspace schedule banner service (coder#10588)
Related to coder#9943
1 parent 2cf2904 commit a328d20

File tree

4 files changed

+50
-165
lines changed

4 files changed

+50
-165
lines changed

site/src/api/queries/workspaces.ts

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as API from "api/api";
22
import { QueryClient, type QueryOptions } from "react-query";
3-
3+
import { putWorkspaceExtension } from "api/api";
4+
import dayjs from "dayjs";
5+
import { getDeadline, getMaxDeadline, getMinDeadline } from "utils/schedule";
46
import {
57
type WorkspaceBuildParameter,
68
type Workspace,
@@ -99,3 +101,26 @@ export function workspaces(config: WorkspacesRequest = {}) {
99101
queryFn: () => API.getWorkspaces({ q, limit }),
100102
} as const satisfies QueryOptions<WorkspacesResponse>;
101103
}
104+
105+
export const decreaseDeadline = (workspace: Workspace) => {
106+
return {
107+
mutationFn: (hours: number) => {
108+
const proposedDeadline = getDeadline(workspace).subtract(hours, "hours");
109+
const newDeadline = dayjs.max(proposedDeadline, getMinDeadline());
110+
return putWorkspaceExtension(workspace.id, newDeadline);
111+
},
112+
};
113+
};
114+
115+
export const increaseDeadline = (workspace: Workspace) => {
116+
return {
117+
mutationFn: (hours: number) => {
118+
const proposedDeadline = getDeadline(workspace).add(hours, "hours");
119+
const newDeadline = dayjs.min(
120+
proposedDeadline,
121+
getMaxDeadline(workspace),
122+
);
123+
return putWorkspaceExtension(workspace.id, newDeadline);
124+
},
125+
};
126+
};

site/src/pages/WorkspacePage/WorkspaceReadyPage.tsx

+24-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { useActor } from "@xstate/react";
21
import { useDashboard } from "components/Dashboard/DashboardProvider";
32
import dayjs from "dayjs";
43
import { useFeatureVisibility } from "hooks/useFeatureVisibility";
@@ -34,6 +33,9 @@ import { templateVersion, templateVersions } from "api/queries/templates";
3433
import { Alert } from "components/Alert/Alert";
3534
import { Stack } from "components/Stack/Stack";
3635
import { useWorkspaceBuildLogs } from "hooks/useWorkspaceBuildLogs";
36+
import { decreaseDeadline, increaseDeadline } from "api/queries/workspaces";
37+
import { getErrorMessage } from "api/errors";
38+
import { displaySuccess, displayError } from "components/GlobalSnackbar/utils";
3739

3840
interface WorkspaceReadyPageProps {
3941
workspaceState: StateFrom<typeof workspaceMachine>;
@@ -56,9 +58,6 @@ export const WorkspaceReadyPage = ({
5658
isLoadingMoreBuilds,
5759
hasMoreBuilds,
5860
}: WorkspaceReadyPageProps): JSX.Element => {
59-
const [_, bannerSend] = useActor(
60-
workspaceState.children["scheduleBannerMachine"],
61-
);
6261
const { buildInfo } = useDashboard();
6362
const featureVisibility = useFeatureVisibility();
6463
const {
@@ -121,10 +120,25 @@ export const WorkspaceReadyPage = ({
121120
} = useMutation({
122121
mutationFn: restartWorkspace,
123122
});
124-
// keep banner machine in sync with workspace
125-
useEffect(() => {
126-
bannerSend({ type: "REFRESH_WORKSPACE", workspace });
127-
}, [bannerSend, workspace]);
123+
124+
const onDeadlineChangeSuccess = () => {
125+
displaySuccess("Updated workspace shutdown time.");
126+
};
127+
const onDeadlineChangeFails = (error: unknown) => {
128+
displayError(
129+
getErrorMessage(error, "Failed to update workspace shutdown time."),
130+
);
131+
};
132+
const decreaseMutation = useMutation({
133+
...decreaseDeadline(workspace),
134+
onSuccess: onDeadlineChangeSuccess,
135+
onError: onDeadlineChangeFails,
136+
});
137+
const increaseMutation = useMutation({
138+
...increaseDeadline(workspace),
139+
onSuccess: onDeadlineChangeSuccess,
140+
onError: onDeadlineChangeFails,
141+
});
128142

129143
return (
130144
<>
@@ -144,18 +158,8 @@ export const WorkspaceReadyPage = ({
144158

145159
<Workspace
146160
scheduleProps={{
147-
onDeadlineMinus: (hours: number) => {
148-
bannerSend({
149-
type: "DECREASE_DEADLINE",
150-
hours,
151-
});
152-
},
153-
onDeadlinePlus: (hours: number) => {
154-
bannerSend({
155-
type: "INCREASE_DEADLINE",
156-
hours,
157-
});
158-
},
161+
onDeadlineMinus: decreaseMutation.mutate,
162+
onDeadlinePlus: increaseMutation.mutate,
159163
maxDeadlineDecrease: getMaxDeadlineChange(deadline, getMinDeadline()),
160164
maxDeadlineIncrease: getMaxDeadlineChange(
161165
getMaxDeadline(workspace),

site/src/xServices/workspace/workspaceXService.ts

-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { getErrorMessage } from "api/errors";
2-
import { workspaceScheduleBannerMachine } from "xServices/workspaceSchedule/workspaceScheduleBannerXService";
32
import { assign, createMachine } from "xstate";
43
import * as API from "api/api";
54
import * as TypesGen from "api/typesGenerated";
@@ -416,15 +415,6 @@ export const workspaceMachine = createMachine(
416415
},
417416
},
418417
},
419-
schedule: {
420-
invoke: {
421-
id: "scheduleBannerMachine",
422-
src: "scheduleBannerMachine",
423-
data: {
424-
workspace: (context: WorkspaceContext) => context.workspace,
425-
},
426-
},
427-
},
428418
},
429419
},
430420
error: {
@@ -672,7 +662,6 @@ export const workspaceMachine = createMachine(
672662
context.eventSource?.close();
673663
};
674664
},
675-
scheduleBannerMachine: workspaceScheduleBannerMachine,
676665
getSSHPrefix: async () => {
677666
return API.getDeploymentSSHConfig();
678667
},

site/src/xServices/workspaceSchedule/workspaceScheduleBannerXService.ts

-133
This file was deleted.

0 commit comments

Comments
 (0)