@@ -74,6 +74,7 @@ import { SettingsSection } from "./components/SettingsSection";
7474import { useSettingsEnvironmentFilter , type SettingsTarget } from "./settings-environment-filter" ;
7575import {
7676 editDraft ,
77+ isIntervalWindowRange ,
7778 parseIntervalMinutes ,
7879 scheduledTaskDefaultModel ,
7980 scheduleFromDraft ,
@@ -115,7 +116,9 @@ function describeSchedule(task: ScheduledTask): string {
115116 const runAt = new Date ( task . schedule . runAt ) ;
116117 return `Once, ${ formatRunAtDate ( runAt ) } at ${ formatRunAtTime ( runAt ) } ` ;
117118 }
118- if ( task . schedule . type === "interval" ) return formatScheduledTaskInterval ( task . schedule . everyMs ) ;
119+ if ( task . schedule . type === "interval" ) {
120+ return formatScheduledTaskInterval ( task . schedule . everyMs , task . schedule . window ) ;
121+ }
119122 const days = task . schedule . weekdays ?. length ? repeatLabel ( task . schedule . weekdays ) : "Every day" ;
120123 return `${ days } at ${ formatTime ( task . schedule . timeOfDay ) } ` ;
121124}
@@ -148,6 +151,19 @@ function timePickerValue(value: string): Date {
148151 return time ;
149152}
150153
154+ function weekdaysAfterRepeatAction (
155+ weekdays : ReadonlyArray < number > ,
156+ id : string ,
157+ ) : ReadonlyArray < number > {
158+ if ( id === "every_day" ) return DAYS . map ( ( day ) => day . index ) ;
159+ if ( id === "weekdays" ) return [ 1 , 2 , 3 , 4 , 5 ] ;
160+ const day = DAYS . find ( ( item ) => String ( item . index ) === id ) ;
161+ if ( ! day ) return weekdays ;
162+ return weekdays . includes ( day . index )
163+ ? weekdays . filter ( ( index ) => index !== day . index )
164+ : [ ...weekdays , day . index ] ;
165+ }
166+
151167function repeatLabel ( weekdays : ReadonlyArray < number > ) : string {
152168 const days = new Set ( weekdays ) ;
153169 if ( days . size === 7 ) return "Every day" ;
@@ -375,6 +391,75 @@ function PickerRow(props: {
375391 ) ;
376392}
377393
394+ function WeekdayRow ( props : {
395+ readonly weekdays : ReadonlyArray < number > ;
396+ readonly onChange : ( weekdays : ReadonlyArray < number > ) => void ;
397+ } ) {
398+ return (
399+ < SelectRow
400+ label = "Repeat"
401+ value = { repeatLabel ( props . weekdays ) }
402+ borderTop
403+ actions = { [
404+ {
405+ id : "every_day" ,
406+ title : "Every day" ,
407+ state : props . weekdays . length === 7 ? "on" : undefined ,
408+ } ,
409+ {
410+ id : "weekdays" ,
411+ title : "Weekdays" ,
412+ state : repeatLabel ( props . weekdays ) === "Weekdays" ? "on" : undefined ,
413+ } ,
414+ ...DAYS . map ( ( day ) => ( {
415+ id : String ( day . index ) ,
416+ title : day . label ,
417+ attributes : { keepsMenuPresented : true } ,
418+ state : props . weekdays . includes ( day . index ) ? ( "on" as const ) : undefined ,
419+ } ) ) ,
420+ ] }
421+ onSelect = { ( id ) => props . onChange ( weekdaysAfterRepeatAction ( props . weekdays , id ) ) }
422+ />
423+ ) ;
424+ }
425+
426+ function clockValue ( selected : Date ) : string {
427+ return `${ String ( selected . getHours ( ) ) . padStart ( 2 , "0" ) } :${ String ( selected . getMinutes ( ) ) . padStart ( 2 , "0" ) } ` ;
428+ }
429+
430+ function ClockField ( props : {
431+ readonly label : string ;
432+ readonly value : string ;
433+ readonly open : boolean ;
434+ readonly onToggle : ( ) => void ;
435+ readonly onDismiss : ( ) => void ;
436+ readonly onChange : ( value : string ) => void ;
437+ } ) {
438+ return (
439+ < View >
440+ < PickerRow
441+ label = { props . label }
442+ value = { formatTime ( props . value ) }
443+ borderTop
444+ onPress = { props . onToggle }
445+ />
446+ { props . open ? (
447+ < DateTimePicker
448+ value = { timePickerValue ( props . value ) }
449+ mode = "time"
450+ display = { Platform . OS === "ios" ? "spinner" : "default" }
451+ onDismiss = { props . onDismiss }
452+ onValueChange = { ( _ , selected ) => {
453+ // Android shows a dialog that closes on pick; iOS stays inline.
454+ if ( Platform . OS === "android" ) props . onDismiss ( ) ;
455+ props . onChange ( clockValue ( selected ) ) ;
456+ } }
457+ />
458+ ) : null }
459+ </ View >
460+ ) ;
461+ }
462+
378463export function SettingsScheduledTasksRouteScreen ( ) {
379464 const [ now , setNow ] = useState ( Date . now ) ;
380465 useFocusEffect ( ( ) => {
@@ -704,6 +789,7 @@ function TaskForm({
704789 } ) ;
705790 const submissionPending = useRef ( false ) ;
706791 const [ timePickerOpen , setTimePickerOpen ] = useState ( false ) ;
792+ const [ windowClock , setWindowClock ] = useState < "start" | "end" | null > ( null ) ;
707793 const [ runAtPicker , setRunAtPicker ] = useState < "date" | "time" | null > ( null ) ;
708794 const taskMissing =
709795 draft . task !== null &&
@@ -1011,6 +1097,7 @@ function TaskForm({
10111097 selected = { draft . schedule . mode }
10121098 onSelect = { ( mode ) => {
10131099 setTimePickerOpen ( false ) ;
1100+ setWindowClock ( null ) ;
10141101 setRunAtPicker ( null ) ;
10151102 setDraft ( {
10161103 ...draft ,
@@ -1066,45 +1153,11 @@ function TaskForm({
10661153 } }
10671154 />
10681155 ) : null }
1069- < SelectRow
1070- label = "Repeat"
1071- value = { repeatLabel ( draft . schedule . weekdays ) }
1072- borderTop
1073- actions = { [
1074- {
1075- id : "every_day" ,
1076- title : "Every day" ,
1077- state : draft . schedule . weekdays . length === 7 ? "on" : undefined ,
1078- } ,
1079- {
1080- id : "weekdays" ,
1081- title : "Weekdays" ,
1082- state : repeatLabel ( draft . schedule . weekdays ) === "Weekdays" ? "on" : undefined ,
1083- } ,
1084- ...DAYS . map ( ( day ) => ( {
1085- id : String ( day . index ) ,
1086- title : day . label ,
1087- attributes : { keepsMenuPresented : true } ,
1088- state : draft . schedule . weekdays . includes ( day . index )
1089- ? ( "on" as const )
1090- : undefined ,
1091- } ) ) ,
1092- ] }
1093- onSelect = { ( id ) => {
1094- const weekdays =
1095- id === "every_day"
1096- ? DAYS . map ( ( day ) => day . index )
1097- : id === "weekdays"
1098- ? [ 1 , 2 , 3 , 4 , 5 ]
1099- : ( ( ) => {
1100- const day = DAYS . find ( ( item ) => String ( item . index ) === id ) ;
1101- if ( ! day ) return draft . schedule . weekdays ;
1102- return draft . schedule . weekdays . includes ( day . index )
1103- ? draft . schedule . weekdays . filter ( ( index ) => index !== day . index )
1104- : [ ...draft . schedule . weekdays , day . index ] ;
1105- } ) ( ) ;
1106- setDraft ( { ...draft , schedule : { ...draft . schedule , weekdays } } ) ;
1107- } }
1156+ < WeekdayRow
1157+ weekdays = { draft . schedule . weekdays }
1158+ onChange = { ( weekdays ) =>
1159+ setDraft ( { ...draft , schedule : { ...draft . schedule , weekdays } } )
1160+ }
11081161 />
11091162 </ >
11101163 ) : draft . schedule . mode === "webhook" && webhookDenial !== undefined && draft . enabled ? (
@@ -1251,6 +1304,66 @@ function TaskForm({
12511304 at least 1 minute.
12521305 </ Text >
12531306 ) : null }
1307+ < View className = "min-h-14 flex-row items-center gap-3 border-t border-border-subtle px-4 py-3" >
1308+ < Text textRole = "body" className = "min-w-0 flex-1 text-foreground" >
1309+ Time window
1310+ </ Text >
1311+ < ThemedSwitch
1312+ accessibilityLabel = "Time window"
1313+ value = { draft . schedule . intervalWindowEnabled }
1314+ onValueChange = { ( intervalWindowEnabled ) => {
1315+ if ( ! intervalWindowEnabled ) setWindowClock ( null ) ;
1316+ setDraft ( {
1317+ ...draft ,
1318+ schedule : { ...draft . schedule , intervalWindowEnabled } ,
1319+ } ) ;
1320+ } }
1321+ />
1322+ </ View >
1323+ { draft . schedule . intervalWindowEnabled ? (
1324+ < >
1325+ < ClockField
1326+ label = "Start"
1327+ value = { draft . schedule . intervalWindowStart }
1328+ open = { windowClock === "start" }
1329+ onToggle = { ( ) => setWindowClock ( ( open ) => ( open === "start" ? null : "start" ) ) }
1330+ onDismiss = { ( ) => setWindowClock ( null ) }
1331+ onChange = { ( intervalWindowStart ) =>
1332+ setDraft ( {
1333+ ...draft ,
1334+ schedule : { ...draft . schedule , intervalWindowStart } ,
1335+ } )
1336+ }
1337+ />
1338+ < ClockField
1339+ label = "End"
1340+ value = { draft . schedule . intervalWindowEnd }
1341+ open = { windowClock === "end" }
1342+ onToggle = { ( ) => setWindowClock ( ( open ) => ( open === "end" ? null : "end" ) ) }
1343+ onDismiss = { ( ) => setWindowClock ( null ) }
1344+ onChange = { ( intervalWindowEnd ) =>
1345+ setDraft ( {
1346+ ...draft ,
1347+ schedule : { ...draft . schedule , intervalWindowEnd } ,
1348+ } )
1349+ }
1350+ />
1351+ { isIntervalWindowRange (
1352+ draft . schedule . intervalWindowStart ,
1353+ draft . schedule . intervalWindowEnd ,
1354+ ) ? null : (
1355+ < Text className = "px-4 pb-3 text-sm text-danger-foreground" >
1356+ End time must be after the start time.
1357+ </ Text >
1358+ ) }
1359+ < WeekdayRow
1360+ weekdays = { draft . schedule . weekdays }
1361+ onChange = { ( weekdays ) =>
1362+ setDraft ( { ...draft , schedule : { ...draft . schedule , weekdays } } )
1363+ }
1364+ />
1365+ </ >
1366+ ) : null }
12541367 </ >
12551368 ) }
12561369 < View className = "min-h-14 flex-row items-center gap-3 border-t border-border-subtle px-4 py-3" >
@@ -1264,7 +1377,8 @@ function TaskForm({
12641377 />
12651378 </ View >
12661379 </ SettingsSection >
1267- { draft . schedule . mode === "fixed_time" ? (
1380+ { draft . schedule . mode === "fixed_time" ||
1381+ ( draft . schedule . mode === "interval" && draft . schedule . intervalWindowEnabled ) ? (
12681382 < Text className = "px-2 text-sm text-foreground-muted" >
12691383 Time uses the environment's time zone, which may differ from your phone's.
12701384 </ Text >
@@ -1280,7 +1394,13 @@ function TaskForm({
12801394 taskMissing ||
12811395 environmentUnavailable ||
12821396 // The server refuses an enabled webhook task under this policy.
1283- ( draft . enabled && draft . schedule . mode === "webhook" && webhookDenial !== undefined )
1397+ ( draft . enabled && draft . schedule . mode === "webhook" && webhookDenial !== undefined ) ||
1398+ ( draft . schedule . mode === "interval" &&
1399+ draft . schedule . intervalWindowEnabled &&
1400+ ! isIntervalWindowRange (
1401+ draft . schedule . intervalWindowStart ,
1402+ draft . schedule . intervalWindowEnd ,
1403+ ) )
12841404 }
12851405 onPress = { ( ) => void save ( ) }
12861406 label = { saving ? "Saving…" : draft . task ? "Save changes" : "Create task" }
0 commit comments