-
Notifications
You must be signed in to change notification settings - Fork 11k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Do not load unnecessary CachedCollection data for embedded lay…
…out (#34138)
- Loading branch information
1 parent
48b4cfe
commit 224cc68
Showing
9 changed files
with
224 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import type { ISubscription, RoomType } from '@rocket.chat/core-typings'; | ||
import { Box, States, StatesIcon, StatesSubtitle, StatesTitle } from '@rocket.chat/fuselage'; | ||
import { FeaturePreviewOff, FeaturePreviewOn } from '@rocket.chat/ui-client'; | ||
import { useEndpoint } from '@rocket.chat/ui-contexts'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import type { ReactElement } from 'react'; | ||
import React, { lazy, Suspense } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
import RoomSkeleton from './RoomSkeleton'; | ||
import RoomSidepanel from './Sidepanel/RoomSidepanel'; | ||
import { useOpenRoom } from './hooks/useOpenRoom'; | ||
import { CachedChatSubscription } from '../../../app/models/client'; | ||
import { FeaturePreviewSidePanelNavigation } from '../../components/FeaturePreviewSidePanelNavigation'; | ||
import { Header } from '../../components/Header'; | ||
import { getErrorMessage } from '../../lib/errorHandling'; | ||
import { NotAuthorizedError } from '../../lib/errors/NotAuthorizedError'; | ||
import { OldUrlRoomError } from '../../lib/errors/OldUrlRoomError'; | ||
import { RoomNotFoundError } from '../../lib/errors/RoomNotFoundError'; | ||
|
||
const RoomProvider = lazy(() => import('./providers/RoomProvider')); | ||
const RoomNotFound = lazy(() => import('./RoomNotFound')); | ||
const Room = lazy(() => import('./Room')); | ||
const RoomLayout = lazy(() => import('./layout/RoomLayout')); | ||
const NotAuthorizedPage = lazy(() => import('../notAuthorized/NotAuthorizedPage')); | ||
|
||
type RoomOpenerProps = { | ||
type: RoomType; | ||
reference: string; | ||
}; | ||
|
||
const isDirectOrOmnichannelRoom = (type: RoomType) => type === 'd' || type === 'l'; | ||
|
||
const RoomOpenerEmbedded = ({ type, reference }: RoomOpenerProps): ReactElement => { | ||
const { data, error, isSuccess, isError, isLoading } = useOpenRoom({ type, reference }); | ||
|
||
const getSubscription = useEndpoint('GET', '/v1/subscriptions.getOne'); | ||
|
||
const rid = data?.rid; | ||
useQuery( | ||
['subscriptions', rid], | ||
() => { | ||
if (!rid) { | ||
throw new Error('Room not found'); | ||
} | ||
return getSubscription({ roomId: rid }); | ||
}, | ||
{ | ||
enabled: !!rid, | ||
suspense: true, | ||
onSuccess({ subscription }) { | ||
if (!subscription) { | ||
throw new Error('Room not found'); | ||
} | ||
CachedChatSubscription.upsertSubscription(subscription as unknown as ISubscription); | ||
}, | ||
}, | ||
); | ||
|
||
const { t } = useTranslation(); | ||
|
||
return ( | ||
<Box display='flex' w='full' h='full'> | ||
{!isDirectOrOmnichannelRoom(type) && ( | ||
<FeaturePreviewSidePanelNavigation> | ||
<FeaturePreviewOff>{null}</FeaturePreviewOff> | ||
<FeaturePreviewOn> | ||
<RoomSidepanel /> | ||
</FeaturePreviewOn> | ||
</FeaturePreviewSidePanelNavigation> | ||
)} | ||
|
||
<Suspense fallback={<RoomSkeleton />}> | ||
{isLoading && <RoomSkeleton />} | ||
{isSuccess && ( | ||
<RoomProvider rid={data.rid}> | ||
<Room /> | ||
</RoomProvider> | ||
)} | ||
{isError && | ||
(() => { | ||
if (error instanceof OldUrlRoomError) { | ||
return <RoomSkeleton />; | ||
} | ||
|
||
if (error instanceof RoomNotFoundError) { | ||
return <RoomNotFound />; | ||
} | ||
|
||
if (error instanceof NotAuthorizedError) { | ||
return <NotAuthorizedPage />; | ||
} | ||
|
||
return ( | ||
<RoomLayout | ||
header={<Header />} | ||
body={ | ||
<States> | ||
<StatesIcon name='circle-exclamation' variation='danger' /> | ||
<StatesTitle>{t('core.Error')}</StatesTitle> | ||
<StatesSubtitle>{getErrorMessage(error)}</StatesSubtitle> | ||
</States> | ||
} | ||
/> | ||
); | ||
})()} | ||
</Suspense> | ||
</Box> | ||
); | ||
}; | ||
|
||
export default RoomOpenerEmbedded; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
apps/meteor/client/views/root/MainLayout/EmbeddedPreload.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { useUserId } from '@rocket.chat/ui-contexts'; | ||
import type { ReactElement, ReactNode } from 'react'; | ||
import React, { useEffect } from 'react'; | ||
|
||
import { CachedChatRoom, CachedChatSubscription } from '../../../../app/models/client'; | ||
import { settings } from '../../../../app/settings/client'; | ||
import { mainReady } from '../../../../app/ui-utils/client'; | ||
import { useReactiveVar } from '../../../hooks/useReactiveVar'; | ||
import { isSyncReady } from '../../../lib/userData'; | ||
import PageLoading from '../PageLoading'; | ||
|
||
const EmbeddedPreload = ({ children }: { children: ReactNode }): ReactElement => { | ||
const uid = useUserId(); | ||
const subscriptionsReady = useReactiveVar(CachedChatSubscription.ready); | ||
const settingsReady = useReactiveVar(settings.cachedCollection.ready); | ||
const userDataReady = useReactiveVar(isSyncReady); | ||
|
||
const ready = !uid || (userDataReady && subscriptionsReady && settingsReady); | ||
|
||
useEffect(() => { | ||
mainReady.set(ready); | ||
}, [ready]); | ||
|
||
useEffect(() => { | ||
CachedChatSubscription.ready.set(true); | ||
CachedChatRoom.ready.set(true); | ||
}, [ready]); | ||
|
||
if (!ready) { | ||
return <PageLoading />; | ||
} | ||
|
||
return <>{children}</>; | ||
}; | ||
|
||
export default EmbeddedPreload; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters