From 7636cafd91da6d88fee5a7981eb32053614080b5 Mon Sep 17 00:00:00 2001 From: Henry Hiles Date: Tue, 26 Apr 2022 14:45:44 -0400 Subject: [PATCH] Fix unnecessarily re-renders --- renderer.js | 170 +++++++++++++++++++++++++--------------------------- 1 file changed, 83 insertions(+), 87 deletions(-) diff --git a/renderer.js b/renderer.js index 2e05354..541689e 100644 --- a/renderer.js +++ b/renderer.js @@ -26,98 +26,94 @@ export default new (class PackageDownloader { ? args[0].children.at(-1).props : null + const Button = () => { + const [disabled, setDisabled] = React.useState(false) + + const gitURL = props.message.content + .slice(props.message.content.indexOf("Repository")) + .match( + /((git@|http(s)?:\/\/)([\w\.@]+)(\/|:))([\w,\-,\_]+)\/([\w,\-,\_]+)(.git){0,1}((\/){0,1})/ + ) + + if (!disabled) + Object.values(kernel.packages.getPackages()).forEach( + (pkg) => { + if (pkg.path.split("/").at(-1) == gitURL[7]) + setDisabled(true) + } + ) + + return [ + React.createElement( + Tooltip, + { + position: "top", + text: disabled + ? "Already Installed" + : "Install Package", + }, + (args) => + React.createElement( + MiniPopover.Button, + { + ...args, + disabled: disabled, + onClick: async () => { + setDisabled(true) + const failed = + await window.installPackage(gitURL) + + if (failed) { + Toasts.showToast( + Toasts.createToast( + "Failed to install package", + Toasts.ToastType.ERROR + ) + ) + pluginLog( + "Package installation failed, error above.", + console.error + ) + setDisabled(false) + } else { + Toasts.showToast( + Toasts.createToast( + "Successfully installed package! Please reload discord with Ctrl+R.", + Toasts.ToastType.SUCCESS + ) + ) + pluginLog( + "Successfully installed package! Please reload discord with Ctrl+R." + ) + } + }, + }, + React.createElement( + "svg", + { + xmlns: "http://www.w3.org/2000/svg", + width: "16", + height: "16", + fill: "currentColor", + class: "bi bi-arrow-down-circle-fill", + viewBox: "0 0 16 16", + }, + React.createElement("path", { + d: "M16 8A8 8 0 1 1 0 8a8 8 0 0 1 16 0zM8.5 4.5a.5.5 0 0 0-1 0v5.793L5.354 8.146a.5.5 0 1 0-.708.708l3 3a.5.5 0 0 0 .708 0l3-3a.5.5 0 0 0-.708-.708L8.5 10.293V4.5z", + }) + ) + ) + ), + React.createElement(MiniPopover.Separator), + ] + } + if ( props?.message && props.channel && props.channel.id == "899717501120806963" ) - args[0].children.unshift( - React.createElement(() => { - const [disabled, setDisabled] = React.useState(false) - - const gitURL = props.message.content - .slice(props.message.content.indexOf("Repository")) - .match( - /((git@|http(s)?:\/\/)([\w\.@]+)(\/|:))([\w,\-,\_]+)\/([\w,\-,\_]+)(.git){0,1}((\/){0,1})/ - ) - - if (!disabled) - Object.values( - kernel.packages.getPackages() - ).forEach((pkg) => { - if (pkg.path.split("/").at(-1) == gitURL[7]) - setDisabled(true) - }) - - return [ - React.createElement( - Tooltip, - { - position: "top", - text: disabled - ? "Already Installed" - : "Install Package", - }, - (args) => - React.createElement( - MiniPopover.Button, - { - ...args, - disabled: disabled, - onClick: async () => { - setDisabled(true) - const failed = - await window.installPackage( - gitURL - ) - - if (failed) { - Toasts.showToast( - Toasts.createToast( - "Failed to install package", - Toasts.ToastType - .ERROR - ) - ) - pluginLog( - "Package installation failed, error above.", - console.error - ) - setDisabled(false) - } else { - Toasts.showToast( - Toasts.createToast( - "Successfully installed package! Please reload discord with Ctrl+R.", - Toasts.ToastType - .SUCCESS - ) - ) - pluginLog( - "Successfully installed package! Please reload discord with Ctrl+R." - ) - } - }, - }, - React.createElement( - "svg", - { - xmlns: "http://www.w3.org/2000/svg", - width: "16", - height: "16", - fill: "currentColor", - class: "bi bi-arrow-down-circle-fill", - viewBox: "0 0 16 16", - }, - React.createElement("path", { - d: "M16 8A8 8 0 1 1 0 8a8 8 0 0 1 16 0zM8.5 4.5a.5.5 0 0 0-1 0v5.793L5.354 8.146a.5.5 0 1 0-.708.708l3 3a.5.5 0 0 0 .708 0l3-3a.5.5 0 0 0-.708-.708L8.5 10.293V4.5z", - }) - ) - ) - ), - React.createElement(MiniPopover.Separator), - ] - }) - ) + args[0].children.unshift(React.createElement(Button)) return funcCopy.apply(this, args) }