Skip to content

feat: add impending deletion indicators to the workspace page #7588

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
May 18, 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
Next Next commit
created WorkspaceDeletion directory
  • Loading branch information
Kira-Pilot committed May 17, 2023
commit 3691b492a213c51a0298b5516c9510db1707a0e3
31 changes: 31 additions & 0 deletions site/src/components/WorkspaceDeletion/DeletionBadge.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Workspace } from "api/typesGenerated"
import { displayImpendingDeletion } from "./utils"
import { useDashboard } from "components/Dashboard/DashboardProvider"
import { Pill } from "components/Pill/Pill"
import ErrorIcon from "@mui/icons-material/ErrorOutline"

export const DeletionBadge = ({
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: Since we are using the copy "Impending deletion" should we not call this ImpendingDeletionBadge?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure! I can adjust the names

workspace,
}: {
workspace: Workspace
}): JSX.Element | null => {
const { entitlements, experiments } = useDashboard()
const allowAdvancedScheduling =
entitlements.features["advanced_template_scheduling"].enabled
// This check can be removed when https://github.com/coder/coder/milestone/19
// is merged up
const allowWorkspaceActions = experiments.includes("workspace_actions")
// return null

if (
!displayImpendingDeletion(
workspace,
allowAdvancedScheduling,
allowWorkspaceActions,
)
) {
return null
}

return <Pill icon={<ErrorIcon />} text="Impending deletion" type="error" />
}
43 changes: 43 additions & 0 deletions site/src/components/WorkspaceDeletion/DeletionBanner.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Workspace } from "api/typesGenerated"
import { displayImpendingDeletion } from "./utils"
import { useDashboard } from "components/Dashboard/DashboardProvider"
import { Maybe } from "components/Conditionals/Maybe"
import { AlertBanner } from "components/AlertBanner/AlertBanner"

export const DeletionBanner = ({
workspace,
onDismiss,
displayImpendingDeletionBanner,
}: {
workspace?: Workspace
onDismiss: () => void
displayImpendingDeletionBanner: boolean
}): JSX.Element | null => {
const { entitlements, experiments } = useDashboard()
const allowAdvancedScheduling =
entitlements.features["advanced_template_scheduling"].enabled
// This check can be removed when https://github.com/coder/coder/milestone/19
// is merged up
const allowWorkspaceActions = experiments.includes("workspace_actions")

return (
<Maybe
condition={Boolean(
workspace &&
displayImpendingDeletion(
workspace,
allowAdvancedScheduling,
allowWorkspaceActions,
) &&
displayImpendingDeletionBanner,
)}
>
<AlertBanner
severity="info"
onDismiss={onDismiss}
dismissible
text="You have workspaces that will be deleted soon."
/>
</Maybe>
)
}
59 changes: 59 additions & 0 deletions site/src/components/WorkspaceDeletion/DeletionStat.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Maybe } from "components/Conditionals/Maybe"
import { StatsItem } from "components/Stats/Stats"
import Link from "@mui/material/Link"
import { Link as RouterLink } from "react-router-dom"
import styled from "@emotion/styled"
import { Workspace } from "api/typesGenerated"
import { displayImpendingDeletion } from "./utils"
import { useDashboard } from "components/Dashboard/DashboardProvider"

export const DeletionStat = ({
workspace,
}: {
workspace: Workspace
}): JSX.Element => {
const { entitlements, experiments } = useDashboard()
const allowAdvancedScheduling =
entitlements.features["advanced_template_scheduling"].enabled
// This check can be removed when https://github.com/coder/coder/milestone/19
// is merged up
const allowWorkspaceActions = experiments.includes("workspace_actions")

return (
<Maybe
condition={displayImpendingDeletion(
workspace,
allowAdvancedScheduling,
allowWorkspaceActions,
)}
>
<StyledStatsItem
label="Deletion on"
className="containerClass"
value={
<Link
component={RouterLink}
to={`/templates/${workspace.template_name}/settings/schedule`}
title="Schedule settings"
>
{/* We check for string existence in the conditional */}
{new Date(workspace.deleting_at as string).toLocaleString()}
</Link>
}
/>
</Maybe>
)
}

const StyledStatsItem = styled(StatsItem)(() => ({
"&.containerClass": {
flexDirection: "column",
gap: 0,
padding: 0,

"& > span:first-of-type": {
fontSize: 12,
fontWeight: 500,
},
},
}))
34 changes: 34 additions & 0 deletions site/src/components/WorkspaceDeletion/DeletionText.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Workspace } from "api/typesGenerated"
import { displayImpendingDeletion } from "./utils"
import { useDashboard } from "components/Dashboard/DashboardProvider"
import styled from "@emotion/styled"
import { Theme as MaterialUITheme } from "@mui/material/styles"

