diff --git a/native/chat/inner-text-message.react.js b/native/chat/inner-text-message.react.js index e1a83caf5..22e7bf4ab 100644 --- a/native/chat/inner-text-message.react.js +++ b/native/chat/inner-text-message.react.js @@ -1,153 +1,146 @@ // @flow -import invariant from 'invariant'; import * as React from 'react'; import { View, StyleSheet, TouchableWithoutFeedback } from 'react-native'; import Animated from 'react-native-reanimated'; import { colorIsDark } from 'lib/shared/thread-utils'; import GestureTouchableOpacity from '../components/gesture-touchable-opacity.react'; import Markdown from '../markdown/markdown.react'; import { useSelector } from '../redux/redux-utils'; import { useColors, colors } from '../themes/colors'; import type { ChatTextMessageInfoItemWithHeight } from '../types/chat-types'; import { useComposedMessageMaxWidth } from './composed-message-width'; -import { MessageListContext } from './message-list-types'; +import { useTextMessageMarkdownRules } from './message-list-types'; import { allCorners, filterCorners, getRoundedContainerStyle, } from './rounded-corners'; /* eslint-disable import/no-named-as-default-member */ const { Node } = Animated; /* eslint-enable import/no-named-as-default-member */ -function useTextMessageMarkdownRules(useDarkStyle: boolean) { - const messageListContext = React.useContext(MessageListContext); - invariant(messageListContext, 'DummyTextNode should have MessageListContext'); - return messageListContext.getTextMessageMarkdownRules(useDarkStyle); -} - function dummyNodeForTextMessageHeightMeasurement( text: string, ): React.Element { return {text}; } type DummyTextNodeProps = { ...React.ElementConfig, +children: string, }; function DummyTextNode(props: DummyTextNodeProps): React.Node { const { children, style, ...rest } = props; const maxWidth = useComposedMessageMaxWidth(); const viewStyle = [props.style, styles.dummyMessage, { maxWidth }]; const rules = useTextMessageMarkdownRules(false); return ( {children} ); } type Props = { +item: ChatTextMessageInfoItemWithHeight, +onPress: () => void, +messageRef?: (message: ?React.ElementRef) => void, +threadColorOverride?: ?Node, +isThreadColorDarkOverride?: ?boolean, }; function InnerTextMessage(props: Props): React.Node { const { item } = props; const { text, creator } = item.messageInfo; const { isViewer } = creator; const activeTheme = useSelector(state => state.globalThemeInfo.activeTheme); const boundColors = useColors(); const messageStyle = {}; let darkColor; if (isViewer) { const threadColor = item.threadInfo.color; messageStyle.backgroundColor = props.threadColorOverride ?? `#${threadColor}`; darkColor = props.isThreadColorDarkOverride ?? colorIsDark(threadColor); } else { messageStyle.backgroundColor = boundColors.listChatBubble; darkColor = activeTheme === 'dark'; } const cornerStyle = getRoundedContainerStyle(filterCorners(allCorners, item)); if (!__DEV__) { // We don't force view height in dev mode because we // want to measure it in Message to see if it's correct messageStyle.height = item.contentHeight; } const rules = useTextMessageMarkdownRules(darkColor); const markdownStyles = React.useMemo(() => { const textStyle = { color: darkColor ? colors.dark.listForegroundLabel : colors.light.listForegroundLabel, }; return [styles.text, textStyle]; }, [darkColor]); const message = ( {text} ); // We need to set onLayout in order to allow .measure() to be on the ref const onLayout = React.useCallback(() => {}, []); const { messageRef } = props; if (!messageRef) { return message; } return ( {message} ); } const styles = StyleSheet.create({ dummyMessage: { paddingHorizontal: 12, paddingVertical: 6, }, message: { overflow: 'hidden', paddingHorizontal: 12, paddingVertical: 6, }, text: { fontFamily: 'Arial', fontSize: 18, }, }); export { InnerTextMessage, dummyNodeForTextMessageHeightMeasurement }; diff --git a/native/chat/message-list-types.js b/native/chat/message-list-types.js index 9bcf45bcc..408260684 100644 --- a/native/chat/message-list-types.js +++ b/native/chat/message-list-types.js @@ -1,78 +1,85 @@ // @flow import { useNavigation } from '@react-navigation/native'; +import invariant from 'invariant'; import * as React from 'react'; import type { ThreadInfo } from 'lib/types/thread-types'; import { type UserInfo } from 'lib/types/user-types'; import type { MarkdownRules } from '../markdown/rules.react'; import { useTextMessageRulesFunc } from '../markdown/rules.react'; import { MessageListRouteName } from '../navigation/route-names'; export type MessageListParams = { +threadInfo: ThreadInfo, +pendingPersonalThreadUserInfo?: UserInfo, +searching?: boolean, }; export type MessageListContextType = { +getTextMessageMarkdownRules: (useDarkStyle: boolean) => MarkdownRules, }; const MessageListContext: React.Context = React.createContext(); function useMessageListContext(threadID: ?string) { const getTextMessageMarkdownRules = useTextMessageRulesFunc(threadID); return React.useMemo( () => ({ getTextMessageMarkdownRules, }), [getTextMessageMarkdownRules], ); } type Props = { +children: React.Node, +threadID: ?string, }; function MessageListContextProvider(props: Props): React.Node { const context = useMessageListContext(props.threadID); return ( {props.children} ); } type NavigateToThreadAction = { +name: typeof MessageListRouteName, +params: MessageListParams, +key: string, }; function createNavigateToThreadAction( params: MessageListParams, ): NavigateToThreadAction { return { name: MessageListRouteName, params, key: `${MessageListRouteName}${params.threadInfo.id}`, }; } function useNavigateToThread(): (params: MessageListParams) => void { const { navigate } = useNavigation(); return React.useCallback( (params: MessageListParams) => { navigate<'MessageList'>(createNavigateToThreadAction(params)); }, [navigate], ); } +function useTextMessageMarkdownRules(useDarkStyle: boolean): MarkdownRules { + const messageListContext = React.useContext(MessageListContext); + invariant(messageListContext, 'DummyTextNode should have MessageListContext'); + return messageListContext.getTextMessageMarkdownRules(useDarkStyle); +} + export { - MessageListContext, MessageListContextProvider, createNavigateToThreadAction, useNavigateToThread, + useTextMessageMarkdownRules, };