Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

Commit

Permalink
improve patchOpenContextMenuLazy
Browse files Browse the repository at this point in the history
  • Loading branch information
userXinos committed May 5, 2022
1 parent afbfe1f commit b8315b3
Showing 1 changed file with 39 additions and 12 deletions.
51 changes: 39 additions & 12 deletions modules/Patcher/General.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module.exports = class General {
MessageContextMenu: this.contextMenuPatch.message,
GuildChannelUserContextMenu: this.contextMenuPatch.user,
DMUserContextMenu: this.contextMenuPatch.user,
UserGenericContextMenu: this.contextMenuPatch.user,
UserGenericContextMenu: (...args) => this.contextMenuPatch.user(...args, (r) => r[0].props.children),
GroupDMUserContextMenu: this.contextMenuPatch.user,
GroupDMContextMenu: this.contextMenuPatch.groupDM,
GuildContextMenu: this.contextMenuPatch.guild,
Expand Down Expand Up @@ -68,36 +68,62 @@ module.exports = class General {

patchOpenContextMenuLazy (id, menus) {
inject(id, getModule([ 'openContextMenuLazy' ], false), 'openContextMenuLazy', ([ event, lazyRender, params ]) => {
const warpLazyRender = async () => {
const wrapLazyRender = async () => {
const render = await lazyRender(event);

return (config) => {
const menu = render(config);
const CMName = menu?.type?.displayName;
const CMName = (menu?.type?.displayName)
? menu.type.displayName
: menu.type(config).props.children.type.displayName;

if (CMName) {
const moduleByDisplayName = getModuleByDisplayName(CMName, false);
const module = getModule((m) => m.default === menu.type || m.__powercordOriginal_default === menu.type, false);

if (moduleByDisplayName !== null) {
if (CMName in menus) {
if (CMName in menus) {
if (moduleByDisplayName !== null) {
this.injectWithSettings(`${CMName}.default`, menus[CMName]);
delete menus[CMName];
}
if (!Object.keys(menus).length) {
uninject(id);
} else if (module !== null) {
wrapAndInjectWithSettings.call(this, CMName, menus[CMName], module);
}

delete menus[CMName];
}

if (moduleByDisplayName !== null) {
menu.type = moduleByDisplayName;
} else if (module !== null) {
menu.type = module.default;
}
}
if (!Object.keys(menus).length) {
uninject(id);
}

return menu;
};
};

return [ event, warpLazyRender, params ];
return [ event, wrapLazyRender, params ];
}, true);

this.uninjectIDs.push(id);


function wrapAndInjectWithSettings (CMName, patch, module) {
const injectId = `image-tools${CMName.replace(/[A-Z]/g, (l) => `-${l.toLowerCase()}`)}`;
const memorizeRender = window._.memoize((render) => (...renderArgs) => (
patch.call(this, renderArgs, render(...renderArgs), this.settings)
));

inject(injectId, module, 'default', (args, res) => {
res.props.children.type = memorizeRender(res.props.children.type);
return res;
});

this.uninjectIDs.push(injectId);
}
}

get contextMenuPatch () {
Expand Down Expand Up @@ -144,7 +170,7 @@ module.exports = class General {
return res;
},

user ([ { user, guildId } ], res, settings) {
user ([ { user, guildId } ], res, settings, getUserContext = (e) => e) {
const { getGuild } = getModule([ 'getGuild' ], false);
const guildMemberAvatarURLParams = { userId: user.id, guildId };
const guildMemberAvatars = Object.entries(user.guildMemberAvatars);
Expand All @@ -170,7 +196,8 @@ module.exports = class General {
};

if (user.discriminator !== '0000') {
initButton(res.props.children.props.children, { images, settings });
const menu = findInReactTree(res, ({ props }) => props?.navId === 'user-context').props.children;
initButton(getUserContext(menu), { images, settings });
}

return res;
Expand Down

0 comments on commit b8315b3

Please sign in to comment.