2021-03-02 13:32:24 +00:00
|
|
|
/*
|
|
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-12-15 09:55:53 +00:00
|
|
|
import React, { ReactNode, useContext, useMemo, useRef, useState } from "react";
|
2021-03-02 13:32:24 +00:00
|
|
|
import classNames from "classnames";
|
2021-06-29 13:11:58 +01:00
|
|
|
import { Room } from "matrix-js-sdk/src/models/room";
|
2021-07-01 22:55:27 +01:00
|
|
|
import { sleep } from "matrix-js-sdk/src/utils";
|
2021-07-23 08:46:20 +01:00
|
|
|
import { EventType } from "matrix-js-sdk/src/@types/event";
|
2021-10-22 17:23:32 -05:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2021-03-02 13:32:24 +00:00
|
|
|
|
2021-12-15 09:55:53 +00:00
|
|
|
import { _t, _td } from '../../../languageHandler';
|
2021-03-02 13:32:24 +00:00
|
|
|
import BaseDialog from "./BaseDialog";
|
|
|
|
|
import Dropdown from "../elements/Dropdown";
|
|
|
|
|
import SearchBox from "../../structures/SearchBox";
|
2021-11-11 13:07:41 +00:00
|
|
|
import SpaceStore from "../../../stores/spaces/SpaceStore";
|
2021-03-02 13:32:24 +00:00
|
|
|
import RoomAvatar from "../avatars/RoomAvatar";
|
2021-06-29 13:11:58 +01:00
|
|
|
import { getDisplayAliasForRoom } from "../../../Rooms";
|
2022-02-10 10:02:34 +00:00
|
|
|
import AccessibleButton, { ButtonEvent } from "../elements/AccessibleButton";
|
2021-03-02 13:32:24 +00:00
|
|
|
import AutoHideScrollbar from "../../structures/AutoHideScrollbar";
|
|
|
|
|
import DMRoomMap from "../../../utils/DMRoomMap";
|
2021-06-29 13:11:58 +01:00
|
|
|
import { calculateRoomVia } from "../../../utils/permalinks/Permalinks";
|
2021-03-02 13:32:24 +00:00
|
|
|
import StyledCheckbox from "../elements/StyledCheckbox";
|
2021-04-26 12:41:04 +01:00
|
|
|
import MatrixClientContext from "../../../contexts/MatrixClientContext";
|
2021-06-29 13:11:58 +01:00
|
|
|
import { sortRooms } from "../../../stores/room-list/algorithms/tag-sorting/RecentAlgorithm";
|
2021-05-05 11:45:12 +01:00
|
|
|
import ProgressBar from "../elements/ProgressBar";
|
2021-05-13 14:23:28 +01:00
|
|
|
import DecoratedRoomAvatar from "../avatars/DecoratedRoomAvatar";
|
2021-05-18 13:27:34 +01:00
|
|
|
import QueryMatcher from "../../../autocomplete/QueryMatcher";
|
2021-12-15 09:55:53 +00:00
|
|
|
import LazyRenderList from "../elements/LazyRenderList";
|
|
|
|
|
|
|
|
|
|
// These values match CSS
|
|
|
|
|
const ROW_HEIGHT = 32 + 12;
|
|
|
|
|
const HEADER_HEIGHT = 15;
|
|
|
|
|
const GROUP_MARGIN = 24;
|
2021-03-02 13:32:24 +00:00
|
|
|
|
2021-07-23 08:46:20 +01:00
|
|
|
interface IProps {
|
2021-03-02 13:32:24 +00:00
|
|
|
space: Room;
|
2022-02-10 10:02:34 +00:00
|
|
|
onCreateRoomClick(ev: ButtonEvent): void;
|
2021-07-23 08:46:20 +01:00
|
|
|
onAddSubspaceClick(): void;
|
|
|
|
|
onFinished(added?: boolean): void;
|
2021-03-02 13:32:24 +00:00
|
|
|
}
|
|
|
|
|
|
2021-07-23 08:46:20 +01:00
|
|
|
export const Entry = ({ room, checked, onChange }) => {
|
2021-04-28 22:49:36 +01:00
|
|
|
return <label className="mx_AddExistingToSpace_entry">
|
2021-05-13 14:23:28 +01:00
|
|
|
{ room?.isSpaceRoom()
|
|
|
|
|
? <RoomAvatar room={room} height={32} width={32} />
|
|
|
|
|
: <DecoratedRoomAvatar room={room} avatarSize={32} />
|
|
|
|
|
}
|
2021-04-26 12:41:04 +01:00
|
|
|
<span className="mx_AddExistingToSpace_entry_name">{ room.name }</span>
|
2021-05-05 11:45:12 +01:00
|
|
|
<StyledCheckbox
|
|
|
|
|
onChange={onChange ? (e) => onChange(e.target.checked) : null}
|
|
|
|
|
checked={checked}
|
|
|
|
|
disabled={!onChange}
|
|
|
|
|
/>
|
2021-04-26 11:29:08 +01:00
|
|
|
</label>;
|
2021-03-02 13:32:24 +00:00
|
|
|
};
|
|
|
|
|
|
2021-12-15 09:55:53 +00:00
|
|
|
type OnChangeFn = (checked: boolean, room: Room) => void;
|
|
|
|
|
|
|
|
|
|
type Renderer = (
|
|
|
|
|
rooms: Room[],
|
|
|
|
|
selectedToAdd: Set<Room>,
|
|
|
|
|
scrollState: IScrollState,
|
|
|
|
|
onChange: undefined | OnChangeFn,
|
|
|
|
|
) => ReactNode;
|
|
|
|
|
|
2021-04-26 12:41:04 +01:00
|
|
|
interface IAddExistingToSpaceProps {
|
|
|
|
|
space: Room;
|
2021-05-13 09:46:45 +01:00
|
|
|
footerPrompt?: ReactNode;
|
2021-07-23 08:46:20 +01:00
|
|
|
filterPlaceholder: string;
|
2021-05-13 09:46:45 +01:00
|
|
|
emptySelectionButton?: ReactNode;
|
|
|
|
|
onFinished(added: boolean): void;
|
2021-12-15 09:55:53 +00:00
|
|
|
roomsRenderer?: Renderer;
|
|
|
|
|
spacesRenderer?: Renderer;
|
|
|
|
|
dmsRenderer?: Renderer;
|
2021-04-26 12:41:04 +01:00
|
|
|
}
|
|
|
|
|
|
2021-12-15 09:55:53 +00:00
|
|
|
interface IScrollState {
|
|
|
|
|
scrollTop: number;
|
|
|
|
|
height: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const getScrollState = (
|
|
|
|
|
{ scrollTop, height }: IScrollState,
|
|
|
|
|
numItems: number,
|
|
|
|
|
...prevGroupSizes: number[]
|
|
|
|
|
): IScrollState => {
|
|
|
|
|
let heightBefore = 0;
|
|
|
|
|
prevGroupSizes.forEach(size => {
|
|
|
|
|
heightBefore += GROUP_MARGIN + HEADER_HEIGHT + (size * ROW_HEIGHT);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const viewportTop = scrollTop;
|
|
|
|
|
const viewportBottom = viewportTop + height;
|
|
|
|
|
const listTop = heightBefore + HEADER_HEIGHT;
|
|
|
|
|
const listBottom = listTop + (numItems * ROW_HEIGHT);
|
|
|
|
|
const top = Math.max(viewportTop, listTop);
|
|
|
|
|
const bottom = Math.min(viewportBottom, listBottom);
|
|
|
|
|
// the viewport height and scrollTop passed to the LazyRenderList
|
|
|
|
|
// is capped at the intersection with the real viewport, so lists
|
|
|
|
|
// out of view are passed height 0, so they won't render any items.
|
|
|
|
|
return {
|
|
|
|
|
scrollTop: Math.max(0, scrollTop - listTop),
|
|
|
|
|
height: Math.max(0, bottom - top),
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-13 09:46:45 +01:00
|
|
|
export const AddExistingToSpace: React.FC<IAddExistingToSpaceProps> = ({
|
|
|
|
|
space,
|
|
|
|
|
footerPrompt,
|
|
|
|
|
emptySelectionButton,
|
2021-07-23 08:46:20 +01:00
|
|
|
filterPlaceholder,
|
|
|
|
|
roomsRenderer,
|
|
|
|
|
dmsRenderer,
|
|
|
|
|
spacesRenderer,
|
2021-05-13 09:46:45 +01:00
|
|
|
onFinished,
|
|
|
|
|
}) => {
|
2021-04-26 12:41:04 +01:00
|
|
|
const cli = useContext(MatrixClientContext);
|
2021-05-18 13:27:34 +01:00
|
|
|
const visibleRooms = useMemo(() => cli.getVisibleRooms().filter(r => r.getMyMembership() === "join"), [cli]);
|
2021-04-28 23:39:24 +01:00
|
|
|
|
2022-08-10 13:14:54 +01:00
|
|
|
const scrollRef = useRef<AutoHideScrollbar<"div">>();
|
2021-12-15 09:55:53 +00:00
|
|
|
const [scrollState, setScrollState] = useState<IScrollState>({
|
|
|
|
|
// these are estimates which update as soon as it mounts
|
|
|
|
|
scrollTop: 0,
|
|
|
|
|
height: 600,
|
|
|
|
|
});
|
|
|
|
|
|
2021-05-13 09:46:45 +01:00
|
|
|
const [selectedToAdd, setSelectedToAdd] = useState(new Set<Room>());
|
|
|
|
|
const [progress, setProgress] = useState<number>(null);
|
|
|
|
|
const [error, setError] = useState<Error>(null);
|
2021-03-02 13:32:24 +00:00
|
|
|
const [query, setQuery] = useState("");
|
2021-05-18 13:27:34 +01:00
|
|
|
const lcQuery = query.toLowerCase().trim();
|
2021-03-02 13:32:24 +00:00
|
|
|
|
2021-05-18 13:27:34 +01:00
|
|
|
const existingSubspacesSet = useMemo(() => new Set(SpaceStore.instance.getChildSpaces(space.roomId)), [space]);
|
|
|
|
|
const existingRoomsSet = useMemo(() => new Set(SpaceStore.instance.getChildRooms(space.roomId)), [space]);
|
2021-03-02 13:32:24 +00:00
|
|
|
|
2021-05-18 13:27:34 +01:00
|
|
|
const [spaces, rooms, dms] = useMemo(() => {
|
|
|
|
|
let rooms = visibleRooms;
|
2021-04-19 08:54:06 +01:00
|
|
|
|
2021-05-18 13:27:34 +01:00
|
|
|
if (lcQuery) {
|
|
|
|
|
const matcher = new QueryMatcher<Room>(visibleRooms, {
|
|
|
|
|
keys: ["name"],
|
2021-05-18 13:31:53 +01:00
|
|
|
funcs: [r => [r.getCanonicalAlias(), ...r.getAltAliases()].filter(Boolean)],
|
2021-05-18 13:27:34 +01:00
|
|
|
shouldMatchWordsOnly: false,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
rooms = matcher.match(lcQuery);
|
2021-04-19 08:54:06 +01:00
|
|
|
}
|
2021-05-18 13:27:34 +01:00
|
|
|
|
|
|
|
|
const joinRule = space.getJoinRule();
|
|
|
|
|
return sortRooms(rooms).reduce((arr, room) => {
|
|
|
|
|
if (room.isSpaceRoom()) {
|
|
|
|
|
if (room !== space && !existingSubspacesSet.has(room)) {
|
|
|
|
|
arr[0].push(room);
|
|
|
|
|
}
|
|
|
|
|
} else if (!existingRoomsSet.has(room)) {
|
|
|
|
|
if (!DMRoomMap.shared().getUserIdForRoomId(room.roomId)) {
|
|
|
|
|
arr[1].push(room);
|
|
|
|
|
} else if (joinRule !== "public") {
|
|
|
|
|
// Only show DMs for non-public spaces as they make very little sense in spaces other than "Just Me" ones.
|
|
|
|
|
arr[2].push(room);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return arr;
|
|
|
|
|
}, [[], [], []]);
|
|
|
|
|
}, [visibleRooms, space, lcQuery, existingRoomsSet, existingSubspacesSet]);
|
2021-03-02 13:32:24 +00:00
|
|
|
|
2021-05-05 11:45:12 +01:00
|
|
|
const addRooms = async () => {
|
|
|
|
|
setError(null);
|
|
|
|
|
setProgress(0);
|
|
|
|
|
|
2022-11-07 13:45:34 +00:00
|
|
|
let error: Error | undefined;
|
2021-05-05 11:45:12 +01:00
|
|
|
|
|
|
|
|
for (const room of selectedToAdd) {
|
|
|
|
|
const via = calculateRoomVia(room);
|
|
|
|
|
try {
|
|
|
|
|
await SpaceStore.instance.addRoomToSpace(space, room.roomId, via).catch(async e => {
|
|
|
|
|
if (e.errcode === "M_LIMIT_EXCEEDED") {
|
|
|
|
|
await sleep(e.data.retry_after_ms);
|
|
|
|
|
return SpaceStore.instance.addRoomToSpace(space, room.roomId, via); // retry
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw e;
|
|
|
|
|
});
|
|
|
|
|
setProgress(i => i + 1);
|
|
|
|
|
} catch (e) {
|
2021-10-15 16:30:53 +02:00
|
|
|
logger.error("Failed to add rooms to space", e);
|
2022-11-07 13:45:34 +00:00
|
|
|
error = e;
|
2021-05-05 11:45:12 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!error) {
|
|
|
|
|
onFinished(true);
|
2022-11-07 13:45:34 +00:00
|
|
|
} else {
|
|
|
|
|
setError(error);
|
2021-05-05 11:45:12 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const busy = progress !== null;
|
|
|
|
|
|
|
|
|
|
let footer;
|
|
|
|
|
if (error) {
|
|
|
|
|
footer = <>
|
|
|
|
|
<img
|
2022-03-03 18:51:38 +01:00
|
|
|
src={require("../../../../res/img/element-icons/warning-badge.svg").default}
|
2021-05-05 11:45:12 +01:00
|
|
|
height="24"
|
|
|
|
|
width="24"
|
|
|
|
|
alt=""
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<span className="mx_AddExistingToSpaceDialog_error">
|
|
|
|
|
<div className="mx_AddExistingToSpaceDialog_errorHeading">{ _t("Not all selected were added") }</div>
|
|
|
|
|
<div className="mx_AddExistingToSpaceDialog_errorCaption">{ _t("Try again") }</div>
|
|
|
|
|
</span>
|
|
|
|
|
|
|
|
|
|
<AccessibleButton className="mx_AddExistingToSpaceDialog_retryButton" onClick={addRooms}>
|
|
|
|
|
{ _t("Retry") }
|
|
|
|
|
</AccessibleButton>
|
|
|
|
|
</>;
|
|
|
|
|
} else if (busy) {
|
|
|
|
|
footer = <span>
|
|
|
|
|
<ProgressBar value={progress} max={selectedToAdd.size} />
|
|
|
|
|
<div className="mx_AddExistingToSpaceDialog_progressText">
|
|
|
|
|
{ _t("Adding rooms... (%(progress)s out of %(count)s)", {
|
|
|
|
|
count: selectedToAdd.size,
|
|
|
|
|
progress,
|
|
|
|
|
}) }
|
|
|
|
|
</div>
|
|
|
|
|
</span>;
|
|
|
|
|
} else {
|
2021-05-13 09:46:45 +01:00
|
|
|
let button = emptySelectionButton;
|
|
|
|
|
if (!button || selectedToAdd.size > 0) {
|
|
|
|
|
button = <AccessibleButton kind="primary" disabled={selectedToAdd.size < 1} onClick={addRooms}>
|
|
|
|
|
{ _t("Add") }
|
|
|
|
|
</AccessibleButton>;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-05 11:45:12 +01:00
|
|
|
footer = <>
|
|
|
|
|
<span>
|
2021-05-13 09:46:45 +01:00
|
|
|
{ footerPrompt }
|
2021-05-05 11:45:12 +01:00
|
|
|
</span>
|
|
|
|
|
|
2021-05-13 09:46:45 +01:00
|
|
|
{ button }
|
2021-05-05 11:45:12 +01:00
|
|
|
</>;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-23 08:46:20 +01:00
|
|
|
const onChange = !busy && !error ? (checked: boolean, room: Room) => {
|
2021-05-13 09:46:45 +01:00
|
|
|
if (checked) {
|
|
|
|
|
selectedToAdd.add(room);
|
|
|
|
|
} else {
|
|
|
|
|
selectedToAdd.delete(room);
|
|
|
|
|
}
|
|
|
|
|
setSelectedToAdd(new Set(selectedToAdd));
|
|
|
|
|
} : null;
|
|
|
|
|
|
2021-12-15 09:55:53 +00:00
|
|
|
// only count spaces when alone as they're shown on a separate modal all on their own
|
|
|
|
|
const numSpaces = (spacesRenderer && !dmsRenderer && !roomsRenderer) ? spaces.length : 0;
|
2022-02-08 19:25:08 +00:00
|
|
|
const numRooms = roomsRenderer ? rooms.length : 0;
|
|
|
|
|
const numDms = dmsRenderer ? dms.length : 0;
|
2021-06-22 10:52:33 +01:00
|
|
|
|
2021-07-23 08:46:20 +01:00
|
|
|
let noResults = true;
|
2022-02-08 19:25:08 +00:00
|
|
|
if (numSpaces > 0 || numRooms > 0 || numDms > 0) {
|
2021-07-23 08:46:20 +01:00
|
|
|
noResults = false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-15 09:55:53 +00:00
|
|
|
const onScroll = () => {
|
|
|
|
|
const body = scrollRef.current?.containerRef.current;
|
|
|
|
|
setScrollState({
|
|
|
|
|
scrollTop: body.scrollTop,
|
|
|
|
|
height: body.clientHeight,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const wrappedRef = (body: HTMLDivElement) => {
|
|
|
|
|
setScrollState({
|
|
|
|
|
scrollTop: body.scrollTop,
|
|
|
|
|
height: body.clientHeight,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-08 19:25:08 +00:00
|
|
|
const roomsScrollState = getScrollState(scrollState, numRooms);
|
|
|
|
|
const spacesScrollState = getScrollState(scrollState, numSpaces, numRooms);
|
|
|
|
|
const dmsScrollState = getScrollState(scrollState, numDms, numSpaces, numRooms);
|
2021-12-15 09:55:53 +00:00
|
|
|
|
2021-05-13 09:46:45 +01:00
|
|
|
return <div className="mx_AddExistingToSpace">
|
|
|
|
|
<SearchBox
|
|
|
|
|
className="mx_textinput_icon mx_textinput_search"
|
2021-07-23 08:46:20 +01:00
|
|
|
placeholder={filterPlaceholder}
|
2021-05-13 09:46:45 +01:00
|
|
|
onSearch={setQuery}
|
|
|
|
|
autoFocus={true}
|
|
|
|
|
/>
|
2021-12-15 09:55:53 +00:00
|
|
|
<AutoHideScrollbar
|
|
|
|
|
className="mx_AddExistingToSpace_content"
|
|
|
|
|
onScroll={onScroll}
|
|
|
|
|
wrappedRef={wrappedRef}
|
|
|
|
|
ref={scrollRef}
|
|
|
|
|
>
|
2021-07-23 08:46:20 +01:00
|
|
|
{ rooms.length > 0 && roomsRenderer ? (
|
2021-12-15 09:55:53 +00:00
|
|
|
roomsRenderer(rooms, selectedToAdd, roomsScrollState, onChange)
|
2021-05-13 09:46:45 +01:00
|
|
|
) : undefined }
|
|
|
|
|
|
2021-07-23 08:46:20 +01:00
|
|
|
{ spaces.length > 0 && spacesRenderer ? (
|
2021-12-15 09:55:53 +00:00
|
|
|
spacesRenderer(spaces, selectedToAdd, spacesScrollState, onChange)
|
2021-05-13 09:46:45 +01:00
|
|
|
) : null }
|
|
|
|
|
|
2021-07-23 08:46:20 +01:00
|
|
|
{ dms.length > 0 && dmsRenderer ? (
|
2021-12-15 09:55:53 +00:00
|
|
|
dmsRenderer(dms, selectedToAdd, dmsScrollState, onChange)
|
2021-05-13 09:46:45 +01:00
|
|
|
) : null }
|
|
|
|
|
|
2021-07-23 08:46:20 +01:00
|
|
|
{ noResults ? <span className="mx_AddExistingToSpace_noResults">
|
2021-05-13 09:46:45 +01:00
|
|
|
{ _t("No results") }
|
|
|
|
|
</span> : undefined }
|
|
|
|
|
</AutoHideScrollbar>
|
|
|
|
|
|
|
|
|
|
<div className="mx_AddExistingToSpace_footer">
|
|
|
|
|
{ footer }
|
|
|
|
|
</div>
|
|
|
|
|
</div>;
|
|
|
|
|
};
|
|
|
|
|
|
2021-12-15 09:55:53 +00:00
|
|
|
const defaultRendererFactory = (title: string): Renderer => (
|
|
|
|
|
rooms,
|
|
|
|
|
selectedToAdd,
|
|
|
|
|
{ scrollTop, height },
|
|
|
|
|
onChange,
|
2021-07-23 08:46:20 +01:00
|
|
|
) => (
|
|
|
|
|
<div className="mx_AddExistingToSpace_section">
|
2021-12-15 09:55:53 +00:00
|
|
|
<h3>{ _t(title) }</h3>
|
|
|
|
|
<LazyRenderList
|
|
|
|
|
itemHeight={ROW_HEIGHT}
|
|
|
|
|
items={rooms}
|
|
|
|
|
scrollTop={scrollTop}
|
|
|
|
|
height={height}
|
|
|
|
|
renderItem={room => (
|
2021-07-23 08:46:20 +01:00
|
|
|
<Entry
|
|
|
|
|
key={room.roomId}
|
|
|
|
|
room={room}
|
|
|
|
|
checked={selectedToAdd.has(room)}
|
|
|
|
|
onChange={onChange ? (checked: boolean) => {
|
|
|
|
|
onChange(checked, room);
|
|
|
|
|
} : null}
|
2021-12-15 09:55:53 +00:00
|
|
|
/>
|
2021-07-23 08:46:20 +01:00
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2021-05-13 09:46:45 +01:00
|
|
|
|
2021-12-15 09:55:53 +00:00
|
|
|
export const defaultRoomsRenderer = defaultRendererFactory(_td("Rooms"));
|
|
|
|
|
export const defaultSpacesRenderer = defaultRendererFactory(_td("Spaces"));
|
|
|
|
|
export const defaultDmsRenderer = defaultRendererFactory(_td("Direct Messages"));
|
2021-07-23 08:46:20 +01:00
|
|
|
|
|
|
|
|
interface ISubspaceSelectorProps {
|
|
|
|
|
title: string;
|
|
|
|
|
space: Room;
|
|
|
|
|
value: Room;
|
|
|
|
|
onChange(space: Room): void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const SubspaceSelector = ({ title, space, value, onChange }: ISubspaceSelectorProps) => {
|
|
|
|
|
const options = useMemo(() => {
|
|
|
|
|
return [space, ...SpaceStore.instance.getChildSpaces(space.roomId).filter(space => {
|
|
|
|
|
return space.currentState.maySendStateEvent(EventType.SpaceChild, space.client.credentials.userId);
|
|
|
|
|
})];
|
|
|
|
|
}, [space]);
|
|
|
|
|
|
|
|
|
|
let body;
|
|
|
|
|
if (options.length > 1) {
|
|
|
|
|
body = (
|
2021-05-13 09:46:45 +01:00
|
|
|
<Dropdown
|
|
|
|
|
id="mx_SpaceSelectDropdown"
|
2021-07-23 08:46:20 +01:00
|
|
|
className="mx_SpaceSelectDropdown"
|
2021-05-13 09:46:45 +01:00
|
|
|
onOptionChange={(key: string) => {
|
2021-07-23 08:46:20 +01:00
|
|
|
onChange(options.find(space => space.roomId === key) || space);
|
2021-05-13 09:46:45 +01:00
|
|
|
}}
|
2021-07-23 08:46:20 +01:00
|
|
|
value={value.roomId}
|
2021-05-13 09:46:45 +01:00
|
|
|
label={_t("Space selection")}
|
|
|
|
|
>
|
2021-07-23 08:46:20 +01:00
|
|
|
{ options.map((space) => {
|
|
|
|
|
const classes = classNames({
|
|
|
|
|
mx_SubspaceSelector_dropdownOptionActive: space === value,
|
|
|
|
|
});
|
|
|
|
|
return <div key={space.roomId} className={classes}>
|
|
|
|
|
<RoomAvatar room={space} width={24} height={24} />
|
|
|
|
|
{ space.name || getDisplayAliasForRoom(space) || space.roomId }
|
|
|
|
|
</div>;
|
|
|
|
|
}) }
|
2021-05-13 09:46:45 +01:00
|
|
|
</Dropdown>
|
|
|
|
|
);
|
|
|
|
|
} else {
|
2021-07-23 08:46:20 +01:00
|
|
|
body = (
|
|
|
|
|
<div className="mx_SubspaceSelector_onlySpace">
|
|
|
|
|
{ space.name || getDisplayAliasForRoom(space) || space.roomId }
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2021-05-13 09:46:45 +01:00
|
|
|
}
|
|
|
|
|
|
2021-07-23 08:46:20 +01:00
|
|
|
return <div className="mx_SubspaceSelector">
|
|
|
|
|
<RoomAvatar room={value} height={40} width={40} />
|
2021-05-13 09:46:45 +01:00
|
|
|
<div>
|
2021-07-23 08:46:20 +01:00
|
|
|
<h1>{ title }</h1>
|
|
|
|
|
{ body }
|
2021-05-13 09:46:45 +01:00
|
|
|
</div>
|
2021-07-23 08:46:20 +01:00
|
|
|
</div>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const AddExistingToSpaceDialog: React.FC<IProps> = ({ space, onCreateRoomClick, onAddSubspaceClick, onFinished }) => {
|
|
|
|
|
const [selectedSpace, setSelectedSpace] = useState(space);
|
2021-05-13 09:46:45 +01:00
|
|
|
|
2021-03-02 13:32:24 +00:00
|
|
|
return <BaseDialog
|
2021-07-23 08:46:20 +01:00
|
|
|
title={(
|
|
|
|
|
<SubspaceSelector
|
|
|
|
|
title={_t("Add existing rooms")}
|
|
|
|
|
space={space}
|
|
|
|
|
value={selectedSpace}
|
|
|
|
|
onChange={setSelectedSpace}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2021-03-02 13:32:24 +00:00
|
|
|
className="mx_AddExistingToSpaceDialog"
|
2021-04-26 12:41:04 +01:00
|
|
|
contentId="mx_AddExistingToSpace"
|
2021-03-02 13:32:24 +00:00
|
|
|
onFinished={onFinished}
|
|
|
|
|
fixedWidth={false}
|
|
|
|
|
>
|
2021-07-02 16:07:17 +01:00
|
|
|
<MatrixClientContext.Provider value={space.client}>
|
2021-04-26 12:41:04 +01:00
|
|
|
<AddExistingToSpace
|
|
|
|
|
space={space}
|
2021-05-13 09:46:45 +01:00
|
|
|
onFinished={onFinished}
|
|
|
|
|
footerPrompt={<>
|
|
|
|
|
<div>{ _t("Want to add a new room instead?") }</div>
|
2021-07-26 12:41:52 +01:00
|
|
|
<AccessibleButton
|
|
|
|
|
kind="link"
|
2022-02-10 10:02:34 +00:00
|
|
|
onClick={(ev: ButtonEvent) => {
|
|
|
|
|
onCreateRoomClick(ev);
|
2021-07-26 12:41:52 +01:00
|
|
|
onFinished();
|
|
|
|
|
}}
|
|
|
|
|
>
|
2021-05-13 09:46:45 +01:00
|
|
|
{ _t("Create a new room") }
|
|
|
|
|
</AccessibleButton>
|
|
|
|
|
</>}
|
2021-07-23 08:46:20 +01:00
|
|
|
filterPlaceholder={_t("Search for rooms")}
|
|
|
|
|
roomsRenderer={defaultRoomsRenderer}
|
|
|
|
|
spacesRenderer={() => (
|
|
|
|
|
<div className="mx_AddExistingToSpace_section">
|
|
|
|
|
<h3>{ _t("Spaces") }</h3>
|
2021-07-26 12:41:52 +01:00
|
|
|
<AccessibleButton
|
|
|
|
|
kind="link"
|
|
|
|
|
onClick={() => {
|
|
|
|
|
onAddSubspaceClick();
|
|
|
|
|
onFinished();
|
|
|
|
|
}}
|
|
|
|
|
>
|
2021-07-23 08:46:20 +01:00
|
|
|
{ _t("Adding spaces has moved.") }
|
|
|
|
|
</AccessibleButton>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
dmsRenderer={defaultDmsRenderer}
|
2021-04-26 12:41:04 +01:00
|
|
|
/>
|
|
|
|
|
</MatrixClientContext.Provider>
|
2021-03-02 13:32:24 +00:00
|
|
|
</BaseDialog>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default AddExistingToSpaceDialog;
|
|
|
|
|
|