32 lines
918 B
TypeScript
32 lines
918 B
TypeScript
|
|
import { atom, WritableAtom } from 'jotai';
|
||
|
|
import { MatrixClient } from 'matrix-js-sdk';
|
||
|
|
import { useMemo } from 'react';
|
||
|
|
import { Membership } from '../../types/matrix/room';
|
||
|
|
import { RoomsAction, useBindRoomsWithMembershipsAtom } from './utils';
|
||
|
|
|
||
|
|
const baseRoomsAtom = atom<string[]>([]);
|
||
|
|
export const allRoomsAtom = atom<string[], RoomsAction>(
|
||
|
|
(get) => get(baseRoomsAtom),
|
||
|
|
(get, set, action) => {
|
||
|
|
if (action.type === 'INITIALIZE') {
|
||
|
|
set(baseRoomsAtom, action.rooms);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
set(baseRoomsAtom, (ids) => {
|
||
|
|
const newIds = ids.filter((id) => id !== action.roomId);
|
||
|
|
if (action.type === 'PUT') newIds.push(action.roomId);
|
||
|
|
return newIds;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
);
|
||
|
|
export const useBindAllRoomsAtom = (
|
||
|
|
mx: MatrixClient,
|
||
|
|
allRooms: WritableAtom<string[], RoomsAction>
|
||
|
|
) => {
|
||
|
|
useBindRoomsWithMembershipsAtom(
|
||
|
|
mx,
|
||
|
|
allRooms,
|
||
|
|
useMemo(() => [Membership.Join], [])
|
||
|
|
);
|
||
|
|
};
|