diff --git a/lib/hooks/inline-sidebar-text.react.js b/lib/hooks/inline-sidebar-text.react.js new file mode 100644 --- /dev/null +++ b/lib/hooks/inline-sidebar-text.react.js @@ -0,0 +1,35 @@ +// @flow + +import * as React from 'react'; + +import { relativeMemberInfoSelectorForMembersOfThread } from '../selectors/user-selectors'; +import { stringForUser } from '../shared/user-utils'; +import type { ThreadInfo } from '../types/thread-types'; +import { useSelector } from '../utils/redux-utils'; +import { pluralizeAndTrim } from '../utils/text-utils'; + +function useInlineSidebarText( + threadInfo: ThreadInfo, +): { + sendersText: string, + repliesText: string, +} { + const repliesCount = threadInfo.repliesCount || 1; + const repliesText = `${repliesCount} ${ + repliesCount > 1 ? 'replies' : 'reply' + }`; + + const threadMembers = useSelector( + relativeMemberInfoSelectorForMembersOfThread(threadInfo.id), + ); + const sendersText = React.useMemo(() => { + const senders = threadMembers + .filter(member => member.isSender) + .map(stringForUser); + return senders.length > 0 ? `${pluralizeAndTrim(senders, 25)} sent ` : ''; + }, [threadMembers]); + + return { sendersText, repliesText }; +} + +export default useInlineSidebarText; diff --git a/native/chat/inline-sidebar.react.js b/native/chat/inline-sidebar.react.js --- a/native/chat/inline-sidebar.react.js +++ b/native/chat/inline-sidebar.react.js @@ -4,13 +4,10 @@ import { Text, View } from 'react-native'; import Icon from 'react-native-vector-icons/Feather'; -import { relativeMemberInfoSelectorForMembersOfThread } from 'lib/selectors/user-selectors'; -import { stringForUser } from 'lib/shared/user-utils'; +import useInlineSidebarText from 'lib/hooks/inline-sidebar-text.react'; import type { ThreadInfo } from 'lib/types/thread-types'; -import { pluralizeAndTrim } from 'lib/utils/text-utils'; import Button from '../components/button.react'; -import { useSelector } from '../redux/redux-utils'; import { useStyles } from '../themes/colors'; import { useNavigateToThread } from './message-list-types'; @@ -20,6 +17,7 @@ }; function InlineSidebar(props: Props): React.Node { const { threadInfo } = props; + const { sendersText, repliesText } = useInlineSidebarText(threadInfo); const navigateToThread = useNavigateToThread(); const onPress = React.useCallback(() => { @@ -44,20 +42,6 @@ } const unreadStyle = threadInfo.currentUser.unread ? styles.unread : null; - const repliesCount = threadInfo.repliesCount || 1; - const repliesText = `${repliesCount} ${ - repliesCount > 1 ? 'replies' : 'reply' - }`; - - const threadMembers = useSelector( - relativeMemberInfoSelectorForMembersOfThread(threadInfo.id), - ); - const sendersText = React.useMemo(() => { - const senders = threadMembers - .filter(member => member.isSender) - .map(stringForUser); - return senders.length > 0 ? `${pluralizeAndTrim(senders, 25)} sent ` : ''; - }, [threadMembers]); return ( diff --git a/web/chat/inline-sidebar.react.js b/web/chat/inline-sidebar.react.js --- a/web/chat/inline-sidebar.react.js +++ b/web/chat/inline-sidebar.react.js @@ -7,12 +7,9 @@ CornerDownLeft as CornerDownLeftIcon, } from 'react-feather'; -import { relativeMemberInfoSelectorForMembersOfThread } from 'lib/selectors/user-selectors'; -import { stringForUser } from 'lib/shared/user-utils'; +import useInlineSidebarText from 'lib/hooks/inline-sidebar-text.react'; import type { ThreadInfo } from 'lib/types/thread-types'; -import { pluralizeAndTrim } from 'lib/utils/text-utils'; -import { useSelector } from '../redux/redux-utils'; import { useOnClickThread } from '../selectors/nav-selectors'; import css from './inline-sidebar.css'; @@ -22,6 +19,7 @@ }; function InlineSidebar(props: Props): React.Node { const { threadInfo } = props; + const { sendersText, repliesText } = useInlineSidebarText(threadInfo); const onClick = useOnClickThread(threadInfo); @@ -44,20 +42,6 @@ } const unreadStyle = threadInfo.currentUser.unread ? css.unread : null; - const repliesCount = threadInfo.repliesCount || 1; - const repliesText = `${repliesCount} ${ - repliesCount > 1 ? 'replies' : 'reply' - }`; - - const threadMembers = useSelector( - relativeMemberInfoSelectorForMembersOfThread(threadInfo.id), - ); - const sendersText = React.useMemo(() => { - const senders = threadMembers - .filter(member => member.isSender) - .map(stringForUser); - return senders.length > 0 ? `${pluralizeAndTrim(senders, 25)} sent ` : ''; - }, [threadMembers]); return (