2021-06-20 12:31:53 -04:00
|
|
|
import { User } from "revolt.js";
|
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";
|
|
|
|
|
|
|
|
import { useForceUpdate, useUser } from "../../../context/revoltjs/hooks";
|
2021-06-20 12:31:53 -04:00
|
|
|
import { MessageObject } from "../../../context/revoltjs/util";
|
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;
|
|
|
|
message: MessageObject;
|
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-07-09 05:10:36 -04:00
|
|
|
export function SystemMessage({ attachContext, message, highlight, hideInfo }: Props) {
|
2021-07-05 06:25:20 -04:00
|
|
|
const ctx = useForceUpdate();
|
2021-06-20 12:31:53 -04:00
|
|
|
|
2021-07-05 06:25:20 -04:00
|
|
|
let data: SystemMessageParsed;
|
|
|
|
let 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,
|
|
|
|
user: useUser(content.id, ctx) as User,
|
|
|
|
by: useUser(content.by, ctx) as User,
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "user_joined":
|
|
|
|
case "user_left":
|
|
|
|
case "user_kicked":
|
|
|
|
case "user_banned":
|
|
|
|
data = {
|
|
|
|
type: content.type,
|
|
|
|
user: useUser(content.id, ctx) as User,
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "channel_renamed":
|
|
|
|
data = {
|
|
|
|
type: "channel_renamed",
|
|
|
|
name: content.name,
|
|
|
|
by: useUser(content.by, ctx) as User,
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "channel_description_changed":
|
|
|
|
case "channel_icon_changed":
|
|
|
|
data = {
|
|
|
|
type: content.type,
|
|
|
|
by: useUser(content.by, ctx) as User,
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
data = { type: "text", content: JSON.stringify(content) };
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
data = { type: "text", content };
|
|
|
|
}
|
2021-06-20 12:31:53 -04:00
|
|
|
|
2021-07-05 06:25:20 -04:00
|
|
|
let children;
|
|
|
|
switch (data.type) {
|
|
|
|
case "text":
|
|
|
|
children = <span>{data.content}</span>;
|
|
|
|
break;
|
|
|
|
case "user_added":
|
|
|
|
case "user_remove":
|
|
|
|
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} />,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case "user_joined":
|
|
|
|
case "user_left":
|
|
|
|
case "user_kicked":
|
|
|
|
case "user_banned":
|
|
|
|
children = (
|
|
|
|
<TextReact
|
|
|
|
id={`app.main.channel.system.${data.type}`}
|
|
|
|
fields={{
|
|
|
|
user: <UserShort user={data.user} />,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case "channel_renamed":
|
|
|
|
children = (
|
|
|
|
<TextReact
|
|
|
|
id={`app.main.channel.system.channel_renamed`}
|
|
|
|
fields={{
|
|
|
|
user: <UserShort user={data.by} />,
|
|
|
|
name: <b>{data.name}</b>,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case "channel_description_changed":
|
|
|
|
case "channel_icon_changed":
|
|
|
|
children = (
|
|
|
|
<TextReact
|
|
|
|
id={`app.main.channel.system.${data.type}`}
|
|
|
|
fields={{
|
|
|
|
user: <UserShort user={data.by} />,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
2021-06-20 12:31:53 -04:00
|
|
|
|
2021-07-05 06:25:20 -04:00
|
|
|
return (
|
|
|
|
<MessageBase
|
2021-07-09 04:58:38 -04:00
|
|
|
highlight={highlight}
|
2021-07-05 06:25:20 -04:00
|
|
|
onContextMenu={
|
|
|
|
attachContext
|
|
|
|
? attachContextMenu("Menu", {
|
|
|
|
message,
|
|
|
|
contextualChannel: message.channel,
|
|
|
|
})
|
|
|
|
: undefined
|
|
|
|
}>
|
2021-07-09 05:10:36 -04:00
|
|
|
{ !hideInfo && <MessageInfo>
|
2021-07-05 06:25:20 -04:00
|
|
|
<MessageDetail message={message} position="left" />
|
2021-07-09 05:10:36 -04:00
|
|
|
</MessageInfo> }
|
2021-07-05 06:25:20 -04:00
|
|
|
<SystemContent>{children}</SystemContent>
|
|
|
|
</MessageBase>
|
|
|
|
);
|
2021-06-20 12:31:53 -04:00
|
|
|
}
|