Skip to content
Snippets Groups Projects
RootViewModel.ts 3.25 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { ViewModel, Client, Navigation, createRouter, Platform } from "hydrogen-view-sdk";
    
    Midhun Suresh's avatar
    Midhun Suresh committed
    import { IChatterboxConfig } from "../types/IChatterboxConfig";
    import { ChatterboxViewModel } from "./ChatterboxViewModel";
    import "hydrogen-view-sdk/style.css";
    import { AccountSetupViewModel } from "./AccountSetupViewModel";
    
    
    type Options = { platform: Platform, navigation: ReturnType<Navigation>, urlCreator: ReturnType<createRouter> };
    
    Midhun Suresh's avatar
    Midhun Suresh committed
    
    export class RootViewModel extends ViewModel {
        private _config: IChatterboxConfig;
        private _client: Client;
        private _chatterBoxViewModel?: ChatterboxViewModel;
        private _accountSetupViewModel?: AccountSetupViewModel;
        private _activeSection: string = "start";
    
        constructor(config: IChatterboxConfig, options: Options) {
            super(options);
            this._config = config;
            this._client = new Client(this.platform);
    
            this._setupNavigation();
        }
    
        private _setupNavigation() {
            this.navigation.observe("account-setup").subscribe(() => this._showAccountSetup());
    
            this.navigation.observe("timeline").subscribe((loginPromise) => this._showTimeline(loginPromise));
    
            this.navigation.observe("start").subscribe(() => this._showStartButton());
    
    Midhun Suresh's avatar
    Midhun Suresh committed
        }
    
        async start() {
            const sessionAlreadyExists = await this.attemptStartWithExistingSession();
            if (sessionAlreadyExists) {
    
                this.navigation.push("timeline");
    
    Midhun Suresh's avatar
    Midhun Suresh committed
                return;
            }
    
            this.navigation.push("account-setup");
    
    Midhun Suresh's avatar
    Midhun Suresh committed
        }
    
    
        private async _showTimeline(loginPromise: Promise<void>) {
    
    Midhun Suresh's avatar
    Midhun Suresh committed
            this._activeSection = "timeline";
    
    Midhun Suresh's avatar
    Midhun Suresh committed
            if (!this._chatterBoxViewModel) {
                this._chatterBoxViewModel = this.track(new ChatterboxViewModel(
                    this.childOptions({
                        client: this._client,
                        config: this._config,
                        state: this._state,
                        loginPromise,
                    })
                ));
                this._chatterBoxViewModel.loadRoom();
            }
    
    Midhun Suresh's avatar
    Midhun Suresh committed
            this.emitChange("activeSection");
        }
    
        private _showAccountSetup() {
            this._activeSection = "account-setup";
    
    Midhun Suresh's avatar
    Midhun Suresh committed
            this._accountSetupViewModel = this.track(new AccountSetupViewModel(
    
    Midhun Suresh's avatar
    Midhun Suresh committed
                this.childOptions({
                    client: this._client,
                    config: this._config,
                    state: this._state,
                })
    
    Midhun Suresh's avatar
    Midhun Suresh committed
            ));
    
    Midhun Suresh's avatar
    Midhun Suresh committed
            this.emitChange("activeSection");
        }
    
    
    Midhun Suresh's avatar
    Midhun Suresh committed
        private _showStartButton() {
            this._activeSection = "start";
            this.emitChange("activeSection");
        }
    
    
    Midhun Suresh's avatar
    Midhun Suresh committed
        /**
         * Try to start Hydrogen based on an existing hydrogen session.
         * If multiple sessions exist, this method chooses the most recent one.
         */
        async attemptStartWithExistingSession(): Promise<boolean> {
            const sessionIds = await this.platform.sessionInfoStorage.getAll();
            const session = sessionIds.pop();
            if (session) {
                const { id } = session;
                await this._client.startWithExistingSession(id);
                return true;
            }
            return false;
        }
    
        get chatterboxViewModel() {
            return this._chatterBoxViewModel;
        }
    
        get accountSetupViewModel() {
            return this._accountSetupViewModel;
        }
    
        get activeSection() {
            return this._activeSection;
        }
    }