import React from 'react'; import { StyleProp, StyleSheet, Text, TextStyle, TouchableOpacity, View, ViewStyle, } from 'react-native'; import { MessageContextValue, useMessageContext, } from '../../contexts/messageContext/MessageContext'; import { useTheme } from '../../contexts/themeContext/ThemeContext'; import type { Attachment } from 'stream-chat'; import type { DefaultAttachmentType, DefaultChannelType, DefaultCommandType, DefaultEventType, DefaultMessageType, DefaultReactionType, DefaultUserType, UnknownType, } from '../../types/types'; const styles = StyleSheet.create({ actionButton: { borderRadius: 20, borderWidth: 1, paddingHorizontal: 10, paddingVertical: 5, }, container: { flexDirection: 'row', justifyContent: 'space-between', padding: 5, }, }); export type AttachmentActionsPropsWithContext< 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, 'actions'> & Pick, 'handleAction'> & { styles?: Partial<{ actionButton: StyleProp; buttonText: StyleProp; container: StyleProp; }>; }; const AttachmentActionsWithContext = < 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: AttachmentActionsPropsWithContext, ) => { const { actions, handleAction, styles: stylesProp = {} } = props; const { theme: { colors: { accent_blue, black, border, transparent, white }, messageSimple: { actions: { button: { defaultBackgroundColor, defaultBorderColor, primaryBackgroundColor, primaryBorderColor, ...buttonStyle }, buttonText: { defaultColor, primaryColor, ...buttonTextStyle }, container, }, }, }, } = useTheme(); return ( {actions?.map((action, index) => { const primary = action.style === 'primary'; return ( { if (action.name && action.value && handleAction) { handleAction(action.name, action.value); } }} style={[ styles.actionButton, { backgroundColor: primary ? primaryBackgroundColor || accent_blue : defaultBackgroundColor || white, borderColor: primary ? primaryBorderColor || border : defaultBorderColor || transparent, }, buttonStyle, stylesProp.actionButton, ]} testID={`attachment-actions-button-${action.name}`} > {action.text} ); })} ); }; 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: AttachmentActionsPropsWithContext, nextProps: AttachmentActionsPropsWithContext, ) => { const { actions: prevActions } = prevProps; const { actions: nextActions } = nextProps; const actionsEqual = prevActions?.length === nextActions?.length; return actionsEqual; }; const MemoizedAttachmentActions = React.memo( AttachmentActionsWithContext, areEqual, ) as typeof AttachmentActionsWithContext; export type AttachmentActionsProps< 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, > = Attachment & Partial, 'handleAction'>>; /** * AttachmentActions - The actions you can take on an attachment. * Actions in combination with attachments can be used to build [commands](https://getstream.io/chat/docs/#channel_commands). */ export const AttachmentActions = < 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: AttachmentActionsProps, ) => { const { handleAction } = useMessageContext(); return ; }; AttachmentActions.displayName = 'AttachmentActions{messageSimple{actions}}';