Skip to content

feat: add impending deletion banner to workspace page #7634

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 5 commits into from
May 25, 2023
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
25 changes: 21 additions & 4 deletions site/src/components/Workspace/Workspace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import {
} from "components/PageHeader/FullWidthPageHeader"
import { TemplateVersionWarnings } from "components/TemplateVersionWarnings/TemplateVersionWarnings"
import { ErrorAlert } from "components/Alert/ErrorAlert"
import { ImpendingDeletionBanner } from "components/WorkspaceDeletion"
import { useLocalStorage } from "hooks"
import { ChooseOne, Cond } from "components/Conditionals/ChooseOne"

export enum WorkspaceErrors {
GET_BUILDS_ERROR = "getBuildsError",
Expand Down Expand Up @@ -105,6 +108,7 @@ export const Workspace: FC<React.PropsWithChildren<WorkspaceProps>> = ({
const navigate = useNavigate()
const serverVersion = buildInfo?.version || ""
const { t } = useTranslation("workspacePage")
const { saveLocal, getLocal } = useLocalStorage()

const buildError = Boolean(workspaceErrors[WorkspaceErrors.BUILD_ERROR]) && (
<ErrorAlert
Expand Down Expand Up @@ -183,10 +187,23 @@ export const Workspace: FC<React.PropsWithChildren<WorkspaceProps>> = ({
{buildError}
{cancellationError}

<WorkspaceDeletedBanner
workspace={workspace}
handleClick={() => navigate(`/templates`)}
/>
<ChooseOne>
<Cond condition={workspace.latest_build.status === "deleted"}>
<WorkspaceDeletedBanner
handleClick={() => navigate(`/templates`)}
/>
</Cond>
<Cond>
{/* <ImpendingDeletionBanner/> determines its own visibility */}
<ImpendingDeletionBanner
workspace={workspace}
shouldRedisplayBanner={
getLocal("dismissedWorkspace") !== workspace.id
}
onDismiss={() => saveLocal("dismissedWorkspace", workspace.id)}
/>
</Cond>
</ChooseOne>

<TemplateVersionWarnings warnings={templateWarnings} />

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { action } from "@storybook/addon-actions"
import { Story } from "@storybook/react"
import * as Mocks from "../../testHelpers/entities"
import {
WorkspaceDeletedBanner,
WorkspaceDeletedBannerProps,
Expand All @@ -18,16 +17,4 @@ const Template: Story<WorkspaceDeletedBannerProps> = (args) => (
export const Example = Template.bind({})
Example.args = {
handleClick: action("extend"),
workspace: {
...Mocks.MockWorkspace,

latest_build: {
...Mocks.MockWorkspaceBuild,
job: {
...Mocks.MockProvisionerJob,
status: "succeeded",
},
transition: "delete",
},
},
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import Button from "@mui/material/Button"
import { FC } from "react"
import * as TypesGen from "api/typesGenerated"
import { Alert } from "components/Alert/Alert"
import { useTranslation } from "react-i18next"
import { Maybe } from "components/Conditionals/Maybe"

export interface WorkspaceDeletedBannerProps {
workspace: TypesGen.Workspace
handleClick: () => void
}

export const WorkspaceDeletedBanner: FC<
React.PropsWithChildren<WorkspaceDeletedBannerProps>
> = ({ workspace, handleClick }) => {
> = ({ handleClick }) => {
const { t } = useTranslation("workspacePage")

const NewWorkspaceButton = (
Expand All @@ -22,10 +19,8 @@ export const WorkspaceDeletedBanner: FC<
)

return (
<Maybe condition={workspace.latest_build.status === "deleted"}>
<Alert severity="warning" actions={[NewWorkspaceButton]}>
{t("warningsAndErrors.workspaceDeletedWarning")}
</Alert>
</Maybe>
<Alert severity="warning" actions={[NewWorkspaceButton]}>
{t("warningsAndErrors.workspaceDeletedWarning")}
</Alert>
)
}
56 changes: 39 additions & 17 deletions site/src/components/WorkspaceDeletion/ImpendingDeletionBanner.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import { Workspace } from "api/typesGenerated"
import { displayImpendingDeletion } from "./utils"
import { useDashboard } from "components/Dashboard/DashboardProvider"
import { Maybe } from "components/Conditionals/Maybe"
import { Alert } from "components/Alert/Alert"
import { formatDistanceToNow, differenceInDays } from "date-fns"

export enum Count {
Singular,
Multiple,
}

export const ImpendingDeletionBanner = ({
workspace,
onDismiss,
displayImpendingDeletionBanner,
shouldRedisplayBanner,
count = Count.Singular,
}: {
workspace?: Workspace
onDismiss: () => void
displayImpendingDeletionBanner: boolean
shouldRedisplayBanner: boolean
count?: Count
}): JSX.Element | null => {
const { entitlements, experiments } = useDashboard()
const allowAdvancedScheduling =
Expand All @@ -20,21 +27,36 @@ export const ImpendingDeletionBanner = ({
// is merged up
const allowWorkspaceActions = experiments.includes("workspace_actions")

if (
!workspace ||
!displayImpendingDeletion(
workspace,
allowAdvancedScheduling,
allowWorkspaceActions,
) ||
// Banners should be redisplayed after dismissal when additional workspaces are newly scheduled for deletion
!shouldRedisplayBanner
) {
return null
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I found it hard to read/understand this "display or not" logic here. I think the names displayImpendingDeletion and displayImpendingDeletionBanner are very similar and I took some time to understand they are different but not sure how to improve this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, let me improve that


// if deleting_at is 7 days away or less, display an 'error' banner to convey urgency to user
const daysUntilDelete = differenceInDays(
Date.parse(workspace.last_used_at),
new Date(),
)

return (
<Maybe
condition={Boolean(
workspace &&
displayImpendingDeletion(
workspace,
allowAdvancedScheduling,
allowWorkspaceActions,
) &&
displayImpendingDeletionBanner,
)}
<Alert
severity={daysUntilDelete <= 7 ? "warning" : "info"}
onDismiss={onDismiss}
dismissible
>
<Alert severity="info" onDismiss={onDismiss} dismissible>
You have workspaces that will be deleted soon.
</Alert>
</Maybe>
{count === Count.Singular
? `This workspace has been unused for ${formatDistanceToNow(
Date.parse(workspace.last_used_at),
)} and is scheduled for deletion. To keep it, connect via SSH or the web terminal.`
: "You have workspaces that will be deleted soon due to inactivity. To keep these workspaces, connect to them via SSH or the web terminal."}
</Alert>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ const TemplateSchedulePage: FC = () => {
{
onSuccess: () => {
displaySuccess("Template updated successfully")
// clear browser-stored list of workspaces impending deletion
clearLocal("dismissedWorkspaceList")
// clear browser storage of workspaces impending deletion
clearLocal("dismissedWorkspaceList") // workspaces page
clearLocal("dismissedWorkspace") // workspace page
},
},
)
Expand Down
2 changes: 1 addition & 1 deletion site/src/pages/WorkspacesPage/WorkspacesPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe("WorkspacesPage", () => {
renderWithAuth(<WorkspacesPage />)

const banner = await screen.findByText(
"You have workspaces that will be deleted soon.",
"You have workspaces that will be deleted soon due to inactivity. To keep these workspaces, connect to them via SSH or the web terminal.",
)
const user = userEvent.setup()
await user.click(screen.getByTestId("dismiss-banner-btn"))
Expand Down
6 changes: 4 additions & 2 deletions site/src/pages/WorkspacesPage/WorkspacesPageView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { WorkspacesTable } from "components/WorkspacesTable/WorkspacesTable"
import { workspaceFilterQuery } from "utils/filters"
import { useLocalStorage } from "hooks"
import difference from "lodash/difference"
import { ImpendingDeletionBanner } from "components/WorkspaceDeletion"
import { ImpendingDeletionBanner, Count } from "components/WorkspaceDeletion"
import { ErrorAlert } from "components/Alert/ErrorAlert"

export const Language = {
Expand Down Expand Up @@ -117,15 +117,17 @@ export const WorkspacesPageView: FC<
<Maybe condition={Boolean(error)}>
<ErrorAlert error={error} />
</Maybe>
{/* <ImpendingDeletionBanner/> determines its own visibility */}
<ImpendingDeletionBanner
workspace={workspaces?.find((workspace) => workspace.deleting_at)}
displayImpendingDeletionBanner={isNewWorkspacesImpendingDeletion()}
shouldRedisplayBanner={isNewWorkspacesImpendingDeletion()}
onDismiss={() =>
saveLocal(
"dismissedWorkspaceList",
JSON.stringify(workspaceIdsWithImpendingDeletions),
)
}
count={Count.Multiple}
/>

<SearchBarWithFilter
Expand Down