Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 2s
93 lines
2.9 KiB
TypeScript
93 lines
2.9 KiB
TypeScript
import { getObjectKeys } from '@rocket.chat/tools';
|
|
import { useEndpoint, useMethod, useRouter, useUserId } from '@rocket.chat/ui-contexts';
|
|
import { useQuery } from '@tanstack/react-query';
|
|
import type { ReactNode } from 'react';
|
|
import { useEffect, useMemo } from 'react';
|
|
|
|
import { roomFields } from '../../../../lib/publishFields';
|
|
import { RoomsCachedStore, SubscriptionsCachedStore } from '../../../cachedStores';
|
|
import { roomsQueryKeys } from '../../../lib/queryKeys';
|
|
import { roomCoordinator } from '../../../lib/rooms/roomCoordinator';
|
|
import { mapSubscriptionFromApi } from '../../../lib/utils/mapSubscriptionFromApi';
|
|
import { Rooms } from '../../../stores';
|
|
import PageLoading from '../PageLoading';
|
|
import { useMainReady } from '../hooks/useMainReady';
|
|
|
|
export type EmbeddedPreloadProps = { children: ReactNode };
|
|
|
|
const EmbeddedPreload = ({ children }: EmbeddedPreloadProps) => {
|
|
const ready = useMainReady();
|
|
const router = useRouter();
|
|
const uid = useUserId();
|
|
|
|
const roomParams = useMemo(() => {
|
|
const routeName = router.getRouteName();
|
|
if (!routeName) {
|
|
return null;
|
|
}
|
|
|
|
const identifier = roomCoordinator.getRouteNameIdentifier(routeName);
|
|
if (!identifier) {
|
|
return null;
|
|
}
|
|
|
|
const directives = roomCoordinator.getRoomDirectives(identifier);
|
|
if (!directives?.extractOpenRoomParams) {
|
|
return null;
|
|
}
|
|
|
|
return directives.extractOpenRoomParams(router.getRouteParameters());
|
|
}, [router]);
|
|
|
|
const getRoomByTypeAndName = useMethod('getRoomByTypeAndName');
|
|
const getSubscription = useEndpoint('GET', '/v1/subscriptions.getOne');
|
|
|
|
const shouldFetch = !!roomParams && !!uid;
|
|
|
|
const { isLoading, isSuccess, isError } = useQuery({
|
|
queryKey: roomParams ? roomsQueryKeys.roomReference(roomParams.reference, roomParams.type, uid ?? undefined) : [],
|
|
queryFn: async () => {
|
|
if (!roomParams) {
|
|
return null;
|
|
}
|
|
|
|
const roomData = await getRoomByTypeAndName(roomParams.type, roomParams.reference);
|
|
if (!roomData?._id) {
|
|
return null;
|
|
}
|
|
|
|
// Populate Rooms store and return same shape as useOpenRoom so the shared
|
|
// React Query cache entry is usable when RoomOpenerEmbedded mounts.
|
|
const unsetKeys = getObjectKeys(roomData).filter((key) => !(key in roomFields));
|
|
unsetKeys.forEach((key) => {
|
|
delete roomData[key];
|
|
});
|
|
Rooms.state.store(roomData);
|
|
|
|
const subResult = await getSubscription({ roomId: roomData._id });
|
|
if (subResult.subscription) {
|
|
SubscriptionsCachedStore.upsertSubscription(mapSubscriptionFromApi(subResult.subscription));
|
|
}
|
|
|
|
return { rid: roomData._id };
|
|
},
|
|
enabled: shouldFetch,
|
|
retry: false,
|
|
});
|
|
|
|
useEffect(() => {
|
|
if (!shouldFetch || isSuccess || isError) {
|
|
SubscriptionsCachedStore.setReady(true);
|
|
RoomsCachedStore.setReady(true);
|
|
}
|
|
}, [shouldFetch, isSuccess, isError]);
|
|
|
|
if (!ready || (shouldFetch && isLoading)) {
|
|
return <PageLoading />;
|
|
}
|
|
|
|
return <>{children}</>;
|
|
};
|
|
|
|
export default EmbeddedPreload;
|