Add centralized typed helpers in src/app/utils/accountData.ts: - getAccountData<T>(mx, eventType): T | undefined (returns content) - setAccountData<T>(mx, eventType, content): Promise<void> These wrap the single `as any` cast needed because matrix-js-sdk's typed overloads reject the fork's custom account-data event names. Every call site now stays fully typed on its content shape. Route all account-data reads/writes that previously used `(mx as any).getAccountData/setAccountData` or `mx.getAccountData(... as any)` through the helpers (or, where a MatrixEvent is needed, through the existing utils/room.ts getAccountData whose param is widened to accept string keys). No behavior change: same event types, same content shapes. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
36 lines
1.3 KiB
TypeScript
36 lines
1.3 KiB
TypeScript
import { atom, useSetAtom } from 'jotai';
|
|
import { ClientEvent, MatrixClient, MatrixEvent } from 'matrix-js-sdk';
|
|
import { useEffect } from 'react';
|
|
import { AccountDataEvent } from '../../types/matrix/accountData';
|
|
import { ThreadNotificationsContent } from '../utils/threadNotifications';
|
|
import { getAccountData } from '../utils/accountData';
|
|
|
|
// Holds the parsed `io.lotus.thread_notifications` account data. Seeded and
|
|
// kept in sync by `useBindThreadNotificationsAtom`.
|
|
export const threadNotificationsAtom = atom<ThreadNotificationsContent>({});
|
|
|
|
const readContent = (mx: MatrixClient): ThreadNotificationsContent =>
|
|
getAccountData<ThreadNotificationsContent>(mx, AccountDataEvent.LotusThreadNotifications) ?? {};
|
|
|
|
export const useBindThreadNotificationsAtom = (
|
|
mx: MatrixClient,
|
|
threadNotifications: typeof threadNotificationsAtom,
|
|
) => {
|
|
const setContent = useSetAtom(threadNotifications);
|
|
|
|
useEffect(() => {
|
|
setContent(readContent(mx));
|
|
|
|
const handleAccountData = (event: MatrixEvent) => {
|
|
if (event.getType() === AccountDataEvent.LotusThreadNotifications) {
|
|
setContent(event.getContent<ThreadNotificationsContent>() ?? {});
|
|
}
|
|
};
|
|
|
|
mx.on(ClientEvent.AccountData, handleAccountData);
|
|
return () => {
|
|
mx.removeListener(ClientEvent.AccountData, handleAccountData);
|
|
};
|
|
}, [mx, setContent]);
|
|
};
|