diff --git a/web/chat/thread-top-bar.react.js b/web/chat/thread-top-bar.react.js index bcd7c80f1..0ba635f0d 100644 --- a/web/chat/thread-top-bar.react.js +++ b/web/chat/thread-top-bar.react.js @@ -1,104 +1,101 @@ // @flow import * as React from 'react'; import { ChevronRight } from 'react-feather'; import { useModalContext } from 'lib/components/modal-provider.react.js'; import SWMansionIcon from 'lib/components/SWMansionIcon.react.js'; import { threadIsPending } from 'lib/shared/thread-utils.js'; import type { ThreadInfo } from 'lib/types/thread-types.js'; import { useResolvedThreadInfo } from 'lib/utils/entity-helpers.js'; import ThreadMenu from './thread-menu.react.js'; import css from './thread-top-bar.css'; import ThreadAvatar from '../avatars/thread-avatar.react.js'; import Button from '../components/button.react.js'; import { InputStateContext } from '../input/input-state.js'; -import MessageResultsModal from '../modals/chat/message-results-modal.react.js'; +import PinnedMessagesModal from '../modals/chat/pinned-messages-modal.react.js'; import MessageSearchModal from '../modals/search/message-search-modal.react.js'; type ThreadTopBarProps = { +threadInfo: ThreadInfo, }; function ThreadTopBar(props: ThreadTopBarProps): React.Node { const { threadInfo } = props; const { pushModal } = useModalContext(); let threadMenu = null; if (!threadIsPending(threadInfo.id)) { threadMenu = ; } - // To allow the pinned messages modal to be re-used by the message search - // modal, it will be useful to make the modal accept a prop that defines it's - // name, instead of setting it directly in the modal. const bannerText = React.useMemo(() => { if (!threadInfo.pinnedCount || threadInfo.pinnedCount === 0) { return ''; } const messageNoun = threadInfo.pinnedCount === 1 ? 'message' : 'messages'; return `${threadInfo.pinnedCount} pinned ${messageNoun}`; }, [threadInfo.pinnedCount]); const inputState = React.useContext(InputStateContext); const pushThreadPinsModal = React.useCallback(() => { pushModal( - + , ); }, [pushModal, inputState, threadInfo, bannerText]); const pinnedCountBanner = React.useMemo(() => { if (!bannerText) { return null; } return (
{bannerText}
); }, [bannerText, pushThreadPinsModal]); const onClickSearch = React.useCallback( () => pushModal( , ), [inputState, pushModal, threadInfo], ); const { uiName } = useResolvedThreadInfo(threadInfo); return ( <>
{uiName}
{threadMenu}
{pinnedCountBanner} ); } export default ThreadTopBar; diff --git a/web/modals/chat/message-results-modal.css b/web/modals/chat/pinned-messages-modal.css similarity index 100% rename from web/modals/chat/message-results-modal.css rename to web/modals/chat/pinned-messages-modal.css diff --git a/web/modals/chat/message-results-modal.react.js b/web/modals/chat/pinned-messages-modal.react.js similarity index 96% rename from web/modals/chat/message-results-modal.react.js rename to web/modals/chat/pinned-messages-modal.react.js index 093fc706a..31d45f706 100644 --- a/web/modals/chat/message-results-modal.react.js +++ b/web/modals/chat/pinned-messages-modal.react.js @@ -1,162 +1,162 @@ // @flow import * as React from 'react'; import { fetchPinnedMessageActionTypes, useFetchPinnedMessages, } from 'lib/actions/message-actions.js'; import { useModalContext } from 'lib/components/modal-provider.react.js'; import { messageListData, type ChatMessageInfoItem, } from 'lib/selectors/chat-selectors.js'; import { createLoadingStatusSelector } from 'lib/selectors/loading-selectors.js'; import { createMessageInfo, isInvalidPinSourceForThread, modifyItemForResultScreen, } from 'lib/shared/message-utils.js'; import type { RawMessageInfo } from 'lib/types/message-types.js'; import { type ThreadInfo } from 'lib/types/thread-types.js'; import { useDispatchActionPromise } from 'lib/utils/action-utils.js'; -import css from './message-results-modal.css'; +import css from './pinned-messages-modal.css'; import MessageResult from '../../components/message-result.react.js'; import LoadingIndicator from '../../loading-indicator.react.js'; import { useSelector } from '../../redux/redux-utils.js'; import Modal from '../modal.react.js'; -type MessageResultsModalProps = { +type Props = { +threadInfo: ThreadInfo, +modalName: string, }; const loadingStatusSelector = createLoadingStatusSelector( fetchPinnedMessageActionTypes, ); -function MessageResultsModal(props: MessageResultsModalProps): React.Node { +function PinnedMessagesModal(props: Props): React.Node { const { threadInfo, modalName } = props; const { id: threadID } = threadInfo; const { popModal } = useModalContext(); const [rawMessageResults, setRawMessageResults] = React.useState< $ReadOnlyArray, >([]); const callFetchPinnedMessages = useFetchPinnedMessages(); const dispatchActionPromise = useDispatchActionPromise(); const userInfos = useSelector(state => state.userStore.userInfos); const loadingStatus = useSelector(loadingStatusSelector); React.useEffect(() => { void dispatchActionPromise( fetchPinnedMessageActionTypes, (async () => { const result = await callFetchPinnedMessages({ threadID }); setRawMessageResults(result.pinnedMessages); })(), ); }, [dispatchActionPromise, callFetchPinnedMessages, threadID]); const translatedMessageResults = React.useMemo(() => { const threadInfos = { [threadID]: threadInfo }; return rawMessageResults .map(messageInfo => createMessageInfo(messageInfo, null, userInfos, threadInfos), ) .filter(Boolean); }, [rawMessageResults, userInfos, threadID, threadInfo]); const chatMessageInfos = useSelector( messageListData(threadInfo.id, translatedMessageResults), ); const sortedUniqueChatMessageInfoItems = React.useMemo(() => { if (!chatMessageInfos) { return ([]: ChatMessageInfoItem[]); } const chatMessageInfoItems = chatMessageInfos.filter( item => item.itemType === 'message' && item.isPinned && !isInvalidPinSourceForThread(item.messageInfo, threadInfo), ); // By the nature of using messageListData and passing in // the desired translatedMessageResults as additional // messages, we will have duplicate ChatMessageInfoItems. const uniqueChatMessageInfoItemsMap = new Map< string, ChatMessageInfoItem, >(); chatMessageInfoItems.forEach( item => item.messageInfo && item.messageInfo.id && uniqueChatMessageInfoItemsMap.set(item.messageInfo.id, item), ); // Push the items in the order they appear in the rawMessageResults // since the messages fetched from the server are already sorted // in the order of pin_time (newest first). const sortedChatMessageInfoItems = []; for (let i = 0; i < rawMessageResults.length; i++) { const rawMessageID = rawMessageResults[i].id; if (!rawMessageID) { continue; } sortedChatMessageInfoItems.push( uniqueChatMessageInfoItemsMap.get(rawMessageID), ); } return sortedChatMessageInfoItems; }, [chatMessageInfos, rawMessageResults, threadInfo]); const modifiedItems = React.useMemo( () => sortedUniqueChatMessageInfoItems .filter(Boolean) .map(item => modifyItemForResultScreen(item)), [sortedUniqueChatMessageInfoItems], ); const messageResultsToDisplay = React.useMemo(() => { const items = modifiedItems.map(item => ( )); return <>{items}; }, [modifiedItems, threadInfo]); const loadingIndicator = React.useMemo(() => { if (loadingStatus === 'loading') { return (
); } return null; }, [loadingStatus]); return (
{loadingIndicator}
{messageResultsToDisplay}
); } -export default MessageResultsModal; +export default PinnedMessagesModal;