|
1 | 1 | "use client"; |
2 | 2 |
|
3 | | -import { useEffect, useState, useCallback } from "react"; |
| 3 | +import { useSyncExternalStore, useEffect, useCallback } from "react"; |
4 | 4 | import { createPortal } from "react-dom"; |
5 | 5 | import { useAgent } from "@copilotkit/react-core/v2"; |
6 | 6 |
|
7 | 7 | /** |
8 | | - * Renders a dismissible chip inside the CopilotChat input pill when a template |
9 | | - * is attached (pending_template is set in agent state). Uses a portal to insert |
10 | | - * itself inside the textarea's column container. |
| 8 | + * Manages the DOM element used as the portal container for the template chip. |
| 9 | + * Uses a subscribe/getSnapshot pattern compatible with useSyncExternalStore |
| 10 | + * to avoid setState-in-effect and ref-during-render lint violations. |
| 11 | + * |
| 12 | + * COUPLING NOTE: This depends on CopilotKit's internal DOM structure — |
| 13 | + * specifically that `[data-testid="copilot-chat-textarea"]` exists and sits inside |
| 14 | + * a parent column div. If CopilotKit changes this structure, the chip falls back |
| 15 | + * to rendering inline instead of portaling into the textarea. |
11 | 16 | */ |
| 17 | +let chipContainer: HTMLElement | null = null; |
| 18 | +const listeners = new Set<() => void>(); |
| 19 | + |
| 20 | +function getChipContainer() { |
| 21 | + return chipContainer; |
| 22 | +} |
| 23 | + |
| 24 | +function subscribeChipContainer(cb: () => void) { |
| 25 | + listeners.add(cb); |
| 26 | + return () => { listeners.delete(cb); }; |
| 27 | +} |
| 28 | + |
| 29 | +function ensureChipContainer(active: boolean) { |
| 30 | + if (!active) { |
| 31 | + if (chipContainer) { |
| 32 | + chipContainer.remove(); |
| 33 | + chipContainer = null; |
| 34 | + listeners.forEach((cb) => cb()); |
| 35 | + } |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + if (chipContainer) return; |
| 40 | + |
| 41 | + const textarea = document.querySelector<HTMLElement>( |
| 42 | + '[data-testid="copilot-chat-textarea"]' |
| 43 | + ); |
| 44 | + const textareaColumn = textarea?.parentElement; |
| 45 | + if (!textareaColumn) return; |
| 46 | + |
| 47 | + let el = textareaColumn.querySelector<HTMLElement>("[data-template-chip]"); |
| 48 | + if (!el) { |
| 49 | + el = document.createElement("div"); |
| 50 | + el.setAttribute("data-template-chip", ""); |
| 51 | + el.style.cssText = "display: flex; padding: 4px 0 0 0;"; |
| 52 | + textareaColumn.insertBefore(el, textarea); |
| 53 | + } |
| 54 | + chipContainer = el; |
| 55 | + listeners.forEach((cb) => cb()); |
| 56 | +} |
| 57 | + |
12 | 58 | export function TemplateChip() { |
13 | 59 | const { agent } = useAgent(); |
14 | 60 | const pending = agent.state?.pending_template as |
15 | 61 | | { id: string; name: string } |
16 | 62 | | null |
17 | 63 | | undefined; |
18 | 64 |
|
19 | | - const [container, setContainer] = useState<HTMLElement | null>(null); |
| 65 | + const container = useSyncExternalStore(subscribeChipContainer, getChipContainer, () => null); |
20 | 66 |
|
21 | 67 | useEffect(() => { |
22 | | - if (!pending?.name) { |
23 | | - // Clean up existing container |
24 | | - document.querySelector("[data-template-chip]")?.remove(); |
25 | | - setContainer(null); |
26 | | - return; |
27 | | - } |
28 | | - |
29 | | - const textarea = document.querySelector<HTMLElement>( |
30 | | - '[data-testid="copilot-chat-textarea"]' |
31 | | - ); |
32 | | - // The textarea sits inside a column div inside the grid |
33 | | - const textareaColumn = textarea?.parentElement; |
34 | | - if (!textareaColumn) { |
35 | | - setContainer(null); |
36 | | - return; |
37 | | - } |
38 | | - |
39 | | - // Reuse existing or create chip container |
40 | | - let el = textareaColumn.querySelector<HTMLElement>("[data-template-chip]"); |
41 | | - if (!el) { |
42 | | - el = document.createElement("div"); |
43 | | - el.setAttribute("data-template-chip", ""); |
44 | | - el.style.cssText = "display: flex; padding: 4px 0 0 0;"; |
45 | | - textareaColumn.insertBefore(el, textarea); |
46 | | - } |
47 | | - setContainer(el); |
| 68 | + ensureChipContainer(!!pending?.name); |
48 | 69 | }, [pending?.name]); |
49 | 70 |
|
| 71 | + // Clean up DOM node on unmount |
| 72 | + useEffect(() => { |
| 73 | + return () => { |
| 74 | + ensureChipContainer(false); |
| 75 | + }; |
| 76 | + }, []); |
| 77 | + |
50 | 78 | const handleDismiss = useCallback(() => { |
51 | 79 | agent.setState({ ...agent.state, pending_template: null }); |
52 | 80 | }, [agent]); |
53 | 81 |
|
54 | | - if (!pending?.name || !container) return null; |
| 82 | + if (!pending?.name) return null; |
55 | 83 |
|
56 | | - return createPortal( |
| 84 | + const chipContent = ( |
57 | 85 | <div |
58 | 86 | className="inline-flex items-center gap-1.5 pl-2 pr-1 py-0.5 rounded-md text-xs font-medium select-none" |
59 | 87 | style={{ |
@@ -101,7 +129,11 @@ export function TemplateChip() { |
101 | 129 | <path d="m6 6 12 12" /> |
102 | 130 | </svg> |
103 | 131 | </button> |
104 | | - </div>, |
105 | | - container |
| 132 | + </div> |
106 | 133 | ); |
| 134 | + |
| 135 | + // Fallback: render inline when CopilotKit DOM structure isn't available |
| 136 | + if (!container) return chipContent; |
| 137 | + |
| 138 | + return createPortal(chipContent, container); |
107 | 139 | } |
0 commit comments