export const DeletionText = ({
workspace,
}: {
workspace: Workspace
}): JSX.Element | null => {
const { entitlements, experiments } = useDashboard()
const allowAdvancedScheduling =
entitlements.features["advanced_template_scheduling"].enabled
// This check can be removed when https://github.com/coder/coder/milestone/19
// is merged up
const allowWorkspaceActions = experiments.includes("workspace_actions")

if (
!displayImpendingDeletion(
workspace,
allowAdvancedScheduling,
allowWorkspaceActions,
)
) {
return null
}
return <StyledSpan role="status">Impending deletion</StyledSpan>
}

const StyledSpan = styled.span<{ theme?: MaterialUITheme }>`
color: ${(props) => props.theme.palette.warning.light};
font-weight: 600;
Copy link
Collaborator

Choose a reason for hiding this comment

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

In MUI v5 you can style things like ChakraUI:

<Box
  component="span"
  fontWeight={600}
  color={theme => theme.palette.warning.light}
>
  Impending Deletion
</Box>

or

<Box
  component="span"
  sx={{
    fontWeight: 600,
    color: theme.palette.warning.light,
  }}
>
  Impending Deletion
</Box>

Just to let you know in case you find it better

`
4 changes: 4 additions & 0 deletions site/src/components/WorkspaceDeletion/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from "./DeletionStat"
export * from "./DeletionBadge"
export * from "./DeletionText"
export * from "./DeletionBanner"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we really need this? Why not access those components directly?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good q!

  1. Just a shorter directory to type out when importing, e.g. components/WorkspaceDeletion instead of components/WorkspaceDeletion/XYZ - I think it looks a little cleaner. This becomes a more important pattern if you have additional nesting.
  2. I can also import multiple related components with 1 line which keeps our file length down
  3. Often times, the barrel file provides a clue as to how the components are meant to be used. For example, in this case, I am exporting everything, which means all components are meant to be consumed. Great. Let's say I was building a table that had a specific widget component, broken apart in a separate file, but used in the table and colocated in the same directory. In the barrel I would export the table component, but not the widget. A user unfamiliar with the code base could glance at the barrel file and see exactly what was meant to be consumed externally, i.e. the table and not the widget.

58 changes: 58 additions & 0 deletions site/src/components/WorkspaceDeletion/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import * as TypesGen from "api/typesGenerated"
import * as Mocks from "testHelpers/entities"
import { displayImpendingDeletion } from "./utils"

describe("util > workspace deletion", () => {
describe("displayImpendingDeletion", () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we could let only this describe here. The previous one didn't "make sense" to me since we are only testing this function.

const today = new Date()
it.each<[string, boolean, boolean, boolean]>([
[
new Date(new Date().setDate(today.getDate() + 15)).toISOString(),
true,
true,
false,
], // today + 15 days out
[
new Date(new Date().setDate(today.getDate() + 14)).toISOString(),
true,
true,
true,
], // today + 14
[
new Date(new Date().setDate(today.getDate() + 13)).toISOString(),
true,
true,
true,
], // today + 13
[
new Date(new Date().setDate(today.getDate() + 1)).toISOString(),
true,
true,
true,
], // today + 1
[new Date().toISOString(), true, true, true], // today + 0
[new Date().toISOString(), false, true, false], // Advanced Scheduling off
[new Date().toISOString(), true, false, false], // Workspace Actions off
])(
`deleting_at=%p, allowAdvancedScheduling=%p, AllowWorkspaceActions=%p, shouldDisplay=%p`,
(
deleting_at,
allowAdvancedScheduling,
allowWorkspaceActions,
shouldDisplay,
) => {
const workspace: TypesGen.Workspace = {
...Mocks.MockWorkspace,
deleting_at,
}
expect(
displayImpendingDeletion(
workspace,
allowAdvancedScheduling,
allowWorkspaceActions,
),
).toBe(shouldDisplay)
},
)
})
})
33 changes: 33 additions & 0 deletions site/src/components/WorkspaceDeletion/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Workspace } from "api/typesGenerated"

// This const dictates how far out we alert the user that a workspace
// has an impending deletion (due to template.InactivityTTL being set)
const IMPENDING_DELETION_DISPLAY_THRESHOLD = 14 // 14 days

