The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
Rocket.Chat/apps/meteor/client/views/room/Header/ParentTeam.tsx

69 lines
2.0 KiB

import type { IRoom } from '@rocket.chat/core-typings';
import { TEAM_TYPE } from '@rocket.chat/core-typings';
import { HeaderTag, HeaderTagIcon, HeaderTagSkeleton } from '@rocket.chat/ui-client';
import { useUserId, useEndpoint } from '@rocket.chat/ui-contexts';
import { useQuery } from '@tanstack/react-query';
import type { ReactElement } from 'react';
import React from 'react';
import { goToRoomById } from '../../../lib/utils/goToRoomById';
type APIErrorResult = { success: boolean; error: string };
const ParentTeam = ({ room }: { room: IRoom }): ReactElement | null => {
const { teamId } = room;
const userId = useUserId();
if (!teamId) {
throw new Error('invalid rid');
}
if (!userId) {
throw new Error('invalid uid');
}
const teamsInfoEndpoint = useEndpoint('GET', '/v1/teams.info');
const userTeamsListEndpoint = useEndpoint('GET', '/v1/users.listTeams');
const {
data: teamInfoData,
isLoading: teamInfoLoading,
isError: teamInfoError,
} = useQuery(['teamId', teamId], async () => teamsInfoEndpoint({ teamId }), {
refetchOnWindowFocus: false,
keepPreviousData: true,
retry: (_, error) => (error as APIErrorResult)?.error === 'unauthorized' && false,
});
const { data: userTeams, isLoading: userTeamsLoading } = useQuery(['userId', userId], async () => userTeamsListEndpoint({ userId }));
const userBelongsToTeam = userTeams?.teams?.find((team) => team._id === teamId) || false;
const isTeamPublic = teamInfoData?.teamInfo.type === TEAM_TYPE.PUBLIC;
const redirectToMainRoom = (): void => {
const rid = teamInfoData?.teamInfo.roomId;
if (!rid) {
return;
}
goToRoomById(rid);
};
if (teamInfoLoading || userTeamsLoading) {
return <HeaderTagSkeleton />;
}
if (teamInfoError) {
return null;
}
return (
<HeaderTag onClick={isTeamPublic || userBelongsToTeam ? redirectToMainRoom : undefined}>
<HeaderTagIcon icon={{ name: isTeamPublic ? 'team' : 'team-lock' }} />
{teamInfoData?.teamInfo.name}
</HeaderTag>
);
};
export default ParentTeam;