Skip to content

Commit

Permalink
fix: memoize integration provided fn
Browse files Browse the repository at this point in the history
  • Loading branch information
isekovanic committed Dec 5, 2024
1 parent 7eb09c9 commit baa322b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
6 changes: 5 additions & 1 deletion package/src/components/Message/Message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,10 @@ const MessageWithContext = <
updateMessage,
} = props;
const isMessageAIGenerated = messagesContext.isMessageAIGenerated;
const isAIGenerated = useMemo(
() => isMessageAIGenerated(message),
[message, isMessageAIGenerated],
);
const { client } = chatContext;
const {
theme: {
Expand Down Expand Up @@ -466,7 +470,7 @@ const MessageWithContext = <
case 'poll':
return !!message.poll_id;
case 'ai_text':
return isMessageAIGenerated(message);
return isAIGenerated;
case 'text':
default:
return !!message.text;
Expand Down
11 changes: 8 additions & 3 deletions package/src/components/Message/MessageSimple/MessageContent.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useMemo } from 'react';
import {
AnimatableNumericValue,
LayoutChangeEvent,
Expand Down Expand Up @@ -188,6 +188,11 @@ const MessageContentWithContext = <
setMessageContentWidth(width);
};

const isAIGenerated = useMemo(
() => isMessageAIGenerated(message),
[message, isMessageAIGenerated],
);

const error = message.type === 'error' || message.status === MessageStatusTypes.FAILED;

const groupStyle = `${alignment}_${groupStyles?.[0]?.toLowerCase?.()}`;
Expand Down Expand Up @@ -398,15 +403,15 @@ const MessageContentWithContext = <
) : null;
}
case 'ai_text':
return isMessageAIGenerated(message) ? (
return isAIGenerated ? (
<StreamingMessageView
key={`ai_message_text_container_${messageContentOrderIndex}`}
/>
) : null;
case 'text':
default:
return (otherAttachments.length && otherAttachments[0].actions) ||
isMessageAIGenerated(message) ? null : (
isAIGenerated ? null : (
<MessageTextContainer<StreamChatGenerics>
key={`message_text_container_${messageContentOrderIndex}`}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useMemo } from 'react';
import { StyleSheet, Text, View } from 'react-native';

import type { Attachment } from 'stream-chat';
Expand Down Expand Up @@ -116,6 +116,11 @@ const MessageFooterWithContext = <
} = useTheme();
const { t } = useTranslationContext();

const isAIGenerated = useMemo(
() => isMessageAIGenerated(message),
[message, isMessageAIGenerated],
);

if (isDeleted) {
return (
<View style={[styles.container, metaContainer]} testID='message-deleted'>
Expand All @@ -131,7 +136,7 @@ const MessageFooterWithContext = <
return null;
}

const isEdited = isEditedMessage(message) && !isMessageAIGenerated(message);
const isEdited = isEditedMessage(message) && !isAIGenerated;

return (
<>
Expand Down

0 comments on commit baa322b

Please sign in to comment.