@@ -109,6 +109,8 @@ export function WorktreesDirectoryRow() {
109109 ) ;
110110}
111111
112+ type SaveOptions = Parameters < ReturnType < typeof useUpdateScopedSettings > > [ 1 ] ;
113+
112114function RetentionControl ( {
113115 label,
114116 value,
@@ -117,42 +119,120 @@ function RetentionControl({
117119} : {
118120 label : string ;
119121 value : number | null ;
120- onChange : ( value : number | null ) => void ;
122+ onChange : ( value : number | null , onFailed : ( ) => void ) => void ;
121123 minimum ?: number ;
122124} ) {
123125 const [ draft , setDraft ] = useState ( value ) ;
124126 const [ savedValue , setSavedValue ] = useState ( value ) ;
127+ // Saving an age can start a cleanup sweep, so turning a rule on only opens a draft.
128+ // While its first save is in flight this holds that submission.
129+ const [ enabling , setEnabling ] = useState < "draft" | "edited" | { days : number } | null > ( null ) ;
130+ // Holds an age until Enter or focus leaves the row: one edited before focus moved to the
131+ // switch, so switching off never saves it first, or one stepped toward a rule's first age,
132+ // so raising it from 8 never saves a shorter age on the way.
133+ const [ held , setHeld ] = useState ( false ) ;
134+ // Hides a saved rule's age once it is switched off, so no blur can save it after null.
135+ const [ switchedOff , setSwitchedOff ] = useState ( false ) ;
125136 if ( savedValue !== value ) {
126137 setSavedValue ( value ) ;
127138 setDraft ( value ) ;
139+ setEnabling ( null ) ;
140+ setHeld ( false ) ;
141+ setSwitchedOff ( false ) ;
128142 }
143+ const pending = typeof enabling === "object" ? enabling : null ;
144+ const on = ( value !== null && ! switchedOff ) || enabling !== null ;
145+ const row = useRef < HTMLDivElement > ( null ) ;
146+
147+ const commit = ( next : number ) => {
148+ const days = Math . min ( 3650 , Math . max ( minimum , Math . round ( next ) ) ) ;
149+ setDraft ( days ) ;
150+ setHeld ( false ) ;
151+ if ( days === pending ?. days ) return ;
152+ if ( value !== null ) {
153+ onChange ( days , ( ) => { } ) ;
154+ return ;
155+ }
156+ const submission = { days } ;
157+ setEnabling ( submission ) ;
158+ onChange ( days , ( ) => setEnabling ( ( current ) => ( current === submission ? null : current ) ) ) ;
159+ } ;
160+
161+ const switchOff = ( ) => {
162+ setDraft ( value ) ;
163+ setHeld ( false ) ;
164+ setEnabling ( null ) ;
165+ if ( value === null && pending === null ) return ;
166+ setSwitchedOff ( true ) ;
167+ onChange ( null , ( ) => setSwitchedOff ( false ) ) ;
168+ } ;
129169
130170 return (
131- < div className = "flex items-center gap-3" >
132- { value !== null ? (
171+ < div
172+ ref = { row }
173+ className = "flex items-center gap-3"
174+ onBlur = { ( event ) => {
175+ if ( event . currentTarget . contains ( event . relatedTarget ) ) return ;
176+ if ( held && draft !== null ) commit ( draft ) ;
177+ else setEnabling ( ( current ) => ( typeof current === "string" ? null : current ) ) ;
178+ } }
179+ >
180+ { on ? (
133181 < NumberField
134182 value = { draft }
135183 min = { minimum }
136184 max = { 3650 }
137185 step = { 1 }
138186 size = "sm"
139187 className = "w-auto"
140- onValueChange = { setDraft }
141- onValueCommitted = { ( next ) => {
188+ onValueChange = { ( next ) => {
189+ setDraft ( next ) ;
190+ setEnabling ( ( current ) => ( current === "draft" ? "edited" : current ) ) ;
191+ } }
192+ onValueCommitted = { ( next , { reason, event } ) => {
142193 if ( next === null ) setDraft ( value ) ;
143- else {
144- const days = Math . min ( 3650 , Math . max ( minimum , Math . round ( next ) ) ) ;
145- setDraft ( days ) ;
146- onChange ( days ) ;
147- }
194+ else if (
195+ reason === "input-blur"
196+ ? event instanceof FocusEvent &&
197+ event . relatedTarget instanceof Node &&
198+ row . current ?. contains ( event . relatedTarget )
199+ : value === null
200+ ) {
201+ setDraft ( next ) ;
202+ setHeld ( true ) ;
203+ } else commit ( next ) ;
148204 } }
149205 >
150206 < NumberFieldGroup >
151207 < NumberFieldDecrement aria-label = { `Decrease ${ label } ` } />
152208 < NumberFieldInput
153209 aria-label = { `${ label } in days` }
210+ autoFocus = { value === null }
154211 size = { 5 }
155212 className = "w-auto grow-0 text-right"
213+ // Any typed or pasted edit replaces a held age: NumberField commits it on blur itself,
214+ // and text it cannot parse yet must not leave the held age to be saved.
215+ onChange = { ( ) => setHeld ( false ) }
216+ onPaste = { ( ) => setHeld ( false ) }
217+ onKeyDown = { ( event ) => {
218+ if ( event . nativeEvent . isComposing || event . keyCode === 229 ) return ;
219+ if ( event . key === "Enter" ) {
220+ // Blurring lets NumberField parse the visible text and commit it once. It
221+ // skips an unedited value, so an untouched draft is committed here.
222+ const input = event . currentTarget ;
223+ const shown =
224+ draft !== null && input . value === new Intl . NumberFormat ( ) . format ( draft ) ;
225+ if ( enabling === "draft" && shown ) commit ( draft ) ;
226+ input . blur ( ) ;
227+ } else if ( event . key === "Escape" && ( held || typeof enabling === "string" ) ) {
228+ // An unhandled Escape closes the settings page.
229+ event . preventDefault ( ) ;
230+ event . stopPropagation ( ) ;
231+ setDraft ( pending ?. days ?? value ) ;
232+ setHeld ( false ) ;
233+ setEnabling ( ( current ) => ( typeof current === "string" ? null : current ) ) ;
234+ }
235+ } }
156236 />
157237 < span aria-hidden = "true" className = "self-center pr-2 text-xs" >
158238 days
@@ -165,8 +245,15 @@ function RetentionControl({
165245 ) }
166246 < Switch
167247 aria-label = { label }
168- checked = { value !== null }
169- onCheckedChange = { ( enabled ) => onChange ( enabled ? 8 : null ) }
248+ checked = { on }
249+ // Keeps focus in the age field, so switching off never commits an edited age first.
250+ onMouseDown = { ( event ) => event . preventDefault ( ) }
251+ onCheckedChange = { ( enabled ) => {
252+ if ( enabled ) {
253+ setDraft ( 8 ) ;
254+ setEnabling ( "draft" ) ;
255+ } else switchOff ( ) ;
256+ } }
170257 />
171258 </ div >
172259 ) ;
@@ -486,12 +573,12 @@ export function WorktreeCleanupSection() {
486573 // A policy locks these through worktreeCleanup, which also reaches each project's own rules.
487574 const lockablePath = ( key : keyof WorktreeCleanupRules ) =>
488575 isProjectScope ? [ "worktreeCleanup" ] : [ "worktreeCleanup" , `storageCleanup.${ key } ` ] ;
489- const update = ( patch : Partial < StorageCleanupSettings > ) =>
490- updateSettings ( { storageCleanup : patch } ) ;
491- const updateWorktree = ( patch : Partial < WorktreeCleanupRules > ) =>
576+ const update = ( patch : Partial < StorageCleanupSettings > , options ?: SaveOptions ) =>
577+ updateSettings ( { storageCleanup : patch } , options ) ;
578+ const updateWorktree = ( patch : Partial < WorktreeCleanupRules > , options ?: SaveOptions ) =>
492579 isProjectScope
493- ? updateSettings ( { worktreeCleanup : { mode : "custom" , rules : patch } } )
494- : update ( patch ) ;
580+ ? updateSettings ( { worktreeCleanup : { mode : "custom" , rules : patch } } , options )
581+ : update ( patch , options ) ;
495582
496583 if (
497584 isProjectScope &&
@@ -602,7 +689,9 @@ export function WorktreeCleanupSection() {
602689 < RetentionControl
603690 label = "Delete inactive worktrees"
604691 value = { settings . worktreeAfterDays }
605- onChange = { ( worktreeAfterDays ) => updateWorktree ( { worktreeAfterDays } ) }
692+ onChange = { ( worktreeAfterDays , onFailed ) =>
693+ updateWorktree ( { worktreeAfterDays } , { onFailed } )
694+ }
606695 />
607696 }
608697 />
@@ -634,8 +723,8 @@ export function WorktreeCleanupSection() {
634723 label = "Delete settled worktrees"
635724 value = { settings . worktreeSettledAfterDays }
636725 minimum = { 0 }
637- onChange = { ( worktreeSettledAfterDays ) =>
638- updateWorktree ( { worktreeSettledAfterDays } )
726+ onChange = { ( worktreeSettledAfterDays , onFailed ) =>
727+ updateWorktree ( { worktreeSettledAfterDays } , { onFailed } )
639728 }
640729 />
641730 }
@@ -737,8 +826,8 @@ export function StorageCleanupSections() {
737826 ? "Mixed across selected machines"
738827 : undefined ;
739828 if ( scope . kind === "project" || scope . kind === "checkout" ) return null ;
740- const update = ( patch : Partial < StorageCleanupSettings > ) =>
741- updateSettings ( { storageCleanup : patch } ) ;
829+ const update = ( patch : Partial < StorageCleanupSettings > , options : SaveOptions ) =>
830+ updateSettings ( { storageCleanup : patch } , options ) ;
742831 const databaseStorageNotice = (
743832 < DatabaseStorageNotice
744833 environments = { connectedEnvironments . filter (
@@ -773,7 +862,9 @@ export function StorageCleanupSections() {
773862 < RetentionControl
774863 label = "Delete old browser artifacts"
775864 value = { settings . browserArtifactsAfterDays }
776- onChange = { ( browserArtifactsAfterDays ) => update ( { browserArtifactsAfterDays } ) }
865+ onChange = { ( browserArtifactsAfterDays , onFailed ) =>
866+ update ( { browserArtifactsAfterDays } , { onFailed } )
867+ }
777868 />
778869 }
779870 />
@@ -789,7 +880,7 @@ export function StorageCleanupSections() {
789880 < RetentionControl
790881 label = "Delete old rotated logs"
791882 value = { settings . logsAfterDays }
792- onChange = { ( logsAfterDays ) => update ( { logsAfterDays } ) }
883+ onChange = { ( logsAfterDays , onFailed ) => update ( { logsAfterDays } , { onFailed } ) }
793884 />
794885 }
795886 />
0 commit comments