Skip to content

Commit

Permalink
Add optional chaining to sender.userId
Browse files Browse the repository at this point in the history
  • Loading branch information
AhyoungRyu committed Oct 6, 2023
1 parent 428f33d commit ffec33a
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/components/CustomChannelComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function CustomChannelComponent(props: CustomChannelComponentProps) {
] as ClientUserMessage;
const isLastBotMessage =
!(lastMessage?.messageType === 'admin') &&
(lastMessage as ClientUserMessage)?.sender.userId === botUser.userId;
(lastMessage as ClientUserMessage)?.sender?.userId === botUser.userId;

const [activeSpinnerId, setActiveSpinnerId] = useState(-1);

Expand Down Expand Up @@ -88,7 +88,7 @@ export function CustomChannelComponent(props: CustomChannelComponentProps) {
allMessages &&
allMessages.length > 1 &&
!(lastMessage?.messageType === 'admin') &&
lastMessage.sender.userId === botUser.userId &&
lastMessage.sender?.userId === botUser.userId &&
// in streaming
lastMessageMeta != null &&
'stream' in lastMessageMeta &&
Expand Down
2 changes: 1 addition & 1 deletion src/components/CustomMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default function CustomMessage(props: Props) {
}

// Sent by current user
if ((message as UserMessage).sender.userId !== botUser.userId) {
if ((message as UserMessage).sender?.userId !== botUser.userId) {
return (
<div>
{<CurrentUserMessage message={message as UserMessage} />}
Expand Down
2 changes: 1 addition & 1 deletion src/components/StaticRepliesPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ const StaticRepliesPanel = (props: Props) => {
useEffect(() => {
if (
lastMessage &&
lastMessage.sender.userId === botUser.userId &&
lastMessage.sender?.userId === botUser.userId &&
isNotLocalMessageCustomType(lastMessage.customType)
) {
setSuggestedReplies(suggestedMessageContent.replyContents);
Expand Down
8 changes: 4 additions & 4 deletions src/utils/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function groupMessagesByShortSpanTime(
prevKey &&
createdAt - Number(prevKey) <= TIME_SPAN &&
messageType !== 'admin' &&
sender?.userId === messages[idx - 1]?.sender.userId
sender?.userId === messages[idx - 1]?.sender?.userId
) {
// Add the message to the existing group
return {
Expand Down Expand Up @@ -60,17 +60,17 @@ export function getBotWelcomeMessages(
) {
// if the list is empty or the first message is not from bot,
// we just assume there's no welcome messages
if (messages.length === 0 || messages[0]?.sender.userId !== botUserId) {
if (messages.length === 0 || messages[0]?.sender?.userId !== botUserId) {
return [];
}

// if the list has only bot messages, then just return the whole list
if (messages.every((message) => message?.sender.userId === botUserId)) {
if (messages.every((message) => message?.sender?.userId === botUserId)) {
return messages;
}

const firstUserMesssage = messages.find(
(message) => message?.sender.userId !== botUserId
(message) => message?.sender?.userId !== botUserId
);
return messages.slice(0, messages.indexOf(firstUserMesssage));
}

0 comments on commit ffec33a

Please sign in to comment.