2026-07-01 22:39:10 -04:00
|
|
|
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';
|
2026-07-07 23:14:35 -04:00
|
|
|
import { getAccountData } from '../utils/accountData';
|
2026-07-01 22:39:10 -04:00
|
|
|
|
|
|
|
|
// 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 =>
|
2026-07-07 23:14:35 -04:00
|
|
|
getAccountData<ThreadNotificationsContent>(mx, AccountDataEvent.LotusThreadNotifications) ?? {};
|
2026-07-01 22:39:10 -04:00
|
|
|
|
|
|
|
|
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]);
|
|
|
|
|
};
|