import React from 'react'; import { Linking, StyleProp, StyleSheet, Text, TextStyle, TouchableOpacity, View, ViewStyle, } from 'react-native'; import { AttachmentActions as AttachmentActionsDefault } from '../../components/Attachment/AttachmentActions'; import { FileIcon as FileIconDefault } from '../../components/Attachment/FileIcon'; import { MessageContextValue, useMessageContext, } from '../../contexts/messageContext/MessageContext'; import { MessagesContextValue, useMessagesContext, } from '../../contexts/messagesContext/MessagesContext'; import { useTheme } from '../../contexts/themeContext/ThemeContext'; import { vw } from '../../utils/utils'; import type { Attachment } from 'stream-chat'; import type { DefaultAttachmentType, DefaultChannelType, DefaultCommandType, DefaultEventType, DefaultMessageType, DefaultReactionType, DefaultUserType, UnknownType, } from '../../types/types'; const styles = StyleSheet.create({ container: { alignItems: 'center', borderRadius: 12, flexDirection: 'row', padding: 8, }, details: { maxWidth: vw(60), paddingLeft: 16, }, size: { fontSize: 12, }, title: { fontSize: 14, fontWeight: '700', }, }); export type FileAttachmentPropsWithContext< At extends DefaultAttachmentType = 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, 'onLongPress' | 'onPress' | 'onPressIn' | 'preventPress' > & Pick< MessagesContextValue, 'additionalTouchableProps' | 'AttachmentActions' | 'FileAttachmentIcon' > & { /** The attachment to render */ attachment: Attachment; attachmentSize?: number; styles?: Partial<{ container: StyleProp; details: StyleProp; size: StyleProp; title: StyleProp; }>; }; const FileAttachmentWithContext = < At extends DefaultAttachmentType = 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: FileAttachmentPropsWithContext, ) => { const { additionalTouchableProps, attachment, attachmentSize, AttachmentActions, FileAttachmentIcon, onLongPress, onPress, onPressIn, preventPress, styles: stylesProp = {}, } = props; const { theme: { colors: { black, grey, white }, messageSimple: { file: { container, details, fileSize, title }, }, }, } = useTheme(); const defaultOnPress = () => goToURL(attachment.asset_url); return ( { if (onLongPress) { onLongPress({ emitter: 'fileAttachment', event, }); } }} onPress={(event) => { if (onPress) { onPress({ defaultHandler: defaultOnPress, emitter: 'fileAttachment', event, }); } }} onPressIn={(event) => { if (onPressIn) { onPressIn({ defaultHandler: defaultOnPress, emitter: 'fileAttachment', event, }); } }} testID='file-attachment' {...additionalTouchableProps} > {attachment.title} {getFileSizeDisplayText(attachment.file_size)} {attachment.actions?.length ? : null} ); }; export type FileAttachmentProps< At extends DefaultAttachmentType = 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, 'attachment'>> & Pick, 'attachment'>; export const FileAttachment = < At extends DefaultAttachmentType = 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: FileAttachmentProps, ) => { const { onLongPress, onPress, onPressIn, preventPress } = useMessageContext(); const { additionalTouchableProps, AttachmentActions = AttachmentActionsDefault, FileAttachmentIcon = FileIconDefault, } = useMessagesContext(); return ( ); }; export const getFileSizeDisplayText = (size?: number | string) => { if (!size) return; if (typeof size === 'string') { size = parseFloat(size); } if (size < 1000 * 1000) { return `${Math.floor(Math.floor(size / 10) / 100)} KB`; } return `${Math.floor(Math.floor(size / 10000) / 100)} MB`; }; export const goToURL = (url?: string) => { if (!url) return; Linking.canOpenURL(url).then((supported) => { if (supported) { Linking.openURL(url); } else { console.log(`Don't know how to open URI: ${url}`); } }); }; FileAttachment.displayName = 'FileAttachment{messageSimple{file}}';