/**
* Returns a boolean indicating if an impending deletion indicator should be
* displayed in the UI. Impending deletions are configured by setting the
* Template.InactivityTTL
* @param {TypesGen.Workspace} workspace
* @returns {boolean}
*/
export const displayImpendingDeletion = (
workspace: Workspace,
allowAdvancedScheduling: boolean,
allowWorkspaceActions: boolean,
) => {
const today = new Date()
if (
!workspace.deleting_at ||
!allowAdvancedScheduling ||
!allowWorkspaceActions
) {
return false
}
return (
new Date(workspace.deleting_at) <=
new Date(
today.setDate(today.getDate() + IMPENDING_DELETION_DISPLAY_THRESHOLD),
)
)
}
2 changes: 2 additions & 0 deletions site/src/components/WorkspaceStats/WorkspaceStats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import Popover from "@mui/material/Popover"
import TextField from "@mui/material/TextField"
import Button from "@mui/material/Button"
import { WorkspaceStatusText } from "components/WorkspaceStatusBadge/WorkspaceStatusBadge"
import { DeletionStat } from "components/WorkspaceDeletion"

const Language = {
workspaceDetails: "Workspace Details",
Expand Down Expand Up @@ -74,6 +75,7 @@ export const WorkspaceStats: FC<WorkspaceStatsProps> = ({
label="Status"
value={<WorkspaceStatusText workspace={workspace} />}
/>
<DeletionStat workspace={workspace} />
<StatsItem
className={styles.statsItem}
label={Language.templateLabel}
Expand Down
75 changes: 32 additions & 43 deletions site/src/components/WorkspaceStatusBadge/WorkspaceStatusBadge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import i18next from "i18next"
import { FC, PropsWithChildren } from "react"
import { makeStyles } from "@mui/styles"
import { combineClasses } from "utils/combineClasses"
import { displayImpendingDeletion } from "utils/workspace"
import { useDashboard } from "components/Dashboard/DashboardProvider"
import { ChooseOne, Cond } from "components/Conditionals/ChooseOne"
import { DeletionBadge, DeletionText } from "components/WorkspaceDeletion"

const LoadingIcon: FC = () => {
return <CircularProgress size={10} style={{ color: "#FFF" }} />
Expand Down Expand Up @@ -93,59 +93,48 @@ export type WorkspaceStatusBadgeProps = {
className?: string
}

const ImpendingDeletionBadge: FC<Partial<WorkspaceStatusBadgeProps>> = ({
className,
}) => {
const { entitlements, experiments } = useDashboard()
const allowAdvancedScheduling =
entitlements.features["advanced_template_scheduling"].enabled
// This check can be removed when https://github.com/coder/coder/milestone/19
// is merged up
const allowWorkspaceActions = experiments.includes("workspace_actions")

if (!allowAdvancedScheduling || !allowWorkspaceActions) {
return null
}
return (
<Pill
className={className}
icon={<ErrorIcon />}
text="Impending deletion"
type="error"
/>
)
}

export const WorkspaceStatusBadge: FC<
PropsWithChildren<WorkspaceStatusBadgeProps>
> = ({ workspace, className }) => {
// The ImpendingDeletionBadge component itself checks to see if the
// Advanced Scheduling feature is turned on and if the
// Workspace Actions flag is turned on.
if (displayImpendingDeletion(workspace)) {
return <ImpendingDeletionBadge className={className} />
}

const { text, icon, type } = getStatus(workspace.latest_build.status)
return <Pill className={className} icon={icon} text={text} type={type} />
return (
<ChooseOne>
{/* <DeletionBadge/> determines its own visibility */}
<Cond condition={Boolean(DeletionBadge({ workspace }))}>
<DeletionBadge workspace={workspace} />
</Cond>
<Cond>
<Pill className={className} icon={icon} text={text} type={type} />
</Cond>
</ChooseOne>
)
}

export const WorkspaceStatusText: FC<
PropsWithChildren<WorkspaceStatusBadgeProps>
> = ({ workspace, className }) => {
const styles = useStyles()
const { text, type } = getStatus(workspace.latest_build.status)

return (
<span
role="status"
className={combineClasses([
className,
styles.root,
styles[`type-${type}`],
])}
>
{text}
</span>
<ChooseOne>
{/* <DeletionText/> determines its own visibility */}
<Cond condition={Boolean(DeletionText({ workspace }))}>
<DeletionText workspace={workspace} />
</Cond>
<Cond>
<span
role="status"
className={combineClasses([
className,
styles.root,
styles[`type-${type}`],
])}
>
{text}
</span>
</Cond>
</ChooseOne>
)
}

Expand Down
Loading