({
workspace,
template,
canUpdateSchedule,
})
| 117 | } |
| 118 | |
| 119 | const AutostopDisplay: FC<AutostopDisplayProps> = ({ |
| 120 | workspace, |
| 121 | template, |
| 122 | canUpdateSchedule, |
| 123 | }) => { |
| 124 | const queryClient = useQueryClient(); |
| 125 | const deadline = getDeadline(workspace); |
| 126 | const maxDeadlineDecrease = getMaxDeadlineChange(deadline, getMinDeadline()); |
| 127 | const maxDeadlineIncrease = getMaxDeadlineChange( |
| 128 | getMaxDeadline(workspace), |
| 129 | deadline, |
| 130 | ); |
| 131 | const deadlinePlusEnabled = maxDeadlineIncrease >= 1; |
| 132 | const deadlineMinusEnabled = maxDeadlineDecrease >= 1; |
| 133 | const deadlineUpdateTimeout = useRef<number>(undefined); |
| 134 | const lastStableDeadline = useRef<Dayjs>(deadline); |
| 135 | |
| 136 | const updateWorkspaceDeadlineQueryData = (deadline: Dayjs) => { |
| 137 | queryClient.setQueryData( |
| 138 | workspaceByOwnerAndNameKey(workspace.owner_name, workspace.name), |
| 139 | { |
| 140 | ...workspace, |
| 141 | latest_build: { |
| 142 | ...workspace.latest_build, |
| 143 | deadline: deadline.toISOString(), |
| 144 | }, |
| 145 | }, |
| 146 | ); |
| 147 | }; |
| 148 | |
| 149 | const updateDeadlineMutation = useMutation({ |
| 150 | ...updateDeadline(workspace), |
| 151 | onSuccess: (_, updatedDeadline) => { |
| 152 | toast.success( |
| 153 | `Shutdown time for "${workspace.name}" updated successfully.`, |
| 154 | ); |
| 155 | lastStableDeadline.current = updatedDeadline; |
| 156 | }, |
| 157 | onError: (error) => { |
| 158 | toast.error( |
| 159 | getErrorMessage( |
| 160 | error, |
| 161 | `Failed to update shutdown time for "${workspace.name}". Please try again.`, |
| 162 | ), |
| 163 | { |
| 164 | description: getErrorDetail(error), |
| 165 | }, |
| 166 | ); |
| 167 | updateWorkspaceDeadlineQueryData(lastStableDeadline.current); |
| 168 | }, |
| 169 | }); |
| 170 | |
| 171 | const handleDeadlineChange = (newDeadline: Dayjs) => { |
| 172 | clearTimeout(deadlineUpdateTimeout.current); |
| 173 | // Optimistic update |
| 174 | updateWorkspaceDeadlineQueryData(newDeadline); |
| 175 | deadlineUpdateTimeout.current = window.setTimeout(() => { |
| 176 | updateDeadlineMutation.mutate(newDeadline); |
nothing calls this directly
no test coverage detected