@@ -134,6 +134,14 @@ export function getProjectStoppingPointMutationOptions({
134134 project . slug
135135 ) ;
136136
137+ function resolveStoppingPointValue ( stoppingPoint : SelectValue ) {
138+ return stoppingPoint === 'root_cause'
139+ ? ( 'root_cause' as const )
140+ : organization . autoOpenPrs
141+ ? ( 'open_pr' as const )
142+ : ( 'code_changes' as const ) ;
143+ }
144+
137145 return mutationOptions ( {
138146 mutationFn : async ( { stoppingPoint} : { stoppingPoint : SelectValue } ) => {
139147 const tuning = stoppingPoint === 'off' ? ( 'off' as const ) : ( 'medium' as const ) ;
@@ -150,22 +158,10 @@ export function getProjectStoppingPointMutationOptions({
150158 const repositories = preference ?. repositories ?? [ ] ;
151159 const automationHandoff = preference ?. automation_handoff ;
152160
153- const stoppingPointValue =
154- stoppingPoint === 'root_cause'
155- ? ( 'root_cause' as const )
156- : organization . autoOpenPrs
157- ? ( 'open_pr' as const )
158- : ( 'code_changes' as const ) ;
159-
160161 const preferencePayload = {
161162 repositories,
162- automated_run_stopping_point : stoppingPointValue ,
163- automation_handoff : automationHandoff
164- ? {
165- ...automationHandoff ,
166- auto_create_pr : stoppingPointValue === 'open_pr' ,
167- }
168- : automationHandoff ,
163+ automated_run_stopping_point : resolveStoppingPointValue ( stoppingPoint ) ,
164+ automation_handoff : automationHandoff ,
169165 } ;
170166
171167 preferencePromise = fetchMutation < SeerPreferencesResponse > ( {
@@ -177,6 +173,28 @@ export function getProjectStoppingPointMutationOptions({
177173
178174 return await Promise . all ( [ projectPromise , preferencePromise ] ) ;
179175 } ,
176+ onMutate : ( { stoppingPoint} : { stoppingPoint : SelectValue } ) => {
177+ const previousProject = ProjectsStore . getById ( project . id ) ;
178+ const previousPreference = getApiQueryData < SeerPreferencesResponse > (
179+ queryClient ,
180+ seerPrefsQueryKey
181+ ) ;
182+
183+ const tuning = stoppingPoint === 'off' ? ( 'off' as const ) : ( 'medium' as const ) ;
184+ ProjectsStore . onUpdateSuccess ( { ...project , autofixAutomationTuning : tuning } ) ;
185+
186+ if ( stoppingPoint !== 'off' && previousPreference ?. preference ) {
187+ setApiQueryData < SeerPreferencesResponse > ( queryClient , seerPrefsQueryKey , {
188+ ...previousPreference ,
189+ preference : {
190+ ...previousPreference . preference ,
191+ automated_run_stopping_point : resolveStoppingPointValue ( stoppingPoint ) ,
192+ } ,
193+ } ) ;
194+ }
195+
196+ return { previousProject, previousPreference} ;
197+ } ,
180198 onSuccess : ( [ updatedProject , preferencePayload ] ) => {
181199 ProjectsStore . onUpdateSuccess ( updatedProject ) ;
182200
@@ -197,6 +215,24 @@ export function getProjectStoppingPointMutationOptions({
197215 }
198216 }
199217 } ,
218+ onError : ( _error : unknown , _variables : unknown , context : unknown ) => {
219+ const ctx = context as
220+ | {
221+ previousPreference : SeerPreferencesResponse | undefined ;
222+ previousProject : Project | undefined ;
223+ }
224+ | undefined ;
225+ if ( ctx ?. previousProject ) {
226+ ProjectsStore . onUpdateSuccess ( ctx . previousProject ) ;
227+ }
228+ if ( ctx ?. previousPreference ) {
229+ setApiQueryData < SeerPreferencesResponse > (
230+ queryClient ,
231+ seerPrefsQueryKey ,
232+ ctx . previousPreference
233+ ) ;
234+ }
235+ } ,
200236 onSettled : ( ) => {
201237 queryClient . invalidateQueries ( { queryKey : seerPrefsQueryKey } ) ;
202238
0 commit comments