PermissionsViewer: Fix permission description tooltip & cleanup (#3040)

This commit is contained in:
Sqaaakoi 2024-11-25 17:14:25 +13:00 committed by GitHub
parent e7a54b0587
commit a9d44e3341
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 87 additions and 234 deletions

View file

@ -1,11 +0,0 @@
.vc-expandableheader-center-flex {
display: flex;
place-items: center;
}
.vc-expandableheader-btn {
all: unset;
cursor: pointer;
width: 24px;
height: 24px;
}

View file

@ -1,121 +0,0 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/
import "./ExpandableHeader.css";
import { classNameFactory } from "@api/Styles";
import { Text, Tooltip, useState } from "@webpack/common";
const cl = classNameFactory("vc-expandableheader-");
export interface ExpandableHeaderProps {
onMoreClick?: () => void;
moreTooltipText?: string;
onDropDownClick?: (state: boolean) => void;
defaultState?: boolean;
headerText: string;
children: React.ReactNode;
buttons?: React.ReactNode[];
forceOpen?: boolean;
}
export function ExpandableHeader({
children,
onMoreClick,
buttons,
moreTooltipText,
onDropDownClick,
headerText,
defaultState = false,
forceOpen = false,
}: ExpandableHeaderProps) {
const [showContent, setShowContent] = useState(defaultState || forceOpen);
return (
<>
<div style={{
display: "flex",
justifyContent: "space-between",
alignItems: "center",
marginBottom: "8px"
}}>
<Text
tag="h2"
variant="eyebrow"
style={{
color: "var(--header-primary)",
display: "inline"
}}
>
{headerText}
</Text>
<div className={cl("center-flex")}>
{
buttons ?? null
}
{
onMoreClick && // only show more button if callback is provided
<Tooltip text={moreTooltipText}>
{tooltipProps => (
<button
{...tooltipProps}
className={cl("btn")}
onClick={onMoreClick}>
<svg
width="24"
height="24"
viewBox="0 0 24 24"
>
<path fill="var(--text-normal)" d="M7 12.001C7 10.8964 6.10457 10.001 5 10.001C3.89543 10.001 3 10.8964 3 12.001C3 13.1055 3.89543 14.001 5 14.001C6.10457 14.001 7 13.1055 7 12.001ZM14 12.001C14 10.8964 13.1046 10.001 12 10.001C10.8954 10.001 10 10.8964 10 12.001C10 13.1055 10.8954 14.001 12 14.001C13.1046 14.001 14 13.1055 14 12.001ZM19 10.001C20.1046 10.001 21 10.8964 21 12.001C21 13.1055 20.1046 14.001 19 14.001C17.8954 14.001 17 13.1055 17 12.001C17 10.8964 17.8954 10.001 19 10.001Z" />
</svg>
</button>
)}
</Tooltip>
}
<Tooltip text={showContent ? "Hide " + headerText : "Show " + headerText}>
{tooltipProps => (
<button
{...tooltipProps}
className={cl("btn")}
onClick={() => {
setShowContent(v => !v);
onDropDownClick?.(showContent);
}}
disabled={forceOpen}
>
<svg
width="24"
height="24"
viewBox="0 0 24 24"
transform={showContent ? "scale(1 -1)" : "scale(1 1)"}
>
<path fill="var(--text-normal)" d="M16.59 8.59003L12 13.17L7.41 8.59003L6 10L12 16L18 10L16.59 8.59003Z" />
</svg>
</button>
)}
</Tooltip>
</div>
</div>
{showContent && children}
</>
);
}

View file

@ -10,7 +10,6 @@ export * from "./CodeBlock";
export * from "./DonateButton"; export * from "./DonateButton";
export { default as ErrorBoundary } from "./ErrorBoundary"; export { default as ErrorBoundary } from "./ErrorBoundary";
export * from "./ErrorCard"; export * from "./ErrorCard";
export * from "./ExpandableHeader";
export * from "./Flex"; export * from "./Flex";
export * from "./Heart"; export * from "./Heart";
export * from "./Icons"; export * from "./Icons";

View file

@ -22,12 +22,12 @@ import { InfoIcon, OwnerCrownIcon } from "@components/Icons";
import { getIntlMessage, getUniqueUsername } from "@utils/discord"; import { getIntlMessage, getUniqueUsername } from "@utils/discord";
import { ModalCloseButton, ModalContent, ModalHeader, ModalProps, ModalRoot, ModalSize, openModal } from "@utils/modal"; import { ModalCloseButton, ModalContent, ModalHeader, ModalProps, ModalRoot, ModalSize, openModal } from "@utils/modal";
import { findByCodeLazy } from "@webpack"; import { findByCodeLazy } from "@webpack";
import { Clipboard, ContextMenuApi, FluxDispatcher, GuildMemberStore, GuildStore, Menu, PermissionsBits, ScrollerThin, Text, Tooltip, useEffect, UserStore, useState, useStateFromStores } from "@webpack/common"; import { Clipboard, ContextMenuApi, FluxDispatcher, GuildMemberStore, GuildStore, i18n, Menu, PermissionsBits, ScrollerThin, Text, Tooltip, useEffect, useMemo, UserStore, useState, useStateFromStores } from "@webpack/common";
import { UnicodeEmoji } from "@webpack/types"; import { UnicodeEmoji } from "@webpack/types";
import type { Guild, Role, User } from "discord-types/general"; import type { Guild, Role, User } from "discord-types/general";
import { settings } from ".."; import { settings } from "..";
import { cl, getPermissionDescription, getPermissionString } from "../utils"; import { cl, getGuildPermissionSpecMap } from "../utils";
import { PermissionAllowedIcon, PermissionDefaultIcon, PermissionDeniedIcon } from "./icons"; import { PermissionAllowedIcon, PermissionDefaultIcon, PermissionDeniedIcon } from "./icons";
export const enum PermissionType { export const enum PermissionType {
@ -56,7 +56,7 @@ function getRoleIconSrc(role: Role) {
} }
function RolesAndUsersPermissionsComponent({ permissions, guild, modalProps, header }: { permissions: Array<RoleOrUserPermission>; guild: Guild; modalProps: ModalProps; header: string; }) { function RolesAndUsersPermissionsComponent({ permissions, guild, modalProps, header }: { permissions: Array<RoleOrUserPermission>; guild: Guild; modalProps: ModalProps; header: string; }) {
permissions.sort((a, b) => a.type - b.type); const guildPermissionSpecMap = useMemo(() => getGuildPermissionSpecMap(guild), [guild.id]);
useStateFromStores( useStateFromStores(
[GuildMemberStore], [GuildMemberStore],
@ -65,6 +65,10 @@ function RolesAndUsersPermissionsComponent({ permissions, guild, modalProps, hea
(old, current) => old.length === current.length (old, current) => old.length === current.length
); );
useEffect(() => {
permissions.sort((a, b) => a.type - b.type);
}, [permissions]);
useEffect(() => { useEffect(() => {
const usersToRequest = permissions const usersToRequest = permissions
.filter(p => p.type === PermissionType.User && !GuildMemberStore.isMember(guild.id, p.id!)) .filter(p => p.type === PermissionType.User && !GuildMemberStore.isMember(guild.id, p.id!))
@ -173,7 +177,7 @@ function RolesAndUsersPermissionsComponent({ permissions, guild, modalProps, hea
</ScrollerThin> </ScrollerThin>
<div className={cl("modal-divider")} /> <div className={cl("modal-divider")} />
<ScrollerThin className={cl("modal-perms")} orientation="auto"> <ScrollerThin className={cl("modal-perms")} orientation="auto">
{Object.entries(PermissionsBits).map(([permissionName, bit]) => ( {Object.values(PermissionsBits).map(bit => (
<div className={cl("modal-perms-item")}> <div className={cl("modal-perms-item")}>
<div className={cl("modal-perms-item-icon")}> <div className={cl("modal-perms-item-icon")}>
{(() => { {(() => {
@ -192,9 +196,14 @@ function RolesAndUsersPermissionsComponent({ permissions, guild, modalProps, hea
return PermissionDefaultIcon(); return PermissionDefaultIcon();
})()} })()}
</div> </div>
<Text variant="text-md/normal">{getPermissionString(permissionName)}</Text> <Text variant="text-md/normal">{guildPermissionSpecMap[String(bit)].title}</Text>
<Tooltip text={getPermissionDescription(permissionName) || "No Description"}> <Tooltip text={
(() => {
const { description } = guildPermissionSpecMap[String(bit)];
return typeof description === "function" ? i18n.intl.format(description, {}) : description;
})()
}>
{props => <InfoIcon {...props} />} {props => <InfoIcon {...props} />}
</Tooltip> </Tooltip>
</div> </div>

View file

@ -17,7 +17,6 @@
*/ */
import ErrorBoundary from "@components/ErrorBoundary"; import ErrorBoundary from "@components/ErrorBoundary";
import { ExpandableHeader } from "@components/ExpandableHeader";
import { getIntlMessage } from "@utils/discord"; import { getIntlMessage } from "@utils/discord";
import { classes } from "@utils/misc"; import { classes } from "@utils/misc";
import { filters, findBulk, proxyLazyWebpack } from "@webpack"; import { filters, findBulk, proxyLazyWebpack } from "@webpack";
@ -25,7 +24,7 @@ import { PermissionsBits, Text, Tooltip, useMemo, UserStore } from "@webpack/com
import type { Guild, GuildMember } from "discord-types/general"; import type { Guild, GuildMember } from "discord-types/general";
import { PermissionsSortOrder, settings } from ".."; import { PermissionsSortOrder, settings } from "..";
import { cl, getPermissionString, getSortedRoles, sortUserRoles } from "../utils"; import { cl, getGuildPermissionSpecMap, getSortedRoles, sortUserRoles } from "../utils";
import openRolesAndUsersPermissionsModal, { PermissionType, type RoleOrUserPermission } from "./RolesAndUsersPermissions"; import openRolesAndUsersPermissionsModal, { PermissionType, type RoleOrUserPermission } from "./RolesAndUsersPermissions";
interface UserPermission { interface UserPermission {
@ -87,9 +86,11 @@ function GrantedByTooltip({ roleName, roleColor }: GrantedByTooltipProps) {
); );
} }
function UserPermissionsComponent({ guild, guildMember, forceOpen = false }: { guild: Guild; guildMember: GuildMember; forceOpen?: boolean; }) { function UserPermissionsComponent({ guild, guildMember, closePopout }: { guild: Guild; guildMember: GuildMember; closePopout: () => void; }) {
const { permissionsSortOrder } = settings.use(["permissionsSortOrder"]); const { permissionsSortOrder } = settings.use(["permissionsSortOrder"]);
const guildPermissionSpecMap = useMemo(() => getGuildPermissionSpecMap(guild), [guild.id]);
const [rolePermissions, userPermissions] = useMemo(() => { const [rolePermissions, userPermissions] = useMemo(() => {
const userPermissions: UserPermissions = []; const userPermissions: UserPermissions = [];
@ -106,7 +107,7 @@ function UserPermissionsComponent({ guild, guildMember, forceOpen = false }: { g
permissions: Object.values(PermissionsBits).reduce((prev, curr) => prev | curr, 0n) permissions: Object.values(PermissionsBits).reduce((prev, curr) => prev | curr, 0n)
}); });
const OWNER = getIntlMessage("GUILD_OWNER") || "Server Owner"; const OWNER = getIntlMessage("GUILD_OWNER") ?? "Server Owner";
userPermissions.push({ userPermissions.push({
permission: OWNER, permission: OWNER,
roleName: "Owner", roleName: "Owner",
@ -117,11 +118,11 @@ function UserPermissionsComponent({ guild, guildMember, forceOpen = false }: { g
sortUserRoles(userRoles); sortUserRoles(userRoles);
for (const [permission, bit] of Object.entries(PermissionsBits)) { for (const bit of Object.values(PermissionsBits)) {
for (const { permissions, colorString, position, name } of userRoles) { for (const { permissions, colorString, position, name } of userRoles) {
if ((permissions & bit) === bit) { if ((permissions & bit) === bit) {
userPermissions.push({ userPermissions.push({
permission: getPermissionString(permission), permission: guildPermissionSpecMap[String(bit)].title,
roleName: name, roleName: name,
roleColor: colorString || "var(--primary-300)", roleColor: colorString || "var(--primary-300)",
rolePosition: position rolePosition: position
@ -137,26 +138,15 @@ function UserPermissionsComponent({ guild, guildMember, forceOpen = false }: { g
return [rolePermissions, userPermissions]; return [rolePermissions, userPermissions];
}, [permissionsSortOrder]); }, [permissionsSortOrder]);
return ( return <div>
<ExpandableHeader <div className={cl("user-header-container")}>
forceOpen={forceOpen} <Text variant="eyebrow">Permissions</Text>
headerText="Permissions" <div className={cl("user-header-btns")}>
moreTooltipText="Role Details"
onMoreClick={() =>
openRolesAndUsersPermissionsModal(
rolePermissions,
guild,
guildMember.nick || UserStore.getUser(guildMember.userId).username
)
}
onDropDownClick={state => settings.store.defaultPermissionsDropdownState = !state}
defaultState={settings.store.defaultPermissionsDropdownState}
buttons={[
<Tooltip text={`Sorting by ${permissionsSortOrder === PermissionsSortOrder.HighestRole ? "Highest Role" : "Lowest Role"}`}> <Tooltip text={`Sorting by ${permissionsSortOrder === PermissionsSortOrder.HighestRole ? "Highest Role" : "Lowest Role"}`}>
{tooltipProps => ( {tooltipProps => (
<div <div
{...tooltipProps} {...tooltipProps}
className={cl("user-sortorder-btn")} className={cl("user-header-btn")}
role="button" role="button"
tabIndex={0} tabIndex={0}
onClick={() => { onClick={() => {
@ -164,8 +154,8 @@ function UserPermissionsComponent({ guild, guildMember, forceOpen = false }: { g
}} }}
> >
<svg <svg
width="20" width="24"
height="20" height="24"
viewBox="0 96 960 960" viewBox="0 96 960 960"
transform={permissionsSortOrder === PermissionsSortOrder.HighestRole ? "scale(1 1)" : "scale(1 -1)"} transform={permissionsSortOrder === PermissionsSortOrder.HighestRole ? "scale(1 1)" : "scale(1 -1)"}
> >
@ -174,7 +164,30 @@ function UserPermissionsComponent({ guild, guildMember, forceOpen = false }: { g
</div> </div>
)} )}
</Tooltip> </Tooltip>
]}> <Tooltip text="Role Details">
{tooltipProps => (
<div
{...tooltipProps}
className={cl("user-header-btn")}
role="button"
tabIndex={0}
onClick={() => {
closePopout();
openRolesAndUsersPermissionsModal(rolePermissions, guild, guildMember.nick || UserStore.getUser(guildMember.userId).username);
}}
>
<svg
width="24"
height="24"
viewBox="0 0 24 24"
>
<path fill="var(--text-normal)" d="M7 12.001C7 10.8964 6.10457 10.001 5 10.001C3.89543 10.001 3 10.8964 3 12.001C3 13.1055 3.89543 14.001 5 14.001C6.10457 14.001 7 13.1055 7 12.001ZM14 12.001C14 10.8964 13.1046 10.001 12 10.001C10.8954 10.001 10 10.8964 10 12.001C10 13.1055 10.8954 14.001 12 14.001C13.1046 14.001 14 13.1055 14 12.001ZM19 10.001C20.1046 10.001 21 10.8964 21 12.001C21 13.1055 20.1046 14.001 19 14.001C17.8954 14.001 17 13.1055 17 12.001C17 10.8964 17.8954 10.001 19 10.001Z" />
</svg>
</div>
)}
</Tooltip>
</div>
</div>
{userPermissions.length > 0 && ( {userPermissions.length > 0 && (
<div className={classes(RoleRootClasses.root)}> <div className={classes(RoleRootClasses.root)}>
{userPermissions.map(({ permission, roleColor, roleName }) => ( {userPermissions.map(({ permission, roleColor, roleName }) => (
@ -190,8 +203,7 @@ function UserPermissionsComponent({ guild, guildMember, forceOpen = false }: { g
))} ))}
</div> </div>
)} )}
</ExpandableHeader> </div>;
);
} }
export default ErrorBoundary.wrap(UserPermissionsComponent, { noop: true }); export default ErrorBoundary.wrap(UserPermissionsComponent, { noop: true });

View file

@ -56,11 +56,6 @@ export const settings = definePluginSettings({
{ label: "Lowest Role", value: PermissionsSortOrder.LowestRole } { label: "Lowest Role", value: PermissionsSortOrder.LowestRole }
] ]
}, },
defaultPermissionsDropdownState: {
description: "Whether the permissions dropdown on user popouts should be open by default",
type: OptionType.BOOLEAN,
default: false
}
}); });
function MenuItem(guildId: string, id?: string, type?: MenuItemParentType) { function MenuItem(guildId: string, id?: string, type?: MenuItemParentType) {
@ -182,9 +177,9 @@ export default definePlugin({
<Popout <Popout
position="bottom" position="bottom"
align="center" align="center"
renderPopout={() => ( renderPopout={({ closePopout }) => (
<Dialog className={PopoutClasses.container} style={{ width: "500px" }}> <Dialog className={PopoutClasses.container} style={{ width: "500px" }}>
<UserPermissions guild={guild} guildMember={guildMember} forceOpen /> <UserPermissions guild={guild} guildMember={guildMember} closePopout={closePopout} />
</Dialog> </Dialog>
)} )}
> >

View file

@ -1,12 +1,22 @@
/* User Permissions Component */ /* User Permissions Component */
.vc-permviewer-user-sortorder-btn { .vc-permviewer-user-header-container {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 8px;
}
.vc-permviewer-user-header-btns {
display: flex;
gap: 8px;
}
.vc-permviewer-user-header-btn {
cursor: pointer; cursor: pointer;
display: flex; display: flex;
align-items: center; align-items: center;
justify-content: center; justify-content: center;
width: 24px;
height: 24px;
} }
/* RolesAndUsersPermissions Component */ /* RolesAndUsersPermissions Component */

View file

@ -17,57 +17,17 @@
*/ */
import { classNameFactory } from "@api/Styles"; import { classNameFactory } from "@api/Styles";
import { getIntlMessage } from "@utils/discord"; import { findByPropsLazy } from "@webpack";
import { wordsToTitle } from "@utils/text"; import { GuildStore } from "@webpack/common";
import { GuildStore, Parser } from "@webpack/common";
import { Guild, GuildMember, Role } from "discord-types/general"; import { Guild, GuildMember, Role } from "discord-types/general";
import type { ReactNode } from "react";
import { PermissionsSortOrder, settings } from "."; import { PermissionsSortOrder, settings } from ".";
import { PermissionType } from "./components/RolesAndUsersPermissions"; import { PermissionType } from "./components/RolesAndUsersPermissions";
export const { getGuildPermissionSpecMap } = findByPropsLazy("getGuildPermissionSpecMap");
export const cl = classNameFactory("vc-permviewer-"); export const cl = classNameFactory("vc-permviewer-");
function formatPermissionWithoutMatchingString(permission: string) {
return wordsToTitle(permission.toLowerCase().split("_"));
}
// because discord is unable to be consistent with their names
const PermissionKeyMap = {
MANAGE_GUILD: "MANAGE_SERVER",
MANAGE_GUILD_EXPRESSIONS: "MANAGE_EXPRESSIONS",
CREATE_GUILD_EXPRESSIONS: "CREATE_EXPRESSIONS",
MODERATE_MEMBERS: "MODERATE_MEMBER", // HELLOOOO ??????
STREAM: "VIDEO",
SEND_VOICE_MESSAGES: "ROLE_PERMISSIONS_SEND_VOICE_MESSAGE",
} as const;
export function getPermissionString(permission: string) {
permission = PermissionKeyMap[permission] || permission;
return getIntlMessage(permission) ||
// shouldn't get here but just in case
formatPermissionWithoutMatchingString(permission);
}
export function getPermissionDescription(permission: string): ReactNode {
// DISCORD PLEEEEEEEEAAAAASE IM BEGGING YOU :(
if (permission === "USE_APPLICATION_COMMANDS")
permission = "USE_APPLICATION_COMMANDS_GUILD";
else if (permission === "SEND_VOICE_MESSAGES")
permission = "SEND_VOICE_MESSAGE_GUILD";
else if (permission !== "STREAM")
permission = PermissionKeyMap[permission] || permission;
const msg = getIntlMessage(`ROLE_PERMISSIONS_${permission}_DESCRIPTION`) as any;
if (msg?.hasMarkdown)
return Parser.parse(msg.message);
if (typeof msg === "string") return msg;
return "";
}
export function getSortedRoles({ id }: Guild, member: GuildMember) { export function getSortedRoles({ id }: Guild, member: GuildMember) {
const roles = GuildStore.getRoles(id); const roles = GuildStore.getRoles(id);