Skip to content

Commit c907c73

Browse files
committed
internalLinks [nfc]: Have getNarrowFromLink take URL object
1 parent 2bb12a8 commit c907c73

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

src/message/messagesActions.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,13 @@ export const messageLinkPress =
222222
const streamsById = getStreamsById(state);
223223
const streamsByName = getStreamsByName(state);
224224
const ownUserId = getOwnUserId(state);
225-
const narrow = getNarrowFromLink(href, auth.realm, streamsById, streamsByName, ownUserId);
225+
const narrow = getNarrowFromLink(
226+
new URL(href, auth.realm), // TODO: Use parsedUrl, below.
227+
auth.realm,
228+
streamsById,
229+
streamsByName,
230+
ownUserId,
231+
);
226232

227233
const parsedUrl = tryParseUrl(href, auth.realm);
228234
// TODO: Replace all uses of `href` below with `parsedUrl`.

src/utils/__tests__/internalLinks-test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,10 @@ describe('getNarrowFromLink', () => {
198198

199199
const streamGeneral = eg.makeStream({ name: 'general' });
200200

201+
// TODO: Take URL object instead of string
201202
const get = (url, streams: $ReadOnlyArray<Stream>) =>
202203
getNarrowFromLink(
203-
url,
204+
new URL(url, 'https://example.com'),
204205
new URL('https://example.com'),
205206
new Map(streams.map(s => [s.stream_id, s])),
206207
new Map(streams.map(s => [s.name, s])),

src/utils/internalLinks.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,29 +155,22 @@ const parsePmOperand = operand => {
155155

156156
/**
157157
* TODO write jsdoc
158-
*
159-
* This performs a call to `new URL` and therefore may take a fraction of a
160-
* millisecond. Avoid using in a context where it might be called more than
161-
* 10 or 100 times per user action.
162158
*/
163159
export const getNarrowFromLink = (
164-
url: string,
160+
url: URL,
165161
realm: URL,
166162
streamsById: Map<number, Stream>,
167163
streamsByName: Map<string, Stream>,
168164
ownUserId: UserId,
169165
): Narrow | null => {
170-
// TODO: Get this from caller
171-
const parsedUrl = new URL(url, realm);
172-
173-
const type = getLinkType(parsedUrl, realm);
166+
const type = getLinkType(url, realm);
174167

175168
if (type === 'non-narrow') {
176169
return null;
177170
}
178171

179172
// isNarrowLink(…) is true, by early return above, so this call is OK.
180-
const hashSegments = getHashSegmentsFromNarrowLink(parsedUrl, realm);
173+
const hashSegments = getHashSegmentsFromNarrowLink(url, realm);
181174

182175
switch (type) {
183176
case 'pm': {

0 commit comments

Comments
 (0)