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-18 11:02:33 +01:00
|
|
|
import { useCallback } from "react";
|
|
|
|
|
|
2025-03-03 16:42:00 +05:30
|
|
|
import type { Room } from "matrix-js-sdk/src/matrix";
|
2025-03-11 18:59:55 +05:30
|
|
|
import { type PrimaryFilter, type SecondaryFilters, useFilteredRooms } from "./useFilteredRooms";
|
2025-03-14 20:40:34 +05:30
|
|
|
import { type SortOption, useSorter } from "./useSorter";
|
2025-03-17 20:37:14 +05:30
|
|
|
import { useMessagePreviewToggle } from "./useMessagePreviewToggle";
|
2025-03-18 11:02:33 +01:00
|
|
|
import { createRoom as createRoomFunc, hasCreateRoomRights } from "./utils";
|
|
|
|
|
import { useEventEmitterState } from "../../../hooks/useEventEmitter";
|
|
|
|
|
import { UPDATE_SELECTED_SPACE } from "../../../stores/spaces";
|
|
|
|
|
import SpaceStore from "../../../stores/spaces/SpaceStore";
|
|
|
|
|
import dispatcher from "../../../dispatcher/dispatcher";
|
|
|
|
|
import { Action } from "../../../dispatcher/actions";
|
|
|
|
|
import { useMatrixClientContext } from "../../../contexts/MatrixClientContext";
|
2025-03-21 17:41:59 +05:30
|
|
|
import { useStickyRoomList } from "./useStickyRoomList";
|
2025-04-22 10:31:12 +02:00
|
|
|
import { useRoomListNavigation } from "./useRoomListNavigation";
|
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-18 11:02:33 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a chat room
|
|
|
|
|
* @param e - The click event
|
|
|
|
|
*/
|
|
|
|
|
createChatRoom: () => void;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Whether the user can create a room in the current space
|
|
|
|
|
*/
|
|
|
|
|
canCreateRoom: boolean;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a room
|
|
|
|
|
* @param e - The click event
|
|
|
|
|
*/
|
|
|
|
|
createRoom: () => void;
|
|
|
|
|
|
2025-03-10 18:53:38 +05:30
|
|
|
/**
|
|
|
|
|
* A list of objects that provide the view enough information
|
|
|
|
|
* to render primary room filters.
|
|
|
|
|
*/
|
|
|
|
|
primaryFilters: PrimaryFilter[];
|
2025-03-11 18:59:55 +05:30
|
|
|
|
2025-03-18 11:02:33 +01:00
|
|
|
/**
|
|
|
|
|
* The currently active primary filter.
|
|
|
|
|
* If no primary filter is active, this will be undefined.
|
|
|
|
|
*/
|
|
|
|
|
activePrimaryFilter?: PrimaryFilter;
|
|
|
|
|
|
2025-03-11 18:59:55 +05:30
|
|
|
/**
|
|
|
|
|
* A function to activate a given secondary filter.
|
|
|
|
|
*/
|
|
|
|
|
activateSecondaryFilter: (filter: SecondaryFilters) => void;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The currently active secondary filter.
|
|
|
|
|
*/
|
|
|
|
|
activeSecondaryFilter: SecondaryFilters;
|
2025-03-14 20:40:34 +05:30
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Change the sort order of the room-list.
|
|
|
|
|
*/
|
|
|
|
|
sort: (option: SortOption) => void;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The currently active sort option.
|
|
|
|
|
*/
|
|
|
|
|
activeSortOption: SortOption;
|
2025-03-17 20:37:14 +05:30
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Whether message previews must be shown or not.
|
|
|
|
|
*/
|
|
|
|
|
shouldShowMessagePreview: boolean;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A function to turn on/off message previews.
|
|
|
|
|
*/
|
|
|
|
|
toggleMessagePreview: () => void;
|
2025-03-18 18:19:10 +05:30
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The index of the active room in the room list.
|
|
|
|
|
*/
|
|
|
|
|
activeIndex: number | undefined;
|
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-18 11:02:33 +01:00
|
|
|
const matrixClient = useMatrixClientContext();
|
2025-03-21 17:41:59 +05:30
|
|
|
const {
|
|
|
|
|
primaryFilters,
|
|
|
|
|
activePrimaryFilter,
|
|
|
|
|
rooms: filteredRooms,
|
|
|
|
|
activateSecondaryFilter,
|
|
|
|
|
activeSecondaryFilter,
|
|
|
|
|
} = useFilteredRooms();
|
|
|
|
|
const { activeIndex, rooms } = useStickyRoomList(filteredRooms);
|
2025-03-18 11:02:33 +01:00
|
|
|
|
2025-04-22 10:31:12 +02:00
|
|
|
useRoomListNavigation(rooms);
|
|
|
|
|
|
2025-03-18 11:02:33 +01:00
|
|
|
const currentSpace = useEventEmitterState<Room | null>(
|
|
|
|
|
SpaceStore.instance,
|
|
|
|
|
UPDATE_SELECTED_SPACE,
|
|
|
|
|
() => SpaceStore.instance.activeSpaceRoom,
|
|
|
|
|
);
|
|
|
|
|
const canCreateRoom = hasCreateRoomRights(matrixClient, currentSpace);
|
|
|
|
|
|
2025-03-14 20:40:34 +05:30
|
|
|
const { activeSortOption, sort } = useSorter();
|
2025-03-17 20:37:14 +05:30
|
|
|
const { shouldShowMessagePreview, toggleMessagePreview } = useMessagePreviewToggle();
|
2025-03-05 14:24:04 +05:30
|
|
|
|
2025-03-18 11:02:33 +01:00
|
|
|
const createChatRoom = useCallback(() => dispatcher.fire(Action.CreateChat), []);
|
|
|
|
|
const createRoom = useCallback(() => createRoomFunc(currentSpace), [currentSpace]);
|
|
|
|
|
|
2025-03-10 18:53:38 +05:30
|
|
|
return {
|
|
|
|
|
rooms,
|
2025-03-18 11:02:33 +01:00
|
|
|
canCreateRoom,
|
|
|
|
|
createRoom,
|
|
|
|
|
createChatRoom,
|
2025-03-10 18:53:38 +05:30
|
|
|
primaryFilters,
|
2025-03-18 11:02:33 +01:00
|
|
|
activePrimaryFilter,
|
2025-03-11 18:59:55 +05:30
|
|
|
activateSecondaryFilter,
|
|
|
|
|
activeSecondaryFilter,
|
2025-03-14 20:40:34 +05:30
|
|
|
activeSortOption,
|
|
|
|
|
sort,
|
2025-03-17 20:37:14 +05:30
|
|
|
shouldShowMessagePreview,
|
|
|
|
|
toggleMessagePreview,
|
2025-03-18 18:19:10 +05:30
|
|
|
activeIndex,
|
2025-03-10 18:53:38 +05:30
|
|
|
};
|
|
|
|
|
}
|