|
| 1 | +import * as React from "react"; |
| 2 | +import { createComponent } from "@lit-labs/react"; |
| 3 | +import { |
| 4 | + WEB_INSPECTOR_TAG, |
| 5 | + WebInspectorElement, |
| 6 | + defineWebInspector, |
| 7 | +} from "@copilotkitnext/web-inspector"; |
| 8 | +import type { CopilotKitCore } from "@copilotkitnext/core"; |
| 9 | + |
| 10 | +defineWebInspector(); |
| 11 | + |
| 12 | +const CopilotKitInspectorBase = createComponent({ |
| 13 | + tagName: WEB_INSPECTOR_TAG, |
| 14 | + elementClass: WebInspectorElement, |
| 15 | + react: React, |
| 16 | +}); |
| 17 | + |
| 18 | +export type CopilotKitInspectorBaseProps = React.ComponentProps<typeof CopilotKitInspectorBase>; |
| 19 | + |
| 20 | +export interface CopilotKitInspectorProps extends Omit<CopilotKitInspectorBaseProps, "core"> { |
| 21 | + core?: CopilotKitCore | null; |
| 22 | +} |
| 23 | + |
| 24 | +export const CopilotKitInspector = React.forwardRef< |
| 25 | + WebInspectorElement, |
| 26 | + CopilotKitInspectorProps |
| 27 | +>( |
| 28 | + ({ core, ...rest }, ref) => { |
| 29 | + const innerRef = React.useRef<WebInspectorElement>(null); |
| 30 | + |
| 31 | + React.useImperativeHandle(ref, () => innerRef.current as WebInspectorElement, []); |
| 32 | + |
| 33 | + React.useEffect(() => { |
| 34 | + if (innerRef.current) { |
| 35 | + innerRef.current.core = core ?? null; |
| 36 | + } |
| 37 | + }, [core]); |
| 38 | + |
| 39 | + return ( |
| 40 | + <CopilotKitInspectorBase |
| 41 | + {...(rest as CopilotKitInspectorBaseProps)} |
| 42 | + ref={innerRef} |
| 43 | + /> |
| 44 | + ); // eslint-disable-line react/jsx-props-no-spreading |
| 45 | + }, |
| 46 | +); |
| 47 | + |
| 48 | +CopilotKitInspector.displayName = "CopilotKitInspector"; |
0 commit comments