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

148 lines
5.4 KiB
TypeScript
Raw Normal View History

2020-10-06 21:33:12 +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-22 13:14:27 +05:30
import type {History} from "../../platform/web/dom/History.js";
import type {Navigation, Segment, Path, OptionalValue} from "./Navigation";
import type {SubscriptionHandle} from "../../observable/BaseObservable";
import type {SegmentType} from "./index";
2022-02-22 13:26:59 +05:30
type ParseURLPath<T> = (urlPath: string, currentNavPath: Path<T>, defaultSessionId?: string) => Segment<T>[];
2022-02-22 13:14:27 +05:30
type StringifyPath<T> = (path: Path<T>) => string;
export class URLRouter<T extends SegmentType> {
private readonly _history: History;
private readonly _navigation: Navigation<T>;
private readonly _parseUrlPath: ParseURLPath<T>;
private readonly _stringifyPath: StringifyPath<T>;
private _subscription?: SubscriptionHandle;
private _pathSubscription?: SubscriptionHandle;
private _isApplyingUrl: boolean = false;
2022-02-22 13:26:59 +05:30
private _defaultSessionId?: string;
2022-02-22 13:14:27 +05:30
constructor(history: History, navigation: Navigation<T>, parseUrlPath: ParseURLPath<T>, stringifyPath: StringifyPath<T>) {
this._history = history;
2020-10-06 21:33:12 +05:30
this._navigation = navigation;
2020-10-12 21:19:06 +05:30
this._parseUrlPath = parseUrlPath;
this._stringifyPath = stringifyPath;
this._defaultSessionId = this._getLastSessionId();
}
2022-02-22 16:24:12 +05:30
private _getLastSessionId(): string | undefined {
const navPath = this._urlAsNavPath(this._history.getLastUrl() || "");
const sessionId = navPath.get("session")?.value;
if (typeof sessionId === "string") {
return sessionId;
}
2022-02-22 13:26:59 +05:30
return undefined;
2020-10-06 21:33:12 +05:30
}
2022-02-22 13:14:27 +05:30
attach(): void {
this._subscription = this._history.subscribe(url => this._applyUrl(url));
// subscribe to path before applying initial url
// so redirects in _applyNavPathToHistory are reflected in url bar
this._pathSubscription = this._navigation.pathObservable.subscribe(path => this._applyNavPathToHistory(path));
this._applyUrl(this._history.get());
2020-10-07 13:10:51 +05:30
}
2022-02-22 13:14:27 +05:30
dispose(): void {
if (this._subscription) { this._subscription = this._subscription(); }
if (this._pathSubscription) { this._pathSubscription = this._pathSubscription(); }
2020-10-06 21:33:12 +05:30
}
2022-02-22 16:24:12 +05:30
private _applyNavPathToHistory(path: Path<T>): void {
const url = this.urlForPath(path);
if (url !== this._history.get()) {
if (this._isApplyingUrl) {
// redirect
this._history.replaceUrlSilently(url);
} else {
this._history.pushUrlSilently(url);
}
}
}
2022-02-22 16:24:12 +05:30
private _applyNavPathToNavigation(navPath: Path<T>): void {
// this will cause _applyNavPathToHistory to be called,
// so set a flag whether this request came from ourselves
// (in which case it is a redirect if the url does not match the current one)
this._isApplyingUrl = true;
2020-10-12 21:19:06 +05:30
this._navigation.applyPath(navPath);
this._isApplyingUrl = false;
2020-10-06 21:33:12 +05:30
}
2022-02-22 16:24:12 +05:30
private _urlAsNavPath(url: string): Path<T> {
const urlPath = this._history.urlAsPath(url);
return this._navigation.pathFrom(this._parseUrlPath(urlPath, this._navigation.path, this._defaultSessionId));
}
2022-02-22 16:24:12 +05:30
private _applyUrl(url: string): void {
const navPath = this._urlAsNavPath(url);
this._applyNavPathToNavigation(navPath);
}
2022-02-22 13:14:27 +05:30
pushUrl(url: string): void {
this._history.pushUrl(url);
}
2022-02-22 13:14:27 +05:30
tryRestoreLastUrl(): boolean {
const lastNavPath = this._urlAsNavPath(this._history.getLastUrl() || "");
if (lastNavPath.segments.length !== 0) {
this._applyNavPathToNavigation(lastNavPath);
return true;
}
return false;
}
2022-02-22 13:14:27 +05:30
urlForSegments(segments: Segment<T>[]): string | undefined {
let path: Path<T> | null = this._navigation.path;
2020-10-12 21:19:06 +05:30
for (const segment of segments) {
path = path.with(segment);
if (!path) {
return;
}
2020-10-06 21:33:12 +05:30
}
2020-10-12 21:19:06 +05:30
return this.urlForPath(path);
}
2022-02-22 13:14:27 +05:30
urlForSegment<K extends keyof T>(type: K, ...value: OptionalValue<T[K]>): string | undefined {
return this.urlForSegments([this._navigation.segment(type, ...value)]);
2020-10-06 21:33:12 +05:30
}
2022-02-22 13:14:27 +05:30
urlUntilSegment(type: keyof T): string {
return this.urlForPath(this._navigation.path.until(type));
}
2022-02-22 13:14:27 +05:30
urlForPath(path: Path<T>): string {
return this._history.pathAsUrl(this._stringifyPath(path));
2020-10-12 21:19:06 +05:30
}
2022-02-22 13:14:27 +05:30
openRoomActionUrl(roomId: string) {
2020-10-12 21:19:06 +05:30
// not a segment to navigation knowns about, so append it manually
const urlPath = `${this._stringifyPath(this._navigation.path.until("session"))}/open-room/${roomId}`;
return this._history.pathAsUrl(urlPath);
2020-10-06 21:33:12 +05:30
}
2022-02-22 13:14:27 +05:30
createSSOCallbackURL(): string {
return window.location.origin;
}
2022-02-22 13:14:27 +05:30
normalizeUrl(): void {
// Remove any queryParameters from the URL
// Gets rid of the loginToken after SSO
this._history.replaceUrlSilently(`${window.location.origin}/${window.location.hash}`);
}
2020-10-06 21:33:12 +05:30
}