|
| 1 | +import type { |
| 2 | + APIMessageInteraction, |
| 3 | + APIRole, |
| 4 | + APIUser, |
| 5 | + Snowflake, |
| 6 | +} from "discord-api-types/v10"; |
| 7 | +import { MessageType } from "discord-api-types/v10"; |
| 8 | +import Moment from "moment"; |
| 9 | +import React, { memo, useMemo, useRef } from "react"; |
| 10 | +import ChatTag from "../../ChatTag"; |
| 11 | +import Content from "../../Content"; |
| 12 | +import Tooltip from "../../Tooltip"; |
| 13 | +import { useConfig } from "../../core/ConfigContext"; |
| 14 | +import type { ChatMessage } from "../../types"; |
| 15 | +import type { GetAvatarOptions } from "../../utils/getAvatar"; |
| 16 | +import getAvatar from "../../utils/getAvatar"; |
| 17 | +import getDisplayName from "../../utils/getDisplayName"; |
| 18 | +import LargeTimestamp from "../LargeTimestamp"; |
| 19 | +import MessageAuthor from "../MessageAuthor"; |
| 20 | +import * as Styles from "../style/message"; |
| 21 | + |
| 22 | +interface ReplyInfoProps { |
| 23 | + channelId: Snowflake; |
| 24 | + referencedMessage: ChatMessage["referenced_message"]; |
| 25 | + mentioned?: boolean; |
| 26 | + interaction: APIMessageInteraction | undefined; |
| 27 | + isContextMenuInteraction?: boolean; |
| 28 | +} |
| 29 | + |
| 30 | +function getMiniAvatarUrl(user: APIUser) { |
| 31 | + const getAvatarSettings: GetAvatarOptions = { |
| 32 | + size: 16, |
| 33 | + }; |
| 34 | + |
| 35 | + return getAvatar(user, getAvatarSettings); |
| 36 | +} |
| 37 | + |
| 38 | +const FLAG_CROSSPOST = 1 << 1; |
| 39 | + |
| 40 | +const ReplyInfo = memo((props: ReplyInfoProps) => { |
| 41 | + const user = props.interaction |
| 42 | + ? props.interaction.user |
| 43 | + : props.referencedMessage?.author; |
| 44 | + |
| 45 | + const { resolveRole, resolveChannel, resolveMember, avatarUrlOverride } = |
| 46 | + useConfig(); |
| 47 | + const miniUserName = useMemo(() => { |
| 48 | + if (!props.interaction && !props.referencedMessage) return null; |
| 49 | + |
| 50 | + if (user === undefined) return null; |
| 51 | + |
| 52 | + if (!resolveChannel) return getDisplayName(user); |
| 53 | + |
| 54 | + const channel = resolveChannel(props.channelId); |
| 55 | + if ( |
| 56 | + !channel || |
| 57 | + !("guild_id" in channel) || |
| 58 | + !channel.guild_id || |
| 59 | + !props.referencedMessage |
| 60 | + ) |
| 61 | + return getDisplayName(user); |
| 62 | + |
| 63 | + const guildMember = resolveMember( |
| 64 | + props.referencedMessage.author, |
| 65 | + channel.guild_id |
| 66 | + ); |
| 67 | + |
| 68 | + if (!guildMember) return getDisplayName(user); |
| 69 | + |
| 70 | + return guildMember.nick ?? getDisplayName(guildMember.user as APIUser); |
| 71 | + }, [props.referencedMessage, props.interaction, resolveChannel]); |
| 72 | + |
| 73 | + const miniAvatarUrl = useMemo( |
| 74 | + () => |
| 75 | + user === undefined |
| 76 | + ? null |
| 77 | + : avatarUrlOverride?.(user) ?? getMiniAvatarUrl(user), |
| 78 | + [props.referencedMessage, props.interaction] |
| 79 | + ); |
| 80 | + |
| 81 | + const miniUserNameColorHex = useMemo(() => { |
| 82 | + if (!props.referencedMessage) return undefined; |
| 83 | + |
| 84 | + const channel = resolveChannel(props.referencedMessage.channel_id); |
| 85 | + if (!channel || !("guild_id" in channel) || !channel.guild_id) |
| 86 | + return undefined; |
| 87 | + |
| 88 | + const guildMember = resolveMember( |
| 89 | + props.referencedMessage.author, |
| 90 | + channel.guild_id |
| 91 | + ); |
| 92 | + |
| 93 | + if (!guildMember) return undefined; |
| 94 | + |
| 95 | + const [role] = guildMember.roles |
| 96 | + .map((id) => resolveRole(id)) |
| 97 | + .filter( |
| 98 | + (role): role is APIRole => |
| 99 | + role !== null && role !== undefined && role.color !== 0 |
| 100 | + ) |
| 101 | + .sort((a, b) => b.position - a.position); |
| 102 | + |
| 103 | + const color = role?.color; |
| 104 | + if (!color) return undefined; |
| 105 | + |
| 106 | + return color > 0 ? `#${color.toString(16).padStart(6, "0")}` : undefined; |
| 107 | + }, [resolveRole]); |
| 108 | + |
| 109 | + const unknownReply = !props.referencedMessage && !props.interaction; |
| 110 | + |
| 111 | + return ( |
| 112 | + <Styles.ReplyInfo> |
| 113 | + <Styles.ReplySpine /> |
| 114 | + {unknownReply ? ( |
| 115 | + <> |
| 116 | + <Styles.UnknownReplyIcon |
| 117 | + width={12} |
| 118 | + height={12} |
| 119 | + svg="IconUnknownReply" |
| 120 | + /> |
| 121 | + <Styles.UnknownReply> |
| 122 | + Original message was deleted or is unknown. |
| 123 | + </Styles.UnknownReply> |
| 124 | + </> |
| 125 | + ) : ( |
| 126 | + <Styles.ReplyUser> |
| 127 | + {miniAvatarUrl && ( |
| 128 | + <Styles.MiniUserAvatar src={miniAvatarUrl.stillAvatarUrl} /> |
| 129 | + )} |
| 130 | + {props.referencedMessage && ( |
| 131 | + <ChatTag |
| 132 | + author={props.referencedMessage.author} |
| 133 | + crossPost={Boolean( |
| 134 | + (props.referencedMessage.flags ?? 0) & FLAG_CROSSPOST |
| 135 | + )} |
| 136 | + referenceGuild={ |
| 137 | + props.referencedMessage.message_reference?.guild_id |
| 138 | + } |
| 139 | + /> |
| 140 | + )} |
| 141 | + <Styles.MiniUserName style={{ color: miniUserNameColorHex }}> |
| 142 | + {props.mentioned && "@"} |
| 143 | + {miniUserName} |
| 144 | + </Styles.MiniUserName> |
| 145 | + </Styles.ReplyUser> |
| 146 | + )} |
| 147 | + {props.referencedMessage ? ( |
| 148 | + <Content message={props.referencedMessage} isReplyContent={true} /> |
| 149 | + ) : ( |
| 150 | + props.interaction && ( |
| 151 | + <Styles.SlashCommand> |
| 152 | + used{" "} |
| 153 | + <Styles.SlashCommandText> |
| 154 | + {!props.isContextMenuInteraction ? "/" : ""} |
| 155 | + {props.interaction.name} |
| 156 | + </Styles.SlashCommandText> |
| 157 | + </Styles.SlashCommand> |
| 158 | + ) |
| 159 | + )} |
| 160 | + </Styles.ReplyInfo> |
| 161 | + ); |
| 162 | +}); |
| 163 | + |
| 164 | +ReplyInfo.displayName = "ReplyInfo"; |
| 165 | + |
| 166 | +// type Message = Omit<MessageData, "referencedMessage"> & Partial<MessageData>; |
| 167 | + |
| 168 | +interface EditMessageInputProps { |
| 169 | + // isFirstMessage?: boolean; |
| 170 | + message: ChatMessage; |
| 171 | + // isHovered?: boolean; |
| 172 | + // noThreadButton?: boolean; |
| 173 | + // isEditing?:boolean; |
| 174 | + // isContextMenuInteraction?: boolean; |
| 175 | + // hideTimestamp?: boolean; |
| 176 | + // overrides?: { |
| 177 | + // userMentioned?: boolean; |
| 178 | + // }; |
| 179 | +} |
| 180 | + |
| 181 | +function EditMessageInput(props: EditMessageInputProps) { |
| 182 | + const { handleMessageEditSubmit } = useConfig(); |
| 183 | + |
| 184 | + const submitMessageCallback = (content: string) => { |
| 185 | + if (!handleMessageEditSubmit || !content) return; |
| 186 | + |
| 187 | + handleMessageEditSubmit({ |
| 188 | + ...props.message, |
| 189 | + content: content, |
| 190 | + edited_timestamp: new Date().getMilliseconds().toString(), |
| 191 | + }); |
| 192 | + }; |
| 193 | + |
| 194 | + return ( |
| 195 | + <Styles.MessageEditor |
| 196 | + autoCorrect={"false"} |
| 197 | + onKeyDown={(e) => { |
| 198 | + if (e.key === "Enter") { |
| 199 | + submitMessageCallback(e.target.value); |
| 200 | + } |
| 201 | + }} |
| 202 | + defaultValue={props.message.content} |
| 203 | + /> |
| 204 | + ); |
| 205 | +} |
| 206 | + |
| 207 | +export default EditMessageInput; |
0 commit comments