From 3eca75be35fe03630ff9332346f8f6a1bab5900c Mon Sep 17 00:00:00 2001 From: EnergoStalin Date: Fri, 7 Feb 2025 23:08:05 +0300 Subject: [PATCH] refactor & fix: delete duplicated code & move menu to separate file --- .../components/ContextMenu.tsx | 44 +++++++++++++++++++ src/plugins/roleColorEverywhere/index.tsx | 44 ++++--------------- 2 files changed, 53 insertions(+), 35 deletions(-) create mode 100644 src/plugins/roleColorEverywhere/components/ContextMenu.tsx diff --git a/src/plugins/roleColorEverywhere/components/ContextMenu.tsx b/src/plugins/roleColorEverywhere/components/ContextMenu.tsx new file mode 100644 index 000000000..58863497a --- /dev/null +++ b/src/plugins/roleColorEverywhere/components/ContextMenu.tsx @@ -0,0 +1,44 @@ +/* + * Vencord, a Discord client mod + * Copyright (c) 2025 Vendicated and contributors + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +import { classNameFactory } from "@api/Styles"; +import { openModal } from "@utils/modal"; +import { Menu } from "@webpack/common"; +import { Guild } from "discord-types/general"; + +import { toggleRole } from "../storeHelper"; +import { RoleModal } from "./RolesModal"; + +export function ContextMenu({ colorsStore, guild, roleId, classFactory }: { guild: Guild, roleId: string, colorsStore: ColorsStore, classFactory: ReturnType }) { + const cl = classFactory; + const togglelabel = (colorsStore[guild.id]?.includes(roleId) ? + "Remove role from" : + "Add role to") + " coloring list"; + + return ( + + toggleRole(colorsStore, guild.id, roleId)} + /> + openModal(modalProps => ( + + ))} + /> + + ); +} diff --git a/src/plugins/roleColorEverywhere/index.tsx b/src/plugins/roleColorEverywhere/index.tsx index 2930a2a42..138747a57 100644 --- a/src/plugins/roleColorEverywhere/index.tsx +++ b/src/plugins/roleColorEverywhere/index.tsx @@ -24,16 +24,13 @@ import { makeRange } from "@components/PluginSettings/components"; import { Devs } from "@utils/constants"; import { getCurrentGuild } from "@utils/discord"; import { Logger } from "@utils/Logger"; -import { openModal } from "@utils/modal"; import definePlugin, { OptionType } from "@utils/types"; import { findByCodeLazy } from "@webpack"; -import { ChannelStore, GuildMemberStore, GuildStore, Menu, React } from "@webpack/common"; +import { ChannelStore, GuildMemberStore, GuildStore, React } from "@webpack/common"; -import { RoleModal } from "./components/RolesModal"; -import { toggleRole } from "./storeHelper"; +import { ContextMenu } from "./components/ContextMenu"; import { brewUserColor } from "./witchCauldron"; -const cl = classNameFactory("rolecolor"); const DeveloperMode = getUserSettingLazy("appearance", "developerMode")!; const useMessageAuthor = findByCodeLazy('"Result cannot be null because the message is not null"'); @@ -265,37 +262,14 @@ export default definePlugin({ settings.store.userColorFromRoles[guild.id] ??= []; const role = GuildStore.getRole(guild.id, id); - if (!role) return; + if (!role || !role.colorString) return; - const togglelabel = (settings.store.userColorFromRoles[guild.id]?.includes(role.id) ? - "Remove role from" : - "Add role to") + " coloring list"; - - if (role.colorString) { - children.push( - - toggleRole(settings.store.userColorFromRoles, guild.id, role.id)} - /> - openModal(modalProps => ( - - ))} - /> - - ); - } + children.push(ContextMenu({ + classFactory: classNameFactory("rolecolor"), + colorsStore: settings.store.userColorFromRoles, + roleId: role.id, + guild + })); } } });