diff --git a/apps/meteor/app/ui-utils/client/lib/LegacyRoomManager.ts b/apps/meteor/app/ui-utils/client/lib/LegacyRoomManager.ts index 23221a49a293..7be66e594395 100644 --- a/apps/meteor/app/ui-utils/client/lib/LegacyRoomManager.ts +++ b/apps/meteor/app/ui-utils/client/lib/LegacyRoomManager.ts @@ -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'; @@ -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) { @@ -152,7 +116,6 @@ const computation = Tracker.autorun(() => { } } - handleTrackSettingsChange({ ...msg }); await callbacks.run('streamMessage', { ...msg, name: room.name || '' }); diff --git a/apps/meteor/client/views/room/providers/RoomProvider.tsx b/apps/meteor/client/views/room/providers/RoomProvider.tsx index 47d65562460a..5c539828a914 100644 --- a/apps/meteor/client/views/room/providers/RoomProvider.tsx +++ b/apps/meteor/client/views/room/providers/RoomProvider.tsx @@ -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 = { @@ -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; diff --git a/apps/meteor/client/views/room/providers/hooks/useRedirectOnSettingsChanged.ts b/apps/meteor/client/views/room/providers/hooks/useRedirectOnSettingsChanged.ts new file mode 100644 index 000000000000..dbdd80e93a26 --- /dev/null +++ b/apps/meteor/client/views/room/providers/hooks/useRedirectOnSettingsChanged.ts @@ -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]); +};