-
Notifications
You must be signed in to change notification settings - Fork 887
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
Kira-Pilot
merged 9 commits into
main
from
deletion-indicators-workspace-page/kira-pilot
May 18, 2023
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3691b49
created WorkspaceDeletion directory
Kira-Pilot 92d0365
remove commented code
Kira-Pilot 819983c
attempting to fix workspace stories
Kira-Pilot 8f728bb
fix lint
Kira-Pilot e987c70
fix the rest of the stories
Kira-Pilot 8322b63
fix right stories
Kira-Pilot 5aec820
PR comments
Kira-Pilot d94f9fb
Merge remote-tracking branch 'origin/main' into deletion-indicators-w…
Kira-Pilot 20ba906
fix lint
Kira-Pilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
site/src/components/WorkspaceDeletion/ImpendingDeletionBadge.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ImpendingDeletionBadge = ({ | ||
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" /> | ||
} |
40 changes: 40 additions & 0 deletions
40
site/src/components/WorkspaceDeletion/ImpendingDeletionBanner.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
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" | ||
|
||
export const ImpendingDeletionBanner = ({ | ||
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, | ||
)} | ||
> | ||
<Alert severity="info" onDismiss={onDismiss} dismissible> | ||
You have workspaces that will be deleted soon. | ||
</Alert> | ||
</Maybe> | ||
) | ||
} |
59 changes: 59 additions & 0 deletions
59
site/src/components/WorkspaceDeletion/ImpendingDeletionStat.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ImpendingDeletionStat = ({ | ||
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
34
site/src/components/WorkspaceDeletion/ImpendingDeletionText.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ImpendingDeletionText = ({ | ||
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; | ||
` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from "./ImpendingDeletionStat" | ||
export * from "./ImpendingDeletionBadge" | ||
export * from "./ImpendingDeletionText" | ||
export * from "./ImpendingDeletionBanner" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import * as TypesGen from "api/typesGenerated" | ||
import * as Mocks from "testHelpers/entities" | ||
import { displayImpendingDeletion } from "./utils" | ||
|
||
describe("displayImpendingDeletion", () => { | ||
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) | ||
}, | ||
) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
) | ||
) | ||
} |
27 changes: 25 additions & 2 deletions
27
site/src/components/WorkspaceStats/WorkspaceStats.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if I'm correct, but maybe, this ProxyContext is already inside of the DashboardProvider so we don't need that here too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can check!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sadly both are needed