Experimental Module API Additions (#30863)
* Module API experiments * Move ResizerNotifier into SDKContext so we don't have to pass it into RoomView * Add the MultiRoomViewStore * Make RoomViewStore able to take a roomId prop * Different interface to add space panel items A bit less flexible but probably simpler and will help keep things actually consistent rather than just allowing modules to stick any JSX into the space panel (which means they also have to worry about styling if they *do* want it to be consistent). * Allow space panel items to be updated and manage which one is selected, allowing module "spaces" to be considered spaces * Remove fetchRoomFn from SpaceNotificationStore which didn't really seem to have any point as it was only called from one place * Switch to using module api via .instance * Fairly awful workaround to actually break the dependency nightmare * Add test for multiroomviewstore * add test * Make room names deterministic So the tests don't fail if you add other tests or run them individually * Add test for builtinsapi * Update module api * RVS is not needed as prop anymore Since it's passed through context * Add roomId to prop * Remove RoomViewStore from state This is now accessed through class field * Fix test * No need to pass RVS from LoggedInView * Add RoomContextType * Implement new builtins api * Add tests * Fix import * Fix circular dependency issue * Fix import * Add more tests * Improve comment * room-id is optional * Update license * Add implementation for AccountDataApi * Add implementation for Room * Add implementation for ClientApi * Create ClientApi in Api.ts * Write tests * Use nullish coalescing assignment * Implement openRoom in NavigationApi * Write tests * Add implementation for StoresApi * Write tests * Fix circular dependency * Add comments in lieu of type and fix else block * Change to class field --------- Co-authored-by: R Midhun Suresh <hi@midhun.dev>
This commit is contained in:
67
src/stores/MultiRoomViewStore.ts
Normal file
67
src/stores/MultiRoomViewStore.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
Copyright 2025 New Vector Ltd.
|
||||
|
||||
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
|
||||
Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
import { logger } from "matrix-js-sdk/src/logger";
|
||||
|
||||
import { RoomViewStore } from "./RoomViewStore";
|
||||
import { type MatrixDispatcher } from "../dispatcher/dispatcher";
|
||||
import { type SdkContextClass } from "../contexts/SDKContext";
|
||||
import { Action } from "../dispatcher/actions";
|
||||
|
||||
/**
|
||||
* Acts as a cache of many RoomViewStore instances, creating them as necessary
|
||||
* given a room ID.
|
||||
*/
|
||||
export class MultiRoomViewStore {
|
||||
/**
|
||||
* Map from room-id to RVS instance.
|
||||
*/
|
||||
private stores: Map<string, RoomViewStore> = new Map();
|
||||
|
||||
public constructor(
|
||||
private dispatcher: MatrixDispatcher,
|
||||
private sdkContextClass: SdkContextClass,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Get a RVS instance for the room identified by the given roomId.
|
||||
*/
|
||||
public getRoomViewStoreForRoom(roomId: string): RoomViewStore {
|
||||
// Get existing store / create new store
|
||||
const store = this.stores.has(roomId)
|
||||
? this.stores.get(roomId)!
|
||||
: new RoomViewStore(this.dispatcher, this.sdkContextClass, roomId);
|
||||
|
||||
// RoomView component does not render the room unless you call viewRoom
|
||||
store.viewRoom({
|
||||
action: Action.ViewRoom,
|
||||
room_id: roomId,
|
||||
metricsTrigger: undefined,
|
||||
});
|
||||
|
||||
// Cache the store, okay to do even if the store is already in the map
|
||||
this.stores.set(roomId, store);
|
||||
|
||||
return store;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a RVS instance that was created by {@link getRoomViewStoreForRoom}.
|
||||
*/
|
||||
public removeRoomViewStore(roomId: string): void {
|
||||
const didRemove = this.stores.delete(roomId);
|
||||
if (!didRemove) {
|
||||
logger.warn(`removeRoomViewStore called with ${roomId} but no store exists for this room.`);
|
||||
}
|
||||
}
|
||||
|
||||
public dispose(): void {
|
||||
for (const id of this.stores.keys()) {
|
||||
this.removeRoomViewStore(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -153,6 +153,7 @@ export class RoomViewStore extends EventEmitter {
|
||||
public constructor(
|
||||
dis: MatrixDispatcher,
|
||||
private readonly stores: SdkContextClass,
|
||||
private readonly lockedToRoomId?: string,
|
||||
) {
|
||||
super();
|
||||
this.resetDispatcher(dis);
|
||||
@@ -187,7 +188,7 @@ export class RoomViewStore extends EventEmitter {
|
||||
|
||||
const lastRoomId = this.state.roomId;
|
||||
this.state = Object.assign(this.state, newState);
|
||||
if (lastRoomId !== this.state.roomId) {
|
||||
if (!this.lockedToRoomId && lastRoomId !== this.state.roomId) {
|
||||
if (lastRoomId) this.emitForRoom(lastRoomId, false);
|
||||
if (this.state.roomId) this.emitForRoom(this.state.roomId, true);
|
||||
|
||||
@@ -204,6 +205,9 @@ export class RoomViewStore extends EventEmitter {
|
||||
}
|
||||
|
||||
private onDispatch(payload: ActionPayload): void {
|
||||
if (this.lockedToRoomId && payload.room_id && this.lockedToRoomId !== payload.room_id) {
|
||||
return;
|
||||
}
|
||||
// eslint-disable-line @typescript-eslint/naming-convention
|
||||
switch (payload.action) {
|
||||
// view_room:
|
||||
@@ -324,7 +328,7 @@ export class RoomViewStore extends EventEmitter {
|
||||
}
|
||||
}
|
||||
|
||||
private async viewRoom(payload: ViewRoomPayload): Promise<void> {
|
||||
public async viewRoom(payload: ViewRoomPayload): Promise<void> {
|
||||
if (payload.room_id) {
|
||||
const room = MatrixClientPeg.safeGet().getRoom(payload.room_id);
|
||||
|
||||
|
||||
@@ -12,15 +12,15 @@ import { NotificationLevel } from "./NotificationLevel";
|
||||
import { arrayDiff } from "../../utils/arrays";
|
||||
import { type RoomNotificationState } from "./RoomNotificationState";
|
||||
import { NotificationState, NotificationStateEvents } from "./NotificationState";
|
||||
import { type FetchRoomFn } from "./ListNotificationState";
|
||||
import { DefaultTagID } from "../room-list/models";
|
||||
import RoomListStore from "../room-list/RoomListStore";
|
||||
import { RoomNotificationStateStore } from "./RoomNotificationStateStore";
|
||||
|
||||
export class SpaceNotificationState extends NotificationState {
|
||||
public rooms: Room[] = []; // exposed only for tests
|
||||
private states: { [spaceId: string]: RoomNotificationState } = {};
|
||||
|
||||
public constructor(private getRoomFn: FetchRoomFn) {
|
||||
public constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ export class SpaceNotificationState extends NotificationState {
|
||||
state.off(NotificationStateEvents.Update, this.onRoomNotificationStateUpdate);
|
||||
}
|
||||
for (const newRoom of diff.added) {
|
||||
const state = this.getRoomFn(newRoom);
|
||||
const state = RoomNotificationStateStore.instance.getRoomState(newRoom);
|
||||
state.on(NotificationStateEvents.Update, this.onRoomNotificationStateUpdate);
|
||||
this.states[newRoom.roomId] = state;
|
||||
}
|
||||
|
||||
@@ -27,7 +27,6 @@ import defaultDispatcher from "../../dispatcher/dispatcher";
|
||||
import RoomListStore from "../room-list/RoomListStore";
|
||||
import SettingsStore from "../../settings/SettingsStore";
|
||||
import DMRoomMap from "../../utils/DMRoomMap";
|
||||
import { type FetchRoomFn } from "../notifications/ListNotificationState";
|
||||
import { SpaceNotificationState } from "../notifications/SpaceNotificationState";
|
||||
import { RoomNotificationStateStore } from "../notifications/RoomNotificationStateStore";
|
||||
import { DefaultTagID } from "../room-list/models";
|
||||
@@ -63,6 +62,7 @@ import { type ViewHomePagePayload } from "../../dispatcher/payloads/ViewHomePage
|
||||
import { type SwitchSpacePayload } from "../../dispatcher/payloads/SwitchSpacePayload";
|
||||
import { type AfterLeaveRoomPayload } from "../../dispatcher/payloads/AfterLeaveRoomPayload";
|
||||
import { SdkContextClass } from "../../contexts/SDKContext";
|
||||
import { ModuleApi } from "../../modules/Api.ts";
|
||||
|
||||
const ACTIVE_SPACE_LS_KEY = "mx_active_space";
|
||||
|
||||
@@ -111,10 +111,6 @@ export const getChildOrder = (
|
||||
return [validOrder(order) ?? NaN, ts, roomId]; // NaN has lodash sort it at the end in asc
|
||||
};
|
||||
|
||||
const getRoomFn: FetchRoomFn = (room: Room) => {
|
||||
return RoomNotificationStateStore.instance.getRoomState(room);
|
||||
};
|
||||
|
||||
type SpaceStoreActions =
|
||||
| SettingUpdatedPayload
|
||||
| ViewRoomPayload
|
||||
@@ -258,7 +254,9 @@ export class SpaceStoreClass extends AsyncStoreWithClient<EmptyObject> {
|
||||
if (!space || !this.matrixClient || space === this.activeSpace) return;
|
||||
|
||||
let cliSpace: Room | null = null;
|
||||
if (!isMetaSpace(space)) {
|
||||
if (ModuleApi.instance.extras.spacePanelItems.has(space)) {
|
||||
// it's a "space" provided by a module: that's good enough
|
||||
} else if (!isMetaSpace(space)) {
|
||||
cliSpace = this.matrixClient.getRoom(space);
|
||||
if (!cliSpace?.isSpaceRoom()) return;
|
||||
} else if (!this.enabledMetaSpaces.includes(space)) {
|
||||
@@ -293,6 +291,8 @@ export class SpaceStoreClass extends AsyncStoreWithClient<EmptyObject> {
|
||||
context_switch: true,
|
||||
metricsTrigger: "WebSpaceContextSwitch",
|
||||
});
|
||||
} else if (ModuleApi.instance.extras.spacePanelItems.has(space)) {
|
||||
// module will handle this
|
||||
} else {
|
||||
defaultDispatcher.dispatch<ViewHomePagePayload>({
|
||||
action: Action.ViewHomePage,
|
||||
@@ -1214,7 +1214,8 @@ export class SpaceStoreClass extends AsyncStoreWithClient<EmptyObject> {
|
||||
const lastSpaceId = window.localStorage.getItem(ACTIVE_SPACE_LS_KEY) as MetaSpace;
|
||||
const valid =
|
||||
lastSpaceId &&
|
||||
(!isMetaSpace(lastSpaceId) ? this.matrixClient.getRoom(lastSpaceId) : enabledMetaSpaces[lastSpaceId]);
|
||||
(ModuleApi.instance.extras.spacePanelItems.has(lastSpaceId) ||
|
||||
(!isMetaSpace(lastSpaceId) ? this.matrixClient.getRoom(lastSpaceId) : enabledMetaSpaces[lastSpaceId]));
|
||||
if (valid) {
|
||||
// don't context switch here as it may break permalinks
|
||||
this.setActiveSpace(lastSpaceId, false);
|
||||
@@ -1369,7 +1370,7 @@ export class SpaceStoreClass extends AsyncStoreWithClient<EmptyObject> {
|
||||
return this.notificationStateMap.get(key)!;
|
||||
}
|
||||
|
||||
const state = new SpaceNotificationState(getRoomFn);
|
||||
const state = new SpaceNotificationState();
|
||||
this.notificationStateMap.set(key, state);
|
||||
return state;
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Com
|
||||
Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
import { type Room, type HierarchyRoom } from "matrix-js-sdk/src/matrix";
|
||||
import { type HierarchyRoom } from "matrix-js-sdk/src/matrix";
|
||||
|
||||
import { _t } from "../../languageHandler";
|
||||
|
||||
@@ -42,7 +42,15 @@ export const getMetaSpaceName = (spaceKey: MetaSpace, allRoomsInHome = false): s
|
||||
}
|
||||
};
|
||||
|
||||
export type SpaceKey = MetaSpace | Room["roomId"];
|
||||
/**
|
||||
* This can be:
|
||||
* - a MetaSpace
|
||||
* - space ID (ie. a room ID)
|
||||
* - A 'custom' space from a module
|
||||
* Unfortunately we can't type the last set as we don't know what modules will define,
|
||||
* so we can't do better than string here.
|
||||
*/
|
||||
export type SpaceKey = string;
|
||||
|
||||
export interface ISuggestedRoom extends HierarchyRoom {
|
||||
viaServers: string[];
|
||||
|
||||
Reference in New Issue
Block a user