/* * Vencord, a Discord client mod * Copyright (c) 2023 Vendicated and contributors * SPDX-License-Identifier: GPL-3.0-or-later */ import { ComponentType } from "react"; import { makeLazy } from "./lazy"; export const NoopComponent = () => null; /** * A lazy component. The factory method is called on first render. * @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(factory: () => React.ComponentType, attempts = 5) { const get = makeLazy(factory, attempts); let failed = false; const LazyComponent = (props: T) => { const Component = get() ?? (() => { if (!failed) { failed = true; console.error(`LazyComponent factory failed:\n${factory}`); } return NoopComponent; })(); return ; }; LazyComponent.$$vencordGetter = get; return LazyComponent as ComponentType; }