diff --git a/apps/mobile/src/features/settings/SettingsScheduledTasksRouteScreen.tsx b/apps/mobile/src/features/settings/SettingsScheduledTasksRouteScreen.tsx index 04e8844d061dd..077f983ac0f2e 100644 --- a/apps/mobile/src/features/settings/SettingsScheduledTasksRouteScreen.tsx +++ b/apps/mobile/src/features/settings/SettingsScheduledTasksRouteScreen.tsx @@ -537,12 +537,19 @@ export function SettingsScheduledTaskEditRouteScreen() { function SettingsScheduledTaskEditorScreen({ title }: { readonly title: string }) { const gt = useGT(); - const { editor, voiceOwnerKey, readEditor, setEditor, hasChanges, draftForEnvironment } = - useScheduledTaskEditor(); + const { + editor, + voiceOwnerKey, + readEditor, + setEditor, + hasChanges, + saving, + setSaving, + draftForEnvironment, + } = useScheduledTaskEditor(); const { availableTargets } = useSettingsEnvironmentFilter(); const navigation = useNavigation(); const insets = useSafeAreaInsets(); - const [saving, setSaving] = useState(false); // Set once the save lands, so the removal guard lets the editor close. const saved = useRef(false); const ownerKey = editor ? `${voiceOwnerKey}:${editor.environmentId}` : null; diff --git a/apps/mobile/src/features/settings/scheduled-task-editor-state.test.ts b/apps/mobile/src/features/settings/scheduled-task-editor-state.test.ts index 4171f49607a3b..1cb656eccd6a9 100644 --- a/apps/mobile/src/features/settings/scheduled-task-editor-state.test.ts +++ b/apps/mobile/src/features/settings/scheduled-task-editor-state.test.ts @@ -8,6 +8,7 @@ import { markScheduledTaskEditorSaved, readScheduledTaskEditor, scheduledTaskEditorSessionAtom, + setScheduledTaskEditorSaving, startScheduledTaskEditor, takeScheduledTaskNewLink, updateScheduledTaskEditor, @@ -95,6 +96,14 @@ describe("new scheduled task link", () => { expect(appAtomRegistry.get(scheduledTaskEditorSessionAtom)).toBeNull(); }); + it("keeps an unchanged task while its save is still pending", () => { + startScheduledTaskEditor(editing); + setScheduledTaskEditorSaving(true, null); + + takeScheduledTaskNewLink(newTaskLink); + expect(appAtomRegistry.get(scheduledTaskEditorSessionAtom)?.current).toBe(editing); + }); + it("starts the new-task screen fresh after the edited task was saved", () => { startScheduledTaskEditor(editing); updateScheduledTaskEditor({ ...editing, draft: { ...editing.draft, prompt: "saved" } }, null); diff --git a/apps/mobile/src/features/settings/scheduled-task-editor-state.ts b/apps/mobile/src/features/settings/scheduled-task-editor-state.ts index 73b8b1da72777..d4a7ba791239f 100644 --- a/apps/mobile/src/features/settings/scheduled-task-editor-state.ts +++ b/apps/mobile/src/features/settings/scheduled-task-editor-state.ts @@ -17,6 +17,7 @@ type ScheduledTaskEditorSession = { readonly id: number; readonly initial: ScheduledTaskEditor | null; readonly current: ScheduledTaskEditor | null; + readonly saving: boolean; }; export const scheduledTaskEditorSessionAtom = Atom.make( @@ -43,6 +44,7 @@ export function startScheduledTaskEditor(next: ScheduledTaskEditor | null): void id: ++nextEditorId, initial: next, current: next, + saving: false, }); } @@ -59,9 +61,24 @@ export function updateScheduledTaskEditor( id: session?.id ?? 0, initial: session ? session.initial : previous, current: typeof update === "function" ? update(previous) : update, + saving: session?.saving ?? false, }); } +/** The pending save lives with the draft so a new-task link cannot clear the task it writes. */ +export function setScheduledTaskEditorSaving( + saving: boolean, + defaultEditor: ScheduledTaskEditor | null, +): void { + const session = appAtomRegistry.get(scheduledTaskEditorSessionAtom); + appAtomRegistry.set( + scheduledTaskEditorSessionAtom, + session + ? { ...session, saving } + : { id: 0, initial: defaultEditor, current: defaultEditor, saving }, + ); +} + /** * A saved task is the new baseline. Without it a later new-task link reads the saved edits as * unsaved, keeps them under "New", and saving there would overwrite the task. @@ -93,6 +110,8 @@ export function takeScheduledTaskNewLink( ): PartialState | undefined { if (findLinkedRoute(state, ["SettingsScheduledTaskNew"])) { const session = appAtomRegistry.get(scheduledTaskEditorSessionAtom); + // The open editor's removal guard refuses the link while the save is pending. + if (session?.saving) return state; if (session && scheduledTaskEditorHasChanges(session.initial, session.current)) { newTaskLinkAwaitsDiscard = true; } else { diff --git a/apps/mobile/src/features/settings/scheduled-task-editor.tsx b/apps/mobile/src/features/settings/scheduled-task-editor.tsx index 4e65e12c87508..fb65ac0be769f 100644 --- a/apps/mobile/src/features/settings/scheduled-task-editor.tsx +++ b/apps/mobile/src/features/settings/scheduled-task-editor.tsx @@ -14,6 +14,7 @@ import { settingsTargetsForProject } from "./settings-environment-filter.logic"; import { scheduledTaskEditorHasChanges, scheduledTaskEditorSessionAtom, + setScheduledTaskEditorSaving, startScheduledTaskEditor, updateScheduledTaskEditor, readScheduledTaskEditor, @@ -98,6 +99,8 @@ export function useScheduledTaskEditor() { const editor = session ? session.current : defaultEditor; const initial = session ? session.initial : defaultEditor; const hasChanges = scheduledTaskEditorHasChanges(initial, editor); + const saving = session?.saving ?? false; + const setSaving = (next: boolean) => setScheduledTaskEditorSaving(next, defaultEditor); const setEditor: Dispatch> = (update) => { updateScheduledTaskEditor(update, defaultEditor); }; @@ -111,6 +114,8 @@ export function useScheduledTaskEditor() { setEditor, startEditor, hasChanges, + saving, + setSaving, resetEditor, draftForEnvironment, };