import React, { ComponentType, createElement, Suspense, SuspenseProps, } from "react" export type WithSuspenseProps = Pick & { suspenseKey?: string } type WithSuspenseOptions = { ssr?: boolean } const isComponent = ( component: ComponentType

| React.ReactNode, ): component is ComponentType

=> { return typeof component === "function" } export const withSuspense =

( WrappedComponent: ComponentType

, defaultFallback?: SuspenseProps["fallback"] | ComponentType

, { ssr = true }: WithSuspenseOptions = {}, ) => { return function WithSuspense(props: P & WithSuspenseProps) { const fallback = props.fallback ?? (isComponent(defaultFallback) ? createElement(defaultFallback, props) : defaultFallback) ?? null if (typeof window === "undefined" && !ssr) { return <>{fallback} } return ( ) } }