Skip to content

Commit 7329433

Browse files
committed
refactor: load subscription before room opens in embedded mode�
1 parent f21ed3a commit 7329433

2 files changed

Lines changed: 66 additions & 38 deletions

File tree

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

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
1-
import type { RoomType } from '@rocket.chat/core-typings';
1+
import type { ISubscription, RoomType } from '@rocket.chat/core-typings';
22
import { Box, States, StatesIcon, StatesSubtitle, StatesTitle } from '@rocket.chat/fuselage';
33
import { Header } from '@rocket.chat/ui-client';
4-
import { useEndpoint, useStream, useUserId } from '@rocket.chat/ui-contexts';
5-
import { useQuery } from '@tanstack/react-query';
4+
import { useStream, useUserId } from '@rocket.chat/ui-contexts';
65
import type { ReactElement } from 'react';
76
import { lazy, Suspense, useEffect } from 'react';
87
import { useTranslation } from 'react-i18next';
98

109
import NotSubscribedRoom from './NotSubscribedRoom';
1110
import RoomSkeleton from './RoomSkeleton';
1211
import { useOpenRoom } from './hooks/useOpenRoom';
13-
import { LegacyRoomManager } from '../../../app/ui-utils/client';
1412
import { SubscriptionsCachedStore } from '../../cachedStores';
1513
import { getErrorMessage } from '../../lib/errorHandling';
1614
import { NotAuthorizedError } from '../../lib/errors/NotAuthorizedError';
1715
import { NotSubscribedToRoomError } from '../../lib/errors/NotSubscribedToRoomError';
1816
import { OldUrlRoomError } from '../../lib/errors/OldUrlRoomError';
1917
import { RoomNotFoundError } from '../../lib/errors/RoomNotFoundError';
20-
import { subscriptionsQueryKeys } from '../../lib/queryKeys';
21-
import { mapSubscriptionFromApi } from '../../lib/utils/mapSubscriptionFromApi';
2218

2319
const RoomProvider = lazy(() => import('./providers/RoomProvider'));
2420
const RoomNotFound = lazy(() => import('./RoomNotFound'));
@@ -34,47 +30,23 @@ type RoomOpenerProps = {
3430
const RoomOpenerEmbedded = ({ type, reference }: RoomOpenerProps): ReactElement => {
3531
const { data, error, isSuccess, isError, isLoading } = useOpenRoom({ type, reference });
3632
const uid = useUserId();
37-
38-
const getSubscription = useEndpoint('GET', '/v1/subscriptions.getOne');
39-
4033
const subscribeToNotifyUser = useStream('notify-user');
4134

4235
const rid = data?.rid;
43-
const { data: subscriptionData, refetch } = useQuery({
44-
queryKey: rid ? subscriptionsQueryKeys.subscription(rid) : [],
45-
queryFn: () => {
46-
if (!rid) {
47-
throw new Error('Room not found');
48-
}
49-
return getSubscription({ roomId: rid });
50-
},
51-
enabled: !!rid,
52-
});
5336

5437
useEffect(() => {
55-
if (!subscriptionData?.subscription) {
38+
if (!uid || !rid) {
5639
return;
5740
}
5841

59-
SubscriptionsCachedStore.upsertSubscription(mapSubscriptionFromApi(subscriptionData.subscription));
60-
61-
// yes this must be done here, this is already called in useOpenRoom, but it skips subscription streams because of the subscriptions list is empty
62-
// now that we inserted the subscription, we can open the room
63-
LegacyRoomManager.open({ typeName: type + reference, rid: subscriptionData.subscription.rid });
64-
}, [subscriptionData, type, rid, reference]);
65-
66-
useEffect(() => {
67-
if (!uid) {
68-
return;
69-
}
7042
return subscribeToNotifyUser(`${uid}/subscriptions-changed`, (event, sub) => {
7143
if (sub.rid !== rid || event === 'removed') {
7244
return;
7345
}
7446

75-
refetch();
47+
SubscriptionsCachedStore.upsertSubscription(sub as ISubscription);
7648
});
77-
}, [refetch, rid, subscribeToNotifyUser, uid]);
49+
}, [rid, subscribeToNotifyUser, uid]);
7850

7951
const { t } = useTranslation();
8052

apps/meteor/client/views/root/MainLayout/EmbeddedPreload.tsx

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,75 @@
1+
import { useEndpoint, useMethod, useRouter, useUserId } from '@rocket.chat/ui-contexts';
2+
import { useQuery } from '@tanstack/react-query';
13
import type { ReactElement, ReactNode } from 'react';
2-
import { useEffect } from 'react';
4+
import { useEffect, useMemo } from 'react';
35

46
import { RoomsCachedStore, SubscriptionsCachedStore } from '../../../cachedStores';
7+
import { roomsQueryKeys } from '../../../lib/queryKeys';
8+
import { roomCoordinator } from '../../../lib/rooms/roomCoordinator';
9+
import { mapSubscriptionFromApi } from '../../../lib/utils/mapSubscriptionFromApi';
510
import PageLoading from '../PageLoading';
611
import { useMainReady } from '../hooks/useMainReady';
712

813
const EmbeddedPreload = ({ children }: { children: ReactNode }): ReactElement => {
914
const ready = useMainReady();
15+
const router = useRouter();
16+
const uid = useUserId();
17+
18+
const roomParams = useMemo(() => {
19+
const routeName = router.getRouteName();
20+
if (!routeName) {
21+
return null;
22+
}
23+
24+
const identifier = roomCoordinator.getRouteNameIdentifier(routeName);
25+
if (!identifier) {
26+
return null;
27+
}
28+
29+
const directives = roomCoordinator.getRoomDirectives(identifier);
30+
if (!directives?.extractOpenRoomParams) {
31+
return null;
32+
}
33+
34+
return directives.extractOpenRoomParams(router.getRouteParameters());
35+
}, [router]);
36+
37+
const getRoomByTypeAndName = useMethod('getRoomByTypeAndName');
38+
const getSubscription = useEndpoint('GET', '/v1/subscriptions.getOne');
39+
40+
const shouldFetch = !!roomParams && !!uid;
41+
42+
const { isLoading } = useQuery({
43+
queryKey: roomParams ? roomsQueryKeys.roomReference(roomParams.reference, roomParams.type, uid ?? undefined) : [],
44+
queryFn: async () => {
45+
if (!roomParams) {
46+
return null;
47+
}
48+
49+
const roomData = await getRoomByTypeAndName(roomParams.type, roomParams.reference);
50+
if (!roomData?._id) {
51+
return null;
52+
}
53+
54+
const subResult = await getSubscription({ roomId: roomData._id });
55+
if (subResult.subscription) {
56+
SubscriptionsCachedStore.upsertSubscription(mapSubscriptionFromApi(subResult.subscription));
57+
}
58+
59+
return subResult;
60+
},
61+
enabled: shouldFetch,
62+
retry: false,
63+
});
1064

1165
useEffect(() => {
12-
SubscriptionsCachedStore.setReady(true);
13-
RoomsCachedStore.setReady(true);
14-
}, [ready]);
66+
if (!shouldFetch || !isLoading) {
67+
SubscriptionsCachedStore.setReady(true);
68+
RoomsCachedStore.setReady(true);
69+
}
70+
}, [shouldFetch, isLoading]);
1571

16-
if (!ready) {
72+
if (!ready || (shouldFetch && isLoading)) {
1773
return <PageLoading />;
1874
}
1975

0 commit comments

Comments
 (0)