@devries-cameron20 / t3code Mirror · read-only / commits / 30a084c463
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 24, 22:45 UTC

[codex] Preserve mobile composer draft failures (#3348)

Co-authored-by: codex <codex@users.noreply.github.com>

Julius Marminge committed Jun 20, 2026 at 11:08 UTC 30a084c463acb7b8b75550a117b9bd82e98c5ac4
1 file changed +66 -15
apps/mobile/src/state/use-composer-drafts.ts
+66 -15
@@ -1,5 +1,6 @@
1 import { useAtomValue } from "@effect/atom-react";
2 import type { EnvironmentId } from "@t3tools/contracts";
3 +import * as Schema from "effect/Schema";
4 import { useEffect } from "react";
5 import { Atom } from "effect/unstable/reactivity";
6
@@ -11,6 +12,20 @@ const COMPOSER_DRAFTS_DIRECTORY = "composer-drafts";
12 const COMPOSER_DRAFTS_FILE = "drafts.json";
13 const PERSIST_DEBOUNCE_MS = 200;
14
15 +export class ComposerDraftPersistenceError extends Schema.TaggedErrorClass<ComposerDraftPersistenceError>()(
16 + "ComposerDraftPersistenceError",
17 + {
18 + operation: Schema.Literals(["open", "read", "decode", "encode", "write", "hydrate"]),
19 + directory: Schema.String,
20 + fileName: Schema.String,
21 + cause: Schema.Defect(),
22 + },
23 +) {
24 + override get message(): string {
25 + return `Composer draft persistence operation ${this.operation} failed for ${this.directory}/${this.fileName}.`;
26 + }
27 +}
28 +
29 export interface ComposerDraft {
30 readonly text: string;
31 readonly attachments: ReadonlyArray<DraftComposerImageAttachment>;
@@ -56,12 +71,16 @@ async function getComposerDraftsFile() {
71 }
72
73 async function loadPersistedComposerDrafts(): Promise<Record<string, ComposerDraft>> {
74 + let operation: ComposerDraftPersistenceError["operation"] = "open";
75 try {
76 const file = await getComposerDraftsFile();
77 if (!file.exists) {
78 return {};
79 }
64 - const parsed = JSON.parse(await file.text()) as Partial<PersistedComposerDrafts>;
80 + operation = "read";
81 + const raw = await file.text();
82 + operation = "decode";
83 + const parsed = JSON.parse(raw) as Partial<PersistedComposerDrafts>;
84 if (parsed.schemaVersion !== COMPOSER_DRAFTS_SCHEMA_VERSION || !parsed.drafts) {
85 return {};
86 }
@@ -75,30 +94,53 @@ async function loadPersistedComposerDrafts(): Promise<Record<string, ComposerDra
94 );
95 }),
96 );
78 - } catch {
97 + } catch (cause) {
98 + console.warn(
99 + "[composer-drafts] ignored persisted draft failure",
100 + new ComposerDraftPersistenceError({
101 + operation,
102 + directory: COMPOSER_DRAFTS_DIRECTORY,
103 + fileName: COMPOSER_DRAFTS_FILE,
104 + cause,
105 + }),
106 + );
107 return {};
108 }
109 }
110
111 async function writePersistedComposerDrafts(drafts: Record<string, ComposerDraft>): Promise<void> {
84 - const file = await getComposerDraftsFile();
85 - const nonEmptyDrafts = Object.fromEntries(
86 - Object.entries(drafts).filter(([, draft]) => !isEmptyDraft(draft)),
87 - );
88 - const document: PersistedComposerDrafts = {
89 - schemaVersion: COMPOSER_DRAFTS_SCHEMA_VERSION,
90 - drafts: nonEmptyDrafts,
91 - };
92 - if (!file.exists) {
93 - file.create({ intermediates: true, overwrite: true });
112 + let operation: ComposerDraftPersistenceError["operation"] = "open";
113 + try {
114 + const file = await getComposerDraftsFile();
115 + operation = "encode";
116 + const nonEmptyDrafts = Object.fromEntries(
117 + Object.entries(drafts).filter(([, draft]) => !isEmptyDraft(draft)),
118 + );
119 + const document: PersistedComposerDrafts = {
120 + schemaVersion: COMPOSER_DRAFTS_SCHEMA_VERSION,
121 + drafts: nonEmptyDrafts,
122 + };
123 + const encoded = JSON.stringify(document);
124 + operation = "write";
125 + if (!file.exists) {
126 + file.create({ intermediates: true, overwrite: true });
127 + }
128 + file.write(encoded);
129 + } catch (cause) {
130 + throw new ComposerDraftPersistenceError({
131 + operation,
132 + directory: COMPOSER_DRAFTS_DIRECTORY,
133 + fileName: COMPOSER_DRAFTS_FILE,
134 + cause,
135 + });
136 }
95 - file.write(JSON.stringify(document));
137 }
138
139 async function savePersistedComposerDrafts(drafts: Record<string, ComposerDraft>): Promise<void> {
140 try {
141 await writePersistedComposerDrafts(drafts);
101 - } catch {
142 + } catch (error) {
143 + console.warn("[composer-drafts] failed to persist drafts", error);
144 // Draft persistence is best-effort; in-memory drafts still keep working.
145 }
146 }
@@ -128,7 +170,16 @@ export function ensureComposerDraftsLoaded(): void {
170 ...current,
171 });
172 })
131 - .catch(() => {
173 + .catch((cause) => {
174 + console.warn(
175 + "[composer-drafts] failed to hydrate drafts",
176 + new ComposerDraftPersistenceError({
177 + operation: "hydrate",
178 + directory: COMPOSER_DRAFTS_DIRECTORY,
179 + fileName: COMPOSER_DRAFTS_FILE,
180 + cause,
181 + }),
182 + );
183 // Draft loading is best-effort; in-memory drafts still keep working.
184 });
185 }