import React, { useCallback, useMemo } from 'react'; import { Pressable, StyleSheet, Text, View } from 'react-native'; import Animated, { LinearTransition } from 'react-native-reanimated'; import { MessageComposerState } from 'stream-chat'; import { ChannelContextValue, useChannelContext, } from '../../contexts/channelContext/ChannelContext'; import { useMessageComposer } from '../../contexts/messageInputContext/hooks/useMessageComposer'; import { useTheme } from '../../contexts/themeContext/ThemeContext'; import { ThreadContextValue, useThreadContext } from '../../contexts/threadContext/ThreadContext'; import { TranslationContextValue, useTranslationContext, } from '../../contexts/translationContext/TranslationContext'; import { useStateStore } from '../../hooks/useStateStore'; import { Check } from '../../icons'; import { primitives } from '../../theme'; const stateSelector = (state: MessageComposerState) => ({ showReplyInChannel: state.showReplyInChannel, }); export type ShowThreadMessageInChannelButtonWithContextProps = Pick< ThreadContextValue, 'allowThreadMessagesInChannel' > & Pick & { threadList?: ChannelContextValue['threadList'] }; export const ShowThreadMessageInChannelButtonWithContext = ( props: ShowThreadMessageInChannelButtonWithContextProps, ) => { const styles = useStyles(); const { allowThreadMessagesInChannel, t, threadList } = props; const messageComposer = useMessageComposer(); const { showReplyInChannel } = useStateStore(messageComposer.state, stateSelector); const { theme: { messageComposer: { showThreadMessageInChannelButton: { check, checkBoxActive, checkBoxInactive, container, innerContainer, text, }, }, semantics, }, } = useTheme(); const onPressHandler = useCallback(() => { messageComposer.toggleShowReplyInChannel(); }, [messageComposer]); if (!threadList || !allowThreadMessagesInChannel) { return null; } return ( ({ opacity: pressed ? 0.8 : 1 })}> {showReplyInChannel && ( )} {t('Also send to channel')} ); }; const areEqual = ( prevProps: ShowThreadMessageInChannelButtonWithContextProps, nextProps: ShowThreadMessageInChannelButtonWithContextProps, ) => { const { allowThreadMessagesInChannel: prevAllowThreadMessagesInChannel, t: prevT, threadList: prevThreadList, } = prevProps; const { allowThreadMessagesInChannel: nextAllowThreadMessagesInChannel, t: nextT, threadList: nextThreadList, } = nextProps; const tEqual = prevT === nextT; if (!tEqual) { return false; } const threadListEqual = prevThreadList === nextThreadList; if (!threadListEqual) { return false; } const allowThreadMessagesInChannelEqual = prevAllowThreadMessagesInChannel === nextAllowThreadMessagesInChannel; if (!allowThreadMessagesInChannelEqual) { return false; } return true; }; const MemoizedShowThreadMessageInChannelButton = React.memo( ShowThreadMessageInChannelButtonWithContext, areEqual, ) as typeof ShowThreadMessageInChannelButtonWithContext; export type ShowThreadMessageInChannelButtonProps = Partial; export const ShowThreadMessageInChannelButton = (props: ShowThreadMessageInChannelButtonProps) => { const { t } = useTranslationContext(); const { allowThreadMessagesInChannel } = useThreadContext(); const { threadList } = useChannelContext(); return ( ); }; ShowThreadMessageInChannelButton.displayName = 'ShowThreadMessageInChannelButton{messageComposer{showThreadMessageInChannelButton}}'; const useStyles = () => { const { theme: { semantics }, } = useTheme(); return useMemo( () => StyleSheet.create({ checkBox: { alignItems: 'center', borderRadius: primitives.radiusSm, borderColor: semantics.controlRadioCheckBorder, borderWidth: 1, height: 20, justifyContent: 'center', width: 20, }, container: { flexDirection: 'row', paddingLeft: primitives.spacingSm, paddingBottom: primitives.spacingSm, }, innerContainer: { flexDirection: 'row', alignItems: 'center', gap: primitives.spacingXs, }, text: { fontSize: primitives.typographyFontSizeXs, lineHeight: primitives.typographyLineHeightTight, color: semantics.textTertiary, }, }), [semantics], ); };