Skip to content

Commit cd286b6

Browse files
committed
internalLinks [nfc]: Have getHashSegmentsFromNarrowLink take URL object
1 parent a390781 commit cd286b6

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

src/utils/internalLinks.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,12 @@ import { isUrlOnRealm } from './url';
1717
*
1818
* If `url` ends with a slash, the returned array won't have '' as its last
1919
* element; that element is removed.
20-
*
21-
* This performs a call to `new URL` and therefore may take a fraction of a
22-
* millisecond. Avoid using in a context where it might be called more than
23-
* 10 or 100 times per user action.
2420
*/
25-
// TODO: Take a URL object for `url` instead of a string, and remove
26-
// performance warning in the jsdoc.
2721
// TODO: Parse into an array of objects with { negated, operator, operand },
2822
// like the web app's parse_narrow in static/js/hash_util.js.
2923
// TODO(#3757): Use @zulip/shared for that parsing.
30-
const getHashSegmentsFromNarrowLink = (url: string, realm: URL) => {
31-
// TODO: Get this from caller
32-
const parsedUrl = new URL(url, realm);
33-
34-
const result = parsedUrl.hash
24+
const getHashSegmentsFromNarrowLink = (url: URL, realm: URL) => {
25+
const result = url.hash
3526
.split('/')
3627
// Remove the first item, "#narrow".
3728
.slice(1);
@@ -77,7 +68,7 @@ export const getLinkType = (url: string, realm: URL): LinkType => {
7768
}
7869

7970
// isNarrowLink(…) is true, by early return above, so this call is OK.
80-
const hashSegments = getHashSegmentsFromNarrowLink(url, realm);
71+
const hashSegments = getHashSegmentsFromNarrowLink(parsedUrl, realm);
8172

8273
if (
8374
(hashSegments.length === 2 && hashSegments[0] === 'pm-with')
@@ -189,8 +180,11 @@ export const getNarrowFromLink = (
189180
return null;
190181
}
191182

183+
// TODO: Get this from caller
184+
const parsedUrl = new URL(url, realm);
185+
192186
// isNarrowLink(…) is true, by early return above, so this call is OK.
193-
const hashSegments = getHashSegmentsFromNarrowLink(url, realm);
187+
const hashSegments = getHashSegmentsFromNarrowLink(parsedUrl, realm);
194188

195189
switch (type) {
196190
case 'pm': {
@@ -233,8 +227,11 @@ export const getNarrowFromLink = (
233227
* 10 or 100 times per user action.
234228
*/
235229
export const getNearOperandFromLink = (url: string, realm: URL): number | null => {
230+
// TODO: Get this from caller
231+
const parsedUrl = new URL(url, realm);
232+
236233
// isNarrowLink(…) is true, by jsdoc, so this call is OK.
237-
const hashSegments = getHashSegmentsFromNarrowLink(url, realm);
234+
const hashSegments = getHashSegmentsFromNarrowLink(parsedUrl, realm);
238235

239236
// This and nearOperandIndex can simplify when we rename/repurpose
240237
// getHashSegmentsFromNarrowLink so it gives an array of

0 commit comments

Comments
 (0)