Skip to content

Commit

Permalink
fix surfaced redirection issue when editing room info
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriellsh committed Jan 2, 2024
1 parent 9da8139 commit 4b57621
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 38 deletions.
39 changes: 1 addition & 38 deletions apps/meteor/app/ui-utils/client/lib/LegacyRoomManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import { RoomManager } from '../../../../client/lib/RoomManager';
import { roomCoordinator } from '../../../../client/lib/rooms/roomCoordinator';
import { fireGlobalEvent } from '../../../../client/lib/utils/fireGlobalEvent';
import { getConfig } from '../../../../client/lib/utils/getConfig';
import { router } from '../../../../client/providers/RouterProvider';
import { callbacks } from '../../../../lib/callbacks';
import { CachedChatRoom, ChatMessage, ChatSubscription, CachedChatSubscription, ChatRoom } from '../../../models/client';
import { CachedChatRoom, ChatMessage, ChatSubscription, CachedChatSubscription } from '../../../models/client';
import { Notifications } from '../../../notifications/client';
import { sdk } from '../../../utils/client/lib/SDKClient';
import { upsertMessage, RoomHistoryManager } from './RoomHistoryManager';
Expand Down Expand Up @@ -80,41 +79,6 @@ function getOpenedRoomByRid(rid: IRoom['_id']) {
.find((openedRoom) => openedRoom.rid === rid);
}

const handleTrackSettingsChange = (msg: IMessage) => {
const openedRoom = RoomManager.opened;
if (openedRoom !== msg.rid) {
return;
}

void Tracker.nonreactive(async () => {
if (msg.t === 'room_changed_privacy') {
const type = router.getRouteName() === 'channel' ? 'c' : 'p';
await close(type + router.getRouteParameters().name);

const subscription = ChatSubscription.findOne({ rid: msg.rid });
if (!subscription) {
throw new Error('Subscription not found');
}
router.navigate({
pattern: subscription.t === 'c' ? '/channel/:name/:tab?/:context?' : '/group/:name/:tab?/:context?',
params: { name: subscription.name },
search: router.getSearchParameters(),
});
}

if (msg.t === 'r') {
const room = ChatRoom.findOne(msg.rid);
if (!room) {
throw new Error('Room not found');
}
if (room.name !== router.getRouteParameters().name) {
await close(room.t + router.getRouteParameters().name);
roomCoordinator.openRouteLink(room.t, room, router.getSearchParameters());
}
}
});
};

const computation = Tracker.autorun(() => {
const ready = CachedChatRoom.ready.get() && mainReady.get();
if (ready !== true) {
Expand Down Expand Up @@ -152,7 +116,6 @@ const computation = Tracker.autorun(() => {
}
}

handleTrackSettingsChange({ ...msg });

await callbacks.run('streamMessage', { ...msg, name: room.name || '' });

Expand Down
3 changes: 3 additions & 0 deletions apps/meteor/client/views/room/providers/RoomProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { useRoomRolesManagement } from '../body/hooks/useRoomRolesManagement';
import { RoomContext } from '../contexts/RoomContext';
import ComposerPopupProvider from './ComposerPopupProvider';
import RoomToolboxProvider from './RoomToolboxProvider';
import { useRedirectOnSettingsChanged } from './hooks/useRedirectOnSettingsChanged';
import { useRoomQuery } from './hooks/useRoomQuery';

type RoomProviderProps = {
Expand All @@ -39,6 +40,8 @@ const RoomProvider = ({ rid, children }: RoomProviderProps): ReactElement => {

const subscriptionQuery = useReactiveQuery(['subscriptions', { rid }], () => ChatSubscription.findOne({ rid }) ?? null);

useRedirectOnSettingsChanged(subscriptionQuery.data);

const pseudoRoom = useMemo(() => {
if (!room) {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { ISubscription } from '@rocket.chat/core-typings';
import { useRouter } from '@rocket.chat/ui-contexts';
import { useEffect } from 'react';

import { LegacyRoomManager } from '../../../../../app/ui-utils/client';

export const useRedirectOnSettingsChanged = (subscription?: ISubscription | null) => {
const router = useRouter();

const routeType = router.getRouteName() === 'channel' ? 'c' : 'p';
const routeName = router.getRouteParameters().name;

const subExists = !!subscription;

useEffect(() => {
if (!subExists) {
return;
}
const redirect = async () => {
if (routeType !== subscription.t || routeName !== subscription.name) {
await LegacyRoomManager.close(routeType + routeName);
router.navigate({
pattern: subscription.t === 'c' ? '/channel/:name/:tab?/:context?' : '/group/:name/:tab?/:context?',
params: { name: subscription.name },
search: router.getSearchParameters(),
});
}
};
redirect();
}, [subscription?.t, subscription?.name, routeType, routeName, router, subExists]);
};

0 comments on commit 4b57621

Please sign in to comment.