/* * Vencord, a modification for Discord's desktop app * Copyright (c) 2023 Vendicated and contributors * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ import ErrorBoundary from "@components/ErrorBoundary"; import { ExpandableHeader } from "@components/ExpandableHeader"; import { classes } from "@utils/misc"; import { filters, findBulk, proxyLazyWebpack } from "@webpack"; import { i18n, PermissionsBits, Text, Tooltip, useMemo, UserStore } from "@webpack/common"; import type { Guild, GuildMember } from "discord-types/general"; import { PermissionsSortOrder, settings } from ".."; import { cl, getPermissionString, getSortedRoles, sortUserRoles } from "../utils"; import openRolesAndUsersPermissionsModal, { PermissionType, type RoleOrUserPermission } from "./RolesAndUsersPermissions"; interface UserPermission { permission: string; roleName: string; roleColor: string; rolePosition: number; } type UserPermissions = Array; const { RoleRootClasses, RoleClasses, RoleBorderClasses } = proxyLazyWebpack(() => { const [RoleRootClasses, RoleClasses, RoleBorderClasses] = findBulk( filters.byProps("root", "expandButton", "collapseButton"), filters.byProps("role", "roleCircle", "roleName"), filters.byProps("roleCircle", "dot", "dotBorderColor") ) as Record[]; return { RoleRootClasses, RoleClasses, RoleBorderClasses }; }); interface FakeRoleProps extends React.HTMLAttributes { text: string; color: string; } function FakeRole({ text, color, ...props }: FakeRoleProps) { return (
{text}
); } interface GrantedByTooltipProps { roleName: string; roleColor: string; } function GrantedByTooltip({ roleName, roleColor }: GrantedByTooltipProps) { return ( <> Granted By ); } function UserPermissionsComponent({ guild, guildMember, forceOpen = false }: { guild: Guild; guildMember: GuildMember; forceOpen?: boolean; }) { const { permissionsSortOrder } = settings.use(["permissionsSortOrder"]); const [rolePermissions, userPermissions] = useMemo(() => { const userPermissions: UserPermissions = []; const userRoles = getSortedRoles(guild, guildMember); const rolePermissions: Array = userRoles.map(role => ({ type: PermissionType.Role, ...role })); if (guild.ownerId === guildMember.userId) { rolePermissions.push({ type: PermissionType.Owner, permissions: Object.values(PermissionsBits).reduce((prev, curr) => prev | curr, 0n) }); const OWNER = i18n.Messages.GUILD_OWNER || "Server Owner"; userPermissions.push({ permission: OWNER, roleName: "Owner", roleColor: "var(--primary-300)", rolePosition: Infinity }); } sortUserRoles(userRoles); for (const [permission, bit] of Object.entries(PermissionsBits)) { for (const { permissions, colorString, position, name } of userRoles) { if ((permissions & bit) === bit) { userPermissions.push({ permission: getPermissionString(permission), roleName: name, roleColor: colorString || "var(--primary-300)", rolePosition: position }); break; } } } userPermissions.sort((a, b) => b.rolePosition - a.rolePosition); return [rolePermissions, userPermissions]; }, [permissionsSortOrder]); return ( openRolesAndUsersPermissionsModal( rolePermissions, guild, guildMember.nick || UserStore.getUser(guildMember.userId).username ) } onDropDownClick={state => settings.store.defaultPermissionsDropdownState = !state} defaultState={settings.store.defaultPermissionsDropdownState} buttons={[ {tooltipProps => (
{ settings.store.permissionsSortOrder = permissionsSortOrder === PermissionsSortOrder.HighestRole ? PermissionsSortOrder.LowestRole : PermissionsSortOrder.HighestRole; }} >
)}
]}> {userPermissions.length > 0 && (
{userPermissions.map(({ permission, roleColor, roleName }) => ( } tooltipClassName={cl("granted-by-container")} tooltipContentClassName={cl("granted-by-content")} > {tooltipProps => ( )} ))}
)}
); } export default ErrorBoundary.wrap(UserPermissionsComponent, { noop: true });