Don't use the whole Location

Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com>
This commit is contained in:
Šimon Brandner
2023-07-03 20:05:08 +02:00
parent 0bba620451
commit 8fbcc06cd8
2 changed files with 41 additions and 97 deletions

View File

@@ -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]
);
};

View File

@@ -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();
});
});