BetterDiscordApp-rauenzi/renderer/src/ui/modals.js

234 lines
13 KiB
JavaScript
Raw Normal View History

2020-02-28 01:00:12 +01:00
import {Config} from "data";
import Logger from "common/logger";
import {WebpackModules, React, Settings, Strings, DOM, DiscordModules} from "modules";
2020-07-16 07:42:56 +02:00
import FormattableString from "../structs/string";
2021-04-06 20:09:43 +02:00
import AddonErrorModal from "./addonerrormodal";
import ErrorBoundary from "./errorboundary";
2019-05-31 07:53:11 +02:00
export default class Modals {
2019-06-27 22:18:40 +02:00
static get shouldShowAddonErrors() {return Settings.get("settings", "addons", "addonErrors");}
2019-06-03 22:25:08 +02:00
static get ModalActions() {return WebpackModules.getByProps("openModal", "updateModal");}
2019-05-31 07:53:11 +02:00
static get ModalStack() {return WebpackModules.getByProps("push", "update", "pop", "popWithKey");}
static get ModalComponents() {return WebpackModules.getByProps("ModalRoot");}
static get ModalClasses() {return WebpackModules.getByProps("modal", "content");}
2019-05-31 07:53:11 +02:00
static get AlertModal() {return WebpackModules.getByPrototypes("handleCancel", "handleSubmit", "handleMinorConfirm");}
static get FlexElements() {return WebpackModules.getByProps("Child", "Align");}
static get FormTitle() {return WebpackModules.findByDisplayName("FormTitle");}
2019-05-31 07:53:11 +02:00
static get TextElement() {return WebpackModules.getByProps("Sizes", "Weights");}
static get ConfirmationModal() {return WebpackModules.findByDisplayName("ConfirmModal");}
2020-07-16 07:42:56 +02:00
static get Markdown() {return WebpackModules.findByDisplayName("Markdown");}
static get Buttons() {return WebpackModules.getByProps("ButtonColors");}
2019-05-31 07:53:11 +02:00
static default(title, content) {
2020-07-16 23:17:02 +02:00
const modal = DOM.createElement(`<div class="bd-modal-wrapper theme-dark">
2020-07-16 07:42:56 +02:00
<div class="bd-backdrop backdrop-1wrmKB"></div>
<div class="bd-modal modal-1UGdnR">
<div class="bd-modal-inner inner-1JeGVc">
2019-05-31 07:53:11 +02:00
<div class="header header-1R_AjF">
<div class="title">${title}</div>
</div>
<div class="bd-modal-body">
<div class="scroller-wrap fade">
<div class="scroller">
${content}
</div>
</div>
</div>
2020-07-16 07:42:56 +02:00
<div class="footer footer-2yfCgX footer-3rDWdC footer-2gL1pp">
2019-06-30 05:09:48 +02:00
<button type="button" class="bd-button">${Strings.Modals.okay}</button>
2019-05-31 07:53:11 +02:00
</div>
</div>
</div>
</div>`);
2019-06-22 06:37:19 +02:00
modal.querySelector(".footer button").addEventListener("click", () => {
2020-07-16 23:17:02 +02:00
modal.classList.add("closing");
setTimeout(() => {modal.remove();}, 300);
2019-05-31 07:53:11 +02:00
});
2019-06-22 06:37:19 +02:00
modal.querySelector(".bd-backdrop").addEventListener("click", () => {
2020-07-16 23:17:02 +02:00
modal.classList.add("closing");
setTimeout(() => {modal.remove();}, 300);
2019-05-31 07:53:11 +02:00
});
2019-06-22 06:37:19 +02:00
document.querySelector("#app-mount").append(modal);
2019-05-31 07:53:11 +02:00
}
static alert(title, content) {
2021-04-06 20:09:43 +02:00
this.showConfirmationModal(title, content, {cancelText: null});
2019-05-31 07:53:11 +02:00
}
/**
* Shows a generic but very customizable confirmation modal with optional confirm and cancel callbacks.
* @param {string} title - title of the modal
2020-07-16 07:42:56 +02:00
* @param {(string|ReactElement|Array<string|ReactElement>)} children - a single or mixed array of react elements and strings. Everything is wrapped in Discord's `Markdown` component so strings will show and render properly.
2019-05-31 07:53:11 +02:00
* @param {object} [options] - options to modify the modal
* @param {boolean} [options.danger=false] - whether the main button should be red or not
* @param {string} [options.confirmText=Okay] - text for the confirmation/submit button
* @param {string} [options.cancelText=Cancel] - text for the cancel button
* @param {callable} [options.onConfirm=NOOP] - callback to occur when clicking the submit button
* @param {callable} [options.onCancel=NOOP] - callback to occur when clicking the cancel button
2020-07-16 07:42:56 +02:00
* @param {string} [options.key] - key used to identify the modal. If not provided, one is generated and returned
* @returns {string} - the key used for this modal
2019-05-31 07:53:11 +02:00
*/
static showConfirmationModal(title, content, options = {}) {
2020-07-16 07:42:56 +02:00
const Markdown = this.Markdown;
2019-05-31 07:53:11 +02:00
const ConfirmationModal = this.ConfirmationModal;
const ModalActions = this.ModalActions;
2020-07-16 07:42:56 +02:00
if (content instanceof FormattableString) content = content.toString();
if (!this.ModalActions || !this.ConfirmationModal || !this.Markdown) return this.default(title, content);
2019-05-31 07:53:11 +02:00
const emptyFunction = () => {};
2020-07-16 07:42:56 +02:00
const {onConfirm = emptyFunction, onCancel = emptyFunction, confirmText = Strings.Modals.okay, cancelText = Strings.Modals.cancel, danger = false, key = undefined} = options;
2020-07-16 07:42:56 +02:00
if (!Array.isArray(content)) content = [content];
content = content.map(c => typeof(c) === "string" ? React.createElement(Markdown, null, c) : c);
return ModalActions.openModal(props => {
return React.createElement(ConfirmationModal, Object.assign({
header: title,
confirmButtonColor: danger ? this.Buttons.ButtonColors.RED : this.Buttons.ButtonColors.BRAND,
confirmText: confirmText,
cancelText: cancelText,
onConfirm: onConfirm,
onCancel: onCancel
}, props), content);
}, {modalKey: key});
2019-05-31 07:53:11 +02:00
}
2019-06-27 22:18:40 +02:00
static showAddonErrors({plugins: pluginErrors = [], themes: themeErrors = []}) {
if (!pluginErrors || !themeErrors || !this.shouldShowAddonErrors) return;
2019-05-31 07:53:11 +02:00
if (!pluginErrors.length && !themeErrors.length) return;
2021-04-06 20:09:43 +02:00
if (this.addonErrorsRef && this.addonErrorsRef.current) {
return this.addonErrorsRef.current.refreshTabs(Array.isArray(pluginErrors) ? pluginErrors : [], Array.isArray(themeErrors) ? themeErrors : []);
}
this.addonErrorsRef = React.createRef();
this.ModalActions.openModal(props => React.createElement(this.ModalComponents.ModalRoot, Object.assign(props, {
size: "medium",
children: React.createElement(AddonErrorModal, {
ref: this.addonErrorsRef,
pluginErrors: Array.isArray(pluginErrors) ? pluginErrors : [],
themeErrors: Array.isArray(themeErrors) ? themeErrors : [],
onClose: props.onClose
})
})));
2019-05-31 07:53:11 +02:00
}
2020-02-28 01:00:12 +01:00
2020-07-16 07:42:56 +02:00
static showChangelogModal(options = {}) {
2020-02-28 01:00:12 +01:00
const ModalStack = WebpackModules.getByProps("push", "update", "pop", "popWithKey");
const ChangelogClasses = WebpackModules.getByProps("fixed", "improved");
2020-07-16 07:42:56 +02:00
const TextElement = WebpackModules.findByDisplayName("Text");
2020-02-28 01:00:12 +01:00
const FlexChild = WebpackModules.getByProps("Child");
const Titles = WebpackModules.getByProps("Tags", "default");
const Changelog = WebpackModules.getModule(m => m.defaultProps && m.defaultProps.selectable == false);
const MarkdownParser = WebpackModules.getByProps("defaultRules", "parse");
2020-07-16 07:42:56 +02:00
if (!Changelog || !ModalStack || !ChangelogClasses || !TextElement || !FlexChild || !Titles || !MarkdownParser) return Logger.warn("Modals", "showChangelogModal missing modules");
const {image = "https://i.imgur.com/8sctUVV.png", description = "", changes = [], title = "BetterDiscord", subtitle = `v${Config.version}`, footer} = options;
2020-02-28 01:00:12 +01:00
const ce = React.createElement;
const changelogItems = [options.video ? ce("video", {src: options.video, poster: options.poster, controls: true, className: ChangelogClasses.video}) : ce("img", {src: image})];
2020-02-28 01:00:12 +01:00
if (description) changelogItems.push(ce("p", null, MarkdownParser.parse(description)));
for (let c = 0; c < changes.length; c++) {
const entry = changes[c];
const type = ChangelogClasses[entry.type] ? ChangelogClasses[entry.type] : ChangelogClasses.added;
const margin = c == 0 ? ChangelogClasses.marginTop : "";
changelogItems.push(ce("h1", {className: `${type} ${margin}`,}, entry.title));
const list = ce("ul", null, entry.items.map(i => ce("li", null, MarkdownParser.parse(i))));
changelogItems.push(list);
}
const renderHeader = function() {
return ce(FlexChild.Child, {grow: 1, shrink: 1},
ce(Titles.default, {tag: Titles.Tags.H4}, title),
2020-07-16 07:42:56 +02:00
ce(TextElement, {size: TextElement.Sizes.SMALL, color: TextElement.Colors.STANDARD, className: ChangelogClasses.date}, subtitle)
2020-02-28 01:00:12 +01:00
);
};
2020-02-28 01:00:12 +01:00
const renderFooter = () => {
const Anchor = WebpackModules.getModule(m => m.displayName == "Anchor");
const AnchorClasses = WebpackModules.getByProps("anchorUnderlineOnHover") || {anchor: "anchor-3Z-8Bb", anchorUnderlineOnHover: "anchorUnderlineOnHover-2ESHQB"};
const joinSupportServer = (click) => {
click.preventDefault();
click.stopPropagation();
ModalStack.pop();
2020-07-16 07:42:56 +02:00
DiscordModules.InviteActions.acceptInviteAndTransitionToInviteChannel("2HScm8j");
2020-02-28 01:00:12 +01:00
};
const supportLink = Anchor ? ce(Anchor, {onClick: joinSupportServer}, "Join our Discord Server.") : ce("a", {className: `${AnchorClasses.anchor} ${AnchorClasses.anchorUnderlineOnHover}`, onClick: joinSupportServer}, "Join our Discord Server.");
2020-07-16 07:42:56 +02:00
const defaultFooter = ce(TextElement, {size: TextElement.Sizes.SMALL, color: TextElement.Colors.STANDARD}, "Need support? ", supportLink);
2020-02-28 01:00:12 +01:00
return ce(FlexChild.Child, {grow: 1, shrink: 1}, footer ? footer : defaultFooter);
};
const ModalActions = this.ModalActions;
const OriginalModalClasses = WebpackModules.getByProps("hideOnFullscreen", "root");
const originalRoot = OriginalModalClasses.root;
if (originalRoot) OriginalModalClasses.root = `${originalRoot} bd-changelog-modal`;
const key = ModalActions.openModal(props => {
return React.createElement(Changelog, Object.assign({
className: `bd-changelog ${ChangelogClasses.container}`,
selectable: true,
onScroll: _ => _,
onClose: _ => _,
renderHeader: renderHeader,
renderFooter: renderFooter,
}, props), changelogItems);
2020-02-28 01:00:12 +01:00
});
2020-07-27 01:11:16 +02:00
const closeModal = ModalActions.closeModal;
ModalActions.closeModal = function(k) {
Reflect.apply(closeModal, this, arguments);
setTimeout(() => {if (originalRoot && k === key) OriginalModalClasses.root = originalRoot;}, 1000);
ModalActions.closeModal = closeModal;
};
return key;
2020-02-28 01:00:12 +01:00
}
static showAddonSettingsModal(name, panel) {
let child = panel;
if (panel instanceof Node || typeof(panel) === "string") {
child = class ReactWrapper extends React.Component {
constructor(props) {
super(props);
this.elementRef = React.createRef();
this.element = panel;
}
componentDidMount() {
if (this.element instanceof Node) this.elementRef.current.appendChild(this.element);
}
render() {
const props = {
className: "bd-addon-settings-wrap",
ref: this.elementRef
};
if (typeof(this.element) === "string") props.dangerouslySetInnerHTML = {__html: this.element};
return React.createElement("div", props);
}
};
}
if (typeof(child) === "function") child = React.createElement(child);
const mc = this.ModalComponents;
const modal = props => {
return React.createElement(mc.ModalRoot, Object.assign({size: mc.ModalSize.MEDIUM, className: "bd-addon-modal"}, props),
React.createElement(mc.ModalHeader, {separator: false, className: "bd-addon-modal-header"},
React.createElement(this.FormTitle, {tag: "h4"}, `${name} Settings`),
React.createElement(this.FlexElements.Child, {grow: 0},
React.createElement(mc.ModalCloseButton, {className: "bd-modal-close", onClick: props.onClose})
)
),
React.createElement(mc.ModalContent, {className: "bd-addon-modal-settings"},
React.createElement(ErrorBoundary, {}, child)
),
React.createElement(mc.ModalFooter, {className: "bd-addon-modal-footer"},
React.createElement(this.Buttons.default, {onClick: props.onClose, className: "bd-button"}, Strings.Modals.done)
)
);
};
return this.ModalActions.openModal(props => {
return React.createElement(modal, props);
});
}
2019-05-31 07:53:11 +02:00
}