Skip to content

Commit

Permalink
stream action buttons: Implement search messages in stream
Browse files Browse the repository at this point in the history
  • Loading branch information
SilentCruzer committed Apr 30, 2022
1 parent 0b1d8f9 commit 3b5dc05
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
10 changes: 10 additions & 0 deletions src/action-sheets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
navigateToEmojiPicker,
navigateToStream,
fetchSomeMessageIdForConversation,
navigateToSearch,
} from '../actions';
import {
navigateToMessageReactionScreen,
Expand Down Expand Up @@ -331,6 +332,14 @@ const unsubscribe = {
},
};

const searchMessage = {
title: 'Search in stream',
errorMessage: 'Failed to open search',
action: ({ streamId, streams }) => {
NavigationService.dispatch(navigateToSearch(streamId, streams.get(streamId)?.name));
},
};

const pinToTop = {
title: 'Pin to top',
errorMessage: 'Failed to pin to top',
Expand Down Expand Up @@ -471,6 +480,7 @@ export const constructStreamActionButtons = (args: {|
buttons.push(subscribe);
}
buttons.push(showStreamSettings);
buttons.push(searchMessage);
buttons.push(cancel);
return buttons;
};
Expand Down
3 changes: 2 additions & 1 deletion src/nav/navActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export const replaceWithChat = (narrow: Narrow): GenericNavigationAction =>

export const navigateToUsersScreen = (): GenericNavigationAction => StackActions.push('users');

export const navigateToSearch = (): GenericNavigationAction => StackActions.push('search-messages');
export const navigateToSearch = (streamId?: number, streamName?: string): GenericNavigationAction =>
StackActions.push('search-messages', { streamId, streamName });

export const navigateToEmojiPicker = (
onPressEmoji: ({| +type: EmojiType, +code: string, +name: string |}) => void,
Expand Down
35 changes: 29 additions & 6 deletions src/search/SearchMessagesScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { fetchMessages } from '../message/fetchActions';
type OuterProps = $ReadOnly<{|
// These should be passed from React Navigation
navigation: AppNavigationProp<'search-messages'>,
route: RouteProp<'search-messages', void>,
route: RouteProp<'search-messages', {| streamId?: number, streamName?: string |}>,
|}>;

type SelectorProps = $ReadOnly<{|
Expand Down Expand Up @@ -63,9 +63,14 @@ class SearchMessagesScreenInner extends PureComponent<Props, State> {
* Stores the fetched messages in the Redux store. Does not read any
* of the component's data except `props.dispatch`.
*/
fetchSearchMessages = async (query: string): Promise<$ReadOnlyArray<Message>> => {
fetchSearchMessages = async (
query: string,
inStream: boolean,
): Promise<$ReadOnlyArray<Message>> => {
const fetchArgs = {
narrow: SEARCH_NARROW(query),
narrow: inStream
? SEARCH_NARROW(query, this.props.route.params.streamId)
: SEARCH_NARROW(query),
anchor: LAST_MESSAGE_ANCHOR,
numBefore: 20,
numAfter: 0,
Expand All @@ -90,7 +95,17 @@ class SearchMessagesScreenInner extends PureComponent<Props, State> {
// invalidate outstanding requests on change will require more work.

handleQuerySubmit = async (e: EditingEvent) => {
const query = e.nativeEvent.text;
const searchString = e.nativeEvent.text;
let query;
if (
this.props.route.params.streamName !== undefined
&& searchString.substring(0, 7) === 'stream:'
) {
const prefixLength = `stream:${this.props.route.params.streamName.replace(' ', '+')}`.length;
query = searchString.substring(prefixLength + 1);
} else {
query = searchString;
}
const id = ++this.lastIdSent;

if (query === '') {
Expand All @@ -103,7 +118,10 @@ class SearchMessagesScreenInner extends PureComponent<Props, State> {

this.setState({ isFetching: true });
try {
const messages = await this.fetchSearchMessages(query);
const messages = await this.fetchSearchMessages(
query,
searchString.substring(0, 7) === 'stream:',
);

// Update `state.messages` if this is our new latest result.
if (id > this.lastIdSuccess) {
Expand Down Expand Up @@ -133,18 +151,23 @@ class SearchMessagesScreenInner extends PureComponent<Props, State> {

render() {
const { messages, isFetching } = this.state;
const searchPrefix =
this.props.route.params.streamId != null
? `stream:${this.props.route.params.streamName?.replace(' ', '+') ?? ''} `
: '';

return (
<Screen
search
autoFocus
searchBarOnSubmit={this.handleQuerySubmitWrapper}
style={styles.flexed}
searchPrefixText={searchPrefix}
>
<SearchMessagesCard
messages={messages}
isFetching={isFetching}
narrow={SEARCH_NARROW(this.state.query)}
narrow={SEARCH_NARROW(this.state.query, this.props.route.params.streamId)}
/>
</Screen>
);
Expand Down
2 changes: 2 additions & 0 deletions static/translations/messages_en.json
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@
"Failed to delete topic": "Failed to delete topic",
"Stream settings": "Stream settings",
"Failed to show stream settings": "Failed to show stream settings",
"Search in stream": "Search in stream",
"Failed to open search": "Failed to open search",
"show": "show",
"hide": "hide",
"Debug": "Debug",
Expand Down

0 comments on commit 3b5dc05

Please sign in to comment.