@@ -5,21 +5,47 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Com
55Please see LICENSE files in the repository root for full details.
66*/
77
8+ import { useCallback , useState } from "react" ;
9+
810import type { Room } from "matrix-js-sdk/src/matrix" ;
11+ import type { ViewRoomPayload } from "../../../dispatcher/payloads/ViewRoomPayload" ;
912import 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
1118export 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 */
2234export 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