Don't use the whole Location
Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com>
This commit is contained in:
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { useCallback, useEffect, useState } from "react";
|
import { useMemo } from "react";
|
||||||
import { useLocation } from "react-router-dom";
|
import { useLocation } from "react-router-dom";
|
||||||
|
|
||||||
import { Config } from "./config/Config";
|
import { Config } from "./config/Config";
|
||||||
@@ -96,27 +96,28 @@ interface UrlParams {
|
|||||||
*/
|
*/
|
||||||
export const getUrlParams = (
|
export const getUrlParams = (
|
||||||
ignoreRoomAlias?: boolean,
|
ignoreRoomAlias?: boolean,
|
||||||
location: Location = window.location
|
search = window.location.search,
|
||||||
|
pathname = window.location.pathname,
|
||||||
|
hash = window.location.hash
|
||||||
): UrlParams => {
|
): UrlParams => {
|
||||||
const { href, origin, search, hash } = location;
|
|
||||||
|
|
||||||
let roomAlias: string | undefined;
|
let roomAlias: string | undefined;
|
||||||
if (!ignoreRoomAlias) {
|
if (!ignoreRoomAlias) {
|
||||||
roomAlias = href.substring(origin.length + 1);
|
if (hash === "") {
|
||||||
|
roomAlias = pathname.substring(1); // Strip the "/"
|
||||||
|
|
||||||
// If we have none, we throw
|
// Delete "/room/" and "?", if present
|
||||||
if (!roomAlias) {
|
if (roomAlias.startsWith("room/")) {
|
||||||
throw Error("No pathname");
|
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/")) {
|
// Add server part, if not present
|
||||||
roomAlias = roomAlias.substring("room/".length).split("?")[0];
|
|
||||||
}
|
|
||||||
// Add "#", if not present
|
|
||||||
if (!roomAlias.includes("#")) {
|
|
||||||
roomAlias = `#${roomAlias}`;
|
|
||||||
}
|
|
||||||
// Add server part, if missing
|
|
||||||
if (!roomAlias.includes(":")) {
|
if (!roomAlias.includes(":")) {
|
||||||
roomAlias = `${roomAlias}:${Config.defaultServerName()}`;
|
roomAlias = `${roomAlias}:${Config.defaultServerName()}`;
|
||||||
}
|
}
|
||||||
@@ -169,18 +170,9 @@ export const getUrlParams = (
|
|||||||
* @returns The app parameters for the current URL
|
* @returns The app parameters for the current URL
|
||||||
*/
|
*/
|
||||||
export const useUrlParams = (): UrlParams => {
|
export const useUrlParams = (): UrlParams => {
|
||||||
const getParams = useCallback(() => {
|
const { search, pathname, hash } = useLocation();
|
||||||
return getUrlParams(false, window.location);
|
return useMemo(
|
||||||
}, []);
|
() => getUrlParams(false, search, pathname, hash),
|
||||||
|
[search, pathname, hash]
|
||||||
const reactDomLocation = useLocation();
|
);
|
||||||
const [urlParams, setUrlParams] = useState(getParams());
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (window.location !== reactDomLocation) {
|
|
||||||
setUrlParams(getParams());
|
|
||||||
}
|
|
||||||
}, [getParams, reactDomLocation]);
|
|
||||||
|
|
||||||
return urlParams;
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -32,115 +32,67 @@ describe("UrlParams", () => {
|
|||||||
|
|
||||||
describe("handles URL with /room/", () => {
|
describe("handles URL with /room/", () => {
|
||||||
it("and nothing else", () => {
|
it("and nothing else", () => {
|
||||||
expect(
|
expect(getUrlParams(false, "", `/room/${ROOM_NAME}`, "").roomAlias).toBe(
|
||||||
getUrlParams(false, {
|
`#${ROOM_NAME}:${HOMESERVER}`
|
||||||
origin: ORIGIN,
|
);
|
||||||
href: `${ORIGIN}/room/${ROOM_NAME}`,
|
|
||||||
search: "",
|
|
||||||
hash: "",
|
|
||||||
} as Location).roomAlias
|
|
||||||
).toBe(`#${ROOM_NAME}:${HOMESERVER}`);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("and #", () => {
|
it("and #", () => {
|
||||||
expect(
|
expect(
|
||||||
getUrlParams(false, {
|
getUrlParams(false, "", `${ORIGIN}/room/`, `#${ROOM_NAME}`).roomAlias
|
||||||
origin: ORIGIN,
|
|
||||||
href: `${ORIGIN}/room/#${ROOM_NAME}`,
|
|
||||||
search: "",
|
|
||||||
hash: "",
|
|
||||||
} as Location).roomAlias
|
|
||||||
).toBe(`#${ROOM_NAME}:${HOMESERVER}`);
|
).toBe(`#${ROOM_NAME}:${HOMESERVER}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("and # and server part", () => {
|
it("and # and server part", () => {
|
||||||
expect(
|
expect(
|
||||||
getUrlParams(false, {
|
getUrlParams(false, "", `/room/`, `#${ROOM_NAME}:${HOMESERVER}`)
|
||||||
origin: ORIGIN,
|
.roomAlias
|
||||||
href: `${ORIGIN}/room/#${ROOM_NAME}:${HOMESERVER}`,
|
|
||||||
search: "",
|
|
||||||
hash: "",
|
|
||||||
} as Location).roomAlias
|
|
||||||
).toBe(`#${ROOM_NAME}:${HOMESERVER}`);
|
).toBe(`#${ROOM_NAME}:${HOMESERVER}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("and server part", () => {
|
it("and server part", () => {
|
||||||
expect(
|
expect(
|
||||||
getUrlParams(false, {
|
getUrlParams(false, "", `/room/${ROOM_NAME}:${HOMESERVER}`, "")
|
||||||
origin: ORIGIN,
|
.roomAlias
|
||||||
href: `${ORIGIN}/room/${ROOM_NAME}:${HOMESERVER}`,
|
|
||||||
search: "",
|
|
||||||
hash: "",
|
|
||||||
} as Location).roomAlias
|
|
||||||
).toBe(`#${ROOM_NAME}:${HOMESERVER}`);
|
).toBe(`#${ROOM_NAME}:${HOMESERVER}`);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("handles URL without /room/", () => {
|
describe("handles URL without /room/", () => {
|
||||||
it("and nothing else", () => {
|
it("and nothing else", () => {
|
||||||
expect(
|
expect(getUrlParams(false, "", `/${ROOM_NAME}`, "").roomAlias).toBe(
|
||||||
getUrlParams(false, {
|
`#${ROOM_NAME}:${HOMESERVER}`
|
||||||
origin: ORIGIN,
|
);
|
||||||
href: `${ORIGIN}/${ROOM_NAME}`,
|
|
||||||
search: "",
|
|
||||||
hash: "",
|
|
||||||
} as Location).roomAlias
|
|
||||||
).toBe(`#${ROOM_NAME}:${HOMESERVER}`);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("and with #", () => {
|
it("and with #", () => {
|
||||||
expect(
|
expect(getUrlParams(false, "", "", `#${ROOM_NAME}`).roomAlias).toBe(
|
||||||
getUrlParams(false, {
|
`#${ROOM_NAME}:${HOMESERVER}`
|
||||||
origin: ORIGIN,
|
);
|
||||||
href: `${ORIGIN}/room/#${ROOM_NAME}`,
|
|
||||||
search: "",
|
|
||||||
hash: "",
|
|
||||||
} as Location).roomAlias
|
|
||||||
).toBe(`#${ROOM_NAME}:${HOMESERVER}`);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("and with # and server part", () => {
|
it("and with # and server part", () => {
|
||||||
expect(
|
expect(
|
||||||
getUrlParams(false, {
|
getUrlParams(false, "", "", `#${ROOM_NAME}:${HOMESERVER}`).roomAlias
|
||||||
origin: ORIGIN,
|
|
||||||
href: `${ORIGIN}/room/#${ROOM_NAME}:${HOMESERVER}`,
|
|
||||||
search: "",
|
|
||||||
hash: "",
|
|
||||||
} as Location).roomAlias
|
|
||||||
).toBe(`#${ROOM_NAME}:${HOMESERVER}`);
|
).toBe(`#${ROOM_NAME}:${HOMESERVER}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("and with server part", () => {
|
it("and with server part", () => {
|
||||||
expect(
|
expect(
|
||||||
getUrlParams(false, {
|
getUrlParams(false, "", `/${ROOM_NAME}:${HOMESERVER}`, "").roomAlias
|
||||||
origin: ORIGIN,
|
|
||||||
href: `${ORIGIN}/room/${ROOM_NAME}:${HOMESERVER}`,
|
|
||||||
search: "",
|
|
||||||
hash: "",
|
|
||||||
} as Location).roomAlias
|
|
||||||
).toBe(`#${ROOM_NAME}:${HOMESERVER}`);
|
).toBe(`#${ROOM_NAME}:${HOMESERVER}`);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("handles search params", () => {
|
describe("handles search params", () => {
|
||||||
it("(roomId)", () => {
|
it("(roomId)", () => {
|
||||||
expect(
|
expect(getUrlParams(true, `?roomId=${ROOM_ID}`).roomId).toBe(ROOM_ID);
|
||||||
getUrlParams(true, {
|
|
||||||
search: `?roomId=${ROOM_ID}`,
|
|
||||||
hash: "",
|
|
||||||
} as Location).roomId
|
|
||||||
).toBe(ROOM_ID);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("ignores room alias", () => {
|
it("ignores room alias", () => {
|
||||||
expect(
|
expect(
|
||||||
getUrlParams(true, {
|
getUrlParams(true, "", `/room/${ROOM_NAME}:${HOMESERVER}`).roomAlias
|
||||||
origin: ORIGIN,
|
|
||||||
href: `${ORIGIN}/room/${ROOM_NAME}:${HOMESERVER}`,
|
|
||||||
hash: "",
|
|
||||||
search: "",
|
|
||||||
} as Location).roomAlias
|
|
||||||
).toBeFalsy();
|
).toBeFalsy();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user