- Notifications
You must be signed in to change notification settings - Fork913
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
3691b49
92d0365
819983c
8f728bb
e987c70
8322b63
5aec820
d94f9fb
20ba906
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -2,12 +2,12 @@ import { action } from "@storybook/addon-actions" | ||
import { Story } from "@storybook/react" | ||
import { WatchAgentMetadataContext } from "components/Resources/AgentMetadata" | ||
import { ProvisionerJobLog } from "api/typesGenerated" | ||
import * as Mocks from "testHelpers/entities" | ||
import { Workspace, WorkspaceErrors, WorkspaceProps } from "./Workspace" | ||
import { withReactContext } from "storybook-react-context" | ||
import EventSource from "eventsourcemock" | ||
import { ProxyContext, getPreferredProxy } from "contexts/ProxyContext" | ||
import {DashboardProviderContext } from "components/Dashboard/DashboardProvider" | ||
export default { | ||
title: "components/Workspace", | ||
@@ -24,21 +24,37 @@ export default { | ||
], | ||
} | ||
const MockedAppearance = { | ||
config: Mocks.MockAppearance, | ||
preview: false, | ||
setPreview: () => null, | ||
save: () => null, | ||
} | ||
const Template: Story<WorkspaceProps> = (args) => ( | ||
<DashboardProviderContext.Provider | ||
value={{ | ||
buildInfo: Mocks.MockBuildInfo, | ||
entitlements: Mocks.MockEntitlementsWithScheduling, | ||
experiments: Mocks.MockExperiments, | ||
appearance: MockedAppearance, | ||
}} | ||
> | ||
<ProxyContext.Provider | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others.Learn more. Sadly both are needed | ||
value={{ | ||
proxyLatencies: Mocks.MockProxyLatencies, | ||
proxy: getPreferredProxy([], undefined), | ||
proxies: [], | ||
isLoading: false, | ||
isFetched: true, | ||
setProxy: () => { | ||
return | ||
}, | ||
}} | ||
> | ||
<Workspace {...args} /> | ||
</ProxyContext.Provider> | ||
</DashboardProviderContext.Provider> | ||
) | ||
export const Running = Template.bind({}) | ||
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" /> | ||
} |
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> | ||
) | ||
} |
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, | ||
}, | ||
}, | ||
})) |
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; | ||
` |
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" |
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) | ||
}, | ||
) | ||
}) |
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), | ||
) | ||
) | ||
} |
Uh oh!
There was an error while loading.Please reload this page.