BetterDiscordApp-rauenzi/src/ui/settings/addoncard.jsx

156 lines
6.7 KiB
React
Raw Normal View History

import {React, Logger, Strings, WebpackModules, DiscordModules} from "modules";
2019-06-27 22:18:40 +02:00
import ReloadIcon from "../icons/reload";
2019-06-30 07:32:14 +02:00
import EditIcon from "../icons/edit";
import DeleteIcon from "../icons/delete";
import CogIcon from "../icons/cog";
2019-06-30 05:09:48 +02:00
import Switch from "./components/switch";
import GitHubIcon from "../icons/github";
import MoneyIcon from "../icons/dollarsign";
import WebIcon from "../icons/globe";
import PatreonIcon from "../icons/patreon";
import SupportIcon from "../icons/support";
import Modals from "../modals";
import Toasts from "../toasts";
const LinkIcons = {
website: WebIcon,
source: GitHubIcon,
invite: SupportIcon,
donate: MoneyIcon,
patreon: PatreonIcon
};
2019-06-27 22:18:40 +02:00
2019-07-03 16:15:48 +02:00
const Tooltip = WebpackModules.getByDisplayName("Tooltip");
2019-06-28 01:50:20 +02:00
export default class AddonCard extends React.Component {
2019-06-27 22:18:40 +02:00
constructor(props) {
super(props);
this.state = {
settingsOpen: false
};
2019-06-28 01:50:20 +02:00
2019-06-27 22:18:40 +02:00
this.settingsPanel = "";
this.panelRef = React.createRef();
2019-06-28 01:50:20 +02:00
this.onChange = this.onChange.bind(this);
2019-06-27 22:18:40 +02:00
this.reload = this.reload.bind(this);
2019-06-28 01:50:20 +02:00
this.showSettings = this.showSettings.bind(this);
}
showSettings() {
if (!this.props.hasSettings || !this.props.enabled) return;
const name = this.getString(this.props.addon.name);
try {
Modals.showAddonSettingsModal(name, this.props.getSettingsPanel());
}
catch (err) {
Toasts.show(Strings.Addons.settingsError.format({name}), {type: "error"});
Logger.stacktrace("Addon Settings", "Unable to get settings panel for " + name + ".", err);
}
2019-06-27 22:18:40 +02:00
}
reload() {
if (!this.props.reload) return;
this.props.addon = this.props.reload(this.props.addon.id);
this.forceUpdate();
}
getString(value) {return typeof value == "string" ? value : value.toString();}
2019-06-28 01:50:20 +02:00
onChange() {
this.props.onChange && this.props.onChange(this.props.addon.id);
2019-06-30 05:09:48 +02:00
this.props.enabled = !this.props.enabled;
this.forceUpdate();
2019-06-28 01:50:20 +02:00
}
2019-06-27 22:18:40 +02:00
buildTitle(name, version, author) {
const title = Strings.Addons.title.split(/({{[A-Za-z]+}})/);
const nameIndex = title.findIndex(s => s == "{{name}}");
if (nameIndex) title[nameIndex] = React.createElement("span", {className: "bd-name"}, name);
const versionIndex = title.findIndex(s => s == "{{version}}");
if (nameIndex) title[versionIndex] = React.createElement("span", {className: "bd-version"}, version);
const authorIndex = title.findIndex(s => s == "{{author}}");
if (nameIndex) title[authorIndex] = React.createElement("span", {className: "bd-author"}, author);
return title.flat();
}
buildLink(which) {
const url = this.props.addon[which];
2020-07-16 07:42:56 +02:00
if (!url) return null;
const icon = React.createElement(LinkIcons[which]);
const link = <a className="bd-link bd-link-website" href={url} target="_blank" rel="noopener noreferrer">{icon}</a>;
2020-02-28 01:00:12 +01:00
if (which == "invite") {
2020-07-16 07:42:56 +02:00
link.props.onClick = function(event) {
event.preventDefault();
event.stopPropagation();
let code = url;
const tester = /\.gg\/(.*)$/;
if (tester.test(code)) code = code.match(tester)[1];
DiscordModules.LayerStack.popLayer();
DiscordModules.InviteActions.acceptInviteAndTransitionToInviteChannel(code);
};
2020-02-28 01:00:12 +01:00
}
return this.makeButton(Strings.Addons[which], link);
}
get controls() { // {this.props.hasSettings && <button onClick={this.showSettings} className="bd-button bd-button-addon-settings" disabled={!this.props.enabled}>{Strings.Addons.addonSettings}</button>}
return <div className="bd-controls">
{this.props.hasSettings && this.makeControlButton(Strings.Addons.addonSettings, <CogIcon size={"24px"} />, this.showSettings, {disabled: !this.props.enabled})}
{this.props.showReloadIcon && this.makeControlButton(Strings.Addons.reload, <ReloadIcon />, this.reload)}
{this.props.editAddon && this.makeControlButton(Strings.Addons.editAddon, <EditIcon />, this.props.editAddon)}
{this.props.deleteAddon && this.makeControlButton(Strings.Addons.deleteAddon, <DeleteIcon />, this.props.deleteAddon, {danger: true})}
</div>;
2019-06-27 22:18:40 +02:00
}
get footer() {
2020-02-28 01:00:12 +01:00
const links = ["website", "source", "invite", "donate", "patreon"];
const linkComponents = links.map(this.buildLink.bind(this)).filter(c => c);// linkComponents.map((comp, i) => i < linkComponents.length - 1 ? [comp, " | "] : comp).flat()
2019-06-27 22:18:40 +02:00
return <div className="bd-footer">
<span className="bd-links">{linkComponents}</span>
{this.controls}
2019-06-27 22:18:40 +02:00
</div>;
}
2019-07-03 16:15:48 +02:00
makeButton(title, children, action) {
return <Tooltip color="black" position="top" text={title}>
2019-07-03 16:15:48 +02:00
{(props) => {
return <div {...props} className="bd-addon-button" onClick={action}>{children}</div>;
}}
</Tooltip>;
}
makeControlButton(title, children, action, {danger = false, disabled = false} = {}) {
return <Tooltip color="black" position="top" text={title}>
{(props) => {
return <button {...props} className={"bd-button bd-addon-button" + (danger ? " bd-button-danger" : "") + (disabled ? " bd-button-disabled" : "")} onClick={action}>{children}</button>;
}}
</Tooltip>;
}
2019-06-27 22:18:40 +02:00
render() {
2019-06-28 01:50:20 +02:00
const addon = this.props.addon;
2019-06-27 22:18:40 +02:00
const name = this.getString(addon.name);
const author = this.getString(addon.author);
const description = this.getString(addon.description);
const version = this.getString(addon.version);
2019-06-30 05:09:48 +02:00
return <div id={`${addon.id}-card`} className="bd-addon-card settings-closed">
<div className="bd-addon-header">
<span className="bd-title">{this.buildTitle(name, version, author)}</span>
<Switch checked={this.props.enabled} onChange={this.onChange} />
2019-06-27 22:18:40 +02:00
</div>
<div className="bd-description-wrap scroller-wrap fade"><div className="bd-description scroller">{description}</div></div>
{this.footer}
2019-06-29 06:47:56 +02:00
</div>;
2019-06-27 22:18:40 +02:00
}
2019-06-28 01:50:20 +02:00
}
2020-07-16 07:42:56 +02:00
const originalRender = AddonCard.prototype.render;
Object.defineProperty(AddonCard.prototype, "render", {
enumerable: false,
configurable: false,
set: function() {Logger.warn("AddonCard", "Addon policy for plugins #5 https://github.com/rauenzi/BetterDiscordApp/wiki/Addon-Policies#plugins");},
get: () => originalRender
});