Plugin

PreviewMessage

Lets you preview your message before sending it.

Chat Utility
index.tsx
Download

Source

src/plugins/previewMessage/index.tsx
1import { ChatBarButton, ChatBarButtonFactory } from "@api/ChatButtons";
2import { generateId, sendBotMessage } from "@api/Commands";
3import { Devs } from "@utils/constants";
4import definePlugin, { IconComponent, StartAt } from "@utils/types";
5import { CloudUpload, MessageAttachment } from "@vencord/discord-types";
6import { DraftStore, DraftType, UploadAttachmentStore, UserStore, useStateFromStores } from "@webpack/common";
7
8const getDraft = (channelId: string) => DraftStore.getDraft(channelId, DraftType.ChannelMessage);
9
10
11const getImageBox = (url: string): Promise<{ width: number, height: number; } | null> =>
12 new Promise(res => {
13 const img = new Image();
14 img.onload = () =>
15 res({ width: img.width, height: img.height });
16
17 img.onerror = () =>
18 res(null);
19
20 img.src = url;
21 });
22
23
24const getAttachments = async (channelId: string) =>
25 await Promise.all(
26 UploadAttachmentStore.getUploads(channelId, DraftType.ChannelMessage)
27 .map(async (upload: CloudUpload) => {
28 const { isImage, filename, spoiler, item: { file } } = upload;
29 const url = URL.createObjectURL(file);
30 const attachment: MessageAttachment = {
31 id: generateId(),
32 filename: spoiler ? "SPOILER_" + filename : filename,
33 // weird eh? if i give it the normal content type the preview doenst work
34 content_type: undefined,
35 size: upload.getSize(),
36 spoiler,
37 // discord adds query params to the url, so we need to add a hash to prevent that
38 url: url + "#",
39 proxy_url: url + "#",
40 };
41
42 if (isImage) {
43 const box = await getImageBox(url);
44 if (!box) return attachment;
45
46 attachment.width = box.width;
47 attachment.height = box.height;
48 }
49
50 return attachment;
51 })
52 );
53
54
55const PreviewIcon: IconComponent = ({ height = 20, width = 20, className }) => {
56 return (
57 <svg
58 fill="currentColor"
59 fillRule="evenodd"
60 width={width}
61 height={height}
62 className={className}
63 viewBox="0 0 24 24"
64 style={{ scale: "1.096", translate: "0 -1px" }}
65 >
66 <path d="M22.89 11.7c.07.2.07.4 0 .6C22.27 13.9 19.1 21 12 21c-7.11 0-10.27-7.11-10.89-8.7a.83.83 0 0 1 0-.6C1.73 10.1 4.9 3 12 3c7.11 0 10.27 7.11 10.89 8.7Zm-4.5-3.62A15.11 15.11 0 0 1 20.85 12c-.38.88-1.18 2.47-2.46 3.92C16.87 17.62 14.8 19 12 19c-2.8 0-4.87-1.38-6.39-3.08A15.11 15.11 0 0 1 3.15 12c.38-.88 1.18-2.47 2.46-3.92C7.13 6.38 9.2 5 12 5c2.8 0 4.87 1.38 6.39 3.08ZM15.56 11.77c.2-.1.44.02.44.23a4 4 0 1 1-4-4c.21 0 .33.25.23.44a2.5 2.5 0 0 0 3.32 3.32Z" />
67 </svg>
68 );
69};
70
71const PreviewButton: ChatBarButtonFactory = ({ isAnyChat, isEmpty, type: { attachments }, channel: { id: channelId } }) => {
72 const draft = useStateFromStores([DraftStore], () => getDraft(channelId));
73
74 if (!isAnyChat) return null;
75
76 const hasAttachments = attachments && UploadAttachmentStore.getUploads(channelId, DraftType.ChannelMessage).length > 0;
77 const hasContent = !isEmpty && draft?.length > 0;
78
79 if (!hasContent && !hasAttachments) return null;
80
81 return (
82 <ChatBarButton
83 tooltip="Preview Message"
84 onClick={async () =>
85 sendBotMessage(
86 channelId,
87 {
88 content: getDraft(channelId),
89 author: UserStore.getCurrentUser(),
90 attachments: hasAttachments ? await getAttachments(channelId) : undefined,
91 }
92 )}
93 buttonProps={{
94 style: {
95 translate: "0 2px"
96 }
97 }}
98 >
99 <PreviewIcon />
100 </ChatBarButton>
101 );
102
103};
104
105export default definePlugin({
106 name: "PreviewMessage",
107 description: "Lets you preview your message before sending it.",
108 tags: ["Chat", "Utility"],
109 authors: [Devs.Aria],
110 // start early to ensure we're the first plugin to add our button
111 // This makes the popping in less awkward
112 startAt: StartAt.Init,
113
114 chatBarButton: {
115 icon: PreviewIcon,
116 render: PreviewButton
117 }
118});
119