Skip to content

Commit 6eaabeb

Browse files
chrisbobbegnprice
authored andcommitted
internalLinks [nfc]: Have parseStreamOperand return a stream ID
1 parent ee3aad0 commit 6eaabeb

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

src/utils/internalLinks.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,27 +66,24 @@ export const decodeHashComponent = (string: string): string => {
6666
};
6767

6868
/**
69-
* Parse the operand of a `stream` operator.
69+
* Parse the operand of a `stream` operator, returning a stream ID.
7070
*
7171
* Return null if the operand doesn't match any known stream.
7272
*/
73-
const parseStreamOperand = (operand, streamsById, streamsByName): null | Stream => {
73+
const parseStreamOperand = (operand, streamsById, streamsByName): null | number => {
7474
// "New" (2018) format: ${stream_id}-${stream_name} .
7575
const match = /^([\d]+)(?:-.*)?$/.exec(operand);
7676
const newFormatStreamId = match ? parseInt(match[1], 10) : null;
77-
if (newFormatStreamId != null) {
78-
const stream = streamsById.get(newFormatStreamId);
79-
if (stream) {
80-
return stream;
81-
}
77+
if (newFormatStreamId != null && streamsById.has(newFormatStreamId)) {
78+
return newFormatStreamId;
8279
}
8380

8481
// Old format: just stream name. This case is relevant indefinitely,
8582
// so that links in old conversations continue to work.
8683
const streamName = decodeHashComponent(operand);
8784
const stream = streamsByName.get(streamName);
8885
if (stream) {
89-
return stream;
86+
return stream.stream_id;
9087
}
9188

9289
// Not any stream we know. (Most likely this means a stream the user
@@ -142,13 +139,13 @@ export const getNarrowFromLink = (
142139
&& hashSegments[0] === 'stream'
143140
&& (hashSegments[2] === 'subject' || hashSegments[2] === 'topic')
144141
) {
145-
const stream = parseStreamOperand(hashSegments[1], streamsById, streamsByName);
146-
return stream && topicNarrow(stream.stream_id, parseTopicOperand(hashSegments[3]));
142+
const streamId = parseStreamOperand(hashSegments[1], streamsById, streamsByName);
143+
return streamId != null ? topicNarrow(streamId, parseTopicOperand(hashSegments[3])) : null;
147144
}
148145

149146
if (hashSegments.length === 2 && hashSegments[0] === 'stream') {
150-
const stream = parseStreamOperand(hashSegments[1], streamsById, streamsByName);
151-
return stream && streamNarrow(stream.stream_id);
147+
const streamId = parseStreamOperand(hashSegments[1], streamsById, streamsByName);
148+
return streamId != null ? streamNarrow(streamId) : null;
152149
}
153150

154151
if (

0 commit comments

Comments
 (0)