Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

scheduled-task-editor-state.ts

113 lines3.8 KB

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