Skip to content

Commit

Permalink
[web] Inline MessageSearchModalContent
Browse files Browse the repository at this point in the history
Summary:
After D8274 `MessageSearchModalContent` can be inlined. This is also needed for later diffs, where this separation was making the code unnecessarily complex
issue: https://linear.app/comm/issue/ENG-3462/add-modal-for-searching-messages

Test Plan: Checked that message search modal still works

Reviewers: kamil, kuba, michal

Reviewed By: michal

Subscribers: ashoat, tomek

Differential Revision: https://phab.comm.dev/D8291
  • Loading branch information
InkaAlicja committed Jun 27, 2023
1 parent 5dfc71e commit 6584acc
Showing 1 changed file with 26 additions and 39 deletions.
65 changes: 26 additions & 39 deletions web/modals/search/message-search-modal.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,23 @@ import { useMessageSearchContext } from '../../search/message-search-state-provi
import Modal from '../modal.react.js';

type ContentProps = {
+query: string,
+threadInfo: ThreadInfo,
};

function MessageSearchModalContent(props: ContentProps): React.Node {
const { query, threadInfo } = props;
function MessageSearchModal(props: ContentProps): React.Node {
const { threadInfo } = props;

const [lastID, setLastID] = React.useState();
const [searchResults, setSearchResults] = React.useState([]);
const [endReached, setEndReached] = React.useState(false);

const { getQuery, setQuery, clearQuery } = useMessageSearchContext();

const query = React.useMemo(
() => getQuery(threadInfo.id),
[getQuery, threadInfo.id],
);

const appendSearchResults = React.useCallback(
(newMessages: $ReadOnlyArray<RawMessageInfo>, end: boolean) => {
setSearchResults(oldMessages => [...oldMessages, ...newMessages]);
Expand Down Expand Up @@ -127,48 +133,13 @@ function MessageSearchModalContent(props: ContentProps): React.Node {
);
}, [query, endReached, modifiedItems.length]);

return (
<div className={css.content} ref={messageContainerRef}>
{messages}
{footer}
</div>
);
}

function oldestMessageID(data: $ReadOnlyArray<ChatMessageItem>) {
for (let i = data.length - 1; i >= 0; i--) {
if (data[i].itemType === 'message' && data[i].messageInfo.id) {
return data[i].messageInfo.id;
}
}
return undefined;
}

type Props = {
+threadInfo: ThreadInfo,
};

function MessageSearchModal(props: Props): React.Node {
const { threadInfo } = props;
const { popModal } = useModalContext();

const { getQuery, setQuery, clearQuery } = useMessageSearchContext();

const query = React.useMemo(
() => getQuery(threadInfo.id),
[getQuery, threadInfo.id],
);

const [input, setInput] = React.useState(query);

const onPressSearch = React.useCallback(
() => setQuery(input, threadInfo.id),
[setQuery, input, threadInfo.id],
);

const { uiName } = useResolvedThreadInfo(threadInfo);
const searchPlaceholder = `Searching in ${uiName}`;

const clearQueryWrapper = React.useCallback(
() => clearQuery(threadInfo.id),
[clearQuery, threadInfo.id],
Expand All @@ -183,6 +154,10 @@ function MessageSearchModal(props: Props): React.Node {
[onPressSearch],
);

const { uiName } = useResolvedThreadInfo(threadInfo);
const searchPlaceholder = `Searching in ${uiName}`;
const { popModal } = useModalContext();

return (
<Modal name="Search Message" onClose={popModal} size="large">
<div className={css.container}>
Expand All @@ -202,10 +177,22 @@ function MessageSearchModal(props: Props): React.Node {
Search
</Button>
</div>
<MessageSearchModalContent threadInfo={threadInfo} query={query} />
<div className={css.content} ref={messageContainerRef}>
{messages}
{footer}
</div>
</div>
</Modal>
);
}

function oldestMessageID(data: $ReadOnlyArray<ChatMessageItem>) {
for (let i = data.length - 1; i >= 0; i--) {
if (data[i].itemType === 'message' && data[i].messageInfo.id) {
return data[i].messageInfo.id;
}
}
return undefined;
}

export default MessageSearchModal;

0 comments on commit 6584acc

Please sign in to comment.