CI / Build & Quality Checks (pull_request) Successful in 2m56s
CI / Trigger Desktop Build (pull_request) Skipped
CI / Docker image build & smoke test (pull_request) Skipped
CI / Secret scan (gitleaks) (pull_request) Successful in 7s
CI / Playwright smoke (e2e) (pull_request) Successful in 15m43s
The tray only knew what this browser had scheduled (localStorage), but Synapse lists every pending one via MSC4140 GET /delayed_events. Load it when the tray mounts and on window focus (localStorage stays as a first- paint cache), keeping only non-state m.room.message events. Logout now says "N scheduled messages still send after you log out" and offers to cancel them. Fixes #322 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017PCb59uY3UKqDqt7B6hzd5
56 lines
1.7 KiB
TypeScript
56 lines
1.7 KiB
TypeScript
import { atom } from 'jotai';
|
|
import { atomWithStorage, createJSONStorage } from 'jotai/utils';
|
|
import { IContent } from 'matrix-js-sdk';
|
|
|
|
export type ScheduledMessage = {
|
|
delayId: string;
|
|
roomId: string;
|
|
content: IContent;
|
|
sendAt: number; // Unix timestamp ms
|
|
};
|
|
|
|
const STORAGE_KEY = 'cinny_scheduled_messages_v1';
|
|
|
|
// Internal atom persists as a plain Record (JSON-serializable).
|
|
const internalAtom = atomWithStorage<Record<string, ScheduledMessage[]>>(
|
|
STORAGE_KEY,
|
|
{},
|
|
createJSONStorage(() => localStorage),
|
|
);
|
|
|
|
/**
|
|
* Global atom: Map<roomId, ScheduledMessage[]>
|
|
* Stores all locally-tracked scheduled messages across rooms.
|
|
* Loaded from the server's MSC4140 `GET /delayed_events` when the tray mounts
|
|
* (Gitea #322); localStorage is a cache for the first paint.
|
|
*/
|
|
export const scheduledMessagesAtom = atom(
|
|
(get): Map<string, ScheduledMessage[]> => new Map(Object.entries(get(internalAtom))),
|
|
(
|
|
_get,
|
|
set,
|
|
updater:
|
|
| Map<string, ScheduledMessage[]>
|
|
| ((prev: Map<string, ScheduledMessage[]>) => Map<string, ScheduledMessage[]>),
|
|
) => {
|
|
set(internalAtom, (prevObj) => {
|
|
const prevMap = new Map(Object.entries(prevObj));
|
|
const nextMap = typeof updater === 'function' ? updater(prevMap) : updater;
|
|
return Object.fromEntries(nextMap.entries());
|
|
});
|
|
},
|
|
);
|
|
|
|
/**
|
|
* Wipe persisted scheduled messages. Called on logout — the stored content is
|
|
* decrypted message plaintext (the E2EE `body`), so it must not survive a
|
|
* session on a shared device.
|
|
*/
|
|
export const clearScheduledMessages = (): void => {
|
|
try {
|
|
localStorage.removeItem(STORAGE_KEY);
|
|
} catch {
|
|
/* localStorage unavailable — nothing to clear */
|
|
}
|
|
};
|