The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { EnvironmentId } from "@t3tools/contracts"; |
| 2 | import type { NavigationState, PartialState } from "@react-navigation/native"; |
| 3 | import { Atom } from "effect/reactivity"; |
| 4 | |
| 5 | import { findLinkedRoute } from "../../lib/appLinking"; |
| 6 | |
| 7 | import { appAtomRegistry } from "../../state/atom-registry"; |
| 8 | import { hasScheduledTaskDraftChanges, type ScheduledTaskDraft } from "./scheduledTaskDraft"; |
| 9 | |
| 10 | export type ScheduledTaskEditor = { |
| 11 | readonly environmentId: EnvironmentId; |
| 12 | readonly environmentLabel: string; |
| 13 | readonly draft: ScheduledTaskDraft; |
| 14 | }; |
| 15 | |
| 16 | type ScheduledTaskEditorSession = { |
| 17 | readonly id: number; |
| 18 | readonly initial: ScheduledTaskEditor | null; |
| 19 | readonly current: ScheduledTaskEditor | null; |
| 20 | readonly saving: boolean; |
| 21 | }; |
| 22 | |
| 23 | export const scheduledTaskEditorSessionAtom = Atom.make<ScheduledTaskEditorSession | null>( |
| 24 | null, |
| 25 | ).pipe(Atom.keepAlive); |
| 26 | let nextEditorId = 0; |
| 27 | let newTaskLinkAwaitsDiscard = false; |
| 28 | |
| 29 | export function scheduledTaskEditorHasChanges( |
| 30 | initial: ScheduledTaskEditor | null, |
| 31 | current: ScheduledTaskEditor | null, |
| 32 | ): boolean { |
| 33 | return Boolean( |
| 34 | initial && |
| 35 | current && |
| 36 | (initial.environmentId !== current.environmentId || |
| 37 | hasScheduledTaskDraftChanges(initial.draft, current.draft)), |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | export function startScheduledTaskEditor(next: ScheduledTaskEditor | null): void { |
| 42 | newTaskLinkAwaitsDiscard = false; |
| 43 | appAtomRegistry.set(scheduledTaskEditorSessionAtom, { |
| 44 | id: ++nextEditorId, |
| 45 | initial: next, |
| 46 | current: next, |
| 47 | saving: false, |
| 48 | }); |
| 49 | } |
| 50 | |
| 51 | export function updateScheduledTaskEditor( |
| 52 | update: |
| 53 | | ScheduledTaskEditor |
| 54 | | null |
| 55 | | ((current: ScheduledTaskEditor | null) => ScheduledTaskEditor | null), |
| 56 | defaultEditor: ScheduledTaskEditor | null, |
| 57 | ): void { |
| 58 | const session = appAtomRegistry.get(scheduledTaskEditorSessionAtom); |
| 59 | const previous = session ? session.current : defaultEditor; |
| 60 | appAtomRegistry.set(scheduledTaskEditorSessionAtom, { |
| 61 | id: session?.id ?? 0, |
| 62 | initial: session ? session.initial : previous, |
| 63 | current: typeof update === "function" ? update(previous) : update, |
| 64 | saving: session?.saving ?? false, |
| 65 | }); |
| 66 | } |
| 67 | |
| 68 | /** The pending save lives with the draft so a new-task link cannot clear the task it writes. */ |
| 69 | export function setScheduledTaskEditorSaving( |
| 70 | saving: boolean, |
| 71 | defaultEditor: ScheduledTaskEditor | null, |
| 72 | ): void { |
| 73 | const session = appAtomRegistry.get(scheduledTaskEditorSessionAtom); |
| 74 | appAtomRegistry.set( |
| 75 | scheduledTaskEditorSessionAtom, |
| 76 | session |
| 77 | ? { ...session, saving } |
| 78 | : { id: 0, initial: defaultEditor, current: defaultEditor, saving }, |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * A saved task is the new baseline. Without it a later new-task link reads the saved edits as |
| 84 | * unsaved, keeps them under "New", and saving there would overwrite the task. |
| 85 | */ |
| 86 | export function markScheduledTaskEditorSaved(): void { |
| 87 | newTaskLinkAwaitsDiscard = false; |
| 88 | const session = appAtomRegistry.get(scheduledTaskEditorSessionAtom); |
| 89 | if (session) { |
| 90 | appAtomRegistry.set(scheduledTaskEditorSessionAtom, { ...session, initial: session.current }); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | export function readScheduledTaskEditor( |
| 95 | voiceOwnerKey: string, |
| 96 | defaultEditor: ScheduledTaskEditor | null, |
| 97 | ): ScheduledTaskEditor | null { |
| 98 | const session = appAtomRegistry.get(scheduledTaskEditorSessionAtom); |
| 99 | if (`scheduled-task:${session?.id ?? 0}` !== voiceOwnerKey) return null; |
| 100 | return session ? session.current : defaultEditor; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * A new-task link starts a fresh editor, or a warm app reopens the last task's draft under |
| 105 | * "New" and saves over it. Links parse before the open editor's removal guard runs, so |
| 106 | * unsaved changes stay until the user discards them there. |
| 107 | */ |
| 108 | export function takeScheduledTaskNewLink( |
| 109 | state: PartialState<NavigationState> | undefined, |
| 110 | ): PartialState<NavigationState> | undefined { |
| 111 | if (findLinkedRoute(state, ["SettingsScheduledTaskNew"])) { |
| 112 | const session = appAtomRegistry.get(scheduledTaskEditorSessionAtom); |
| 113 | // The open editor's removal guard refuses the link while the save is pending. |
| 114 | if (session?.saving) return state; |
| 115 | if (session && scheduledTaskEditorHasChanges(session.initial, session.current)) { |
| 116 | newTaskLinkAwaitsDiscard = true; |
| 117 | } else { |
| 118 | appAtomRegistry.set(scheduledTaskEditorSessionAtom, null); |
| 119 | } |
| 120 | } |
| 121 | return state; |
| 122 | } |
| 123 | |
| 124 | /** Drops unsaved changes; a new-task link that waited on them then opens a fresh editor. */ |
| 125 | export function discardScheduledTaskEditor(): void { |
| 126 | if (newTaskLinkAwaitsDiscard) { |
| 127 | newTaskLinkAwaitsDiscard = false; |
| 128 | appAtomRegistry.set(scheduledTaskEditorSessionAtom, null); |
| 129 | return; |
| 130 | } |
| 131 | startScheduledTaskEditor(null); |
| 132 | } |