Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

hash_util: Add get_link_hash.

Evy Kassirerbdca455046e1
diff --git a/web/src/hash_util.ts b/web/src/hash_util.ts
index 86095992f40..c6e4d27c49d 100644
--- a/web/src/hash_util.ts
+++ b/web/src/hash_util.ts
@@ -435,3 +435,14 @@ export function decode_stream_topic_from_url(
return null;
}
}
+
+export function get_link_hash(link: string): string {
+ if (link.startsWith("/#narrow/")) {
+ return link.slice(1);
+ }
+ try {
+ return new URL(link).hash;
+ } catch {
+ return "";
+ }
+}
diff --git a/web/tests/hash_util.test.cjs b/web/tests/hash_util.test.cjs
index 8b4802b77db..87af7745bbf 100644
--- a/web/tests/hash_util.test.cjs
+++ b/web/tests/hash_util.test.cjs
@@ -271,3 +271,15 @@ run_test("test_current_hash_as_next", () => {
window.location.hash = "#foo";
assert.equal(spectators.current_hash_as_next(), "next=/%23foo");
});
+
+run_test("get_link_hash", () => {
+ assert.equal(
+ hash_util.get_link_hash("/#narrow/channel/9-announce"),
+ "#narrow/channel/9-announce",
+ );
+ assert.equal(
+ hash_util.get_link_hash("https://chat.zulip.org/#narrow/channel/127-integrations/"),
+ "#narrow/channel/127-integrations/",
+ );
+ assert.equal(hash_util.get_link_hash("bad-url"), "");
+});