import React from 'react'; import { StyleProp, StyleSheet, View, ViewStyle } from 'react-native'; import { Attachment as AttachmentDefault } from './Attachment'; import { MessageContextValue, useMessageContext, } from '../../contexts/messageContext/MessageContext'; import { MessagesContextValue, useMessagesContext, } from '../../contexts/messagesContext/MessagesContext'; import { useTheme } from '../../contexts/themeContext/ThemeContext'; import type { DefaultAttachmentType, DefaultChannelType, DefaultCommandType, DefaultEventType, DefaultMessageType, DefaultReactionType, DefaultUserType, UnknownType, } from '../../types/types'; const styles = StyleSheet.create({ container: { padding: 4, }, }); export type FileAttachmentGroupPropsWithContext< 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, 'files'> & Pick, 'Attachment'> & { /** * The unique id for the message with file attachments */ messageId: string; styles?: Partial<{ attachmentContainer: StyleProp; container: StyleProp; }>; }; const FileAttachmentGroupWithContext = < 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: FileAttachmentGroupPropsWithContext, ) => { const { Attachment, files, messageId, styles: stylesProp = {} } = props; const { theme: { messageSimple: { fileAttachmentGroup: { container }, }, }, } = useTheme(); return ( {files.map((file, index) => ( ))} ); }; 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: FileAttachmentGroupPropsWithContext, nextProps: FileAttachmentGroupPropsWithContext, ) => { const { files: prevFiles } = prevProps; const { files: nextFiles } = nextProps; const filesEqual = prevFiles.length === nextFiles.length; return filesEqual; }; const MemoizedFileAttachmentGroup = React.memo( FileAttachmentGroupWithContext, areEqual, ) as typeof FileAttachmentGroupWithContext; export type FileAttachmentGroupProps< 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, > = Partial, 'messageId'>> & Pick, 'messageId'>; export const FileAttachmentGroup = < 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: FileAttachmentGroupProps, ) => { const { files: propFiles, messageId } = props; const { files: contextFiles } = useMessageContext(); const { Attachment = AttachmentDefault } = useMessagesContext(); const files = propFiles || contextFiles; if (!files.length) return null; return ( ); }; FileAttachmentGroup.displayName = 'FileAttachmentGroup{messageSimple{fileAttachmentGroup}}';