import { ApplicationRef, createComponent, Type, ComponentRef } from '@angular/core'; import { createApplication } from '@angular/platform-browser'; import { HostedComponentContext } from '@vendure/admin-ui/react'; import React, { useContext, useEffect, useRef, useState, PropsWithChildren } from 'react'; type AnyComponentRef = ComponentRef; export function Wrap>( props: PropsWithChildren<{ cmp: T; inputs?: { [key: string]: any } }>, ) { const context = useContext(HostedComponentContext); const hostRef = useRef(null); const [appRef, setAppRef] = useState(null); const [compRef, setCompRef] = useState(null); useEffect(() => { void createApplication().then(setAppRef); return () => appRef?.destroy(); }, []); useEffect(() => { if (appRef && hostRef.current) { setCompRef( createComponent(props.cmp, { environmentInjector: appRef!.injector as any, hostElement: hostRef!.current!, projectableNodes: [props.children as any], }), ); } return () => compRef?.destroy(); }, [appRef, hostRef, props.cmp]); useEffect(() => { if (compRef) { for (const [key, value] of Object.entries(props.inputs || {})) { compRef.setInput(key, value); } compRef.changeDetectorRef.detectChanges(); } }, [compRef, props.inputs]); return
; }