2025-03-03 16:42:00 +05:30
|
|
|
/*
|
|
|
|
|
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.
|
|
|
|
|
*/
|
|
|
|
|
|
2025-03-05 14:24:04 +05:30
|
|
|
import { useCallback, useState } from "react";
|
|
|
|
|
|
2025-03-03 16:42:00 +05:30
|
|
|
import type { Room } from "matrix-js-sdk/src/matrix";
|
2025-03-05 14:24:04 +05:30
|
|
|
import type { ViewRoomPayload } from "../../../dispatcher/payloads/ViewRoomPayload";
|
2025-03-03 16:42:00 +05:30
|
|
|
import RoomListStoreV3 from "../../../stores/room-list-v3/RoomListStoreV3";
|
2025-03-05 14:24:04 +05:30
|
|
|
import { useEventEmitter } from "../../../hooks/useEventEmitter";
|
|
|
|
|
import { LISTS_UPDATE_EVENT } from "../../../stores/room-list/RoomListStore";
|
|
|
|
|
import dispatcher from "../../../dispatcher/dispatcher";
|
|
|
|
|
import { Action } from "../../../dispatcher/actions";
|
2025-03-03 16:42:00 +05:30
|
|
|
|
|
|
|
|
export interface RoomListViewState {
|
|
|
|
|
/**
|
|
|
|
|
* A list of rooms to be displayed in the left panel.
|
|
|
|
|
*/
|
|
|
|
|
rooms: Room[];
|
2025-03-05 14:24:04 +05:30
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Open the room having given roomId.
|
|
|
|
|
*/
|
|
|
|
|
openRoom: (roomId: string) => void;
|
2025-03-03 16:42:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* View model for the new room list
|
|
|
|
|
* @see {@link RoomListViewState} for more information about what this view model returns.
|
|
|
|
|
*/
|
|
|
|
|
export function useRoomListViewModel(): RoomListViewState {
|
2025-03-05 14:24:04 +05:30
|
|
|
const [rooms, setRooms] = useState(RoomListStoreV3.instance.getSortedRoomsInActiveSpace());
|
|
|
|
|
|
|
|
|
|
useEventEmitter(RoomListStoreV3.instance, LISTS_UPDATE_EVENT, () => {
|
|
|
|
|
const newRooms = RoomListStoreV3.instance.getSortedRoomsInActiveSpace();
|
|
|
|
|
setRooms(newRooms);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const openRoom = useCallback((roomId: string): void => {
|
|
|
|
|
dispatcher.dispatch<ViewRoomPayload>({
|
|
|
|
|
action: Action.ViewRoom,
|
|
|
|
|
room_id: roomId,
|
|
|
|
|
metricsTrigger: "RoomList",
|
|
|
|
|
});
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
return { rooms, openRoom };
|
2025-03-03 16:42:00 +05:30
|
|
|
}
|