Vencord/src/utils/lazyReact.tsx

50 lines
1.5 KiB
TypeScript
Raw Normal View History

/*
* Vencord, a Discord client mod
* Copyright (c) 2023 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import { makeLazy } from "./lazy";
/**
* A lazy component. The factory method is called on first render.
2024-05-08 09:44:18 +00:00
*
* IMPORTANT: You cannot access properties set on the lazy component using this method.
*
* Example of how you cannot access the properties set on the component:
* ```
* const Component = LazyComponent(...);
* console.log(Component.Types); // This will not work
* ````
2024-05-03 02:18:12 +00:00
* @param factory Function returning a component
* @param attempts How many times to try to get the component before giving up
* @returns Result of factory function
*/
export function LazyComponent<T extends object = any>(factory: () => React.ComponentType<T>, attempts = 5) {
const get = makeLazy(factory, attempts, { isIndirect: true });
2024-05-03 02:18:12 +00:00
let InnerComponent = null as React.ComponentType<T> | null;
2024-05-05 02:56:02 +00:00
let lazyFailedLogged = false;
const LazyComponent = (props: T) => {
2024-05-05 02:56:02 +00:00
if (!get.$$vencordLazyFailed()) {
const ResultComponent = get();
if (ResultComponent != null) {
InnerComponent = ResultComponent;
}
2024-05-05 02:56:02 +00:00
}
2024-05-03 02:18:12 +00:00
if (InnerComponent === null && !lazyFailedLogged) {
if (get.$$vencordLazyFailed()) {
lazyFailedLogged = true;
}
console.error(`LazyComponent factory failed:\n\n${factory}`);
}
return InnerComponent && <InnerComponent {...props} />;
};
return LazyComponent as React.ComponentType<T>;
}