MCPcopy Create free account
hub / github.com/CopilotKit/OpenGenerativeUI / HomePage

Function HomePage

apps/app/src/app/page.tsx:13–176  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

11import { CopilotChat, useAgent, useCopilotKit } from "@copilotkit/react-core/v2";
12
13export default function HomePage() {
14 useGenerativeUIExamples();
15 useExampleSuggestions();
16
17 const [demoDrawerOpen, setDemoDrawerOpen] = useState(false);
18 const [qrOpen, setQrOpen] = useState(false);
19 const [qrSessionId, setQrSessionId] = useState("");
20 const [scanStatus, setScanStatus] = useState<"waiting" | "scanned" | "picked">("waiting");
21 const { agent } = useAgent();
22 const { copilotkit } = useCopilotKit();
23
24 // Ref to always have the latest agent/copilotkit for async callbacks
25 const agentRef = useRef(agent);
26 const copilotkitRef = useRef(copilotkit);
27 useEffect(() => { agentRef.current = agent; }, [agent]);
28 useEffect(() => { copilotkitRef.current = copilotkit; }, [copilotkit]);
29
30 // Guard: prevent duplicate prompt dispatch across re-renders
31 const pickedRef = useRef(false);
32
33 const sendPrompt = useCallback((prompt: string) => {
34 const a = agentRef.current;
35 const ck = copilotkitRef.current;
36 a.addMessage({ id: crypto.randomUUID(), content: prompt, role: "user" });
37 ck.runAgent({ agent: a });
38 }, []);
39
40 const handleTryDemo = (demo: DemoItem) => {
41 setDemoDrawerOpen(false);
42 sendPrompt(demo.prompt);
43 };
44
45 const openQrModal = () => {
46 pickedRef.current = false;
47 setScanStatus("waiting");
48 setQrSessionId(crypto.randomUUID().slice(0, 12));
49 setQrOpen(true);
50 };
51
52 // Poll for QR pick status
53 useEffect(() => {
54 if (!qrOpen || !qrSessionId) return;
55 const interval = setInterval(async () => {
56 if (pickedRef.current) return;
57 try {
58 const res = await fetch(`/api/pick?sessionId=${qrSessionId}`);
59 const data = await res.json();
60 if (data.status === "scanned") {
61 setScanStatus("scanned");
62 } else if (data.status === "picked" && data.prompt && !pickedRef.current) {
63 pickedRef.current = true;
64 clearInterval(interval);
65 setScanStatus("picked");
66 setTimeout(() => {
67 setQrOpen(false);
68 sendPrompt(data.prompt);
69 }, 800);
70 }

Callers

nothing calls this directly

Calls 2

useGenerativeUIExamplesFunction · 0.90
useExampleSuggestionsFunction · 0.90

Tested by

no test coverage detected