2024-01-21 23:50:56 +11:00
|
|
|
import { atom } from 'jotai';
|
2023-06-12 21:15:23 +10:00
|
|
|
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[]>([]);
|
2024-01-21 23:50:56 +11:00
|
|
|
export const allRoomsAtom = atom<string[], [RoomsAction], undefined>(
|
2023-06-12 21:15:23 +10:00
|
|
|
(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;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
);
|
2024-01-21 23:50:56 +11:00
|
|
|
export const useBindAllRoomsAtom = (mx: MatrixClient, allRooms: typeof allRoomsAtom) => {
|
2023-06-12 21:15:23 +10:00
|
|
|
useBindRoomsWithMembershipsAtom(
|
|
|
|
|
mx,
|
|
|
|
|
allRooms,
|
|
|
|
|
useMemo(() => [Membership.Join], [])
|
|
|
|
|
);
|
|
|
|
|
};
|