diff --git a/apps/meteor/client/views/room/providers/RoomProvider.tsx b/apps/meteor/client/views/room/providers/RoomProvider.tsx index 94f37dfafe2ef..e4c380475c894 100644 --- a/apps/meteor/client/views/room/providers/RoomProvider.tsx +++ b/apps/meteor/client/views/room/providers/RoomProvider.tsx @@ -1,5 +1,4 @@ import type { IRoom } from '@rocket.chat/core-typings'; -import { useRouter } from '@rocket.chat/ui-contexts'; import type { ReactNode, ContextType, ReactElement } from 'react'; import { useMemo, memo, useEffect, useCallback } from 'react'; @@ -38,14 +37,6 @@ const RoomProvider = ({ rid, children }: RoomProviderProps): ReactElement => { const resultFromLocal = useRoomQuery(rid); - // TODO: the following effect is a workaround while we don't have a general and definitive solution for it - const router = useRouter(); - useEffect(() => { - if (resultFromLocal.isSuccess && !resultFromLocal.data) { - router.navigate('/home'); - } - }, [resultFromLocal.data, resultFromLocal.isSuccess, resultFromServer, router]); - const subscriptionQuery = useReactiveQuery(subscriptionsQueryKeys.subscription(rid), () => Subscriptions.findOne({ rid }) ?? null); useRedirectOnSettingsChanged(subscriptionQuery.data); diff --git a/apps/meteor/client/views/room/providers/hooks/useRoomQuery.ts b/apps/meteor/client/views/room/providers/hooks/useRoomQuery.ts index 63e60e43e8ac4..65f07f8b85885 100644 --- a/apps/meteor/client/views/room/providers/hooks/useRoomQuery.ts +++ b/apps/meteor/client/views/room/providers/hooks/useRoomQuery.ts @@ -4,17 +4,26 @@ import { useQuery } from '@tanstack/react-query'; import { useEffect } from 'react'; import { Rooms } from '../../../../../app/models/client'; +import { RoomNotFoundError } from '../../../../lib/errors/RoomNotFoundError'; import { queueMicrotask } from '../../../../lib/utils/queueMicrotask'; export function useRoomQuery( rid: IRoom['_id'], - options?: UseQueryOptions, -): UseQueryResult { + options?: UseQueryOptions, +): UseQueryResult { const queryKey = ['rooms', rid] as const; const queryResult = useQuery({ queryKey, - queryFn: async (): Promise => Rooms.findOne({ _id: rid }, { reactive: false }) ?? null, + queryFn: async () => { + const room = Rooms.findOne({ _id: rid }, { reactive: false }); + + if (!room) { + throw new RoomNotFoundError(undefined, { rid }); + } + + return room; + }, staleTime: Infinity, ...options, });