This commit is contained in:
RMidhunSuresh 2022-05-27 22:38:53 +05:30
parent ec1cc89cf9
commit fc873757d8
4 changed files with 29 additions and 16 deletions

View File

@ -16,10 +16,11 @@ limitations under the License.
import {Options, ViewModel} from "./ViewModel"; import {Options, ViewModel} from "./ViewModel";
import {Client} from "../matrix/Client.js"; import {Client} from "../matrix/Client.js";
import {SegmentType} from "./navigation/index";
type LogoutOptions = { sessionId: string; } & Options; type LogoutOptions = { sessionId: string; } & Options;
export class LogoutViewModel extends ViewModel<LogoutOptions> { export class LogoutViewModel extends ViewModel<SegmentType, LogoutOptions> {
private _sessionId: string; private _sessionId: string;
private _busy: boolean; private _busy: boolean;
private _showConfirm: boolean; private _showConfirm: boolean;
@ -41,7 +42,7 @@ export class LogoutViewModel extends ViewModel<LogoutOptions> {
return this._busy; return this._busy;
} }
get cancelUrl(): string { get cancelUrl(): string | undefined {
return this.urlCreator.urlForSegment("session", true); return this.urlCreator.urlForSegment("session", true);
} }

View File

@ -28,17 +28,16 @@ import type {Clock} from "../platform/web/dom/Clock";
import type {ILogger} from "../logging/types"; import type {ILogger} from "../logging/types";
import type {Navigation} from "./navigation/Navigation"; import type {Navigation} from "./navigation/Navigation";
import type {SegmentType} from "./navigation/index"; import type {SegmentType} from "./navigation/index";
import type {URLRouter} from "./navigation/URLRouter"; import type {IURLRouter} from "./navigation/URLRouter";
type OptionsWithoutUrlCreator<N extends object> = { export type Options<T extends object = SegmentType> = {
platform: Platform platform: Platform;
logger: ILogger logger: ILogger;
navigation: Navigation<N> urlCreator: IURLRouter<T>;
emitChange?: (params: any) => void navigation: Navigation<T>;
emitChange?: (params: any) => void;
} }
type OptionsWithUrlCreator<N extends { session: string }> = OptionsWithoutUrlCreator<N> & {urlCreator: URLRouter<N>};
type Options<N extends object> = N extends { session: string } ? OptionsWithUrlCreator<N> : OptionsWithoutUrlCreator<N>;
export class ViewModel<N extends object = SegmentType, O extends Options<N> = Options<N>> extends EventEmitter<{change: never}> { export class ViewModel<N extends object = SegmentType, O extends Options<N> = Options<N>> extends EventEmitter<{change: never}> {
private disposables?: Disposables; private disposables?: Disposables;
@ -138,9 +137,8 @@ export class ViewModel<N extends object = SegmentType, O extends Options<N> = Op
return this.platform.logger; return this.platform.logger;
} }
get urlCreator(): N extends { session: string }? URLRouter<N>: undefined { get urlCreator(): IURLRouter<N> {
// typescript needs a little help here return this._options.urlCreator;
return (this._options as unknown as {urlCreator: any}).urlCreator;
} }
get navigation(): Navigation<N> { get navigation(): Navigation<N> {

View File

@ -21,7 +21,21 @@ import type {SubscriptionHandle} from "../../observable/BaseObservable";
type ParseURLPath<T> = (urlPath: string, currentNavPath: Path<T>, defaultSessionId?: string) => Segment<T>[]; type ParseURLPath<T> = (urlPath: string, currentNavPath: Path<T>, defaultSessionId?: string) => Segment<T>[];
type StringifyPath<T> = (path: Path<T>) => string; type StringifyPath<T> = (path: Path<T>) => string;
export class URLRouter<T extends {session: string}> { export interface IURLRouter<T> {
attach(): void;
dispose(): void;
pushUrl(url: string): void;
tryRestoreLastUrl(): boolean;
urlForSegments(segments: Segment<T>[]): string | undefined;
urlForSegment<K extends keyof T>(type: K, ...value: OptionalValue<T[K]>): string | undefined;
urlUntilSegment(type: keyof T): string;
urlForPath(path: Path<T>): string;
openRoomActionUrl(roomId: string): string;
createSSOCallbackURL(): string;
normalizeUrl(): void;
}
export class URLRouter<T extends {session: string | boolean}> implements IURLRouter<T> {
private readonly _history: History; private readonly _history: History;
private readonly _navigation: Navigation<T>; private readonly _navigation: Navigation<T>;
private readonly _parseUrlPath: ParseURLPath<T>; private readonly _parseUrlPath: ParseURLPath<T>;
@ -128,7 +142,7 @@ export class URLRouter<T extends {session: string}> {
return this._history.pathAsUrl(this._stringifyPath(path)); 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 // not a segment to navigation knowns about, so append it manually
const urlPath = `${this._stringifyPath(this._navigation.path.until("session"))}/open-room/${roomId}`; const urlPath = `${this._stringifyPath(this._navigation.path.until("session"))}/open-room/${roomId}`;
return this._history.pathAsUrl(urlPath); return this._history.pathAsUrl(urlPath);

View File

@ -20,7 +20,7 @@ import type {Path, OptionalValue} from "./Navigation";
export type SegmentType = { export type SegmentType = {
"login": true; "login": true;
"session": string; "session": string | boolean;
"sso": string; "sso": string;
"logout": true; "logout": true;
"room": string; "room": string;