-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
236 lines (199 loc) · 7.04 KB
/
index.js
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
* Copyright (c) 2020 - 2021 NurMarvin (Marvin Witt)
* Licensed under the Open Software License version 3.0
*/
const { Plugin } = require("powercord/entities");
const { inject, uninject } = require("powercord/injector");
const {
React,
getModule,
getModuleByDisplayName,
FluxDispatcher,
subscribe,
i18n: { Messages },
} = require("powercord/webpack");
const { open } = require("powercord/modal");
const { findInReactTree } = require("powercord/util");
const i18n = require("./i18n");
const GuildProfileModal = require("./components/GuildProfileModal");
const GuildProfileIcon = require("./components/GuildProfileIcon");
const memberCountsStore = require("./memberCountsStore/store");
const memberCountsActions = require("./memberCountsStore/actions");
const { getCurrentUser } = getModule(["getCurrentUser"], false);
module.exports = class GuildProfile extends Plugin {
async startPlugin() {
powercord.api.i18n.loadAllStrings(i18n);
this.loadStylesheet("styles.scss");
this._injectContextMenu();
this._injectMenu();
this.handleMemberListUpdate = this.handleMemberListUpdate.bind(this);
FluxDispatcher.subscribe(
"GUILD_MEMBER_LIST_UPDATE",
this.handleMemberListUpdate
);
}
handleMemberListUpdate(memberListUpdate) {
this.updateMemberCounts(memberListUpdate);
}
getMemberCounts(id) {
return new Promise((resolve) => {
const memberCounts = memberCountsStore.getMemberCounts(id);
// If the member count is in the Flux store just send that data
if (memberCounts) {
resolve(memberCounts);
return;
}
const { requestMembers } = getModule(["requestMembers"], false);
requestMembers(id);
const updateMemberCounts = (memberListUpdate) => {
return this.updateMemberCounts(memberListUpdate);
};
function onReceived(memberListUpdate) {
if (memberListUpdate.guildId === id) {
resolve(updateMemberCounts(memberListUpdate));
}
}
FluxDispatcher.subscribe("GUILD_MEMBER_LIST_UPDATE", onReceived);
});
}
updateMemberCounts(memberListUpdate) {
const { guildId, memberCount, groups } = memberListUpdate;
const onlineCount = groups
.map((group) => (group.id != "offline" ? group.count : 0))
.reduce((a, b) => {
return a + b;
}, 0);
const memberCounts = { guildId, memberCount, onlineCount };
memberCountsActions.updateMemberCounts(memberCounts);
return memberCounts;
}
async _injectContextMenu() {
const Menu = await getModule(["MenuItem"]);
const getMemberCounts = (guildId) => {
return this.getMemberCounts(guildId);
};
subscribe(x => x.default?.displayName === "GuildContextMenuWrapper", GuildContextMenuWrapper => {
inject("guild-profile-context-menu", GuildContextMenuWrapper, "default", ([{ guild }], res) => {
const renderContextMenu = res.props.children.type;
res.props.children.type = (props) => {
const contextMenu = renderContextMenu(props);
contextMenu.props.children.unshift(
React.createElement(
Menu.MenuGroup,
null,
React.createElement(Menu.MenuItem, {
id: "guild-profile",
label: Messages.GUILD_PROFILE,
action: () =>
this._openModalHandler(() =>
React.createElement(GuildProfileModal, {
guild,
section: "GUILD_INFO",
getMemberCounts,
})
),
})
)
);
return contextMenu;
};
return res;
})
});
}
async _injectMenu() {
const id = "guild-profile";
const Menu = await getModule(["MenuItem"]);
const { getGuild } = await getModule(["getGuild"]);
const { getGuildId } = await getModule(["getLastSelectedGuildId"]);
const getMemberCounts = (guildId) => {
return this.getMemberCounts(guildId);
};
inject("guild-profile-menu", Menu, "default", ([{ children }], res) => {
const menuId = res.props.children.props.id;
if (menuId !== "guild-header-popout")
return res;
if (!findInReactTree(res, (c) => c.props && c.props.id == id)) {
children.unshift(
React.createElement(
Menu.MenuGroup,
null,
React.createElement(Menu.MenuItem, {
id,
label: Messages.GUILD_PROFILE,
icon: () => React.createElement(GuildProfileIcon),
action: () =>
this._openModalHandler(() =>
React.createElement(GuildProfileModal, {
guild: getGuild(getGuildId()),
section: "GUILD_INFO",
getMemberCounts,
})
),
})
)
);
}
return res;
});
Menu.default.displayName = "Menu";
}
_openModalHandler(element) {
const { openUserProfileModal } = getModule(["openUserProfileModal"], false);
const module = getModule(["openModalLazy"], false);
if (getModuleByDisplayName("UserProfileModal", false) !== null) {
open(element);
return;
}
// So, the modules that are needed to render our modal are in another chunk, which is still not loaded.
// We call a function that loads modules for UserProfileModal,
// but the user will not see the final UserProfileModal -
// since it will be replaced at the last stage with our element
const { ModalRoot } = getModule(["ModalRoot"], false);
const userId = getCurrentUser().id;
inject(
"guild-profile-open-modal-lazy",
module,
"openModalLazy",
([initLazyRender]) => {
const warpInitLazyRender = () => {
const lazyRender = initLazyRender();
return new Promise(async (resolve) => {
const render = await lazyRender;
resolve((event) => {
const res = render(event);
if (res?.type?.displayName === "UserProfileModal") {
const { props } = res;
const { root } = getModule(["root", "body"], false);
if (props.guildId === "@me" && props.user.id === userId) {
if (props.transitionState === 3) {
// = close modal
uninject("guild-profile-open-modal-lazy");
}
return React.createElement(ModalRoot, {
transitionState: props.transitionState,
className: root,
children: element(),
});
}
}
return res;
});
});
};
return [warpInitLazyRender];
},
true
);
openUserProfileModal({ userId });
}
pluginWillUnload() {
uninject("guild-profile-context-menu");
uninject("guild-profile-menu");
uninject("guild-profile-open-modal-lazy");
FluxDispatcher.unsubscribe(
"GUILD_MEMBER_LIST_UPDATE",
this.handleMemberListUpdate
);
}
};