Skip to content

Commit fe6e01a

Browse files
committed
narrow: Add optional param streamId in search narrow
1 parent 59dbf41 commit fe6e01a

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/utils/narrow.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export opaque type Narrow =
3636
| {| type: 'stream', streamId: number |}
3737
| {| type: 'topic', streamId: number, topic: string |}
3838
| {| type: 'pm', userIds: PmKeyRecipients |}
39-
| {| type: 'search', query: string |}
39+
| {| type: 'search', query: string, streamId?: number |}
4040
| {| type: 'all' | 'starred' | 'mentioned' | 'all-pm' |};
4141

4242
export const HOME_NARROW: Narrow = Object.freeze({ type: 'all' });
@@ -153,7 +153,8 @@ export const streamNarrow = (streamId: number): Narrow =>
153153
export const topicNarrow = (streamId: number, topic: string): Narrow =>
154154
Object.freeze({ type: 'topic', streamId, topic });
155155

156-
export const SEARCH_NARROW = (query: string): Narrow => Object.freeze({ type: 'search', query });
156+
export const SEARCH_NARROW = (query: string, streamId?: number): Narrow =>
157+
Object.freeze({ type: 'search', query, streamId });
157158

158159
type NarrowCases<T> = {|
159160
home: () => T,
@@ -163,7 +164,7 @@ type NarrowCases<T> = {|
163164
allPrivate: () => T,
164165
stream: (streamId: number) => T,
165166
topic: (streamId: number, topic: string) => T,
166-
search: (query: string) => T,
167+
search: (query: string, streamId?: number) => T,
167168
|};
168169

169170
/* prettier-ignore */
@@ -176,7 +177,7 @@ export function caseNarrow<T>(narrow: Narrow, cases: NarrowCases<T>): T {
176177
case 'stream': return cases.stream(narrow.streamId);
177178
case 'topic': return cases.topic(narrow.streamId, narrow.topic);
178179
case 'pm': return cases.pm(narrow.userIds);
179-
case 'search': return cases.search(narrow.query);
180+
case 'search': return cases.search(narrow.query, narrow.streamId);
180181
case 'all': return cases.home();
181182
case 'starred': return cases.starred();
182183
case 'mentioned': return cases.mentioned();
@@ -437,7 +438,18 @@ export const apiNarrowOfNarrow = (
437438
// TODO(server-2.1): just send IDs instead
438439
return [{ operator: 'pm-with', operand: emails.join(',') }];
439440
},
440-
search: query => [{ operator: 'search', operand: query }],
441+
// The search narrow can behave in two ways,
442+
// one where there is no filters, so the user can search a message in all the streams
443+
// the second where the user wants to search in a particular stream
444+
// Below we are checking if the streamId is mentioned or not
445+
// If it is mentioned, it will construct a narrow based on that.
446+
search: (query, streamId) =>
447+
streamId !== undefined
448+
? [
449+
{ operator: 'search', operand: query },
450+
{ operator: 'stream', operand: streamId },
451+
]
452+
: [{ operator: 'search', operand: query }],
441453
home: () => [],
442454
starred: () => [{ operator: 'is', operand: 'starred' }],
443455
mentioned: () => [{ operator: 'is', operand: 'mentioned' }],

0 commit comments

Comments
 (0)