Skip to content

Commit f6b33c9

Browse files
committed
Add more vm functionality
- Listen for updates from the store - Provide a method to open rooms
1 parent 21e9d93 commit f6b33c9

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

src/components/viewmodels/roomlist/RoomListViewModel.tsx

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,47 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Com
55
Please see LICENSE files in the repository root for full details.
66
*/
77

8+
import { useCallback, useState } from "react";
9+
810
import type { Room } from "matrix-js-sdk/src/matrix";
11+
import type { ViewRoomPayload } from "../../../dispatcher/payloads/ViewRoomPayload";
912
import RoomListStoreV3 from "../../../stores/room-list-v3/RoomListStoreV3";
13+
import { useEventEmitter } from "../../../hooks/useEventEmitter";
14+
import { LISTS_UPDATE_EVENT } from "../../../stores/room-list/RoomListStore";
15+
import dispatcher from "../../../dispatcher/dispatcher";
16+
import { Action } from "../../../dispatcher/actions";
1017

1118
export interface RoomListViewState {
1219
/**
1320
* A list of rooms to be displayed in the left panel.
1421
*/
1522
rooms: Room[];
23+
24+
/**
25+
* Open the room having given roomId.
26+
*/
27+
openRoom: (roomId: string) => void;
1628
}
1729

1830
/**
1931
* View model for the new room list
2032
* @see {@link RoomListViewState} for more information about what this view model returns.
2133
*/
2234
export function useRoomListViewModel(): RoomListViewState {
23-
const rooms = RoomListStoreV3.instance.getSortedRooms();
24-
return { rooms };
35+
const [rooms, setRooms] = useState(RoomListStoreV3.instance.getSortedRoomInActiveSpace());
36+
37+
useEventEmitter(RoomListStoreV3.instance, LISTS_UPDATE_EVENT, () => {
38+
const newRooms = RoomListStoreV3.instance.getSortedRoomInActiveSpace();
39+
setRooms(newRooms);
40+
});
41+
42+
const openRoom = useCallback((roomId: string): void => {
43+
dispatcher.dispatch<ViewRoomPayload>({
44+
action: Action.ViewRoom,
45+
room_id: roomId,
46+
metricsTrigger: "RoomList",
47+
});
48+
}, []);
49+
50+
return { rooms, openRoom };
2551
}

0 commit comments

Comments
 (0)