forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopilotKitInspector.tsx
More file actions
52 lines (40 loc) · 1.47 KB
/
Copy pathCopilotKitInspector.tsx
File metadata and controls
52 lines (40 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import * as React from "react";
import { createComponent } from "@lit-labs/react";
import type { CopilotKitCore } from "@copilotkit/core";
import type { Anchor } from "@copilotkit/web-inspector";
type CopilotKitInspectorBaseProps = {
core?: CopilotKitCore | null;
defaultAnchor?: Anchor;
[key: string]: unknown;
};
type InspectorComponent = React.ComponentType<CopilotKitInspectorBaseProps>;
export interface CopilotKitInspectorProps extends CopilotKitInspectorBaseProps {}
export const CopilotKitInspector: React.FC<CopilotKitInspectorProps> = ({
core,
...rest
}) => {
const [InspectorComponent, setInspectorComponent] =
React.useState<InspectorComponent | null>(null);
React.useEffect(() => {
let mounted = true;
// Load the web component only on the client to keep SSR output stable.
import("@copilotkit/web-inspector").then((mod) => {
mod.defineWebInspector?.();
const Component = createComponent({
tagName: mod.WEB_INSPECTOR_TAG,
elementClass: mod.WebInspectorElement,
react: React,
}) as InspectorComponent;
if (mounted) {
setInspectorComponent(() => Component);
}
});
return () => {
mounted = false;
};
}, []);
// During SSR (and until the client finishes loading), render nothing to keep markup consistent.
if (!InspectorComponent) return null;
return <InspectorComponent {...rest} core={core ?? null} />;
};
CopilotKitInspector.displayName = "CopilotKitInspector";