Skip to content

Commit daa4b6f

Browse files
Leslie NgoLeslie Ngo
Leslie Ngo
authored and
Leslie Ngo
committed
topic edit modal: Wrap MessageList in functional component.
We can't call Context hooks from SearchMessagesCard because it's a class component. This wrapper allows the StartEditTopic callback to be passed to SearchMessagesCard's MessageList child without breaking the Rules of Hooks.
1 parent d9bebe6 commit daa4b6f

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

src/search/MessageListWrapper.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* @flow strict-local */
2+
import React from 'react';
3+
import type { Node } from 'react';
4+
import MessageList from '../webview/MessageList';
5+
import { useTopicModalHandler } from '../boot/TopicModalProvider';
6+
import type { Message, Narrow } from '../types';
7+
8+
type Props = $ReadOnly<{|
9+
messages: $ReadOnlyArray<Message>,
10+
narrow: Narrow,
11+
|}>;
12+
13+
/* We can't call Context hooks from SearchMessagesCard because it's a class component. This wrapper allows the startEditTopic callback to be passed to this particular MessageList child without breaking Rules of Hooks. */
14+
15+
export default function MessageListWrapper({ messages, narrow }: Props): Node {
16+
const { startEditTopic } = useTopicModalHandler();
17+
18+
return (
19+
<MessageList
20+
initialScrollMessageId={
21+
// This access is OK only because of the `.length === 0` check
22+
// above.
23+
messages[messages.length - 1].id
24+
}
25+
messages={messages}
26+
narrow={narrow}
27+
showMessagePlaceholders={false}
28+
// TODO: handle editing a message from the search results,
29+
// or make this prop optional
30+
startEditMessage={() => undefined}
31+
startEditTopic={startEditTopic}
32+
/>
33+
);
34+
}

src/search/SearchMessagesCard.js

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { Message, Narrow } from '../types';
88
import { createStyleSheet } from '../styles';
99
import LoadingIndicator from '../common/LoadingIndicator';
1010
import SearchEmptyState from '../common/SearchEmptyState';
11-
import MessageList from '../webview/MessageList';
11+
import MessageListWrapper from './MessageListWrapper';
1212

1313
const styles = createStyleSheet({
1414
results: {
@@ -24,8 +24,7 @@ type Props = $ReadOnly<{|
2424

2525
export default class SearchMessagesCard extends PureComponent<Props> {
2626
render(): Node {
27-
const { isFetching, messages } = this.props;
28-
27+
const { isFetching, messages, narrow } = this.props;
2928
if (isFetching) {
3029
// Display loading indicator only if there are no messages to
3130
// display from a previous search.
@@ -44,19 +43,7 @@ export default class SearchMessagesCard extends PureComponent<Props> {
4443

4544
return (
4645
<View style={styles.results}>
47-
<MessageList
48-
initialScrollMessageId={
49-
// This access is OK only because of the `.length === 0` check
50-
// above.
51-
messages[messages.length - 1].id
52-
}
53-
messages={messages}
54-
narrow={this.props.narrow}
55-
showMessagePlaceholders={false}
56-
// TODO: handle editing a message from the search results,
57-
// or make this prop optional
58-
startEditMessage={() => undefined}
59-
/>
46+
<MessageListWrapper messages={messages} narrow={narrow} />
6047
</View>
6148
);
6249
}

0 commit comments

Comments
 (0)