|
| 1 | +/* |
| 2 | + * Copyright 2025 New Vector Ltd. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial |
| 5 | + * Please see LICENSE files in the repository root for full details. |
| 6 | + */ |
| 7 | + |
| 8 | +import type React from "react"; |
| 9 | +import { ViewModelSubscriptions } from "../ViewModelSubscriptions"; |
| 10 | +import SpaceStore from "../../stores/spaces/SpaceStore"; |
| 11 | +import { MetaSpace, type SpaceKey, UPDATE_SELECTED_SPACE } from "../../stores/spaces"; |
| 12 | +import { |
| 13 | + type RoomListSearchSnapshot, |
| 14 | + type RoomListSearchViewModel as RoomListSearchViewModelType, |
| 15 | +} from "../../shared-components/room-list/RoomListSearch"; |
| 16 | +import { shouldShowComponent } from "../../customisations/helpers/UIComponents"; |
| 17 | +import { UIComponent } from "../../settings/UIFeature"; |
| 18 | +import defaultDispatcher from "../../dispatcher/dispatcher"; |
| 19 | +import { Action } from "../../dispatcher/actions"; |
| 20 | +import PosthogTrackers from "../../PosthogTrackers"; |
| 21 | +import LegacyCallHandler, { LegacyCallHandlerEvent } from "../../LegacyCallHandler"; |
| 22 | + |
| 23 | +/** |
| 24 | + * ViewModel for the RoomListSearch component. |
| 25 | + */ |
| 26 | +export class RoomListSearchViewModel implements RoomListSearchViewModelType { |
| 27 | + private subs: ViewModelSubscriptions; |
| 28 | + |
| 29 | + /** |
| 30 | + * The current snapshot of the RoomListSearch state. |
| 31 | + * It contains flags to determine whether to display the dial button and explore button. |
| 32 | + */ |
| 33 | + private snapshot = { |
| 34 | + displayDialButton: false, |
| 35 | + displayExploreButton: false, |
| 36 | + }; |
| 37 | + |
| 38 | + /** |
| 39 | + * Creates an instance of RoomListSearchViewModel. |
| 40 | + * It listens to SpaceStore and LegacyCallHandler events to update the snapshot. |
| 41 | + */ |
| 42 | + public constructor() { |
| 43 | + this.subs = new ViewModelSubscriptions(this.updateSubscription); |
| 44 | + } |
| 45 | + |
| 46 | + private updateSubscription = (): void => { |
| 47 | + // Put the listener on the first time the subscription is called |
| 48 | + console.log("this.listeners.size", this.subs.listeners.size); |
| 49 | + if (this.subs.listeners.size > 0 && this.subs.listeners.size < 2) { |
| 50 | + SpaceStore.instance.on(UPDATE_SELECTED_SPACE, this.updateDisplayExploreButton); |
| 51 | + LegacyCallHandler.instance.on(LegacyCallHandlerEvent.ProtocolSupport, this.updateSupportPstn); |
| 52 | + |
| 53 | + this.updateDisplayExploreButton(SpaceStore.instance.activeSpace); |
| 54 | + this.updateSupportPstn(); |
| 55 | + } else { |
| 56 | + SpaceStore.instance.off(UPDATE_SELECTED_SPACE, this.updateDisplayExploreButton); |
| 57 | + LegacyCallHandler.instance.off(LegacyCallHandlerEvent.ProtocolSupport, this.updateSupportPstn); |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + /** |
| 62 | + * Update the displayExploreButton flag based on the active space. |
| 63 | + */ |
| 64 | + private updateDisplayExploreButton = (activeSpace: SpaceKey): void => { |
| 65 | + this.snapshot.displayExploreButton = |
| 66 | + activeSpace === MetaSpace.Home && shouldShowComponent(UIComponent.ExploreRooms); |
| 67 | + this.subs.emit(); |
| 68 | + }; |
| 69 | + |
| 70 | + /** |
| 71 | + * Update the displayDialButton flag based on whether the PTSN protocol is supported. |
| 72 | + */ |
| 73 | + private updateSupportPstn = (): void => { |
| 74 | + this.snapshot.displayDialButton = LegacyCallHandler.instance.getSupportsPstnProtocol(); |
| 75 | + this.subs.emit(); |
| 76 | + }; |
| 77 | + |
| 78 | + public subscribe = (listener: () => void): (() => void) => { |
| 79 | + console.log("subscribing to RoomListSearchViewModel"); |
| 80 | + return this.subs.subscribe(listener); |
| 81 | + }; |
| 82 | + |
| 83 | + public getSnapshot = (): RoomListSearchSnapshot => { |
| 84 | + return this.snapshot; |
| 85 | + }; |
| 86 | + |
| 87 | + /** |
| 88 | + * Open the dial pad when called. |
| 89 | + */ |
| 90 | + public onDialPadClick(): void { |
| 91 | + defaultDispatcher.fire(Action.OpenDialPad); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Open the room directory when called. |
| 96 | + */ |
| 97 | + public onExploreClick(evt: React.MouseEvent<HTMLButtonElement>): void { |
| 98 | + defaultDispatcher.fire(Action.ViewRoomDirectory); |
| 99 | + PosthogTrackers.trackInteraction("WebLeftPanelExploreRoomsButton", evt); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Open the spotlight when called. |
| 104 | + */ |
| 105 | + public onSearchClick(): void { |
| 106 | + defaultDispatcher.fire(Action.OpenSpotlight); |
| 107 | + } |
| 108 | +} |
0 commit comments