From 8fbcc06cd8f5635fc166665204c7300f1557883d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Mon, 3 Jul 2023 20:05:08 +0200 Subject: [PATCH] Don't use the whole `Location` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- src/UrlParams.ts | 54 ++++++++++++--------------- test/UrlParams-test.ts | 84 +++++++++--------------------------------- 2 files changed, 41 insertions(+), 97 deletions(-) diff --git a/src/UrlParams.ts b/src/UrlParams.ts index 55d8ee10..f41e3cfa 100644 --- a/src/UrlParams.ts +++ b/src/UrlParams.ts @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import { useCallback, useEffect, useState } from "react"; +import { useMemo } from "react"; import { useLocation } from "react-router-dom"; import { Config } from "./config/Config"; @@ -96,27 +96,28 @@ interface UrlParams { */ export const getUrlParams = ( ignoreRoomAlias?: boolean, - location: Location = window.location + search = window.location.search, + pathname = window.location.pathname, + hash = window.location.hash ): UrlParams => { - const { href, origin, search, hash } = location; - let roomAlias: string | undefined; if (!ignoreRoomAlias) { - roomAlias = href.substring(origin.length + 1); + if (hash === "") { + roomAlias = pathname.substring(1); // Strip the "/" - // If we have none, we throw - if (!roomAlias) { - throw Error("No pathname"); + // Delete "/room/" and "?", if present + if (roomAlias.startsWith("room/")) { + roomAlias = roomAlias.substring("room/".length); + } + // Add "#", if not present + if (!roomAlias.startsWith("#")) { + roomAlias = `#${roomAlias}`; + } + } else { + roomAlias = hash; } - // Delete "/room/" and "?", if present - if (roomAlias.startsWith("room/")) { - roomAlias = roomAlias.substring("room/".length).split("?")[0]; - } - // Add "#", if not present - if (!roomAlias.includes("#")) { - roomAlias = `#${roomAlias}`; - } - // Add server part, if missing + + // Add server part, if not present if (!roomAlias.includes(":")) { roomAlias = `${roomAlias}:${Config.defaultServerName()}`; } @@ -169,18 +170,9 @@ export const getUrlParams = ( * @returns The app parameters for the current URL */ export const useUrlParams = (): UrlParams => { - const getParams = useCallback(() => { - return getUrlParams(false, window.location); - }, []); - - const reactDomLocation = useLocation(); - const [urlParams, setUrlParams] = useState(getParams()); - - useEffect(() => { - if (window.location !== reactDomLocation) { - setUrlParams(getParams()); - } - }, [getParams, reactDomLocation]); - - return urlParams; + const { search, pathname, hash } = useLocation(); + return useMemo( + () => getUrlParams(false, search, pathname, hash), + [search, pathname, hash] + ); }; diff --git a/test/UrlParams-test.ts b/test/UrlParams-test.ts index 9f691326..9d1e5198 100644 --- a/test/UrlParams-test.ts +++ b/test/UrlParams-test.ts @@ -32,115 +32,67 @@ describe("UrlParams", () => { describe("handles URL with /room/", () => { it("and nothing else", () => { - expect( - getUrlParams(false, { - origin: ORIGIN, - href: `${ORIGIN}/room/${ROOM_NAME}`, - search: "", - hash: "", - } as Location).roomAlias - ).toBe(`#${ROOM_NAME}:${HOMESERVER}`); + expect(getUrlParams(false, "", `/room/${ROOM_NAME}`, "").roomAlias).toBe( + `#${ROOM_NAME}:${HOMESERVER}` + ); }); it("and #", () => { expect( - getUrlParams(false, { - origin: ORIGIN, - href: `${ORIGIN}/room/#${ROOM_NAME}`, - search: "", - hash: "", - } as Location).roomAlias + getUrlParams(false, "", `${ORIGIN}/room/`, `#${ROOM_NAME}`).roomAlias ).toBe(`#${ROOM_NAME}:${HOMESERVER}`); }); it("and # and server part", () => { expect( - getUrlParams(false, { - origin: ORIGIN, - href: `${ORIGIN}/room/#${ROOM_NAME}:${HOMESERVER}`, - search: "", - hash: "", - } as Location).roomAlias + getUrlParams(false, "", `/room/`, `#${ROOM_NAME}:${HOMESERVER}`) + .roomAlias ).toBe(`#${ROOM_NAME}:${HOMESERVER}`); }); it("and server part", () => { expect( - getUrlParams(false, { - origin: ORIGIN, - href: `${ORIGIN}/room/${ROOM_NAME}:${HOMESERVER}`, - search: "", - hash: "", - } as Location).roomAlias + getUrlParams(false, "", `/room/${ROOM_NAME}:${HOMESERVER}`, "") + .roomAlias ).toBe(`#${ROOM_NAME}:${HOMESERVER}`); }); }); describe("handles URL without /room/", () => { it("and nothing else", () => { - expect( - getUrlParams(false, { - origin: ORIGIN, - href: `${ORIGIN}/${ROOM_NAME}`, - search: "", - hash: "", - } as Location).roomAlias - ).toBe(`#${ROOM_NAME}:${HOMESERVER}`); + expect(getUrlParams(false, "", `/${ROOM_NAME}`, "").roomAlias).toBe( + `#${ROOM_NAME}:${HOMESERVER}` + ); }); it("and with #", () => { - expect( - getUrlParams(false, { - origin: ORIGIN, - href: `${ORIGIN}/room/#${ROOM_NAME}`, - search: "", - hash: "", - } as Location).roomAlias - ).toBe(`#${ROOM_NAME}:${HOMESERVER}`); + expect(getUrlParams(false, "", "", `#${ROOM_NAME}`).roomAlias).toBe( + `#${ROOM_NAME}:${HOMESERVER}` + ); }); it("and with # and server part", () => { expect( - getUrlParams(false, { - origin: ORIGIN, - href: `${ORIGIN}/room/#${ROOM_NAME}:${HOMESERVER}`, - search: "", - hash: "", - } as Location).roomAlias + getUrlParams(false, "", "", `#${ROOM_NAME}:${HOMESERVER}`).roomAlias ).toBe(`#${ROOM_NAME}:${HOMESERVER}`); }); it("and with server part", () => { expect( - getUrlParams(false, { - origin: ORIGIN, - href: `${ORIGIN}/room/${ROOM_NAME}:${HOMESERVER}`, - search: "", - hash: "", - } as Location).roomAlias + getUrlParams(false, "", `/${ROOM_NAME}:${HOMESERVER}`, "").roomAlias ).toBe(`#${ROOM_NAME}:${HOMESERVER}`); }); }); describe("handles search params", () => { it("(roomId)", () => { - expect( - getUrlParams(true, { - search: `?roomId=${ROOM_ID}`, - hash: "", - } as Location).roomId - ).toBe(ROOM_ID); + expect(getUrlParams(true, `?roomId=${ROOM_ID}`).roomId).toBe(ROOM_ID); }); }); it("ignores room alias", () => { expect( - getUrlParams(true, { - origin: ORIGIN, - href: `${ORIGIN}/room/${ROOM_NAME}:${HOMESERVER}`, - hash: "", - search: "", - } as Location).roomAlias + getUrlParams(true, "", `/room/${ROOM_NAME}:${HOMESERVER}`).roomAlias ).toBeFalsy(); }); });