2020-07-16 07:42:56 +02:00
|
|
|
import {React, Logger, Strings, WebpackModules, DOM, DiscordModules} from "modules";
|
2019-06-27 22:18:40 +02:00
|
|
|
import CloseButton from "../icons/close";
|
|
|
|
import ReloadIcon from "../icons/reload";
|
2019-06-30 07:32:14 +02:00
|
|
|
import EditIcon from "../icons/edit";
|
|
|
|
import DeleteIcon from "../icons/delete";
|
2019-06-30 05:09:48 +02:00
|
|
|
import Switch from "./components/switch";
|
2020-07-16 07:42:56 +02:00
|
|
|
import ErrorBoundary from "../errorboundary";
|
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);
|
2019-06-27 22:18:40 +02:00
|
|
|
this.closeSettings = this.closeSettings.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
reload() {
|
|
|
|
if (!this.props.reload) return;
|
|
|
|
this.props.addon = this.props.reload(this.props.addon.id);
|
|
|
|
this.forceUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
|
|
|
if (!this.state.settingsOpen) return;
|
|
|
|
if (this.settingsPanel instanceof Node) this.panelRef.current.appendChild(this.settingsPanel);
|
|
|
|
|
2020-07-16 07:42:56 +02:00
|
|
|
setImmediate(() => {
|
|
|
|
const isHidden = (container, element) => {
|
|
|
|
const cTop = container.scrollTop;
|
|
|
|
const cBottom = cTop + container.clientHeight;
|
|
|
|
const eTop = element.offsetTop;
|
|
|
|
const eBottom = eTop + element.clientHeight;
|
|
|
|
return (eTop < cTop || eBottom > cBottom);
|
|
|
|
};
|
|
|
|
|
|
|
|
const thisNode = this.panelRef.current;
|
|
|
|
const container = thisNode.closest(".scroller");
|
|
|
|
if (!isHidden(container, thisNode)) return;
|
|
|
|
const thisNodeOffset = DOM.offset(thisNode);
|
|
|
|
const containerOffset = DOM.offset(container);
|
|
|
|
const original = container.scrollTop;
|
|
|
|
const endPoint = thisNodeOffset.top - containerOffset.top + container.scrollTop - 30;
|
|
|
|
DOM.animate({
|
|
|
|
duration: 300,
|
|
|
|
update: function(progress) {
|
|
|
|
if (endPoint > original) container.scrollTop = original + (progress * (endPoint - original));
|
|
|
|
else container.scrollTop = original - (progress * (original - endPoint));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2019-06-27 22:18:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2019-06-28 01:50:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
showSettings() {
|
|
|
|
if (!this.props.hasSettings) return;
|
|
|
|
this.setState({settingsOpen: true});
|
|
|
|
}
|
|
|
|
|
2019-06-27 22:18:40 +02:00
|
|
|
closeSettings() {
|
|
|
|
this.panelRef.current.innerHTML = "";
|
|
|
|
this.setState({settingsOpen: false});
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
get settingsComponent() {
|
|
|
|
const addon = this.props.addon;
|
|
|
|
const name = this.getString(addon.name);
|
2019-06-28 01:50:20 +02:00
|
|
|
try { this.settingsPanel = this.props.getSettingsPanel(); }
|
|
|
|
catch (err) { Logger.stacktrace("Addon Settings", "Unable to get settings panel for " + name + ".", err); }
|
2019-06-27 22:18:40 +02:00
|
|
|
|
2019-06-28 01:50:20 +02:00
|
|
|
const props = {id: `${name}-settings`, className: "addon-settings", ref: this.panelRef};
|
2020-07-16 07:42:56 +02:00
|
|
|
if (typeof(settingsPanel) == "string") {
|
|
|
|
Logger.warn("Addon Settings", "Using a DOMString is officially deprecated.");
|
|
|
|
props.dangerouslySetInnerHTML = this.settingsPanel;
|
|
|
|
}
|
2019-06-27 22:18:40 +02:00
|
|
|
|
2019-06-29 06:47:56 +02:00
|
|
|
return <div className="bd-addon-card settings-open bd-switch-item">
|
2019-06-27 22:18:40 +02:00
|
|
|
<div className="bd-close" onClick={this.closeSettings}><CloseButton /></div>
|
2020-07-16 07:42:56 +02:00
|
|
|
<div {...props}><ErrorBoundary>{this.settingsPanel instanceof React.Component || typeof(this.settingsPanel) === "function" ? this.settingsPanel : null}</ErrorBoundary></div>
|
2019-06-29 06:47:56 +02:00
|
|
|
</div>;
|
2019-06-27 22:18:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
buildLink(which) {
|
|
|
|
const url = this.props.addon[which];
|
2020-07-16 07:42:56 +02:00
|
|
|
if (!url) return null;
|
|
|
|
const link = <a className="bd-link bd-link-website" href={url} target="_blank" rel="noopener noreferrer">{Strings.Addons[which]}</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
|
|
|
}
|
2020-07-16 07:42:56 +02:00
|
|
|
return link;
|
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"];
|
2019-06-28 01:50:20 +02:00
|
|
|
if (!links.some(l => this.props.addon[l]) && !this.props.hasSettings) return null;
|
2019-06-27 22:18:40 +02:00
|
|
|
const linkComponents = links.map(this.buildLink.bind(this)).filter(c => c);
|
|
|
|
return <div className="bd-footer">
|
2020-07-16 07:42:56 +02:00
|
|
|
<span className="bd-links">{linkComponents.map((comp, i) => i < linkComponents.length - 1 ? [comp, " | "] : comp).flat()}</span>
|
2019-06-30 05:09:48 +02:00
|
|
|
{this.props.hasSettings && <button onClick={this.showSettings} className="bd-button bd-button-addon-settings" disabled={!this.props.enabled}>{Strings.Addons.addonSettings}</button>}
|
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}>
|
|
|
|
{(props) => {
|
|
|
|
return <div {...props} className="bd-addon-button" onClick={action}>{children}</div>;
|
|
|
|
}}
|
|
|
|
</Tooltip>;
|
|
|
|
}
|
|
|
|
|
2019-06-27 22:18:40 +02:00
|
|
|
render() {
|
|
|
|
if (this.state.settingsOpen) return this.settingsComponent;
|
|
|
|
|
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>
|
2019-06-27 22:18:40 +02:00
|
|
|
<div className="bd-controls">
|
2019-07-03 16:15:48 +02:00
|
|
|
{this.props.editAddon && this.makeButton(Strings.Addons.editAddon, <EditIcon />, this.props.editAddon)}
|
|
|
|
{this.props.deleteAddon && this.makeButton(Strings.Addons.deleteAddon, <DeleteIcon />, this.props.deleteAddon)}
|
|
|
|
{this.props.showReloadIcon && this.makeButton(Strings.Addons.reload, <ReloadIcon className="bd-reload bd-reload-card" />, this.reload)}
|
2019-06-30 05:09:48 +02:00
|
|
|
<Switch checked={this.props.enabled} onChange={this.onChange} />
|
2019-06-27 22:18:40 +02:00
|
|
|
</div>
|
|
|
|
</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
|
|
|
|
});
|