Files
ThreadNet-Web/src/components/views/elements/Pill.tsx
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

177 lines
6.0 KiB
TypeScript
Raw Normal View History

/*
2021-03-05 18:49:32 -07:00
Copyright 2017 - 2019, 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.
*/
2022-05-05 11:13:09 +02:00
import React, { ReactElement, useState } from "react";
import classNames from "classnames";
import { Room } from "matrix-js-sdk/src/models/room";
2023-03-21 10:23:20 +01:00
import { RoomMember } from "matrix-js-sdk/src/matrix";
2021-10-22 17:23:32 -05:00
2021-06-29 13:11:58 +01:00
import { MatrixClientPeg } from "../../../MatrixClientPeg";
2019-12-17 17:26:12 +00:00
import MatrixClientContext from "../../../contexts/MatrixClientContext";
2023-03-08 13:06:50 +01:00
import Tooltip, { Alignment } from "../elements/Tooltip";
import { usePermalink } from "../../../hooks/usePermalink";
import RoomAvatar from "../avatars/RoomAvatar";
import MemberAvatar from "../avatars/MemberAvatar";
2023-03-21 10:23:20 +01:00
import { _t } from "../../../languageHandler";
import { Icon as LinkIcon } from "../../../../res/img/element-icons/room/composer/link.svg";
import { Icon as UserIcon } from "../../../../res/img/compound/user.svg";
2022-05-05 11:13:09 +02:00
export enum PillType {
UserMention = "TYPE_USER_MENTION",
RoomMention = "TYPE_ROOM_MENTION",
AtRoomMention = "TYPE_AT_ROOM_MENTION", // '@room' mention
2023-03-21 10:23:20 +01:00
EventInSameRoom = "TYPE_EVENT_IN_SAME_ROOM",
EventInOtherRoom = "TYPE_EVENT_IN_OTHER_ROOM",
2022-05-05 11:13:09 +02:00
}
2023-03-08 13:06:50 +01:00
export const pillRoomNotifPos = (text: string): number => {
return text.indexOf("@room");
};
export const pillRoomNotifLen = (): number => {
return "@room".length;
};
2023-03-21 10:23:20 +01:00
const PillRoomAvatar: React.FC<{
shouldShowPillAvatar: boolean;
room: Room | null;
}> = ({ shouldShowPillAvatar, room }) => {
if (!shouldShowPillAvatar) {
return null;
}
if (room) {
return <RoomAvatar room={room} width={16} height={16} aria-hidden="true" />;
}
return <LinkIcon className="mx_Pill_LinkIcon mx_BaseAvatar mx_BaseAvatar_image" />;
};
const PillMemberAvatar: React.FC<{
shouldShowPillAvatar: boolean;
member: RoomMember | null;
}> = ({ shouldShowPillAvatar, member }) => {
if (!shouldShowPillAvatar) {
return null;
}
if (member) {
return <MemberAvatar member={member} width={16} height={16} aria-hidden="true" hideTitle />;
}
return <UserIcon className="mx_Pill_UserIcon mx_BaseAvatar mx_BaseAvatar_image" />;
};
2023-03-08 13:06:50 +01:00
export interface PillProps {
2022-05-05 11:13:09 +02:00
// The Type of this Pill. If url is given, this is auto-detected.
type?: PillType;
// The URL to pillify (no validation is done)
url?: string;
2023-03-08 13:06:50 +01:00
/** Whether the pill is in a message. It will act as a link then. */
2022-05-05 11:13:09 +02:00
inMessage?: boolean;
// The room in which this pill is being rendered
room?: Room;
// Whether to include an avatar in the pill
shouldShowPillAvatar?: boolean;
}
2023-03-21 10:23:20 +01:00
export const Pill: React.FC<PillProps> = ({ type: propType, url, inMessage, room, shouldShowPillAvatar = true }) => {
2023-03-08 13:06:50 +01:00
const [hover, setHover] = useState(false);
const { member, onClick, resourceId, targetRoom, text, type } = usePermalink({
2023-03-08 13:06:50 +01:00
room,
type: propType,
url,
});
2022-05-05 11:13:09 +02:00
if (!type || !text) {
2023-03-08 13:06:50 +01:00
return null;
2020-08-29 12:14:16 +01:00
}
2023-03-08 13:06:50 +01:00
const classes = classNames("mx_Pill", {
mx_AtRoomPill: type === PillType.AtRoomMention,
mx_RoomPill: type === PillType.RoomMention,
mx_SpacePill: type === "space",
mx_UserPill: type === PillType.UserMention,
mx_UserPill_me: resourceId === MatrixClientPeg.get().getUserId(),
2023-03-21 10:23:20 +01:00
mx_EventPill: type === PillType.EventInOtherRoom || type === PillType.EventInSameRoom,
2023-03-08 13:06:50 +01:00
});
2020-08-29 12:14:16 +01:00
2023-03-08 13:06:50 +01:00
const onMouseOver = (): void => {
setHover(true);
2021-02-12 08:18:27 +01:00
};
2023-03-08 13:06:50 +01:00
const onMouseLeave = (): void => {
setHover(false);
2021-02-12 08:18:27 +01:00
};
2023-03-08 13:06:50 +01:00
const tip = hover && resourceId ? <Tooltip label={resourceId} alignment={Alignment.Right} /> : null;
2023-03-21 10:23:20 +01:00
let content: (ReactElement | string)[] = [];
const textElement = <span className="mx_Pill_text">{text}</span>;
switch (type) {
2023-03-21 10:23:20 +01:00
case PillType.EventInOtherRoom:
{
const avatar = <PillRoomAvatar shouldShowPillAvatar={shouldShowPillAvatar} room={targetRoom} />;
content = [_t("Message in"), avatar || " ", textElement];
}
break;
case PillType.EventInSameRoom:
{
const avatar = <PillMemberAvatar shouldShowPillAvatar={shouldShowPillAvatar} member={member} />;
content = [_t("Message from"), avatar || " ", textElement];
}
break;
case PillType.AtRoomMention:
case PillType.RoomMention:
case "space":
2023-03-21 10:23:20 +01:00
{
const avatar = <PillRoomAvatar shouldShowPillAvatar={shouldShowPillAvatar} room={targetRoom} />;
content = [avatar, textElement];
}
break;
case PillType.UserMention:
2023-03-21 10:23:20 +01:00
{
const avatar = <PillMemberAvatar shouldShowPillAvatar={shouldShowPillAvatar} member={member} />;
content = [avatar, textElement];
}
break;
default:
return null;
}
2023-03-08 13:06:50 +01:00
return (
<bdi>
<MatrixClientContext.Provider value={MatrixClientPeg.get()}>
{inMessage && url ? (
<a
className={classes}
href={url}
onClick={onClick}
onMouseOver={onMouseOver}
onMouseLeave={onMouseLeave}
>
{content}
2023-03-08 13:06:50 +01:00
{tip}
</a>
) : (
<span className={classes} onMouseOver={onMouseOver} onMouseLeave={onMouseLeave}>
{content}
2023-03-08 13:06:50 +01:00
{tip}
</span>
)}
</MatrixClientContext.Provider>
</bdi>
);
};