@@ -5,21 +5,47 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Com
5
5
Please see LICENSE files in the repository root for full details.
6
6
*/
7
7
8
+ import { useCallback , useState } from "react" ;
9
+
8
10
import type { Room } from "matrix-js-sdk/src/matrix" ;
11
+ import type { ViewRoomPayload } from "../../../dispatcher/payloads/ViewRoomPayload" ;
9
12
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" ;
10
17
11
18
export interface RoomListViewState {
12
19
/**
13
20
* A list of rooms to be displayed in the left panel.
14
21
*/
15
22
rooms : Room [ ] ;
23
+
24
+ /**
25
+ * Open the room having given roomId.
26
+ */
27
+ openRoom : ( roomId : string ) => void ;
16
28
}
17
29
18
30
/**
19
31
* View model for the new room list
20
32
* @see {@link RoomListViewState } for more information about what this view model returns.
21
33
*/
22
34
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 } ;
25
51
}
0 commit comments