mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-30 22:02:43 -08:00
49 lines
1.3 KiB
TypeScript
49 lines
1.3 KiB
TypeScript
import { atom, useSetAtom } from 'jotai';
|
|
import type { MatrixClient, MatrixEvent } from '$types/matrix-sdk';
|
|
import { ClientEvent, EventType } from '$types/matrix-sdk';
|
|
import { useEffect } from 'react';
|
|
|
|
import { getAccountData } from '$utils/room/hierarchy';
|
|
import { getMDirects } from '$utils/room/unread';
|
|
|
|
type MDirectAction = {
|
|
type: 'INITIALIZE' | 'UPDATE';
|
|
rooms: Set<string>;
|
|
};
|
|
|
|
const baseMDirectAtom = atom(new Set<string>());
|
|
export const mDirectAtom = atom<Set<string>, [MDirectAction], undefined>(
|
|
(get) => get(baseMDirectAtom),
|
|
(get, set, action) => {
|
|
set(baseMDirectAtom, action.rooms);
|
|
}
|
|
);
|
|
|
|
export const useBindMDirectAtom = (mx: MatrixClient, mDirect: typeof mDirectAtom) => {
|
|
const setMDirect = useSetAtom(mDirect);
|
|
|
|
useEffect(() => {
|
|
const mDirectEvent = getAccountData(mx, EventType.Direct);
|
|
if (mDirectEvent) {
|
|
setMDirect({
|
|
type: 'INITIALIZE',
|
|
rooms: getMDirects(mDirectEvent),
|
|
});
|
|
}
|
|
|
|
const handleAccountData = (event: MatrixEvent) => {
|
|
if (event.getType() === (EventType.Direct as string)) {
|
|
setMDirect({
|
|
type: 'UPDATE',
|
|
rooms: getMDirects(event),
|
|
});
|
|
}
|
|
};
|
|
|
|
mx.on(ClientEvent.AccountData, handleAccountData);
|
|
return () => {
|
|
mx.removeListener(ClientEvent.AccountData, handleAccountData);
|
|
};
|
|
}, [mx, setMDirect]);
|
|
};
|