|
|
|
@ -16,79 +16,7 @@
|
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { Clipboard, React, Toasts, useEffect, useState } from "@webpack/common";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Makes a lazy function. On first call, the value is computed.
|
|
|
|
|
* On subsequent calls, the same computed value will be returned
|
|
|
|
|
* @param factory Factory function
|
|
|
|
|
*/
|
|
|
|
|
export function makeLazy<T>(factory: () => T): () => T {
|
|
|
|
|
let cache: T;
|
|
|
|
|
return () => cache ?? (cache = factory());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type AwaiterRes<T> = [T, any, boolean];
|
|
|
|
|
interface AwaiterOpts<T> {
|
|
|
|
|
fallbackValue: T,
|
|
|
|
|
deps?: unknown[],
|
|
|
|
|
onError?(e: any): void,
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Await a promise
|
|
|
|
|
* @param factory Factory
|
|
|
|
|
* @param fallbackValue The fallback value that will be used until the promise resolved
|
|
|
|
|
* @returns [value, error, isPending]
|
|
|
|
|
*/
|
|
|
|
|
export function useAwaiter<T>(factory: () => Promise<T>): AwaiterRes<T | null>;
|
|
|
|
|
export function useAwaiter<T>(factory: () => Promise<T>, providedOpts: AwaiterOpts<T>): AwaiterRes<T>;
|
|
|
|
|
export function useAwaiter<T>(factory: () => Promise<T>, providedOpts?: AwaiterOpts<T | null>): AwaiterRes<T | null> {
|
|
|
|
|
const opts: Required<AwaiterOpts<T | null>> = Object.assign({
|
|
|
|
|
fallbackValue: null,
|
|
|
|
|
deps: [],
|
|
|
|
|
onError: null,
|
|
|
|
|
}, providedOpts);
|
|
|
|
|
const [state, setState] = useState({
|
|
|
|
|
value: opts.fallbackValue,
|
|
|
|
|
error: null,
|
|
|
|
|
pending: true
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
let isAlive = true;
|
|
|
|
|
if (!state.pending) setState({ ...state, pending: true });
|
|
|
|
|
|
|
|
|
|
factory()
|
|
|
|
|
.then(value => isAlive && setState({ value, error: null, pending: false }))
|
|
|
|
|
.catch(error => isAlive && (setState({ value: null, error, pending: false }), opts.onError?.(error)));
|
|
|
|
|
|
|
|
|
|
return () => void (isAlive = false);
|
|
|
|
|
}, opts.deps);
|
|
|
|
|
|
|
|
|
|
return [state.value, state.error, state.pending];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a function that can be used to force rerender react components
|
|
|
|
|
*/
|
|
|
|
|
export function useForceUpdater() {
|
|
|
|
|
const [, set] = useState(0);
|
|
|
|
|
return () => set(s => s + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A lazy component. The factory method is called on first render. For example useful
|
|
|
|
|
* for const Component = LazyComponent(() => findByDisplayName("...").default)
|
|
|
|
|
* @param factory Function returning a Component
|
|
|
|
|
* @returns Result of factory function
|
|
|
|
|
*/
|
|
|
|
|
export function LazyComponent<T = any>(factory: () => React.ComponentType<T>) {
|
|
|
|
|
const get = makeLazy(factory);
|
|
|
|
|
return (props: T & JSX.IntrinsicAttributes) => {
|
|
|
|
|
const Component = get();
|
|
|
|
|
return <Component {...props} />;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
import { Clipboard, Toasts } from "@webpack/common";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Recursively merges defaults into an object and returns the same object
|
|
|
|
@ -109,34 +37,6 @@ export function mergeDefaults<T>(obj: T, defaults: T): T {
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Join an array of strings in a human readable way (1, 2 and 3)
|
|
|
|
|
* @param elements Elements
|
|
|
|
|
*/
|
|
|
|
|
export function humanFriendlyJoin(elements: string[]): string;
|
|
|
|
|
/**
|
|
|
|
|
* Join an array of strings in a human readable way (1, 2 and 3)
|
|
|
|
|
* @param elements Elements
|
|
|
|
|
* @param mapper Function that converts elements to a string
|
|
|
|
|
*/
|
|
|
|
|
export function humanFriendlyJoin<T>(elements: T[], mapper: (e: T) => string): string;
|
|
|
|
|
export function humanFriendlyJoin(elements: any[], mapper: (e: any) => string = s => s): string {
|
|
|
|
|
const { length } = elements;
|
|
|
|
|
if (length === 0) return "";
|
|
|
|
|
if (length === 1) return mapper(elements[0]);
|
|
|
|
|
|
|
|
|
|
let s = "";
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
|
s += mapper(elements[i]);
|
|
|
|
|
if (length - i > 2) s += ", ";
|
|
|
|
|
else if (length - i > 1) s += " and ";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Calls .join(" ") on the arguments
|
|
|
|
|
* classes("one", "two") => "one two"
|
|
|
|
@ -152,14 +52,6 @@ export function sleep(ms: number): Promise<void> {
|
|
|
|
|
return new Promise(r => setTimeout(r, ms));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Wrap the text in ``` with an optional language
|
|
|
|
|
*/
|
|
|
|
|
export function makeCodeblock(text: string, language?: string) {
|
|
|
|
|
const chars = "```";
|
|
|
|
|
return `${chars}${language || ""}\n${text.replaceAll("```", "\\`\\`\\`")}\n${chars}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function copyWithToast(text: string, toastMessage = "Copied to clipboard!") {
|
|
|
|
|
if (Clipboard.SUPPORTS_COPY) {
|
|
|
|
|
Clipboard.copy(text);
|
|
|
|
|