| ... | ... | @@ -1,5 +1,6 @@ |
| 1 | 1 | import { useAtomValue } from "@effect/atom-react"; |
| 2 | 2 | import type { EnvironmentId } from "@t3tools/contracts"; |
| 3 | import * as Schema from "effect/Schema"; |
| 3 | 4 | import { useEffect } from "react"; |
| 4 | 5 | import { Atom } from "effect/unstable/reactivity"; |
| 5 | 6 | |
| ... | ... | @@ -11,6 +12,20 @@ const COMPOSER_DRAFTS_DIRECTORY = "composer-drafts"; |
| 11 | 12 | const COMPOSER_DRAFTS_FILE = "drafts.json"; |
| 12 | 13 | const PERSIST_DEBOUNCE_MS = 200; |
| 13 | 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 | |
| 14 | 29 | export interface ComposerDraft { |
| 15 | 30 | readonly text: string; |
| 16 | 31 | readonly attachments: ReadonlyArray<DraftComposerImageAttachment>; |
| ... | ... | @@ -56,12 +71,16 @@ async function getComposerDraftsFile() { |
| 56 | 71 | } |
| 57 | 72 | |
| 58 | 73 | async function loadPersistedComposerDrafts(): Promise<Record<string, ComposerDraft>> { |
| 74 | let operation: ComposerDraftPersistenceError["operation"] = "open"; |
| 59 | 75 | try { |
| 60 | 76 | const file = await getComposerDraftsFile(); |
| 61 | 77 | if (!file.exists) { |
| 62 | 78 | return {}; |
| 63 | 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>; |
| 65 | 84 | if (parsed.schemaVersion !== COMPOSER_DRAFTS_SCHEMA_VERSION || !parsed.drafts) { |
| 66 | 85 | return {}; |
| 67 | 86 | } |
| ... | ... | @@ -75,30 +94,53 @@ async function loadPersistedComposerDrafts(): Promise<Record<string, ComposerDra |
| 75 | 94 | ); |
| 76 | 95 | }), |
| 77 | 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 | ); |
| 79 | 107 | return {}; |
| 80 | 108 | } |
| 81 | 109 | } |
| 82 | 110 | |
| 83 | 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 | }); |
| 94 | 136 | } |
| 95 | | file.write(JSON.stringify(document)); |
| 96 | 137 | } |
| 97 | 138 | |
| 98 | 139 | async function savePersistedComposerDrafts(drafts: Record<string, ComposerDraft>): Promise<void> { |
| 99 | 140 | try { |
| 100 | 141 | await writePersistedComposerDrafts(drafts); |
| 101 | | } catch { |
| 142 | } catch (error) { |
| 143 | console.warn("[composer-drafts] failed to persist drafts", error); |
| 102 | 144 | // Draft persistence is best-effort; in-memory drafts still keep working. |
| 103 | 145 | } |
| 104 | 146 | } |
| ... | ... | @@ -128,7 +170,16 @@ export function ensureComposerDraftsLoaded(): void { |
| 128 | 170 | ...current, |
| 129 | 171 | }); |
| 130 | 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 | ); |
| 132 | 183 | // Draft loading is best-effort; in-memory drafts still keep working. |
| 133 | 184 | }); |
| 134 | 185 | } |