2021-05-09 02:48:34 +03:00
|
|
|
/*
|
2021-06-06 04:41:28 +03:00
|
|
|
Copyright 2021 Robin Townsend <robin@robin.town>
|
2021-05-09 02:48:34 +03:00
|
|
|
|
|
|
|
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-06-23 00:27:12 +03:00
|
|
|
import React, { useMemo, useState, useEffect } from "react";
|
2021-05-09 02:48:34 +03:00
|
|
|
import classnames from "classnames";
|
2021-06-23 00:27:12 +03:00
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
|
|
|
import { Room } from "matrix-js-sdk/src/models/room";
|
|
|
|
import { MatrixClient } from "matrix-js-sdk/src/client";
|
|
|
|
import { RoomMember } from "matrix-js-sdk/src/models/room-member";
|
2021-05-09 02:48:34 +03:00
|
|
|
|
2021-06-23 00:27:12 +03:00
|
|
|
import { _t } from "../../../languageHandler";
|
2021-05-10 20:00:06 +03:00
|
|
|
import dis from "../../../dispatcher/dispatcher";
|
2021-06-23 00:27:12 +03:00
|
|
|
import { useSettingValue, useFeatureEnabled } from "../../../hooks/useSettings";
|
|
|
|
import { UIFeature } from "../../../settings/UIFeature";
|
|
|
|
import { Layout } from "../../../settings/Layout";
|
|
|
|
import { IDialogProps } from "./IDialogProps";
|
2021-05-09 02:48:34 +03:00
|
|
|
import BaseDialog from "./BaseDialog";
|
2021-06-23 00:27:12 +03:00
|
|
|
import { avatarUrlForUser } from "../../../Avatar";
|
2021-05-09 02:48:34 +03:00
|
|
|
import EventTile from "../rooms/EventTile";
|
|
|
|
import SearchBox from "../../structures/SearchBox";
|
2021-05-16 15:39:22 +03:00
|
|
|
import DecoratedRoomAvatar from "../avatars/DecoratedRoomAvatar";
|
2021-06-23 00:27:12 +03:00
|
|
|
import { Alignment } from '../elements/Tooltip';
|
2021-05-10 08:04:25 +03:00
|
|
|
import AccessibleTooltipButton from "../elements/AccessibleTooltipButton";
|
2021-05-09 02:48:34 +03:00
|
|
|
import AutoHideScrollbar from "../../structures/AutoHideScrollbar";
|
2021-06-23 00:27:12 +03:00
|
|
|
import { StaticNotificationState } from "../../../stores/notifications/StaticNotificationState";
|
2021-05-21 19:41:29 +03:00
|
|
|
import NotificationBadge from "../rooms/NotificationBadge";
|
2021-06-23 00:27:12 +03:00
|
|
|
import { RoomPermalinkCreator } from "../../../utils/permalinks/Permalinks";
|
|
|
|
import { sortRooms } from "../../../stores/room-list/algorithms/tag-sorting/RecentAlgorithm";
|
2021-06-02 03:17:20 +03:00
|
|
|
import QueryMatcher from "../../../autocomplete/QueryMatcher";
|
2021-06-21 14:29:59 +03:00
|
|
|
import TruncatedList from "../elements/TruncatedList";
|
|
|
|
import EntityTile from "../rooms/EntityTile";
|
|
|
|
import BaseAvatar from "../avatars/BaseAvatar";
|
2021-05-09 02:48:34 +03:00
|
|
|
|
|
|
|
const AVATAR_SIZE = 30;
|
|
|
|
|
|
|
|
interface IProps extends IDialogProps {
|
2021-05-24 15:51:04 +03:00
|
|
|
matrixClient: MatrixClient;
|
2021-05-10 07:38:01 +03:00
|
|
|
// The event to forward
|
2021-05-09 02:48:34 +03:00
|
|
|
event: MatrixEvent;
|
2021-05-10 07:40:54 +03:00
|
|
|
// We need a permalink creator for the source room to pass through to EventTile
|
|
|
|
// in case the event is a reply (even though the user can't get at the link)
|
|
|
|
permalinkCreator: RoomPermalinkCreator;
|
2021-05-09 02:48:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
interface IEntryProps {
|
|
|
|
room: Room;
|
2021-05-10 20:00:06 +03:00
|
|
|
event: MatrixEvent;
|
2021-05-24 15:51:04 +03:00
|
|
|
matrixClient: MatrixClient;
|
2021-05-10 20:00:06 +03:00
|
|
|
onFinished(success: boolean): void;
|
2021-05-09 02:48:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
enum SendState {
|
|
|
|
CanSend,
|
|
|
|
Sending,
|
|
|
|
Sent,
|
|
|
|
Failed,
|
|
|
|
}
|
|
|
|
|
2021-05-24 15:51:04 +03:00
|
|
|
const Entry: React.FC<IEntryProps> = ({ room, event, matrixClient: cli, onFinished }) => {
|
2021-05-09 02:48:34 +03:00
|
|
|
const [sendState, setSendState] = useState<SendState>(SendState.CanSend);
|
|
|
|
|
2021-05-10 20:00:06 +03:00
|
|
|
const jumpToRoom = () => {
|
|
|
|
dis.dispatch({
|
|
|
|
action: "view_room",
|
|
|
|
room_id: room.roomId,
|
|
|
|
});
|
|
|
|
onFinished(true);
|
|
|
|
};
|
|
|
|
const send = async () => {
|
|
|
|
setSendState(SendState.Sending);
|
|
|
|
try {
|
|
|
|
await cli.sendEvent(room.roomId, event.getType(), event.getContent());
|
|
|
|
setSendState(SendState.Sent);
|
|
|
|
} catch (e) {
|
|
|
|
setSendState(SendState.Failed);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-05-21 19:41:29 +03:00
|
|
|
let className;
|
|
|
|
let disabled = false;
|
|
|
|
let title;
|
|
|
|
let icon;
|
|
|
|
if (sendState === SendState.CanSend) {
|
|
|
|
className = "mx_ForwardList_canSend";
|
|
|
|
if (room.maySendMessage()) {
|
|
|
|
title = _t("Send");
|
2021-05-10 08:04:25 +03:00
|
|
|
} else {
|
2021-05-21 19:41:29 +03:00
|
|
|
disabled = true;
|
2021-05-21 19:59:13 +03:00
|
|
|
title = _t("You don't have permission to do this");
|
2021-05-10 08:04:25 +03:00
|
|
|
}
|
2021-05-21 19:41:29 +03:00
|
|
|
} else if (sendState === SendState.Sending) {
|
|
|
|
className = "mx_ForwardList_sending";
|
|
|
|
disabled = true;
|
2021-05-24 15:55:08 +03:00
|
|
|
title = _t("Sending");
|
|
|
|
icon = <div className="mx_ForwardList_sendIcon" aria-label={title}></div>;
|
2021-05-21 19:41:29 +03:00
|
|
|
} else if (sendState === SendState.Sent) {
|
|
|
|
className = "mx_ForwardList_sent";
|
|
|
|
disabled = true;
|
|
|
|
title = _t("Sent");
|
2021-05-24 15:55:08 +03:00
|
|
|
icon = <div className="mx_ForwardList_sendIcon" aria-label={title}></div>;
|
2021-05-09 02:48:34 +03:00
|
|
|
} else {
|
2021-05-21 19:41:29 +03:00
|
|
|
className = "mx_ForwardList_sendFailed";
|
|
|
|
disabled = true;
|
|
|
|
title = _t("Failed to send");
|
|
|
|
icon = <NotificationBadge
|
|
|
|
notification={StaticNotificationState.RED_EXCLAMATION}
|
|
|
|
/>;
|
2021-05-09 02:48:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return <div className="mx_ForwardList_entry">
|
2021-05-21 19:41:29 +03:00
|
|
|
<AccessibleTooltipButton
|
|
|
|
className="mx_ForwardList_roomButton"
|
|
|
|
onClick={jumpToRoom}
|
|
|
|
title={_t("Open link")}
|
|
|
|
yOffset={-20}
|
|
|
|
alignment={Alignment.Top}
|
|
|
|
>
|
2021-05-16 15:39:22 +03:00
|
|
|
<DecoratedRoomAvatar room={room} avatarSize={32} />
|
2021-05-10 20:00:06 +03:00
|
|
|
<span className="mx_ForwardList_entry_name">{ room.name }</span>
|
2021-05-21 19:41:29 +03:00
|
|
|
</AccessibleTooltipButton>
|
|
|
|
<AccessibleTooltipButton
|
|
|
|
kind={sendState === SendState.Failed ? "danger_outline" : "primary_outline"}
|
|
|
|
className={`mx_ForwardList_sendButton ${className}`}
|
|
|
|
onClick={send}
|
|
|
|
disabled={disabled}
|
|
|
|
title={title}
|
|
|
|
yOffset={-20}
|
|
|
|
alignment={Alignment.Top}
|
|
|
|
>
|
|
|
|
<div className="mx_ForwardList_sendLabel">{ _t("Send") }</div>
|
|
|
|
{ icon }
|
|
|
|
</AccessibleTooltipButton>
|
2021-05-09 02:48:34 +03:00
|
|
|
</div>;
|
|
|
|
};
|
|
|
|
|
2021-05-24 15:51:04 +03:00
|
|
|
const ForwardDialog: React.FC<IProps> = ({ matrixClient: cli, event, permalinkCreator, onFinished }) => {
|
2021-05-09 02:48:34 +03:00
|
|
|
const userId = cli.getUserId();
|
|
|
|
const [profileInfo, setProfileInfo] = useState<any>({});
|
|
|
|
useEffect(() => {
|
|
|
|
cli.getProfileInfo(userId).then(info => setProfileInfo(info));
|
|
|
|
}, [cli, userId]);
|
|
|
|
|
|
|
|
// For the message preview we fake the sender as ourselves
|
|
|
|
const mockEvent = new MatrixEvent({
|
|
|
|
type: "m.room.message",
|
|
|
|
sender: userId,
|
|
|
|
content: event.getContent(),
|
|
|
|
unsigned: {
|
|
|
|
age: 97,
|
|
|
|
},
|
|
|
|
event_id: "$9999999999999999999999999999999999999999999",
|
2021-05-10 07:40:54 +03:00
|
|
|
room_id: event.getRoomId(),
|
2021-05-09 02:48:34 +03:00
|
|
|
});
|
|
|
|
mockEvent.sender = {
|
|
|
|
name: profileInfo.displayname || userId,
|
2021-06-17 01:17:25 +03:00
|
|
|
rawDisplayName: profileInfo.displayname,
|
2021-05-10 07:38:01 +03:00
|
|
|
userId,
|
2021-05-09 02:48:34 +03:00
|
|
|
getAvatarUrl: (..._) => {
|
|
|
|
return avatarUrlForUser(
|
|
|
|
{ avatarUrl: profileInfo.avatar_url },
|
|
|
|
AVATAR_SIZE, AVATAR_SIZE, "crop",
|
|
|
|
);
|
|
|
|
},
|
|
|
|
getMxcAvatarUrl: () => profileInfo.avatar_url,
|
2021-06-23 00:27:12 +03:00
|
|
|
} as RoomMember;
|
2021-05-09 02:48:34 +03:00
|
|
|
|
2021-05-19 20:33:48 +03:00
|
|
|
const [query, setQuery] = useState("");
|
|
|
|
const lcQuery = query.toLowerCase();
|
|
|
|
|
2021-06-02 01:09:51 +03:00
|
|
|
const spacesEnabled = useFeatureEnabled("feature_spaces");
|
|
|
|
const flairEnabled = useFeatureEnabled(UIFeature.Flair);
|
2021-06-02 03:17:20 +03:00
|
|
|
const previewLayout = useSettingValue<Layout>("layout");
|
2021-06-02 01:09:51 +03:00
|
|
|
|
2021-06-02 03:17:20 +03:00
|
|
|
let rooms = useMemo(() => sortRooms(
|
2021-05-09 02:48:34 +03:00
|
|
|
cli.getVisibleRooms().filter(
|
|
|
|
room => room.getMyMembership() === "join" &&
|
2021-06-02 01:09:51 +03:00
|
|
|
!(spacesEnabled && room.isSpaceRoom()),
|
2021-05-09 02:48:34 +03:00
|
|
|
),
|
2021-06-02 03:17:20 +03:00
|
|
|
), [cli, spacesEnabled]);
|
|
|
|
|
|
|
|
if (lcQuery) {
|
|
|
|
rooms = new QueryMatcher<Room>(rooms, {
|
|
|
|
keys: ["name"],
|
|
|
|
funcs: [r => [r.getCanonicalAlias(), ...r.getAltAliases()].filter(Boolean)],
|
|
|
|
shouldMatchWordsOnly: false,
|
|
|
|
}).match(lcQuery);
|
|
|
|
}
|
2021-05-09 02:48:34 +03:00
|
|
|
|
2021-06-21 14:29:59 +03:00
|
|
|
const [truncateAt, setTruncateAt] = useState(20);
|
|
|
|
function overflowTile(overflowCount, totalCount) {
|
|
|
|
const text = _t("and %(count)s others...", { count: overflowCount });
|
|
|
|
return (
|
|
|
|
<EntityTile className="mx_EntityTile_ellipsis" avatarJsx={
|
|
|
|
<BaseAvatar url={require("../../../../res/img/ellipsis.svg")} name="..." width={36} height={36} />
|
|
|
|
} name={text} presenceState="online" suppressOnHover={true}
|
|
|
|
onClick={() => setTruncateAt(totalCount)} />
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-05-09 02:48:34 +03:00
|
|
|
return <BaseDialog
|
2021-06-02 00:37:31 +03:00
|
|
|
title={_t("Forward message")}
|
2021-05-09 02:48:34 +03:00
|
|
|
className="mx_ForwardDialog"
|
|
|
|
contentId="mx_ForwardList"
|
|
|
|
onFinished={onFinished}
|
|
|
|
fixedWidth={false}
|
|
|
|
>
|
2021-06-08 02:16:00 +03:00
|
|
|
<h3>{ _t("Message preview") }</h3>
|
2021-05-09 02:48:34 +03:00
|
|
|
<div className={classnames("mx_ForwardDialog_preview", {
|
|
|
|
"mx_IRCLayout": previewLayout == Layout.IRC,
|
|
|
|
"mx_GroupLayout": previewLayout == Layout.Group,
|
|
|
|
})}>
|
|
|
|
<EventTile
|
|
|
|
mxEvent={mockEvent}
|
|
|
|
layout={previewLayout}
|
2021-06-02 01:09:51 +03:00
|
|
|
enableFlair={flairEnabled}
|
2021-05-10 07:40:54 +03:00
|
|
|
permalinkCreator={permalinkCreator}
|
2021-06-08 02:10:45 +03:00
|
|
|
as="div"
|
2021-05-09 02:48:34 +03:00
|
|
|
/>
|
|
|
|
</div>
|
2021-05-21 19:41:29 +03:00
|
|
|
<hr />
|
2021-06-05 18:15:06 +03:00
|
|
|
<div className="mx_ForwardList" id="mx_ForwardList">
|
2021-05-09 02:48:34 +03:00
|
|
|
<SearchBox
|
|
|
|
className="mx_textinput_icon mx_textinput_search"
|
2021-06-02 00:37:31 +03:00
|
|
|
placeholder={_t("Search for rooms or people")}
|
2021-05-09 02:48:34 +03:00
|
|
|
onSearch={setQuery}
|
|
|
|
autoComplete={true}
|
|
|
|
autoFocus={true}
|
|
|
|
/>
|
2021-06-05 18:15:06 +03:00
|
|
|
<AutoHideScrollbar className="mx_ForwardList_content">
|
2021-05-09 02:48:34 +03:00
|
|
|
{ rooms.length > 0 ? (
|
2021-05-19 20:33:48 +03:00
|
|
|
<div className="mx_ForwardList_results">
|
2021-06-21 14:29:59 +03:00
|
|
|
<TruncatedList
|
|
|
|
truncateAt={truncateAt}
|
|
|
|
createOverflowElement={overflowTile}
|
|
|
|
getChildren={(start, end) => rooms.slice(start, end).map(room =>
|
|
|
|
<Entry
|
|
|
|
key={room.roomId}
|
|
|
|
room={room}
|
|
|
|
event={event}
|
|
|
|
matrixClient={cli}
|
|
|
|
onFinished={onFinished}
|
|
|
|
/>,
|
|
|
|
)}
|
|
|
|
getChildCount={() => rooms.length}
|
|
|
|
/>
|
2021-05-09 02:48:34 +03:00
|
|
|
</div>
|
2021-05-19 20:33:48 +03:00
|
|
|
) : <span className="mx_ForwardList_noResults">
|
2021-05-09 02:48:34 +03:00
|
|
|
{ _t("No results") }
|
2021-05-19 20:33:48 +03:00
|
|
|
</span> }
|
2021-05-09 02:48:34 +03:00
|
|
|
</AutoHideScrollbar>
|
|
|
|
</div>
|
|
|
|
</BaseDialog>;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ForwardDialog;
|