Skip to content

Commit d226724

Browse files
refactor: remove stream in favor of unmount
1 parent 7edfe59 commit d226724

File tree

2 files changed

+17
-51
lines changed

2 files changed

+17
-51
lines changed

apps/meteor/client/views/room/RoomInvite.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { isRoomFederated } from '@rocket.chat/core-typings';
22
import type { IUser, IInviteSubscription } from '@rocket.chat/core-typings';
3-
import type { ComponentProps } from 'react';
3+
import { useEffectEvent } from '@rocket.chat/fuselage-hooks';
4+
import { useQueryClient } from '@tanstack/react-query';
5+
import { useEffect, type ComponentProps } from 'react';
46
import { useTranslation } from 'react-i18next';
57

68
import Header from './Header';
@@ -10,6 +12,7 @@ import type { IRoomWithFederationOriginalName } from './contexts/RoomContext';
1012
import { useRoomInvitation } from './hooks/useRoomInvitation';
1113
import RoomLayout from './layout/RoomLayout';
1214
import { links } from '../../lib/links';
15+
import { roomsQueryKeys, subscriptionsQueryKeys } from '../../lib/queryKeys';
1316

1417
type RoomInviteProps = Omit<ComponentProps<typeof RoomLayout>, 'header' | 'body' | 'aside'> & {
1518
userId?: IUser['_id'];
@@ -19,12 +22,25 @@ type RoomInviteProps = Omit<ComponentProps<typeof RoomLayout>, 'header' | 'body'
1922

2023
const RoomInvite = ({ room, subscription, userId, ...props }: RoomInviteProps) => {
2124
const { t } = useTranslation();
25+
const queryClient = useQueryClient();
2226
const { acceptInvite, rejectInvite, isPending } = useRoomInvitation(room);
2327

2428
const infoLink = isRoomFederated(room) ? { label: t('Learn_more_about_Federation'), href: links.go.matrixFederation } : undefined;
2529

2630
useGoToHomeOnRemoved(room, userId);
2731

32+
const invalidateQueries = useEffectEvent(() => {
33+
const reference = room.federationOriginalName ?? room.name ?? room._id;
34+
void queryClient.invalidateQueries({ queryKey: roomsQueryKeys.room(room._id) });
35+
void queryClient.invalidateQueries({ queryKey: subscriptionsQueryKeys.subscription(room._id) });
36+
void queryClient.invalidateQueries({ queryKey: roomsQueryKeys.roomReference(reference, room.t, userId) });
37+
});
38+
39+
useEffect(() => {
40+
// Invalidate room and subscription queries when unmounting (invite accepted or rejected)
41+
return () => invalidateQueries();
42+
}, [invalidateQueries]);
43+
2844
return (
2945
<RoomLayout
3046
{...props}

apps/meteor/client/views/room/hooks/useRoomInvitation.tsx

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,11 @@
1-
import { useEffectEvent } from '@rocket.chat/fuselage-hooks';
2-
import { useStream, useUser } from '@rocket.chat/ui-contexts';
3-
import { useQueryClient } from '@tanstack/react-query';
4-
import { useEffect } from 'react';
5-
61
import { useRoomRejectInvitationModal } from './useRoomRejectInvitationModal';
72
import { useEndpointMutation } from '../../../hooks/useEndpointMutation';
8-
import { roomsQueryKeys, subscriptionsQueryKeys } from '../../../lib/queryKeys';
93
import type { IRoomWithFederationOriginalName } from '../contexts/RoomContext';
104

115
export const useRoomInvitation = (room: IRoomWithFederationOriginalName) => {
12-
const user = useUser();
13-
14-
if (!user) {
15-
throw new Error('error-user-not-found');
16-
}
17-
18-
const queryClient = useQueryClient();
19-
const subscribeToNotifyUser = useStream('notify-user');
206
const { open: openConfirmationModal } = useRoomRejectInvitationModal(room);
217
const replyInvite = useEndpointMutation('POST', '/v1/rooms.invite');
228

23-
const invalidateQueries = useEffectEvent(() => {
24-
const reference = room.federationOriginalName ?? room.name ?? room._id;
25-
return Promise.all([
26-
queryClient.invalidateQueries({ queryKey: roomsQueryKeys.room(room._id) }),
27-
queryClient.invalidateQueries({ queryKey: subscriptionsQueryKeys.subscription(room._id) }),
28-
queryClient.refetchQueries({
29-
queryKey: roomsQueryKeys.roomReference(reference, room.t, user._id, user.username),
30-
}),
31-
]);
32-
});
33-
34-
useEffect(() => {
35-
// Only listen for subscription changes if the mutation has been initiated
36-
if (!replyInvite.isPending) {
37-
return;
38-
}
39-
40-
/*
41-
* NOTE: We need to listen for subscription changes here because when accepting an invitation
42-
* to a federated room, the server processes the acceptance asynchronously. Therefore,
43-
* we cannot rely solely on the mutation's completion to know when the subscription status
44-
* has changed. By subscribing to the 'notify-user' stream, we can react to changes in the
45-
* subscription status and ensure that our UI reflects the most up-to-date information.
46-
*/
47-
return subscribeToNotifyUser(`${user._id}/subscriptions-changed`, async (event, data) => {
48-
if (data.rid !== room._id) {
49-
return;
50-
}
51-
52-
// Only invalidate when subscription is removed OR invite is accepted (status cleared)
53-
if (event === 'removed' || data.status === undefined) {
54-
await invalidateQueries();
55-
}
56-
});
57-
}, [room._id, user._id, invalidateQueries, replyInvite.isPending, subscribeToNotifyUser]);
58-
599
return {
6010
...replyInvite,
6111
acceptInvite: async () => replyInvite.mutate({ roomId: room._id, action: 'accept' }),

0 commit comments

Comments
 (0)