MCPcopy Create free account
hub / github.com/coder/coder / AutostopDisplay

Function AutostopDisplay

site/src/pages/WorkspacePage/WorkspaceScheduleControls.tsx:119–272  ·  view source on GitHub ↗
({
	workspace,
	template,
	canUpdateSchedule,
})

Source from the content-addressed store, hash-verified

117}
118
119const 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);

Callers

nothing calls this directly

Calls 15

getDeadlineFunction · 0.90
getMaxDeadlineChangeFunction · 0.90
getMinDeadlineFunction · 0.90
getMaxDeadlineFunction · 0.90
updateDeadlineFunction · 0.90
getErrorMessageFunction · 0.90
getErrorDetailFunction · 0.90
useTimeFunction · 0.90
autostopDisplayFunction · 0.90
cnFunction · 0.90

Tested by

no test coverage detected