1
0
mirror of https://github.com/spacebarchat/client.git synced 2024-11-22 18:32:34 +01:00

role colors in messages

This commit is contained in:
Puyodead1 2023-09-17 17:36:55 -04:00
parent 89ff9ef83f
commit abef1354ed
No known key found for this signature in database
GPG Key ID: A4FA4FEC0DD353FC
12 changed files with 89 additions and 35 deletions

View File

@ -1,4 +1,6 @@
import { runInAction } from "mobx";
import { observer } from "mobx-react-lite"; import { observer } from "mobx-react-lite";
import React from "react";
import styled from "styled-components"; import styled from "styled-components";
import useLogger from "../../hooks/useLogger"; import useLogger from "../../hooks/useLogger";
import { useAppStore } from "../../stores/AppStore"; import { useAppStore } from "../../stores/AppStore";
@ -77,13 +79,13 @@ function Chat({ channel, guild, guildId }: Props) {
const app = useAppStore(); const app = useAppStore();
const logger = useLogger("Messages"); const logger = useLogger("Messages");
// React.useEffect(() => { React.useEffect(() => {
// if (!channel || !guild) return; if (!channel || !guild) return;
// runInAction(() => { runInAction(() => {
// app.gateway.onChannelOpen(guild.id, channel.id); app.gateway.onChannelOpen(guild.id, channel.id);
// }); });
// }, [channel, guild]); }, [channel, guild]);
if (guildId && guildId === "@me") { if (guildId && guildId === "@me") {
return ( return (

View File

@ -9,6 +9,7 @@ import Avatar from "../Avatar";
import { Link } from "../Link"; import { Link } from "../Link";
import { IContextMenuItem } from "./../ContextMenuItem"; import { IContextMenuItem } from "./../ContextMenuItem";
import MessageAttachment from "./MessageAttachment"; import MessageAttachment from "./MessageAttachment";
import MessageAuthor from "./MessageAuthor";
import MessageBase from "./MessageBase"; import MessageBase from "./MessageBase";
import MessageEmbed from "./MessageEmbed"; import MessageEmbed from "./MessageEmbed";
import MessageTimestamp from "./MessageTimestamp"; import MessageTimestamp from "./MessageTimestamp";
@ -43,11 +44,6 @@ const MessageHeader = styled.div`
align-items: center; align-items: center;
`; `;
const MessageAuthor = styled.div`
font-size: 16px;
font-weight: var(--font-weight-medium);
`;
const MessageContent = styled.div<{ sending?: boolean; failed?: boolean }>` const MessageContent = styled.div<{ sending?: boolean; failed?: boolean }>`
font-size: 16px; font-size: 16px;
font-weight: var(--font-weight-light); font-weight: var(--font-weight-light);
@ -116,7 +112,7 @@ function Message({ message, isHeader, isSending, isFailed }: Props) {
<MessageContentContainer isHeader={showHeader}> <MessageContentContainer isHeader={showHeader}>
{showHeader && ( {showHeader && (
<MessageHeader> <MessageHeader>
<MessageAuthor>{message.author.username}</MessageAuthor> <MessageAuthor message={message} />
<MessageTimestamp date={message.timestamp} /> <MessageTimestamp date={message.timestamp} />
</MessageHeader> </MessageHeader>
)} )}

View File

@ -0,0 +1,45 @@
import { observer } from "mobx-react-lite";
import React from "react";
import styled from "styled-components";
import { useAppStore } from "../../stores/AppStore";
import { MessageLike } from "../../stores/objects/Message";
const Container = styled.div`
font-size: 16px;
font-weight: var(--font-weight-medium);
`;
interface Props {
message: MessageLike;
}
function MessageAuthor({ message }: Props) {
const app = useAppStore();
const [color, setColor] = React.useState<string | undefined>(undefined);
React.useEffect(() => {
if ("guild_id" in message && message.guild_id) {
const guild = app.guilds.get(message.guild_id);
if (!guild) return;
const member = guild.members.get(message.author.id);
if (!member) return;
const highestRole = member.roles.reduce((prev, role) => {
if (role.position > prev.position) return role;
return prev;
}, member.roles[0]);
setColor(highestRole?.color);
}
}, [message]);
return (
<Container
style={{
color,
}}
>
{message.author.username}
</Container>
);
}
export default observer(MessageAuthor);

View File

@ -100,7 +100,7 @@ function MessageInput({ channel }: Props) {
content: contentCopy, content: contentCopy,
files: attachmentsCopy, files: attachmentsCopy,
author: app.account!.raw, author: app.account!.raw,
channel: channel.id, channel_id: channel.id,
timestamp: new Date().toISOString(), timestamp: new Date().toISOString(),
type: MessageType.Default, type: MessageType.Default,
}); });

View File

@ -4,7 +4,6 @@ import secureLocalStorage from "react-secure-storage";
import Logger from "../utils/Logger"; import Logger from "../utils/Logger";
import REST from "../utils/REST"; import REST from "../utils/REST";
import AccountStore from "./AccountStore"; import AccountStore from "./AccountStore";
import ChannelStore from "./ChannelStore";
import ExperimentsStore from "./ExperimentsStore"; import ExperimentsStore from "./ExperimentsStore";
import GatewayConnectionStore from "./GatewayConnectionStore"; import GatewayConnectionStore from "./GatewayConnectionStore";
import GuildStore from "./GuildStore"; import GuildStore from "./GuildStore";
@ -34,7 +33,7 @@ export default class AppStore {
@observable account: AccountStore | null = null; @observable account: AccountStore | null = null;
@observable gateway = new GatewayConnectionStore(this); @observable gateway = new GatewayConnectionStore(this);
@observable guilds = new GuildStore(this); @observable guilds = new GuildStore(this);
@observable channels = new ChannelStore(this); // @observable channels = new ChannelStore(this);
@observable users = new UserStore(); @observable users = new UserStore();
@observable privateChannels = new PrivateChannelStore(this); @observable privateChannels = new PrivateChannelStore(this);
@observable rest = new REST(this); @observable rest = new REST(this);

View File

@ -63,21 +63,19 @@ export default class GuildMemberListStore {
data: [], data: [],
}); });
} else { } else {
// try to get the existing member const member = this.guild.members.get(item.member.id);
if (item.member.user?.id) { if (member) {
const member = this.guild.members.get(item.member.user.id); listData[listData.length - 1].data.push({
if (member) { member,
listData[listData.length - 1].data.push({ index: item.member.index,
member, });
index: item.member.index, } else {
}); const member = this.guild.members.add(item.member);
return; listData[listData.length - 1].data.push({
} member: member!,
index: item.member.index,
});
} }
listData[listData.length - 1].data.push({
member: new GuildMember(this.app, this.guild, item.member),
index: item.member.index,
});
} }
} }

View File

@ -26,7 +26,10 @@ export default class GuildMemberStore {
if (this.members.has(member.user.id)) { if (this.members.has(member.user.id)) {
return; return;
} }
this.members.set(member.user.id, new GuildMember(this.app, this.guild, member)); const m = new GuildMember(this.app, this.guild, member);
this.members.set(member.user.id, m);
console.log(`added member ${m.user?.username}`);
return m;
} }
@action @action

View File

@ -37,7 +37,7 @@ export default class MessageQueue {
@computed @computed
get(channel: Snowflake) { get(channel: Snowflake) {
return this.messages.filter((message) => message.channel === channel); return this.messages.filter((message) => message.channel_id === channel);
} }
@action @action

View File

@ -267,4 +267,10 @@ export default class Channel {
this.typing = null; this.typing = null;
} }
} }
@computed
get guild() {
if (!this.guildId) return undefined;
return this.app.guilds.get(this.guildId);
}
} }

View File

@ -103,6 +103,7 @@ export default class Guild {
this.roles.addAll(data.roles); this.roles.addAll(data.roles);
// FIXME: hack to prevent errors after guild creation where channels is undefined // FIXME: hack to prevent errors after guild creation where channels is undefined
if (data.channels) { if (data.channels) {
console.log(data.channels);
this.channels.addAll(data.channels); this.channels.addAll(data.channels);
} }

View File

@ -198,8 +198,9 @@ export default class Message extends MessageBase {
* It can be used to estimate the relative position of the message in a thread in company with `total_message_sent` on parent thread * It can be used to estimate the relative position of the message in a thread in company with `total_message_sent` on parent thread
*/ */
position?: number; position?: number;
guild_id?: Snowflake;
constructor(app: AppStore, data: APIMessage) { constructor(app: AppStore, data: APIMessage & { guild_id?: Snowflake }) {
super(app, data); super(app, data);
this.id = data.id; this.id = data.id;
@ -232,6 +233,9 @@ export default class Message extends MessageBase {
this.sticker_items = data.sticker_items; this.sticker_items = data.sticker_items;
this.stickers = data.stickers; this.stickers = data.stickers;
this.position = data.position; this.position = data.position;
if (data.guild_id) {
this.guild_id = data.guild_id;
}
makeObservable(this); makeObservable(this);
} }

View File

@ -10,7 +10,7 @@ export enum QueuedMessageStatus {
export type QueuedMessageData = { export type QueuedMessageData = {
id: string; id: string;
channel: string; channel_id: string;
content: string; content: string;
files?: File[]; files?: File[];
timestamp: string; timestamp: string;
@ -19,7 +19,7 @@ export type QueuedMessageData = {
}; };
export default class QueuedMessage extends MessageBase { export default class QueuedMessage extends MessageBase {
channel: string; channel_id: string;
files?: File[]; files?: File[];
@observable progress = 0; @observable progress = 0;
@observable status: QueuedMessageStatus; @observable status: QueuedMessageStatus;
@ -29,7 +29,7 @@ export default class QueuedMessage extends MessageBase {
constructor(app: AppStore, data: QueuedMessageData) { constructor(app: AppStore, data: QueuedMessageData) {
super(app, data); super(app, data);
this.id = data.id; this.id = data.id;
this.channel = data.channel; this.channel_id = data.channel_id;
this.files = data.files; this.files = data.files;
this.status = QueuedMessageStatus.SENDING; this.status = QueuedMessageStatus.SENDING;