import React from 'react'; import { Text, View } from 'react-native'; import { Alignment, MessageContextValue, useMessageContext, } from '../../../contexts/messageContext/MessageContext'; import { MessagesContextValue, useMessagesContext, } from '../../../contexts/messagesContext/MessagesContext'; import { useTheme } from '../../../contexts/themeContext/ThemeContext'; import { useTranslationContext } from '../../../contexts/translationContext/TranslationContext'; import { Eye } from '../../../icons'; import type { Attachment } from 'stream-chat'; import type { MessageStatusProps } from './MessageStatus'; import type { MessageType } from '../../MessageList/hooks/useMessageList'; import type { ChannelContextValue } from '../../../contexts/channelContext/ChannelContext'; import type { DefaultAttachmentType, DefaultChannelType, DefaultCommandType, DefaultEventType, DefaultMessageType, DefaultReactionType, DefaultUserType, UnknownType, } from '../../../types/types'; type MessageFooterComponentProps = { formattedDate: string | Date; isDeleted?: boolean; }; type MessageFooterPropsWithContext< At extends UnknownType = DefaultAttachmentType, Ch extends UnknownType = DefaultChannelType, Co extends string = DefaultCommandType, Ev extends UnknownType = DefaultEventType, Me extends UnknownType = DefaultMessageType, Re extends UnknownType = DefaultReactionType, Us extends UnknownType = DefaultUserType, > = Pick< MessageContextValue, | 'alignment' | 'members' | 'message' | 'otherAttachments' | 'showMessageStatus' | 'lastGroupMessage' > & Pick, 'MessageStatus'> & MessageFooterComponentProps; const MessageFooterWithContext = < At extends UnknownType = DefaultAttachmentType, Ch extends UnknownType = DefaultChannelType, Co extends string = DefaultCommandType, Ev extends UnknownType = DefaultEventType, Me extends UnknownType = DefaultMessageType, Re extends UnknownType = DefaultReactionType, Us extends UnknownType = DefaultUserType, >( props: MessageFooterPropsWithContext, ) => { const { alignment, formattedDate, isDeleted, lastGroupMessage, members, message, MessageStatus, otherAttachments, showMessageStatus, } = props; const { theme: { colors: { grey }, messageSimple: { content: { deletedMetaText, eyeIcon, messageUser, metaContainer, metaText }, }, }, } = useTheme(); const { t } = useTranslationContext(); if (isDeleted) { return ( {t('Only visible to you')} {formattedDate} ); } if (lastGroupMessage === false) { return null; } return ( {otherAttachments.length && otherAttachments[0].actions ? ( <> {t('Only visible to you')} ) : null} {Object.keys(members).length > 2 && alignment === 'left' && message.user?.name ? ( {message.user.name} ) : null} {showMessageStatus && } {formattedDate} ); }; const areEqual = < At extends UnknownType = DefaultAttachmentType, Ch extends UnknownType = DefaultChannelType, Co extends string = DefaultCommandType, Ev extends UnknownType = DefaultEventType, Me extends UnknownType = DefaultMessageType, Re extends UnknownType = DefaultReactionType, Us extends UnknownType = DefaultUserType, >( prevProps: MessageFooterPropsWithContext, nextProps: MessageFooterPropsWithContext, ) => { const { alignment: prevAlignment, formattedDate: prevFormattedDate, lastGroupMessage: prevLastGroupMessage, members: prevMembers, message: prevMessage, otherAttachments: prevOtherAttachments, showMessageStatus: prevShowMessageStatus, } = prevProps; const { alignment: nextAlignment, formattedDate: nextFormattedDate, lastGroupMessage: nextLastGroupMessage, members: nextMembers, message: nextMessage, otherAttachments: nextOtherAttachments, showMessageStatus: nextShowMessageStatus, } = nextProps; const alignmentEqual = prevAlignment === nextAlignment; if (!alignmentEqual) return false; const membersEqual = Object.keys(prevMembers).length === Object.keys(nextMembers).length; if (!membersEqual) return false; const lastGroupMessageEqual = prevLastGroupMessage === nextLastGroupMessage; if (!lastGroupMessageEqual) return false; const messageEqual = prevMessage.deleted_at === nextMessage.deleted_at && prevMessage.reply_count === nextMessage.reply_count && prevMessage.status === nextMessage.status && prevMessage.type === nextMessage.type && prevMessage.text === nextMessage.text; if (!messageEqual) return false; const otherAttachmentsEqual = prevOtherAttachments.length === nextOtherAttachments.length && prevOtherAttachments?.[0]?.actions?.length === nextOtherAttachments?.[0]?.actions?.length; if (!otherAttachmentsEqual) return false; const showMessageStatusEqual = prevShowMessageStatus === nextShowMessageStatus; if (!showMessageStatusEqual) return false; const formattedDateEqual = prevFormattedDate === nextFormattedDate; if (!formattedDateEqual) return false; return true; }; const MemoizedMessageFooter = React.memo( MessageFooterWithContext, areEqual, ) as typeof MessageFooterWithContext; export type MessageFooterProps< At extends UnknownType = DefaultAttachmentType, Ch extends UnknownType = DefaultChannelType, Co extends string = DefaultCommandType, Ev extends UnknownType = DefaultEventType, Me extends UnknownType = DefaultMessageType, Re extends UnknownType = DefaultReactionType, Us extends DefaultUserType = DefaultUserType, > = Partial, 'members'>> & MessageFooterComponentProps & { alignment?: Alignment; lastGroupMessage?: boolean; message?: MessageType; MessageStatus?: React.ComponentType>; otherAttachments?: Attachment[]; showMessageStatus?: boolean; }; export const MessageFooter = < At extends UnknownType = DefaultAttachmentType, Ch extends UnknownType = DefaultChannelType, Co extends string = DefaultCommandType, Ev extends UnknownType = DefaultEventType, Me extends UnknownType = DefaultMessageType, Re extends UnknownType = DefaultReactionType, Us extends DefaultUserType = DefaultUserType, >( props: MessageFooterProps, ) => { const { alignment, lastGroupMessage, members, message, otherAttachments, showMessageStatus } = useMessageContext(); const { MessageStatus } = useMessagesContext(); return ( ); };