Skip to content

refactor(site): Refactor the schedule controls in the workspace page #7083

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 11 commits into from
Apr 11, 2023
Prev Previous commit
Next Next commit
Make schedule controls work
  • Loading branch information
BrunoQuaresma committed Apr 11, 2023
commit 06a6b2e40384dfe52831359aecbb5d72f1ac75dc
2 changes: 2 additions & 0 deletions site/src/components/Workspace/Workspace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ export const Workspace: FC<React.PropsWithChildren<WorkspaceProps>> = ({
maxDeadlineDecrease={scheduleProps.maxDeadlineDecrease}
maxDeadlineIncrease={scheduleProps.maxDeadlineIncrease}
canUpdateWorkspace={canUpdateWorkspace}
onDeadlineMinus={scheduleProps.onDeadlineMinus}
onDeadlinePlus={scheduleProps.onDeadlinePlus}
/>

{failedBuildLogs && (
Expand Down
86 changes: 81 additions & 5 deletions site/src/components/WorkspaceStats/WorkspaceStats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export interface WorkspaceStatsProps {
maxDeadlineDecrease: number
canUpdateWorkspace: boolean
quota_budget?: number
onDeadlinePlus: (hours: number) => void
onDeadlineMinus: (hours: number) => void
handleUpdate: () => void
}

Expand All @@ -46,8 +48,10 @@ export const WorkspaceStats: FC<WorkspaceStatsProps> = ({
quota_budget,
maxDeadlineDecrease,
maxDeadlineIncrease,
handleUpdate,
canUpdateWorkspace,
handleUpdate,
onDeadlineMinus,
onDeadlinePlus,
}) => {
const initiatedBy = getDisplayWorkspaceBuildInitiatedBy(
workspace.latest_build,
Expand All @@ -56,8 +60,10 @@ export const WorkspaceStats: FC<WorkspaceStatsProps> = ({
const styles = useStyles()
const deadlinePlusEnabled = maxDeadlineIncrease >= 1
const deadlineMinusEnabled = maxDeadlineDecrease >= 1
const addingButtonRef = useRef<HTMLButtonElement>(null)
const addButtonRef = useRef<HTMLButtonElement>(null)
const subButtonRef = useRef<HTMLButtonElement>(null)
const [isAddingTime, setIsAddingTime] = useState(false)
const [isSubTime, setIsSubTime] = useState(false)

return (
<>
Expand Down Expand Up @@ -123,6 +129,8 @@ export const WorkspaceStats: FC<WorkspaceStatsProps> = ({
size="small"
title="Subtract hours from deadline"
className={styles.scheduleButton}
ref={subButtonRef}
onClick={() => setIsSubTime(true)}
>
<RemoveIcon />
</IconButton>
Expand All @@ -131,7 +139,7 @@ export const WorkspaceStats: FC<WorkspaceStatsProps> = ({
size="small"
title="Add hours to deadline"
className={styles.scheduleButton}
ref={addingButtonRef}
ref={addButtonRef}
onClick={() => setIsAddingTime(true)}
>
<AddIcon />
Expand All @@ -156,7 +164,7 @@ export const WorkspaceStats: FC<WorkspaceStatsProps> = ({
id="schedule-add"
classes={{ paper: styles.timePopoverPaper }}
open={isAddingTime}
anchorEl={addingButtonRef.current}
anchorEl={addButtonRef.current}
onClose={() => setIsAddingTime(false)}
anchorOrigin={{
vertical: "bottom",
Expand All @@ -172,8 +180,18 @@ export const WorkspaceStats: FC<WorkspaceStatsProps> = ({
Delay the shutdown of this workspace for a few more hours. This is
only applied once.
</span>
<form className={styles.timePopoverForm}>
<form
className={styles.timePopoverForm}
onSubmit={(e) => {
e.preventDefault()
const formData = new FormData(e.currentTarget)
const hours = Number(formData.get("hours"))
onDeadlinePlus(hours)
setIsAddingTime(false)
}}
>
<TextField
name="hours"
type="number"
size="small"
fullWidth
Expand All @@ -197,6 +215,64 @@ export const WorkspaceStats: FC<WorkspaceStatsProps> = ({
</Button>
</form>
</Popover>

<Popover
id="schedule-sub"
classes={{ paper: styles.timePopoverPaper }}
open={isSubTime}
anchorEl={subButtonRef.current}
onClose={() => setIsSubTime(false)}
anchorOrigin={{
vertical: "bottom",
horizontal: "right",
}}
transformOrigin={{
vertical: "top",
horizontal: "right",
}}
>
<span className={styles.timePopoverTitle}>
Subtract hours to deadline
</span>
<span className={styles.timePopoverDescription}>
Anticipate the shutdown of this workspace for a few more hours. This
is only applied once.
</span>
<form
className={styles.timePopoverForm}
onSubmit={(e) => {
e.preventDefault()
const formData = new FormData(e.currentTarget)
const hours = Number(formData.get("hours"))
onDeadlineMinus(hours)
setIsSubTime(false)
}}
>
<TextField
name="hours"
type="number"
size="small"
fullWidth
className={styles.timePopoverField}
InputProps={{ className: styles.timePopoverFieldInput }}
inputProps={{
min: 0,
max: maxDeadlineDecrease,
step: 1,
defaultValue: 1,
}}
/>

<Button
variant="outlined"
size="small"
className={styles.timePopoverButton}
type="submit"
>
Apply
</Button>
</form>
</Popover>
</>
)
}
Expand Down