2023-06-12 21:15:23 +10:00
|
|
|
import { atom, WritableAtom } from 'jotai';
|
|
|
|
|
import { MatrixClient } from 'matrix-js-sdk';
|
|
|
|
|
import { useMemo } from 'react';
|
2024-05-31 19:49:46 +05:30
|
|
|
import { Membership } from '../../../types/matrix/room';
|
2023-06-12 21:15:23 +10:00
|
|
|
import { RoomsAction, useBindRoomsWithMembershipsAtom } from './utils';
|
|
|
|
|
|
|
|
|
|
const baseRoomsAtom = atom<string[]>([]);
|
2024-01-21 23:50:56 +11:00
|
|
|
export const allInvitesAtom = 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;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export const useBindAllInvitesAtom = (
|
|
|
|
|
mx: MatrixClient,
|
2024-01-21 23:50:56 +11:00
|
|
|
allRooms: WritableAtom<string[], [RoomsAction], undefined>
|
2023-06-12 21:15:23 +10:00
|
|
|
) => {
|
|
|
|
|
useBindRoomsWithMembershipsAtom(
|
|
|
|
|
mx,
|
|
|
|
|
allRooms,
|
|
|
|
|
useMemo(() => [Membership.Invite], [])
|
|
|
|
|
);
|
|
|
|
|
};
|