Skip to content

Commit 0b28628

Browse files
committed
Refactor outdated warning
1 parent 525e6e5 commit 0b28628

File tree

5 files changed

+74
-41
lines changed

5 files changed

+74
-41
lines changed

site/src/api/queries/workspaceQuota.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const workspaceQuota = (username: string) => {
1212
};
1313
};
1414

15-
const getWorkspaceResolveAutostartQueryKey = (workspaceId: string) => [
15+
export const getWorkspaceResolveAutostartQueryKey = (workspaceId: string) => [
1616
workspaceId,
1717
"workspaceResolveAutostart",
1818
];

site/src/pages/WorkspacePage/Workspace.stories.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import EventSource from "eventsourcemock";
99
import { ProxyContext, getPreferredProxy } from "contexts/ProxyContext";
1010
import { DashboardProviderContext } from "components/Dashboard/DashboardProvider";
1111
import { WorkspaceBuildLogsSection } from "pages/WorkspacePage/WorkspaceBuildLogsSection";
12+
import { getWorkspaceResolveAutostartQueryKey } from "api/queries/workspaceQuota";
1213

1314
const MockedAppearance = {
1415
config: Mocks.MockAppearanceConfig,
@@ -196,9 +197,20 @@ export const Outdated: Story = {
196197
export const CantAutostart: Story = {
197198
args: {
198199
...Running.args,
199-
canAutostart: false,
200200
workspace: Mocks.MockOutdatedRunningWorkspaceRequireActiveVersion,
201201
},
202+
parameters: {
203+
queries: [
204+
{
205+
key: getWorkspaceResolveAutostartQueryKey(
206+
Mocks.MockOutdatedRunningWorkspaceRequireActiveVersion.id,
207+
),
208+
data: {
209+
parameter_mismatch: false,
210+
},
211+
},
212+
],
213+
},
202214
};
203215

204216
export const GetBuildsError: Story = {

site/src/pages/WorkspacePage/Workspace.tsx

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { SidebarIconButton } from "components/FullPageLayout/Sidebar";
2626
import HubOutlined from "@mui/icons-material/HubOutlined";
2727
import { ResourcesSidebar } from "./ResourcesSidebar";
2828
import { ResourceCard } from "components/Resources/ResourceCard";
29+
import { WorkspaceNotifications } from "./WorkspaceNotifications";
2930

3031
export type WorkspaceError =
3132
| "getBuildsError"
@@ -48,7 +49,6 @@ export interface WorkspaceProps {
4849
isRestarting: boolean;
4950
workspace: TypesGen.Workspace;
5051
canUpdateWorkspace: boolean;
51-
updateMessage?: string;
5252
canChangeVersions: boolean;
5353
hideSSHButton?: boolean;
5454
hideVSCodeDesktopButton?: boolean;
@@ -60,7 +60,7 @@ export interface WorkspaceProps {
6060
handleBuildRetry: () => void;
6161
handleBuildRetryDebug: () => void;
6262
buildLogs?: React.ReactNode;
63-
canAutostart: boolean;
63+
latestVersion?: TypesGen.TemplateVersion;
6464
}
6565

6666
/**
@@ -80,7 +80,6 @@ export const Workspace: FC<WorkspaceProps> = ({
8080
isUpdating,
8181
isRestarting,
8282
canUpdateWorkspace,
83-
updateMessage,
8483
canChangeVersions,
8584
workspaceErrors,
8685
hideSSHButton,
@@ -92,7 +91,7 @@ export const Workspace: FC<WorkspaceProps> = ({
9291
handleBuildRetry,
9392
handleBuildRetryDebug,
9493
buildLogs,
95-
canAutostart,
94+
latestVersion,
9695
}) => {
9796
const navigate = useNavigate();
9897
const { saveLocal, getLocal } = useLocalStorage();
@@ -135,13 +134,6 @@ export const Workspace: FC<WorkspaceProps> = ({
135134
};
136135
}, [workspace, now, showAlertPendingInQueue]);
137136

138-
const updateRequired =
139-
(workspace.template_require_active_version ||
140-
workspace.automatic_updates === "always") &&
141-
workspace.outdated;
142-
const autoStartFailing = workspace.autostart_schedule && !canAutostart;
143-
const requiresManualUpdate = updateRequired && autoStartFailing;
144-
145137
const transitionStats =
146138
template !== undefined ? ActiveTransition(template, workspace) : undefined;
147139

@@ -244,25 +236,10 @@ export const Workspace: FC<WorkspaceProps> = ({
244236
<div css={styles.content}>
245237
<div css={styles.dotBackground}>
246238
<Stack direction="column" css={styles.firstColumnSpacer} spacing={4}>
247-
{workspace.outdated &&
248-
(requiresManualUpdate ? (
249-
<Alert severity="warning">
250-
<AlertTitle>
251-
Autostart has been disabled for your workspace.
252-
</AlertTitle>
253-
<AlertDetail>
254-
Autostart is unable to automatically update your workspace.
255-
Manually update your workspace to reenable Autostart.
256-
</AlertDetail>
257-
</Alert>
258-
) : (
259-
<Alert severity="info">
260-
<AlertTitle>
261-
An update is available for your workspace
262-
</AlertTitle>
263-
{updateMessage && <AlertDetail>{updateMessage}</AlertDetail>}
264-
</Alert>
265-
))}
239+
<WorkspaceNotifications
240+
workspace={workspace}
241+
latestVersion={latestVersion}
242+
/>
266243

267244
{Boolean(workspaceErrors.buildError) && (
268245
<ErrorAlert error={workspaceErrors.buildError} dismissible />
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import AlertTitle from "@mui/material/AlertTitle";
2+
import { workspaceResolveAutostart } from "api/queries/workspaceQuota";
3+
import { TemplateVersion, Workspace } from "api/typesGenerated";
4+
import { Alert, AlertDetail } from "components/Alert/Alert";
5+
import { FC } from "react";
6+
import { useQuery } from "react-query";
7+
8+
type WorkspaceNotificationsProps = {
9+
workspace: Workspace;
10+
latestVersion?: TemplateVersion;
11+
};
12+
13+
export const WorkspaceNotifications: FC<WorkspaceNotificationsProps> = (
14+
props,
15+
) => {
16+
const { workspace, latestVersion } = props;
17+
18+
// Outdated
19+
const canAutostartResponse = useQuery(
20+
workspaceResolveAutostart(workspace.id),
21+
);
22+
const canAutostart = !canAutostartResponse.data?.parameter_mismatch ?? false;
23+
const updateRequired =
24+
(workspace.template_require_active_version ||
25+
workspace.automatic_updates === "always") &&
26+
workspace.outdated;
27+
const autoStartFailing = workspace.autostart_schedule && !canAutostart;
28+
const requiresManualUpdate = updateRequired && autoStartFailing;
29+
30+
return (
31+
<>
32+
{workspace.outdated &&
33+
latestVersion &&
34+
(requiresManualUpdate ? (
35+
<Alert severity="warning">
36+
<AlertTitle>
37+
Autostart has been disabled for your workspace.
38+
</AlertTitle>
39+
<AlertDetail>
40+
Autostart is unable to automatically update your workspace.
41+
Manually update your workspace to reenable Autostart.
42+
</AlertDetail>
43+
</Alert>
44+
) : (
45+
<Alert severity="info">
46+
<AlertTitle>An update is available for your workspace</AlertTitle>
47+
<AlertDetail>{latestVersion.message}</AlertDetail>
48+
</Alert>
49+
))}
50+
</>
51+
);
52+
};

site/src/pages/WorkspacePage/WorkspaceReadyPage.tsx

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import { getErrorMessage } from "api/errors";
3333
import { displayError } from "components/GlobalSnackbar/utils";
3434
import { deploymentConfig, deploymentSSHConfig } from "api/queries/deployment";
3535
import { WorkspacePermissions } from "./permissions";
36-
import { workspaceResolveAutostart } from "api/queries/workspaceQuota";
3736
import { WorkspaceDeleteDialog } from "./WorkspaceDeleteDialog";
3837
import dayjs from "dayjs";
3938

@@ -83,12 +82,6 @@ export const WorkspaceReadyPage = ({
8382
mutationFn: restartWorkspace,
8483
});
8584

86-
// Auto start
87-
const canAutostartResponse = useQuery(
88-
workspaceResolveAutostart(workspace.id),
89-
);
90-
const canAutostart = !canAutostartResponse.data?.parameter_mismatch ?? false;
91-
9285
// SSH Prefix
9386
const sshPrefixQuery = useQuery(deploymentSSHConfig());
9487

@@ -225,7 +218,7 @@ export const WorkspaceReadyPage = ({
225218
}
226219
}}
227220
canUpdateWorkspace={canUpdateWorkspace}
228-
updateMessage={latestVersion?.message}
221+
latestVersion={latestVersion}
229222
canChangeVersions={canChangeVersions}
230223
hideSSHButton={featureVisibility["browser_only"]}
231224
hideVSCodeDesktopButton={featureVisibility["browser_only"]}
@@ -246,7 +239,6 @@ export const WorkspaceReadyPage = ({
246239
<WorkspaceBuildLogsSection logs={buildLogs} />
247240
)
248241
}
249-
canAutostart={canAutostart}
250242
/>
251243

252244
<WorkspaceDeleteDialog

0 commit comments

Comments
 (0)