From fc873757d80c264b9f01995029bfcda093f69ed7 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Fri, 27 May 2022 22:38:53 +0530 Subject: [PATCH] WIP --- src/domain/LogoutViewModel.ts | 5 +++-- src/domain/ViewModel.ts | 20 +++++++++----------- src/domain/navigation/URLRouter.ts | 18 ++++++++++++++++-- src/domain/navigation/index.ts | 2 +- 4 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/domain/LogoutViewModel.ts b/src/domain/LogoutViewModel.ts index 3edfcad5..9a39f601 100644 --- a/src/domain/LogoutViewModel.ts +++ b/src/domain/LogoutViewModel.ts @@ -16,10 +16,11 @@ limitations under the License. import {Options, ViewModel} from "./ViewModel"; import {Client} from "../matrix/Client.js"; +import {SegmentType} from "./navigation/index"; type LogoutOptions = { sessionId: string; } & Options; -export class LogoutViewModel extends ViewModel { +export class LogoutViewModel extends ViewModel { private _sessionId: string; private _busy: boolean; private _showConfirm: boolean; @@ -41,7 +42,7 @@ export class LogoutViewModel extends ViewModel { return this._busy; } - get cancelUrl(): string { + get cancelUrl(): string | undefined { return this.urlCreator.urlForSegment("session", true); } diff --git a/src/domain/ViewModel.ts b/src/domain/ViewModel.ts index 7442d921..64db4266 100644 --- a/src/domain/ViewModel.ts +++ b/src/domain/ViewModel.ts @@ -28,17 +28,16 @@ import type {Clock} from "../platform/web/dom/Clock"; import type {ILogger} from "../logging/types"; import type {Navigation} from "./navigation/Navigation"; import type {SegmentType} from "./navigation/index"; -import type {URLRouter} from "./navigation/URLRouter"; +import type {IURLRouter} from "./navigation/URLRouter"; -type OptionsWithoutUrlCreator = { - platform: Platform - logger: ILogger - navigation: Navigation - emitChange?: (params: any) => void +export type Options = { + platform: Platform; + logger: ILogger; + urlCreator: IURLRouter; + navigation: Navigation; + emitChange?: (params: any) => void; } -type OptionsWithUrlCreator = OptionsWithoutUrlCreator & {urlCreator: URLRouter}; -type Options = N extends { session: string } ? OptionsWithUrlCreator : OptionsWithoutUrlCreator; export class ViewModel = Options> extends EventEmitter<{change: never}> { private disposables?: Disposables; @@ -138,9 +137,8 @@ export class ViewModel = Op return this.platform.logger; } - get urlCreator(): N extends { session: string }? URLRouter: undefined { - // typescript needs a little help here - return (this._options as unknown as {urlCreator: any}).urlCreator; + get urlCreator(): IURLRouter { + return this._options.urlCreator; } get navigation(): Navigation { diff --git a/src/domain/navigation/URLRouter.ts b/src/domain/navigation/URLRouter.ts index 31dffe58..923c9b43 100644 --- a/src/domain/navigation/URLRouter.ts +++ b/src/domain/navigation/URLRouter.ts @@ -21,7 +21,21 @@ import type {SubscriptionHandle} from "../../observable/BaseObservable"; type ParseURLPath = (urlPath: string, currentNavPath: Path, defaultSessionId?: string) => Segment[]; type StringifyPath = (path: Path) => string; -export class URLRouter { +export interface IURLRouter { + attach(): void; + dispose(): void; + pushUrl(url: string): void; + tryRestoreLastUrl(): boolean; + urlForSegments(segments: Segment[]): string | undefined; + urlForSegment(type: K, ...value: OptionalValue): string | undefined; + urlUntilSegment(type: keyof T): string; + urlForPath(path: Path): string; + openRoomActionUrl(roomId: string): string; + createSSOCallbackURL(): string; + normalizeUrl(): void; +} + +export class URLRouter implements IURLRouter { private readonly _history: History; private readonly _navigation: Navigation; private readonly _parseUrlPath: ParseURLPath; @@ -128,7 +142,7 @@ export class URLRouter { return this._history.pathAsUrl(this._stringifyPath(path)); } - openRoomActionUrl(roomId: string) { + openRoomActionUrl(roomId: string): string { // 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); diff --git a/src/domain/navigation/index.ts b/src/domain/navigation/index.ts index f739b668..afba0d86 100644 --- a/src/domain/navigation/index.ts +++ b/src/domain/navigation/index.ts @@ -20,7 +20,7 @@ import type {Path, OptionalValue} from "./Navigation"; export type SegmentType = { "login": true; - "session": string; + "session": string | boolean; "sso": string; "logout": true; "room": string;