2021-08-06 18:34:01 -04:00
|
|
|
import {
|
|
|
|
InfoCircle,
|
|
|
|
UserPlus,
|
|
|
|
UserMinus,
|
|
|
|
ArrowToRight,
|
|
|
|
ArrowToLeft,
|
|
|
|
UserX,
|
|
|
|
ShieldX,
|
|
|
|
EditAlt,
|
|
|
|
Edit,
|
|
|
|
MessageSquareEdit,
|
|
|
|
} from "@styled-icons/boxicons-solid";
|
2021-07-29 10:51:19 -04:00
|
|
|
import { observer } from "mobx-react-lite";
|
2021-07-30 17:40:49 -04:00
|
|
|
import { Message } from "revolt.js/dist/maps/Messages";
|
|
|
|
import { User } from "revolt.js/dist/maps/Users";
|
2021-06-21 16:11:53 -04:00
|
|
|
import styled from "styled-components";
|
2021-07-05 06:23:23 -04:00
|
|
|
|
2021-06-20 12:31:53 -04:00
|
|
|
import { attachContextMenu } from "preact-context-menu";
|
2021-07-05 06:23:23 -04:00
|
|
|
|
|
|
|
import { TextReact } from "../../../lib/i18n";
|
|
|
|
|
2021-07-30 17:40:49 -04:00
|
|
|
import { useClient } from "../../../context/revoltjs/RevoltClient";
|
2021-07-05 06:23:23 -04:00
|
|
|
|
|
|
|
import UserShort from "../user/UserShort";
|
2021-06-20 12:31:53 -04:00
|
|
|
import MessageBase, { MessageDetail, MessageInfo } from "./MessageBase";
|
|
|
|
|
|
|
|
const SystemContent = styled.div`
|
2021-07-05 06:25:20 -04:00
|
|
|
gap: 4px;
|
|
|
|
display: flex;
|
|
|
|
padding: 2px 0;
|
|
|
|
flex-wrap: wrap;
|
|
|
|
align-items: center;
|
|
|
|
flex-direction: row;
|
2021-06-20 12:31:53 -04:00
|
|
|
`;
|
|
|
|
|
|
|
|
type SystemMessageParsed =
|
2021-07-05 06:25:20 -04:00
|
|
|
| { type: "text"; content: string }
|
|
|
|
| { type: "user_added"; user: User; by: User }
|
|
|
|
| { type: "user_remove"; user: User; by: User }
|
|
|
|
| { type: "user_joined"; user: User }
|
|
|
|
| { type: "user_left"; user: User }
|
|
|
|
| { type: "user_kicked"; user: User }
|
|
|
|
| { type: "user_banned"; user: User }
|
|
|
|
| { type: "channel_renamed"; name: string; by: User }
|
|
|
|
| { type: "channel_description_changed"; by: User }
|
|
|
|
| { type: "channel_icon_changed"; by: User };
|
2021-06-20 12:31:53 -04:00
|
|
|
|
|
|
|
interface Props {
|
2021-07-05 06:25:20 -04:00
|
|
|
attachContext?: boolean;
|
2021-07-30 17:40:49 -04:00
|
|
|
message: Message;
|
2021-07-09 04:58:38 -04:00
|
|
|
highlight?: boolean;
|
2021-07-09 05:10:36 -04:00
|
|
|
hideInfo?: boolean;
|
2021-06-20 12:31:53 -04:00
|
|
|
}
|
|
|
|
|
2021-08-06 18:34:01 -04:00
|
|
|
const iconDictionary = {
|
|
|
|
user_added: UserPlus,
|
|
|
|
user_remove: UserMinus,
|
|
|
|
user_joined: ArrowToRight,
|
|
|
|
user_left: ArrowToLeft,
|
|
|
|
user_kicked: UserX,
|
|
|
|
user_banned: ShieldX,
|
|
|
|
channel_renamed: EditAlt,
|
|
|
|
channel_description_changed: Edit,
|
|
|
|
channel_icon_changed: MessageSquareEdit,
|
|
|
|
text: InfoCircle,
|
|
|
|
};
|
|
|
|
|
2021-07-29 10:51:19 -04:00
|
|
|
export const SystemMessage = observer(
|
|
|
|
({ attachContext, message, highlight, hideInfo }: Props) => {
|
2021-07-30 17:40:49 -04:00
|
|
|
const client = useClient();
|
2021-07-29 10:51:19 -04:00
|
|
|
|
|
|
|
let data: SystemMessageParsed;
|
|
|
|
const content = message.content;
|
|
|
|
if (typeof content === "object") {
|
|
|
|
switch (content.type) {
|
|
|
|
case "text":
|
|
|
|
data = content;
|
|
|
|
break;
|
|
|
|
case "user_added":
|
|
|
|
case "user_remove":
|
|
|
|
data = {
|
|
|
|
type: content.type,
|
2021-07-30 17:40:49 -04:00
|
|
|
user: client.users.get(content.id)!,
|
|
|
|
by: client.users.get(content.by)!,
|
2021-07-29 10:51:19 -04:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "user_joined":
|
|
|
|
case "user_left":
|
|
|
|
case "user_kicked":
|
|
|
|
case "user_banned":
|
|
|
|
data = {
|
|
|
|
type: content.type,
|
2021-07-30 17:40:49 -04:00
|
|
|
user: client.users.get(content.id)!,
|
2021-07-29 10:51:19 -04:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "channel_renamed":
|
|
|
|
data = {
|
|
|
|
type: "channel_renamed",
|
|
|
|
name: content.name,
|
2021-07-30 17:40:49 -04:00
|
|
|
by: client.users.get(content.by)!,
|
2021-07-29 10:51:19 -04:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "channel_description_changed":
|
|
|
|
case "channel_icon_changed":
|
|
|
|
data = {
|
|
|
|
type: content.type,
|
2021-07-30 17:40:49 -04:00
|
|
|
by: client.users.get(content.by)!,
|
2021-07-29 10:51:19 -04:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
data = { type: "text", content: JSON.stringify(content) };
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
data = { type: "text", content };
|
|
|
|
}
|
2021-06-20 12:31:53 -04:00
|
|
|
|
2021-08-06 18:34:01 -04:00
|
|
|
const SystemMessageIcon = iconDictionary[data.type] ?? InfoCircle;
|
|
|
|
|
|
|
|
const SystemIcon = styled(SystemMessageIcon)`
|
|
|
|
height: 1.33em;
|
|
|
|
width: 1.33em;
|
|
|
|
margin-right: 0.5em;
|
|
|
|
color: var(--tertiary-foreground);
|
|
|
|
`;
|
|
|
|
|
2021-07-29 10:51:19 -04:00
|
|
|
let children;
|
|
|
|
switch (data.type) {
|
2021-07-05 06:25:20 -04:00
|
|
|
case "text":
|
2021-07-29 10:51:19 -04:00
|
|
|
children = <span>{data.content}</span>;
|
2021-07-05 06:25:20 -04:00
|
|
|
break;
|
|
|
|
case "user_added":
|
|
|
|
case "user_remove":
|
2021-07-29 10:51:19 -04:00
|
|
|
children = (
|
|
|
|
<TextReact
|
|
|
|
id={`app.main.channel.system.${
|
|
|
|
data.type === "user_added"
|
|
|
|
? "added_by"
|
|
|
|
: "removed_by"
|
|
|
|
}`}
|
|
|
|
fields={{
|
|
|
|
user: <UserShort user={data.user} />,
|
|
|
|
other_user: <UserShort user={data.by} />,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
2021-07-05 06:25:20 -04:00
|
|
|
break;
|
|
|
|
case "user_joined":
|
|
|
|
case "user_left":
|
|
|
|
case "user_kicked":
|
|
|
|
case "user_banned":
|
2021-07-29 10:51:19 -04:00
|
|
|
children = (
|
|
|
|
<TextReact
|
|
|
|
id={`app.main.channel.system.${data.type}`}
|
|
|
|
fields={{
|
|
|
|
user: <UserShort user={data.user} />,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
2021-07-05 06:25:20 -04:00
|
|
|
break;
|
|
|
|
case "channel_renamed":
|
2021-07-29 10:51:19 -04:00
|
|
|
children = (
|
|
|
|
<TextReact
|
|
|
|
id={`app.main.channel.system.channel_renamed`}
|
|
|
|
fields={{
|
|
|
|
user: <UserShort user={data.by} />,
|
|
|
|
name: <b>{data.name}</b>,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
2021-07-05 06:25:20 -04:00
|
|
|
break;
|
|
|
|
case "channel_description_changed":
|
|
|
|
case "channel_icon_changed":
|
2021-07-29 10:51:19 -04:00
|
|
|
children = (
|
|
|
|
<TextReact
|
|
|
|
id={`app.main.channel.system.${data.type}`}
|
|
|
|
fields={{
|
|
|
|
user: <UserShort user={data.by} />,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
2021-07-05 06:25:20 -04:00
|
|
|
break;
|
|
|
|
}
|
2021-06-20 12:31:53 -04:00
|
|
|
|
2021-07-29 10:51:19 -04:00
|
|
|
return (
|
|
|
|
<MessageBase
|
|
|
|
highlight={highlight}
|
|
|
|
onContextMenu={
|
|
|
|
attachContext
|
|
|
|
? attachContextMenu("Menu", {
|
|
|
|
message,
|
|
|
|
contextualChannel: message.channel,
|
|
|
|
})
|
|
|
|
: undefined
|
|
|
|
}>
|
|
|
|
{!hideInfo && (
|
|
|
|
<MessageInfo>
|
|
|
|
<MessageDetail message={message} position="left" />
|
2021-08-06 18:34:01 -04:00
|
|
|
<SystemIcon className="system-message-icon" />
|
2021-07-29 10:51:19 -04:00
|
|
|
</MessageInfo>
|
|
|
|
)}
|
|
|
|
<SystemContent>{children}</SystemContent>
|
|
|
|
</MessageBase>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|