hydrogen-web/src/domain/navigation/index.ts

493 lines
22 KiB
TypeScript
Raw Normal View History

2020-10-09 13:26:01 +05:30
/*
Copyright 2020 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2022-02-16 23:37:10 +05:30
import {Navigation, Segment} from "./Navigation";
2020-10-09 23:13:11 +05:30
import {URLRouter} from "./URLRouter.js";
2022-02-22 11:41:36 +05:30
import type { Path } from "./Navigation";
type SegmentType = {
2022-02-22 11:48:02 +05:30
"login": true;
"session": string;
"sso": string;
"logout": true;
"room": string;
"rooms": string[];
"settings": true;
2022-02-22 11:41:36 +05:30
"create-room": true;
"empty-grid-tile": number;
2022-02-22 11:48:02 +05:30
"lightbox": string;
2022-02-22 11:41:36 +05:30
"right-panel": true;
2022-02-22 11:48:02 +05:30
"details": true;
"members": true;
"member": string;
2022-02-22 11:41:36 +05:30
};
2020-10-09 13:26:01 +05:30
export function createNavigation() {
2020-10-12 21:19:06 +05:30
return new Navigation(allowsChild);
2020-10-09 13:26:01 +05:30
}
2020-10-09 23:13:11 +05:30
export function createRouter({history, navigation}) {
2020-10-12 21:19:06 +05:30
return new URLRouter({history, navigation, stringifyPath, parseUrlPath});
2020-10-09 23:13:11 +05:30
}
2022-02-22 11:41:36 +05:30
function allowsChild(parent: {type: string, value: any} | undefined, child: {type: string, value: any}): boolean {
2020-10-12 21:19:06 +05:30
const {type} = child;
switch (parent?.type) {
case undefined:
// allowed root segments
return type === "login" || type === "session" || type === "sso" || type === "logout";
2020-10-12 21:19:06 +05:30
case "session":
2022-02-09 23:30:41 +05:30
return type === "room" || type === "rooms" || type === "settings" || type === "create-room";
2020-10-12 21:19:06 +05:30
case "rooms":
// downside of the approach: both of these will control which tile is selected
return type === "room" || type === "empty-grid-tile";
case "room":
return type === "lightbox" || type === "right-panel";
case "right-panel":
return type === "details"|| type === "members" || type === "member";
2020-10-12 21:19:06 +05:30
default:
return false;
}
2020-10-09 23:13:11 +05:30
}
2022-02-22 11:41:36 +05:30
export function removeRoomFromPath(path: Path<SegmentType>, roomId: string): Path<SegmentType> | null {
let newPath: Path<SegmentType> | null = path;
const rooms = newPath.get("rooms");
let roomIdGridIndex = -1;
// first delete from rooms segment
if (rooms) {
roomIdGridIndex = rooms.value.indexOf(roomId);
if (roomIdGridIndex !== -1) {
const idsWithoutRoom = rooms.value.slice();
idsWithoutRoom[roomIdGridIndex] = "";
2022-02-22 11:41:36 +05:30
newPath = newPath.replace(new Segment("rooms", idsWithoutRoom));
}
}
2022-02-22 11:41:36 +05:30
const room = newPath!.get("room");
// then from room (which occurs with or without rooms)
if (room && room.value === roomId) {
if (roomIdGridIndex !== -1) {
2022-02-22 11:41:36 +05:30
newPath = newPath!.with(new Segment("empty-grid-tile", roomIdGridIndex));
} else {
2022-02-22 11:41:36 +05:30
newPath = newPath!.until("session");
}
}
2022-02-22 11:41:36 +05:30
return newPath;
}
2022-02-22 11:41:36 +05:30
function roomsSegmentWithRoom(rooms: Segment<SegmentType, "rooms">, roomId: string, path: Path<SegmentType>): Segment<SegmentType, "rooms"> {
2020-10-13 16:43:15 +05:30
if(!rooms.value.includes(roomId)) {
2020-10-09 23:13:11 +05:30
const emptyGridTile = path.get("empty-grid-tile");
2020-10-13 16:43:15 +05:30
const oldRoom = path.get("room");
let index = 0;
2020-10-09 23:13:11 +05:30
if (emptyGridTile) {
index = emptyGridTile.value;
2020-10-13 16:43:15 +05:30
} else if (oldRoom) {
index = rooms.value.indexOf(oldRoom.value);
2020-10-09 23:13:11 +05:30
}
2020-10-13 16:43:15 +05:30
const roomIds = rooms.value.slice();
roomIds[index] = roomId;
return new Segment("rooms", roomIds);
} else {
return rooms;
}
2020-10-09 23:13:11 +05:30
}
2022-02-22 11:41:36 +05:30
// todo-self: verify code change here is okay
function pushRightPanelSegment<T extends keyof SegmentType>(array: Segment<SegmentType>[], segment: T, ...value: SegmentType[T] extends true? [(undefined | true)?]: [SegmentType[T]]) {
array.push(new Segment("right-panel"));
2022-02-22 11:41:36 +05:30
array.push(new Segment(segment, ...value));
}
2022-02-22 11:41:36 +05:30
export function addPanelIfNeeded<T extends SegmentType>(navigation: Navigation<T>, path: Path<T>): Path<T> {
const segments = navigation.path.segments;
const i = segments.findIndex(segment => segment.type === "right-panel");
let _path = path;
if (i !== -1) {
_path = path.until("room");
2022-02-22 11:41:36 +05:30
_path = _path.with(segments[i])!;
_path = _path.with(segments[i + 1])!;
}
return _path;
}
2022-02-22 11:41:36 +05:30
export function parseUrlPath(urlPath: string, currentNavPath: Path<SegmentType>, defaultSessionId: string): Segment<SegmentType>[] {
2020-10-12 21:19:06 +05:30
// substr(1) to take of initial /
2022-02-22 11:43:35 +05:30
const parts = urlPath.substring(1).split("/");
2020-10-12 21:19:06 +05:30
const iterator = parts[Symbol.iterator]();
2022-02-22 11:41:36 +05:30
const segments: Segment<SegmentType>[] = [];
2020-10-12 21:19:06 +05:30
let next;
while (!(next = iterator.next()).done) {
const type = next.value;
if (type === "rooms") {
const roomsValue = iterator.next().value;
if (roomsValue === undefined) { break; }
2020-10-12 21:19:06 +05:30
const roomIds = roomsValue.split(",");
segments.push(new Segment(type, roomIds));
const selectedIndex = parseInt(iterator.next().value || "0", 10);
const roomId = roomIds[selectedIndex];
if (roomId) {
segments.push(new Segment("room", roomId));
} else {
segments.push(new Segment("empty-grid-tile", selectedIndex));
}
} else if (type === "open-room") {
const roomId = iterator.next().value;
if (!roomId) { break; }
const rooms = currentNavPath.get("rooms");
if (rooms) {
segments.push(roomsSegmentWithRoom(rooms, roomId, currentNavPath));
}
segments.push(new Segment("room", roomId));
const openRoomPartIndex = parts.findIndex(part => part === "open-room");
const hasOnlyRoomIdAfterPart = openRoomPartIndex >= parts.length - 2;
if (hasOnlyRoomIdAfterPart) {
// Copy right-panel segments from previous path only if there are no other parts after open-room
// fixes memberlist -> member details closing/opening grid view
const previousSegments = currentNavPath.segments;
const i = previousSegments.findIndex(s => s.type === "right-panel");
if (i !== -1) {
segments.push(...previousSegments.slice(i));
}
}
} else if (type === "last-session") {
let sessionSegment = currentNavPath.get("session");
if (typeof sessionSegment?.value !== "string" && defaultSessionId) {
sessionSegment = new Segment("session", defaultSessionId);
}
if (sessionSegment) {
segments.push(sessionSegment);
}
} else if (type === "details" || type === "members") {
pushRightPanelSegment(segments, type);
} else if (type === "member") {
const userId = iterator.next().value;
if (!userId) { break; }
pushRightPanelSegment(segments, type, userId);
} else if (type.includes("loginToken")) {
// Special case for SSO-login with query parameter loginToken=<token>
const loginToken = type.split("=").pop();
segments.push(new Segment("sso", loginToken));
2020-10-09 23:13:11 +05:30
} else {
2020-10-12 21:19:06 +05:30
// might be undefined, which will be turned into true by Segment
const value = iterator.next().value;
segments.push(new Segment(type, value));
}
}
return segments;
}
2022-02-22 11:41:36 +05:30
export function stringifyPath(path: Path<SegmentType>): string {
2020-10-12 21:19:06 +05:30
let urlPath = "";
2022-02-22 11:41:36 +05:30
let prevSegment: Segment<SegmentType> | undefined;
2020-10-12 21:19:06 +05:30
for (const segment of path.segments) {
switch (segment.type) {
case "rooms":
urlPath += `/rooms/${segment.value.join(",")}`;
break;
case "empty-grid-tile":
urlPath += `/${segment.value}`;
break;
case "room":
if (prevSegment?.type === "rooms") {
const index = prevSegment.value.indexOf(segment.value);
urlPath += `/${index}`;
} else {
urlPath += `/${segment.type}/${segment.value}`;
}
break;
case "right-panel":
case "sso":
// Do not put these segments in URL
continue;
2020-10-12 21:19:06 +05:30
default:
urlPath += `/${segment.type}`;
if (segment.value && segment.value !== true) {
urlPath += `/${segment.value}`;
}
}
prevSegment = segment;
}
return urlPath;
}
export function tests() {
2022-02-22 11:41:36 +05:30
function createEmptyPath() {
const nav: Navigation<SegmentType> = new Navigation(allowsChild);
const path = nav.pathFrom([]);
return path;
}
2020-10-12 21:19:06 +05:30
return {
"stringify grid url with focused empty tile": assert => {
2022-02-22 11:41:36 +05:30
const nav: Navigation<SegmentType> = new Navigation(allowsChild);
2020-10-12 21:19:06 +05:30
const path = nav.pathFrom([
new Segment("session", 1),
new Segment("rooms", ["a", "b", "c"]),
new Segment("empty-grid-tile", 3)
]);
const urlPath = stringifyPath(path);
assert.equal(urlPath, "/session/1/rooms/a,b,c/3");
},
"stringify grid url with focused room": assert => {
2022-02-22 11:41:36 +05:30
const nav: Navigation<SegmentType> = new Navigation(allowsChild);
2020-10-12 21:19:06 +05:30
const path = nav.pathFrom([
new Segment("session", 1),
new Segment("rooms", ["a", "b", "c"]),
new Segment("room", "b")
]);
const urlPath = stringifyPath(path);
assert.equal(urlPath, "/session/1/rooms/a,b,c/1");
},
"stringify url with right-panel and details segment": assert => {
2022-02-22 11:41:36 +05:30
const nav: Navigation<SegmentType> = new Navigation(allowsChild);
const path = nav.pathFrom([
new Segment("session", 1),
new Segment("rooms", ["a", "b", "c"]),
new Segment("room", "b"),
new Segment("right-panel"),
new Segment("details")
]);
const urlPath = stringifyPath(path);
assert.equal(urlPath, "/session/1/rooms/a,b,c/1/details");
},
"Parse loginToken query parameter into SSO segment": assert => {
2022-02-22 11:41:36 +05:30
const path = createEmptyPath();
const segments = parseUrlPath("?loginToken=a1232aSD123", path, "");
assert.equal(segments.length, 1);
assert.equal(segments[0].type, "sso");
assert.equal(segments[0].value, "a1232aSD123");
},
2020-10-12 21:19:06 +05:30
"parse grid url path with focused empty tile": assert => {
2022-02-22 11:41:36 +05:30
const path = createEmptyPath();
const segments = parseUrlPath("/session/1/rooms/a,b,c/3", path, "");
2020-10-12 21:19:06 +05:30
assert.equal(segments.length, 3);
assert.equal(segments[0].type, "session");
assert.equal(segments[0].value, "1");
assert.equal(segments[1].type, "rooms");
assert.deepEqual(segments[1].value, ["a", "b", "c"]);
assert.equal(segments[2].type, "empty-grid-tile");
assert.equal(segments[2].value, 3);
},
"parse grid url path with focused room": assert => {
2022-02-22 11:41:36 +05:30
const path = createEmptyPath();
const segments = parseUrlPath("/session/1/rooms/a,b,c/1", path, "");
2020-10-12 21:19:06 +05:30
assert.equal(segments.length, 3);
assert.equal(segments[0].type, "session");
assert.equal(segments[0].value, "1");
assert.equal(segments[1].type, "rooms");
assert.deepEqual(segments[1].value, ["a", "b", "c"]);
assert.equal(segments[2].type, "room");
assert.equal(segments[2].value, "b");
},
"parse empty grid url": assert => {
2022-02-22 11:41:36 +05:30
const path = createEmptyPath();
const segments = parseUrlPath("/session/1/rooms/", path, "");
assert.equal(segments.length, 3);
assert.equal(segments[0].type, "session");
assert.equal(segments[0].value, "1");
assert.equal(segments[1].type, "rooms");
assert.deepEqual(segments[1].value, [""]);
assert.equal(segments[2].type, "empty-grid-tile");
assert.equal(segments[2].value, 0);
},
"parse empty grid url with focus": assert => {
2022-02-22 11:41:36 +05:30
const path = createEmptyPath();
const segments = parseUrlPath("/session/1/rooms//1", path, "");
assert.equal(segments.length, 3);
assert.equal(segments[0].type, "session");
assert.equal(segments[0].value, "1");
assert.equal(segments[1].type, "rooms");
assert.deepEqual(segments[1].value, [""]);
assert.equal(segments[2].type, "empty-grid-tile");
assert.equal(segments[2].value, 1);
},
2020-10-12 21:19:06 +05:30
"parse open-room action replacing the current focused room": assert => {
2022-02-22 11:41:36 +05:30
const nav: Navigation<SegmentType> = new Navigation(allowsChild);
2020-10-12 21:19:06 +05:30
const path = nav.pathFrom([
new Segment("session", 1),
new Segment("rooms", ["a", "b", "c"]),
new Segment("room", "b")
]);
2022-02-22 11:41:36 +05:30
const segments = parseUrlPath("/session/1/open-room/d", path, "");
2020-10-12 21:19:06 +05:30
assert.equal(segments.length, 3);
assert.equal(segments[0].type, "session");
assert.equal(segments[0].value, "1");
assert.equal(segments[1].type, "rooms");
assert.deepEqual(segments[1].value, ["a", "d", "c"]);
assert.equal(segments[2].type, "room");
assert.equal(segments[2].value, "d");
},
2020-10-13 16:43:47 +05:30
"parse open-room action changing focus to an existing room": assert => {
2022-02-22 11:41:36 +05:30
const nav: Navigation<SegmentType> = new Navigation(allowsChild);
2020-10-13 16:43:47 +05:30
const path = nav.pathFrom([
new Segment("session", 1),
new Segment("rooms", ["a", "b", "c"]),
new Segment("room", "b")
]);
2022-02-22 11:41:36 +05:30
const segments = parseUrlPath("/session/1/open-room/a", path, "");
2020-10-13 16:43:47 +05:30
assert.equal(segments.length, 3);
assert.equal(segments[0].type, "session");
assert.equal(segments[0].value, "1");
assert.equal(segments[1].type, "rooms");
assert.deepEqual(segments[1].value, ["a", "b", "c"]);
assert.equal(segments[2].type, "room");
assert.equal(segments[2].value, "a");
},
"parse open-room action changing focus to an existing room with details open": assert => {
2022-02-22 11:41:36 +05:30
const nav: Navigation<SegmentType> = new Navigation(allowsChild);
const path = nav.pathFrom([
new Segment("session", 1),
new Segment("rooms", ["a", "b", "c"]),
new Segment("room", "b"),
new Segment("right-panel", true),
new Segment("details", true)
]);
2022-02-22 11:41:36 +05:30
const segments = parseUrlPath("/session/1/open-room/a", path, "");
assert.equal(segments.length, 5);
assert.equal(segments[0].type, "session");
assert.equal(segments[0].value, "1");
assert.equal(segments[1].type, "rooms");
assert.deepEqual(segments[1].value, ["a", "b", "c"]);
assert.equal(segments[2].type, "room");
assert.equal(segments[2].value, "a");
assert.equal(segments[3].type, "right-panel");
assert.equal(segments[3].value, true);
assert.equal(segments[4].type, "details");
assert.equal(segments[4].value, true);
},
"open-room action should only copy over previous segments if there are no parts after open-room": assert => {
2022-02-22 11:41:36 +05:30
const nav: Navigation<SegmentType> = new Navigation(allowsChild);
const path = nav.pathFrom([
new Segment("session", 1),
new Segment("rooms", ["a", "b", "c"]),
new Segment("room", "b"),
new Segment("right-panel", true),
new Segment("members", true)
]);
2022-02-22 11:41:36 +05:30
const segments = parseUrlPath("/session/1/open-room/a/member/foo", path, "");
assert.equal(segments.length, 5);
assert.equal(segments[0].type, "session");
assert.equal(segments[0].value, "1");
assert.equal(segments[1].type, "rooms");
assert.deepEqual(segments[1].value, ["a", "b", "c"]);
assert.equal(segments[2].type, "room");
assert.equal(segments[2].value, "a");
assert.equal(segments[3].type, "right-panel");
assert.equal(segments[3].value, true);
assert.equal(segments[4].type, "member");
assert.equal(segments[4].value, "foo");
},
2020-10-12 21:19:06 +05:30
"parse open-room action setting a room in an empty tile": assert => {
2022-02-22 11:41:36 +05:30
const nav: Navigation<SegmentType> = new Navigation(allowsChild);
2020-10-12 21:19:06 +05:30
const path = nav.pathFrom([
new Segment("session", 1),
new Segment("rooms", ["a", "b", "c"]),
new Segment("empty-grid-tile", 4)
]);
2022-02-22 11:41:36 +05:30
const segments = parseUrlPath("/session/1/open-room/d", path, "");
2020-10-12 21:19:06 +05:30
assert.equal(segments.length, 3);
assert.equal(segments[0].type, "session");
assert.equal(segments[0].value, "1");
assert.equal(segments[1].type, "rooms");
assert.deepEqual(segments[1].value, ["a", "b", "c", , "d"]); //eslint-disable-line no-sparse-arrays
assert.equal(segments[2].type, "room");
assert.equal(segments[2].value, "d");
},
"parse session url path without id": assert => {
2022-02-22 11:41:36 +05:30
const path = createEmptyPath();
const segments = parseUrlPath("/session", path, "");
2020-10-12 21:19:06 +05:30
assert.equal(segments.length, 1);
assert.equal(segments[0].type, "session");
assert.strictEqual(segments[0].value, true);
},
"remove active room from grid path turns it into empty tile": assert => {
2022-02-22 11:41:36 +05:30
const nav: Navigation<SegmentType> = new Navigation(allowsChild);
const path = nav.pathFrom([
new Segment("session", 1),
new Segment("rooms", ["a", "b", "c"]),
new Segment("room", "b")
]);
const newPath = removeRoomFromPath(path, "b");
2022-02-22 11:41:36 +05:30
assert.equal(newPath?.segments.length, 3);
assert.equal(newPath?.segments[0].type, "session");
assert.equal(newPath?.segments[0].value, 1);
assert.equal(newPath?.segments[1].type, "rooms");
assert.deepEqual(newPath?.segments[1].value, ["a", "", "c"]);
assert.equal(newPath?.segments[2].type, "empty-grid-tile");
assert.equal(newPath?.segments[2].value, 1);
},
"remove inactive room from grid path": assert => {
2022-02-22 11:41:36 +05:30
const nav: Navigation<SegmentType> = new Navigation(allowsChild);
const path = nav.pathFrom([
new Segment("session", 1),
new Segment("rooms", ["a", "b", "c"]),
new Segment("room", "b")
]);
const newPath = removeRoomFromPath(path, "a");
2022-02-22 11:41:36 +05:30
assert.equal(newPath?.segments.length, 3);
assert.equal(newPath?.segments[0].type, "session");
assert.equal(newPath?.segments[0].value, 1);
assert.equal(newPath?.segments[1].type, "rooms");
assert.deepEqual(newPath?.segments[1].value, ["", "b", "c"]);
assert.equal(newPath?.segments[2].type, "room");
assert.equal(newPath?.segments[2].value, "b");
},
"remove inactive room from grid path with empty tile": assert => {
2022-02-22 11:41:36 +05:30
const nav: Navigation<SegmentType> = new Navigation(allowsChild);
const path = nav.pathFrom([
new Segment("session", 1),
new Segment("rooms", ["a", "b", ""]),
new Segment("empty-grid-tile", 3)
]);
const newPath = removeRoomFromPath(path, "b");
2022-02-22 11:41:36 +05:30
assert.equal(newPath?.segments.length, 3);
assert.equal(newPath?.segments[0].type, "session");
assert.equal(newPath?.segments[0].value, 1);
assert.equal(newPath?.segments[1].type, "rooms");
assert.deepEqual(newPath?.segments[1].value, ["a", "", ""]);
assert.equal(newPath?.segments[2].type, "empty-grid-tile");
assert.equal(newPath?.segments[2].value, 3);
},
"remove active room": assert => {
2022-02-22 11:41:36 +05:30
const nav: Navigation<SegmentType> = new Navigation(allowsChild);
const path = nav.pathFrom([
new Segment("session", 1),
new Segment("room", "b")
]);
const newPath = removeRoomFromPath(path, "b");
2022-02-22 11:41:36 +05:30
assert.equal(newPath?.segments.length, 1);
assert.equal(newPath?.segments[0].type, "session");
assert.equal(newPath?.segments[0].value, 1);
},
"remove inactive room doesn't do anything": assert => {
2022-02-22 11:41:36 +05:30
const nav: Navigation<SegmentType> = new Navigation(allowsChild);
const path = nav.pathFrom([
new Segment("session", 1),
new Segment("room", "b")
]);
const newPath = removeRoomFromPath(path, "a");
2022-02-22 11:41:36 +05:30
assert.equal(newPath?.segments.length, 2);
assert.equal(newPath?.segments[0].type, "session");
assert.equal(newPath?.segments[0].value, 1);
assert.equal(newPath?.segments[1].type, "room");
assert.equal(newPath?.segments[1].value, "b");
},
2020-10-09 23:13:11 +05:30
}
}