Skip to content

Commit 0c5e53f

Browse files
committed
fix: add null guards for user in AgentInfo and AddAgent
- AgentInfo.tsx: handle user: null by showing 'User_not_found' - DepartmentAgentsTable/AddAgent.tsx: guard against null user - useAgentsList.ts: relax formatAgentItem type for object response
1 parent a4c902e commit 0c5e53f

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

apps/meteor/client/views/omnichannel/agents/AgentInfo.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { ILivechatAgent, Serialized } from '@rocket.chat/core-typings';
12
import { Box, Margins, ButtonGroup } from '@rocket.chat/fuselage';
23
import {
34
ContextualbarTitle,
@@ -39,11 +40,12 @@ const AgentInfo = ({ uid }: AgentInfoProps) => {
3940
return <ContextualbarSkeletonBody />;
4041
}
4142

42-
if (isError) {
43+
if (isError || !data?.user) {
4344
return <Box mbs={16}>{t('User_not_found')}</Box>;
4445
}
4546

46-
const { username, statusLivechat, status: userStatus } = data?.user;
47+
const user = data.user as Serialized<ILivechatAgent>;
48+
const { username, statusLivechat, status: userStatus } = user;
4749

4850
return (
4951
<>
@@ -77,7 +79,7 @@ const AgentInfo = ({ uid }: AgentInfoProps) => {
7779
<InfoPanelText>{statusLivechat === 'available' ? t('Available') : t('Not_Available')}</InfoPanelText>
7880
</>
7981
)}
80-
{MaxChatsPerAgentDisplay && <MaxChatsPerAgentDisplay maxNumberSimultaneousChat={data.user.livechat?.maxNumberSimultaneousChat} />}
82+
{MaxChatsPerAgentDisplay && <MaxChatsPerAgentDisplay maxNumberSimultaneousChat={user.livechat?.maxNumberSimultaneousChat} />}
8183
</Margins>
8284
</ContextualbarScrollableContent>
8385
</>

apps/meteor/client/views/omnichannel/departments/DepartmentAgentsTable/AddAgent.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@ function AddAgent({ agentList, onAdd, 'aria-labelledby': ariaLabelledBy }: AddAg
3030
return;
3131
}
3232

33-
const {
34-
user: { _id, username, name },
35-
} = await getAgent();
33+
const { user } = await getAgent();
34+
if (!user) {
35+
dispatchToastMessage({ type: 'error', message: t('User_not_found') });
36+
return;
37+
}
38+
const { _id, username, name } = user;
3639

3740
if (!agentList.some(({ agentId }) => agentId === _id)) {
3841
setUserId('');

apps/meteor/client/views/omnichannel/hooks/useAgentsList.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { ILivechatAgent, Serialized } from '@rocket.chat/core-typings';
21
import { useEndpoint } from '@rocket.chat/ui-contexts';
32
import { useInfiniteQuery } from '@tanstack/react-query';
43
import { useTranslation } from 'react-i18next';
@@ -34,7 +33,7 @@ export const useAgentsList = (options: AgentsListOptions) => {
3433
limit = DEFAULT_QUERY_LIMIT,
3534
} = options;
3635

37-
const formatAgentItem = (agent: Serialized<ILivechatAgent>) => ({
36+
const formatAgentItem = (agent: { _id: string; name?: string; username?: string }) => ({
3837
_id: agent._id,
3938
label: `${agent.name || agent._id} (@${agent.username})`,
4039
value: agent._id,
@@ -55,7 +54,7 @@ export const useAgentsList = (options: AgentsListOptions) => {
5554

5655
return {
5756
...data,
58-
users: users.map(formatAgentItem),
57+
users: users.map((u: any) => formatAgentItem(u)),
5958
};
6059
},
6160
select: (data) => {

0 commit comments

Comments
 (0)