Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
markScheduledTaskEditorSaved,
readScheduledTaskEditor,
scheduledTaskEditorSessionAtom,
setScheduledTaskEditorSaving,
startScheduledTaskEditor,
takeScheduledTaskNewLink,
updateScheduledTaskEditor,
Expand Down Expand Up @@ -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);
Expand Down
19 changes: 19 additions & 0 deletions apps/mobile/src/features/settings/scheduled-task-editor-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ScheduledTaskEditorSession | null>(
Expand All @@ -43,6 +44,7 @@ export function startScheduledTaskEditor(next: ScheduledTaskEditor | null): void
id: ++nextEditorId,
initial: next,
current: next,
saving: false,
});
}

Expand All @@ -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.
Expand Down Expand Up @@ -93,6 +110,8 @@ export function takeScheduledTaskNewLink(
): PartialState<NavigationState> | 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 {
Expand Down
5 changes: 5 additions & 0 deletions apps/mobile/src/features/settings/scheduled-task-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { settingsTargetsForProject } from "./settings-environment-filter.logic";
import {
scheduledTaskEditorHasChanges,
scheduledTaskEditorSessionAtom,
setScheduledTaskEditorSaving,
startScheduledTaskEditor,
updateScheduledTaskEditor,
readScheduledTaskEditor,
Expand Down Expand Up @@ -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<SetStateAction<ScheduledTaskEditor | null>> = (update) => {
updateScheduledTaskEditor(update, defaultEditor);
};
Expand All @@ -111,6 +114,8 @@ export function useScheduledTaskEditor() {
setEditor,
startEditor,
hasChanges,
saving,
setSaving,
resetEditor,
draftForEnvironment,
};
Expand Down
Loading