|
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 | | - |
6 | 1 | import { useRoomRejectInvitationModal } from './useRoomRejectInvitationModal'; |
7 | 2 | import { useEndpointMutation } from '../../../hooks/useEndpointMutation'; |
8 | | -import { roomsQueryKeys, subscriptionsQueryKeys } from '../../../lib/queryKeys'; |
9 | 3 | import type { IRoomWithFederationOriginalName } from '../contexts/RoomContext'; |
10 | 4 |
|
11 | 5 | 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'); |
20 | 6 | const { open: openConfirmationModal } = useRoomRejectInvitationModal(room); |
21 | 7 | const replyInvite = useEndpointMutation('POST', '/v1/rooms.invite'); |
22 | 8 |
|
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 | | - |
59 | 9 | return { |
60 | 10 | ...replyInvite, |
61 | 11 | acceptInvite: async () => replyInvite.mutate({ roomId: room._id, action: 'accept' }), |
|
0 commit comments