From 7af885c10d3f0f45746174b724f4d56de1669be6 Mon Sep 17 00:00:00 2001 From: Zack Rauen Date: Sun, 4 Sep 2022 18:51:15 -0400 Subject: [PATCH] Move public servers --- .../src/builtins/general/publicservers.js | 45 +++++++-- renderer/src/modules/componentpatcher.js | 99 ------------------- renderer/src/ui/icons/globe.jsx | 3 +- 3 files changed, 40 insertions(+), 107 deletions(-) diff --git a/renderer/src/builtins/general/publicservers.js b/renderer/src/builtins/general/publicservers.js index a52b1262..2f891f3e 100644 --- a/renderer/src/builtins/general/publicservers.js +++ b/renderer/src/builtins/general/publicservers.js @@ -1,26 +1,57 @@ import Builtin from "../../structs/builtin"; -import {DiscordModules, WebpackModules, Strings, DOM} from "modules"; +import {DiscordModules, WebpackModules, Strings, DOM, React} from "modules"; import PublicServersMenu from "../../ui/publicservers/menu"; +import Globe from "../../ui/icons/globe"; const LayerStack = WebpackModules.getByProps("pushLayer"); +class ErrorBoundary extends React.Component { + constructor(props) { + super(props); + this.state = {hasError: false}; + } + + componentDidCatch() { + this.setState({hasError: true}); + } + + render() { + if (this.state.hasError) return null; + return this.props.children; + } +} + export default new class PublicServers extends Builtin { get name() {return "PublicServers";} get category() {return "general";} get id() {return "publicServers";} enabled() { - this._appendButton(); - const ListNavigators = WebpackModules.getByProps("ListNavigatorProvider"); - this.after(ListNavigators, "ListNavigatorProvider", (_, __, returnValue) => { - if (returnValue.props.value.id !== "guildsnav") return; - this._appendButton(); + const PrivateChannelList = WebpackModules.getModule(m => m?.displayName === "ConnectedPrivateChannelsList", {defaultExport: false}); + const PrivateChannelListComponents = WebpackModules.getByProps("LinkButton"); + this.after(PrivateChannelList, "default", (_, __, returnValue) => { + const destination = returnValue?.props?.children?.props?.children; + if (!destination || !Array.isArray(destination)) return; + if (destination.find(b => b?.props?.children?.props?.id === "public-server-button")) return; + + destination.push( + React.createElement(ErrorBoundary, null, + React.createElement(PrivateChannelListComponents.LinkButton, + { + id: "public-server-button", + onClick: () => this.openPublicServers(), + text: "Public Servers", + icon: () => React.createElement(Globe, {color: "currentColor"}) + } + ) + ) + ); }); } disabled() { this.unpatchAll(); - DOM.query("#bd-pub-li").remove(); + // DOM.query("#bd-pub-li").remove(); } async _appendButton() { diff --git a/renderer/src/modules/componentpatcher.js b/renderer/src/modules/componentpatcher.js index 819b9cb9..dd982b4e 100644 --- a/renderer/src/modules/componentpatcher.js +++ b/renderer/src/modules/componentpatcher.js @@ -34,9 +34,6 @@ export default new class ComponentPatcher { initialize() { Utilities.suppressErrors(this.patchSocial.bind(this), "BD Social Patch")(); - Utilities.suppressErrors(this.patchGuildPills.bind(this), "BD Guild Pills Patch")(); - Utilities.suppressErrors(this.patchGuildListItems.bind(this), "BD Guild List Items Patch")(); - Utilities.suppressErrors(this.patchMessageHeader.bind(this), "BD Message Header Patch")(); Utilities.suppressErrors(this.patchMemberList.bind(this), "BD Member List Patch")(); Utilities.suppressErrors(this.patchProfile.bind(this), "BD Profile Badges Patch")(); } @@ -75,102 +72,6 @@ export default new class ComponentPatcher { }; }); } - - patchGuildListItems() { - if (this.guildListItemsPatch) return; - const ListNavigators = WebpackModules.getByProps("ListNavigatorProvider"); - const GuildComponent = WebpackModules.find(m => m.type && m.type.toString().includes("guildNode") && m.type.toString().includes("treeitem")); - if (!GuildComponent || typeof(GuildComponent.type) !== "function") return this.warn("Failed to get Guild component."); - if (!ListNavigators || typeof(ListNavigators.ListNavigatorProvider) !== "function") return this.warn("Failed to get ListNavigatorProvider component."); - - this.guildListItemsPatch = Patcher.after("ComponentPatcher", GuildComponent, "type", (_, [props], returnValue) => { - if (!returnValue || !returnValue.props) return; - - try { - returnValue.props.className += " bd-guild"; - if (props.unread) returnValue.props.className += " bd-unread"; - if (props.selected) returnValue.props.className += " bd-selected"; - if (props.mediaState.audio) returnValue.props.className += " bd-audio"; - if (props.mediaState.video) returnValue.props.className += " bd-video"; - if (props.badge) returnValue.props.className += " bd-badge"; - if (props.animatable) returnValue.props.className += " bd-animatable"; - if (props.unavailable) returnValue.props.className += " bd-unavailable"; - if (props.mediaState.screenshare) returnValue.props.className += " bd-screenshare"; - if (props.mediaState.liveStage) returnValue.props.className += " bd-live-stage"; - if (props.muted) returnValue.props.className += " bd-muted"; - } - catch (err) { - Logger.error("ComponentPatcher:Guilds", `Error inside BDGuild:`, err); - this.guildListItemsPatch(); - } - }); - - const {useState} = DiscordModules.React; - function useForceUpdate() { - const [, setValue] = useState(false); - return () => setValue(v => !v); // update the state to force render - } - - let hasForced = false; - this.cancelForceUpdate = Patcher.after("ComponentPatcher", ListNavigators, "ListNavigatorProvider", (_, __, returnValue) => { - if (returnValue.props.value.id !== "guildsnav") return; - - const originalParent = Utilities.findInTree(returnValue, m => m?.props?.className, {walkable: ["children", "props"]}); - if (!originalParent) return; - const original = originalParent.type; - originalParent.type = e => { - const forceUpdate = useForceUpdate(); - if (!hasForced) { - hasForced = true; - setTimeout(() => { - forceUpdate(); - this.cancelForceUpdate(); - }, 1); - } - - return Reflect.apply(original, null, [e]); - }; - }); - } - - patchGuildPills() { - if (this.guildPillPatch) return; - const guildPill = WebpackModules.find(m => m?.default?.displayName === "AnimatedHalfPill"); - if (!guildPill) return; - this.guildPillPatch = Patcher.after("ComponentPatcher", guildPill, "default", (_, args, returnValue) => { - const props = args[0]; - if (props.unread) returnValue.props.className += " bd-unread"; - if (props.selected) returnValue.props.className += " bd-selected"; - if (props.hovered) returnValue.props.className += " bd-hovered"; - return returnValue; - }); - } - - patchMessageHeader() { - if (this.messageHeaderPatch) return; - // const MessageTimestamp = WebpackModules.getModule(m => m?.default?.toString().indexOf("showTimestampOnHover") > -1); - // this.messageHeaderPatch = Patcher.after("ComponentPatcher", MessageTimestamp, "default", (_, [{message}], returnValue) => { - // const userId = Utilities.getNestedProp(message, "author.id"); - // if (Developers.indexOf(userId) < 0) return; - // if (!returnValue?.type) return; - // const orig = returnValue.type; - // returnValue.type = function() { - // const retVal = Reflect.apply(orig, this, arguments); - - // const children = Utilities.getNestedProp(retVal, "props.children.1.props.children"); - // if (!Array.isArray(children)) return; - - // children.splice(3, 0, - // React.createElement(DeveloperBadge, { - // type: "chat" - // }) - // ); - - // return retVal; - // }; - - // }); - } async patchMemberList() { if (this.memberListPatch) return; diff --git a/renderer/src/ui/icons/globe.jsx b/renderer/src/ui/icons/globe.jsx index 4ad7dd13..c543802e 100644 --- a/renderer/src/ui/icons/globe.jsx +++ b/renderer/src/ui/icons/globe.jsx @@ -3,7 +3,8 @@ import {React} from "modules"; export default class Globe extends React.Component { render() { const size = this.props.size || "18px"; - return + const color = this.props.color || "#FFFFFF"; + return ;