Open History to see the last commit
1import { Atom } from "effect/reactivity";
2
3import { appAtomRegistry } from "./atom-registry";
4
5interface ThreadComposerError {
6 readonly message: string;
7 readonly messageId: string | null;
8}
9
10/**
11 * The outbox drain can reject a message after the user has left the thread, so
12 * the reason is kept per thread until they dismiss it, send again, or the message
13 * it describes is delivered after all.
14 */
15export const threadComposerErrorsAtom = Atom.make<Readonly<Record<string, ThreadComposerError>>>(
16 {},
17).pipe(Atom.keepAlive, Atom.withLabel("mobile:thread-composer-errors"));
18
19export function setThreadComposerError(
20 threadKey: string,
21 message: string,
22 messageId: string | null = null,
23): void {
24 appAtomRegistry.set(threadComposerErrorsAtom, {
25 ...appAtomRegistry.get(threadComposerErrorsAtom),
26 [threadKey]: { message, messageId },
27 });
28}
29
30export function clearThreadComposerError(threadKey: string, messageId?: string): void {
31 const current = appAtomRegistry.get(threadComposerErrorsAtom);
32 const error = current[threadKey];
33 if (!error || (messageId !== undefined && error.messageId !== messageId)) {
34 return;
35 }
36 const next = { ...current };
37 delete next[threadKey];
38 appAtomRegistry.set(threadComposerErrorsAtom, next);
39}
40
41export function clearThreadComposerErrorsForEnvironment(environmentId: string): void {
42 const current = appAtomRegistry.get(threadComposerErrorsAtom);
43 const prefix = `${environmentId}:`;
44 const keys = Object.keys(current).filter((key) => key.startsWith(prefix));
45 if (keys.length === 0) {
46 return;
47 }
48 const next = { ...current };
49 for (const key of keys) {
50 delete next[key];
51 }
52 appAtomRegistry.set(threadComposerErrorsAtom, next);
53}