PermViewer: Fix context menu for roleless users & muted channels (#1138)

Co-authored-by: V <vendicated@riseup.net>
Co-authored-by: Nuckyz <61953774+Nuckyz@users.noreply.github.com>
This commit is contained in:
V 2023-05-16 00:19:20 +02:00 committed by GitHub
parent bb83c0b672
commit 263884cbd8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 20 deletions

View file

@ -57,6 +57,8 @@ export const settings = definePluginSettings({
});
function MenuItem(guildId: string, id?: string, type?: MenuItemParentType) {
if (type === MenuItemParentType.User && !GuildMemberStore.isMember(guildId, id!)) return null;
return (
<Menu.MenuItem
id="perm-viewer-permissions"
@ -122,25 +124,32 @@ function MenuItem(guildId: string, id?: string, type?: MenuItemParentType) {
);
}
function makeContextMenuPatch(childId: string, type?: MenuItemParentType): NavContextMenuPatchCallback {
function makeContextMenuPatch(childId: string | string[], type?: MenuItemParentType): NavContextMenuPatchCallback {
return (children, props) => () => {
if (!props) return children;
const group = findGroupChildrenByChildId(childId, children);
if (group) {
const item = (() => {
switch (type) {
case MenuItemParentType.User:
group.push(MenuItem(props.guildId, props.user.id, type));
break;
return MenuItem(props.guildId, props.user.id, type);
case MenuItemParentType.Channel:
group.push(MenuItem(props.guild.id, props.channel.id, type));
break;
return MenuItem(props.guild.id, props.channel.id, type);
case MenuItemParentType.Guild:
group.push(MenuItem(props.guild.id));
break;
return MenuItem(props.guild.id);
default:
return null;
}
}
})();
if (item == null) return;
if (group)
group.push(item);
else if (childId === "roles" && props.guildId)
// "roles" may not be present due to the member not having any roles. In that case, add it above "Copy ID"
children.splice(-1, 0, <Menu.MenuGroup>{item}</Menu.MenuGroup>);
};
}
@ -160,10 +169,10 @@ export default definePlugin({
}
],
UserPermissions: (guild: Guild, guildMember: GuildMember) => <UserPermissions guild={guild} guildMember={guildMember} />,
UserPermissions: (guild: Guild, guildMember?: GuildMember) => !!guildMember && <UserPermissions guild={guild} guildMember={guildMember} />,
userContextMenuPatch: makeContextMenuPatch("roles", MenuItemParentType.User),
channelContextMenuPatch: makeContextMenuPatch("mute-channel", MenuItemParentType.Channel),
channelContextMenuPatch: makeContextMenuPatch(["mute-channel", "unmute-channel"], MenuItemParentType.Channel),
guildContextMenuPatch: makeContextMenuPatch("privacy", MenuItemParentType.Guild),
start() {