forked from jaimeadf/BetterDiscordPlugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.jsx
More file actions
106 lines (86 loc) · 3.43 KB
/
index.jsx
File metadata and controls
106 lines (86 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import React from 'react';
import { DiscordModules, WebpackModules, Patcher, DCM } from '@zlibrary/api';
import Plugin from '@zlibrary/plugin';
import { useStateFromStores } from '@discord/Flux';
import { ModalRoot, ModalSize } from '@discord/components/Modal';
const { StreamStore, StreamPreviewStore, ModalActions } = DiscordModules;
const ImageModal = WebpackModules.getByDisplayName('ImageModal');
const MaskedLink = WebpackModules.getByDisplayName('MaskedLink');
const Menu = WebpackModules.getByProps('MenuItem');
export default class BiggerStreamPreview extends Plugin {
onStart() {
this.patchUserContextMenus();
this.patchStreamContextMenu();
}
onStop() {
Patcher.unpatchAll();
}
async patchUserContextMenus() {
const module = await DCM.getDiscordMenu('useUserRolesItems');
Patcher.after(module, 'default', (thisObject, [userId], returnValue) => {
const [stream, previewURL] = useStateFromStores([StreamStore, StreamPreviewStore], () => {
const stream = StreamStore.getStreamForUser(userId);
const previewURL = stream
? StreamPreviewStore.getPreviewURL(stream.guildId, stream.channelId, stream.ownerId)
: null;
return [stream, previewURL];
});
if (!stream) {
return;
}
return (
<Menu.MenuGroup>
{returnValue}
<Menu.MenuSeparator />
{this.buildPreviewMenuItem(previewURL)}
</Menu.MenuGroup>
);
});
}
async patchStreamContextMenu() {
const module = await DCM.getDiscordMenu('StreamContextMenu');
Patcher.after(module, 'default', (thisObject, [{ stream }], returnValue) => {
const previewURL = useStateFromStores([StreamPreviewStore], () => {
return StreamPreviewStore.getPreviewURL(stream.guildId, stream.channelId, stream.ownerId);
});
returnValue.props.children.props.children.push(
<Menu.MenuGroup>{this.buildPreviewMenuItem(previewURL)}</Menu.MenuGroup>
);
});
}
buildPreviewMenuItem(previewURL) {
return (
<Menu.MenuItem
id="stream-preview"
key="stream-preview"
label="View Stream Preview"
action={() => this.openImageModal(previewURL)}
disabled={previewURL === null}
/>
);
}
async openImageModal(url) {
const image = await this.fetchImage(url);
ModalActions.openModal(props => (
<ModalRoot className="modal-3Crloo" size={ModalSize.DYNAMIC} {...props}>
<ImageModal
className="image-36HiZc"
src={url}
original={url}
width={image.width}
height={image.height}
renderLinkComponent={props => <MaskedLink {...props} />}
shouldAnimate={true}
/>
</ModalRoot>
));
}
async fetchImage(url) {
return new Promise((resolve, reject) => {
const image = new Image();
image.src = url;
image.addEventListener('load', () => resolve(image));
image.addEventListener('error', () => reject('Unable to fetch image.'));
});
}
}