parent
98cb301df5
commit
a7ccbcfca4
@ -1,7 +1,11 @@
|
||||
export * as Plugins from "./plugins";
|
||||
export * as Webpack from "./webpack";
|
||||
export * as Api from "./api";
|
||||
export * as Components from "./components";
|
||||
|
||||
import "./utils/patchWebpack";
|
||||
import "./utils/quickCss";
|
||||
import { waitFor } from "./webpack";
|
||||
|
||||
export let Components;
|
||||
|
||||
waitFor("useState", () => setTimeout(() => import("./components").then(mod => Components = mod), 0));
|
@ -0,0 +1,71 @@
|
||||
import Logger from "../utils/logger";
|
||||
import { Card, React } from "../webpack/common";
|
||||
|
||||
interface Props {
|
||||
fallback?: React.ComponentType<React.PropsWithChildren<{ error: any; }>>;
|
||||
onError?(error: Error, errorInfo: React.ErrorInfo): void;
|
||||
}
|
||||
|
||||
const color = "#e78284";
|
||||
|
||||
const logger = new Logger("React ErrorBoundary", color);
|
||||
|
||||
const NO_ERROR = {};
|
||||
|
||||
export default class ErrorBoundary extends React.Component<React.PropsWithChildren<Props>> {
|
||||
static wrap<T = any>(Component: React.ComponentType<T>): (props: T) => React.ReactElement {
|
||||
return (props) => (
|
||||
<ErrorBoundary>
|
||||
<Component {...props} />
|
||||
</ErrorBoundary>
|
||||
);
|
||||
}
|
||||
|
||||
state = {
|
||||
error: NO_ERROR as any,
|
||||
message: ""
|
||||
};
|
||||
|
||||
static getDerivedStateFromError(error: any) {
|
||||
|
||||
return {
|
||||
error: error?.stack?.replace(/https:\/\/\S+\/assets\//g, "") || error?.message || String(error)
|
||||
};
|
||||
}
|
||||
|
||||
componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
|
||||
this.props.onError?.(error, errorInfo);
|
||||
logger.error("A component threw an Error\n", error);
|
||||
logger.error("Component Stack", errorInfo.componentStack);
|
||||
}
|
||||
|
||||
render() {
|
||||
if (this.state.error === NO_ERROR) return this.props.children;
|
||||
|
||||
if (this.props.fallback)
|
||||
return <this.props.fallback
|
||||
children={this.props.children}
|
||||
error={this.state.error}
|
||||
/>;
|
||||
|
||||
return (
|
||||
<Card style={{
|
||||
overflow: "hidden",
|
||||
padding: "2em",
|
||||
backgroundColor: color + "30",
|
||||
borderColor: color,
|
||||
color: "var(--text-normal)"
|
||||
}}>
|
||||
<h1>Oh no!</h1>
|
||||
<p>
|
||||
An error occurred while rendering this Component. More info can be found below
|
||||
and in your console.
|
||||
</p>
|
||||
<code>
|
||||
<pre>{this.state.error}
|
||||
</pre>
|
||||
</code>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
import { startAll } from "../plugins";
|
||||
import { waitFor, filters } from './webpack';
|
||||
|
||||
export let FluxDispatcher: any;
|
||||
export let React: typeof import("react");
|
||||
export let UserStore: any;
|
||||
export let Forms: any;
|
||||
export let Button: any;
|
||||
export let ButtonProps: any;
|
||||
export let Switch: any;
|
||||
export let Flex: any;
|
||||
export let Card: any;
|
||||
|
||||
waitFor("useState", m => React = m);
|
||||
waitFor(["dispatch", "subscribe"], m => {
|
||||
FluxDispatcher = m;
|
||||
const cb = () => {
|
||||
m.unsubscribe("CONNECTION_OPEN", cb);
|
||||
startAll();
|
||||
};
|
||||
m.subscribe("CONNECTION_OPEN", cb);
|
||||
});
|
||||
waitFor(["getCurrentUser", "initialize"], m => UserStore = m);
|
||||
waitFor("FormSection", m => Forms = m);
|
||||
waitFor(["ButtonLooks", "default"], m => {
|
||||
Button = m.default;
|
||||
ButtonProps = m;
|
||||
});
|
||||
waitFor(filters.byDisplayName("SwitchItem"), m => Switch = m.default);
|
||||
waitFor(filters.byDisplayName("Flex"), m => Flex = m.default);
|
||||
waitFor(filters.byDisplayName("Card"), m => Card = m.default);
|
@ -1,104 +1,2 @@
|
||||
import { startAll } from "../plugins";
|
||||
|
||||
let webpackCache: typeof window.webpackChunkdiscord_app;
|
||||
|
||||
export const subscriptions = new Map<FilterFn, CallbackFn>();
|
||||
export const listeners = new Set<CallbackFn>();
|
||||
|
||||
type FilterFn = (mod: any) => boolean;
|
||||
type CallbackFn = (mod: any) => void;
|
||||
|
||||
export let React: typeof import("react");
|
||||
export let FluxDispatcher: any;
|
||||
export let Forms: any;
|
||||
export let UserStore: any;
|
||||
|
||||
export function _initWebpack(instance: typeof window.webpackChunkdiscord_app) {
|
||||
if (webpackCache !== void 0) throw "no.";
|
||||
|
||||
webpackCache = instance.push([[Symbol()], {}, (r) => r.c]);
|
||||
instance.pop();
|
||||
|
||||
// Abandon Hope All Ye Who Enter Here
|
||||
|
||||
let started = false;
|
||||
waitFor("getCurrentUser", x => UserStore = x);
|
||||
waitFor(["dispatch", "subscribe"], x => {
|
||||
FluxDispatcher = x;
|
||||
const cb = () => {
|
||||
console.info("Connection open");
|
||||
x.unsubscribe("CONNECTION_OPEN", cb);
|
||||
startAll();
|
||||
};
|
||||
x.subscribe("CONNECTION_OPEN", cb);
|
||||
});
|
||||
waitFor("useState", x => (React = x));
|
||||
waitFor("FormSection", x => Forms = x);
|
||||
}
|
||||
|
||||
export function find(filter: FilterFn, getDefault = true) {
|
||||
if (typeof filter !== "function")
|
||||
throw new Error("Invalid filter. Expected a function got", filter);
|
||||
|
||||
for (const key in webpackCache) {
|
||||
const mod = webpackCache[key];
|
||||
if (mod?.exports && filter(mod.exports))
|
||||
return mod.exports;
|
||||
if (mod?.exports?.default && filter(mod.exports.default))
|
||||
return getDefault ? mod.exports.default : mod.exports;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export function findAll(filter: FilterFn, getDefault = true) {
|
||||
if (typeof filter !== "function") throw new Error("Invalid filter. Expected a function got", filter);
|
||||
|
||||
const ret = [] as any[];
|
||||
for (const key in webpackCache) {
|
||||
const mod = webpackCache[key];
|
||||
if (mod?.exports && filter(mod.exports)) ret.push(mod.exports);
|
||||
if (mod?.exports?.default && filter(mod.exports.default)) ret.push(getDefault ? mod.exports.default : mod.exports);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
export const filters = {
|
||||
byProps: (props: string[]): FilterFn =>
|
||||
props.length === 1
|
||||
? m => m[props[0]] !== void 0
|
||||
: m => props.every(p => m[p] !== void 0),
|
||||
byDisplayName: (deezNuts: string): FilterFn => m => m.default?.displayName === deezNuts
|
||||
};
|
||||
|
||||
export function findByProps(...props: string[]) {
|
||||
return find(filters.byProps(props));
|
||||
}
|
||||
|
||||
export function findAllByProps(...props: string[]) {
|
||||
return findAll(filters.byProps(props));
|
||||
}
|
||||
|
||||
export function findByDisplayName(deezNuts: string) {
|
||||
return find(filters.byDisplayName(deezNuts));
|
||||
}
|
||||
|
||||
export function waitFor(filter: string | string[] | FilterFn, callback: CallbackFn) {
|
||||
if (typeof filter === "string") filter = filters.byProps([filter]);
|
||||
else if (Array.isArray(filter)) filter = filters.byProps(filter);
|
||||
else if (typeof filter !== "function") throw new Error("filter must be a string, string[] or function, got", filter);
|
||||
|
||||
const existing = find(filter!);
|
||||
if (existing) return void callback(existing);
|
||||
|
||||
subscriptions.set(filter, callback);
|
||||
}
|
||||
|
||||
export function addListener(callback: CallbackFn) {
|
||||
listeners.add(callback);
|
||||
}
|
||||
|
||||
export function removeListener(callback: CallbackFn) {
|
||||
listeners.delete(callback);
|
||||
}
|
||||
export * from "./webpack";
|
||||
export * as Common from "./common";
|
@ -0,0 +1,84 @@
|
||||
let webpackCache: typeof window.webpackChunkdiscord_app;
|
||||
|
||||
export type FilterFn = (mod: any) => boolean;
|
||||
|
||||
export const filters = {
|
||||
byProps: (props: string[]): FilterFn =>
|
||||
props.length === 1
|
||||
? m => m[props[0]] !== void 0
|
||||
: m => props.every(p => m[p] !== void 0),
|
||||
byDisplayName: (deezNuts: string): FilterFn => m => m.default?.displayName === deezNuts
|
||||
};
|
||||
|
||||
export const subscriptions = new Map<FilterFn, CallbackFn>();
|
||||
export const listeners = new Set<CallbackFn>();
|
||||
|
||||
export type CallbackFn = (mod: any) => void;
|
||||
|
||||
export function _initWebpack(instance: typeof window.webpackChunkdiscord_app) {
|
||||
if (webpackCache !== void 0) throw "no.";
|
||||
|
||||
webpackCache = instance.push([[Symbol()], {}, (r) => r.c]);
|
||||
instance.pop();
|
||||
}
|
||||
|
||||
export function find(filter: FilterFn, getDefault = true) {
|
||||
if (typeof filter !== "function")
|
||||
throw new Error("Invalid filter. Expected a function got", filter);
|
||||
|
||||
for (const key in webpackCache) {
|
||||
const mod = webpackCache[key];
|
||||
if (mod?.exports && filter(mod.exports))
|
||||
return mod.exports;
|
||||
if (mod?.exports?.default && filter(mod.exports.default))
|
||||
return getDefault ? mod.exports.default : mod.exports;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export function findAll(filter: FilterFn, getDefault = true) {
|
||||
if (typeof filter !== "function") throw new Error("Invalid filter. Expected a function got", filter);
|
||||
|
||||
const ret = [] as any[];
|
||||
for (const key in webpackCache) {
|
||||
const mod = webpackCache[key];
|
||||
if (mod?.exports && filter(mod.exports)) ret.push(mod.exports);
|
||||
if (mod?.exports?.default && filter(mod.exports.default)) ret.push(getDefault ? mod.exports.default : mod.exports);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
export function findByProps(...props: string[]) {
|
||||
return find(filters.byProps(props));
|
||||
}
|
||||
|
||||
export function findAllByProps(...props: string[]) {
|
||||
return findAll(filters.byProps(props));
|
||||
}
|
||||
|
||||
export function findByDisplayName(deezNuts: string) {
|
||||
return find(filters.byDisplayName(deezNuts));
|
||||
}
|
||||
|
||||
export function waitFor(filter: string | string[] | FilterFn, callback: CallbackFn) {
|
||||
if (typeof filter === "string") filter = filters.byProps([filter]);
|
||||
else if (Array.isArray(filter)) filter = filters.byProps(filter);
|
||||
else if (typeof filter !== "function") throw new Error("filter must be a string, string[] or function, got", filter);
|
||||
|
||||
const existing = find(filter!);
|
||||
if (existing) return void callback(existing);
|
||||
|
||||
subscriptions.set(filter, callback);
|
||||
}
|
||||
|
||||
export function addListener(callback: CallbackFn) {
|
||||
listeners.add(callback);
|
||||
}
|
||||
|
||||
export function removeListener(callback: CallbackFn) {
|
||||
listeners.delete(callback);
|
||||
}
|
Loading…
Reference in new issue