@@ -96,79 +96,84 @@ export function useProjectScriptSettings(
9696 }
9797 savingRef . current = true ;
9898 setSaving ( true ) ;
99- try {
100- for ( const { environmentId, settings, keybindings, project } of targets ) {
101- const current = project
102- ? resolveProjectScripts ( settings , project )
103- : settings . defaultProjectScripts ;
104- const nextScripts = transform ( current ) ;
105- const effectiveScripts = nextScripts ?? settings . defaultProjectScripts ;
106- const result = await updateSettings ( {
107- environmentId,
108- input : {
109- patch : project
110- ? {
111- projectSettingsOverrides : {
112- [ project . id ] :
113- nextScripts === null
114- ? clearProjectSettingsOverrides ( settings , project . id , [
115- "defaultProjectScripts" ,
116- ] )
117- : {
118- ...settings . projectSettingsOverrides [ project . id ] ,
119- defaultProjectScripts : nextScripts ,
120- } ,
121- } ,
122- }
123- : { defaultProjectScripts : nextScripts ?? [ ] } ,
124- } ,
125- } ) ;
126- if ( result . _tag === "Failure" ) return reportScriptFailure ( result ) ;
127- const changedIds = scriptId
128- ? [ scriptId ]
129- : current
130- . filter ( ( script ) => ! effectiveScripts . some ( ( next ) => next . id === script . id ) )
131- . map ( ( script ) => script . id ) ;
132- for ( const id of changedIds ) {
133- const command = commandForProjectScript ( id ) ;
134- const previousValue = keybindingValueForCommand ( keybindings , command ) ;
135- const previous = previousValue
136- ? decodeProjectScriptKeybindingRule ( { keybinding : previousValue , command } )
137- : null ;
138- const next = decodeProjectScriptKeybindingRule ( { keybinding, command } ) ;
139- const retainedElsewhere =
140- ! nextScripts ?. some ( ( script ) => script . id === id ) &&
141- ( ( project && settings . defaultProjectScripts . some ( ( script ) => script . id === id ) ) ||
142- Object . entries ( settings . projectSettingsOverrides ) . some (
143- ( [ projectId , entry ] ) =>
144- projectId !== project ?. id &&
145- entry . defaultProjectScripts ?. some ( ( script ) => script . id === id ) ,
146- ) ||
147- projects . some (
148- ( other ) =>
149- other . environmentId === environmentId &&
150- other . id !== project ?. id &&
151- ( project ? resolveProjectScripts ( settings , other ) : other . scripts ) . some (
152- ( script ) => script . id === id ,
153- ) ,
154- ) ) ;
155- const bindingResult = next
156- ? await upsertKeybinding ( {
157- environmentId,
158- input :
159- previous && previous . key !== next . key ? { ...next , replace : previous } : next ,
160- } )
161- : previous && ! retainedElsewhere
162- ? await removeKeybinding ( { environmentId, input : previous } )
163- : null ;
164- if ( bindingResult ?. _tag === "Failure" ) return reportScriptFailure ( bindingResult ) ;
165- }
166- }
167- return AsyncResult . success ( undefined ) ;
168- } finally {
99+ return writeTargets ( transform , scriptId , keybinding ) . finally ( ( ) => {
169100 savingRef . current = false ;
170101 setSaving ( false ) ;
102+ } ) ;
103+ }
104+
105+ async function writeTargets (
106+ transform : ( current : readonly ProjectScript [ ] ) => readonly ProjectScript [ ] | null ,
107+ scriptId : string | undefined ,
108+ keybinding : string | null | undefined ,
109+ ) : Promise < AtomCommandResult < void , unknown > > {
110+ for ( const { environmentId, settings, keybindings, project } of targets ) {
111+ const current = project
112+ ? resolveProjectScripts ( settings , project )
113+ : settings . defaultProjectScripts ;
114+ const nextScripts = transform ( current ) ;
115+ const effectiveScripts = nextScripts ?? settings . defaultProjectScripts ;
116+ const result = await updateSettings ( {
117+ environmentId,
118+ input : {
119+ patch : project
120+ ? {
121+ projectSettingsOverrides : {
122+ [ project . id ] :
123+ nextScripts === null
124+ ? clearProjectSettingsOverrides ( settings , project . id , [
125+ "defaultProjectScripts" ,
126+ ] )
127+ : {
128+ ...settings . projectSettingsOverrides [ project . id ] ,
129+ defaultProjectScripts : nextScripts ,
130+ } ,
131+ } ,
132+ }
133+ : { defaultProjectScripts : nextScripts ?? [ ] } ,
134+ } ,
135+ } ) ;
136+ if ( result . _tag === "Failure" ) return reportScriptFailure ( result ) ;
137+ const changedIds = scriptId
138+ ? [ scriptId ]
139+ : current
140+ . filter ( ( script ) => ! effectiveScripts . some ( ( next ) => next . id === script . id ) )
141+ . map ( ( script ) => script . id ) ;
142+ for ( const id of changedIds ) {
143+ const command = commandForProjectScript ( id ) ;
144+ const previousValue = keybindingValueForCommand ( keybindings , command ) ;
145+ const previous = previousValue
146+ ? decodeProjectScriptKeybindingRule ( { keybinding : previousValue , command } )
147+ : null ;
148+ const next = decodeProjectScriptKeybindingRule ( { keybinding, command } ) ;
149+ const retainedElsewhere =
150+ ! nextScripts ?. some ( ( script ) => script . id === id ) &&
151+ ( ( project && settings . defaultProjectScripts . some ( ( script ) => script . id === id ) ) ||
152+ Object . entries ( settings . projectSettingsOverrides ) . some (
153+ ( [ projectId , entry ] ) =>
154+ projectId !== project ?. id &&
155+ entry . defaultProjectScripts ?. some ( ( script ) => script . id === id ) ,
156+ ) ||
157+ projects . some (
158+ ( other ) =>
159+ other . environmentId === environmentId &&
160+ other . id !== project ?. id &&
161+ ( project ? resolveProjectScripts ( settings , other ) : other . scripts ) . some (
162+ ( script ) => script . id === id ,
163+ ) ,
164+ ) ) ;
165+ const bindingResult = next
166+ ? await upsertKeybinding ( {
167+ environmentId,
168+ input : previous && previous . key !== next . key ? { ...next , replace : previous } : next ,
169+ } )
170+ : previous && ! retainedElsewhere
171+ ? await removeKeybinding ( { environmentId, input : previous } )
172+ : null ;
173+ if ( bindingResult ?. _tag === "Failure" ) return reportScriptFailure ( bindingResult ) ;
174+ }
171175 }
176+ return AsyncResult . success ( undefined ) ;
172177 }
173178
174179 function submit ( scriptId : string | null , input : NewProjectScriptInput ) {
0 commit comments