|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect, useState, useCallback } from "react"; |
| 4 | +import { createPortal } from "react-dom"; |
| 5 | +import { useAgent } from "@copilotkit/react-core/v2"; |
| 6 | + |
| 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. |
| 11 | + */ |
| 12 | +export function TemplateChip() { |
| 13 | + const { agent } = useAgent(); |
| 14 | + const pending = agent.state?.pending_template as |
| 15 | + | { id: string; name: string } |
| 16 | + | null |
| 17 | + | undefined; |
| 18 | + |
| 19 | + const [container, setContainer] = useState<HTMLElement | null>(null); |
| 20 | + |
| 21 | + 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); |
| 48 | + }, [pending?.name]); |
| 49 | + |
| 50 | + const handleDismiss = useCallback(() => { |
| 51 | + agent.setState({ ...agent.state, pending_template: null }); |
| 52 | + }, [agent]); |
| 53 | + |
| 54 | + if (!pending?.name || !container) return null; |
| 55 | + |
| 56 | + return createPortal( |
| 57 | + <div |
| 58 | + className="inline-flex items-center gap-1.5 pl-2 pr-1 py-0.5 rounded-md text-xs font-medium select-none" |
| 59 | + style={{ |
| 60 | + background: |
| 61 | + "linear-gradient(135deg, rgba(99,102,241,0.12), rgba(16,185,129,0.12))", |
| 62 | + border: "1px solid rgba(99,102,241,0.22)", |
| 63 | + color: "var(--copilot-kit-contrast-color, #1a1a1a)", |
| 64 | + animation: "chipIn 0.15s ease-out", |
| 65 | + }} |
| 66 | + > |
| 67 | + <svg |
| 68 | + width="11" |
| 69 | + height="11" |
| 70 | + viewBox="0 0 24 24" |
| 71 | + fill="none" |
| 72 | + stroke="currentColor" |
| 73 | + strokeWidth="2" |
| 74 | + strokeLinecap="round" |
| 75 | + strokeLinejoin="round" |
| 76 | + style={{ opacity: 0.55, flexShrink: 0 }} |
| 77 | + > |
| 78 | + <path d="m19 21-7-4-7 4V5a2 2 0 0 1 2-2h10a2 2 0 0 1 2 2v16z" /> |
| 79 | + </svg> |
| 80 | + <span className="max-w-[140px] truncate" style={{ lineHeight: "16px" }}> |
| 81 | + {pending.name} |
| 82 | + </span> |
| 83 | + <button |
| 84 | + onClick={handleDismiss} |
| 85 | + className="p-0.5 rounded transition-colors duration-100 hover:bg-black/10 dark:hover:bg-white/10" |
| 86 | + style={{ lineHeight: 0 }} |
| 87 | + aria-label="Remove template" |
| 88 | + type="button" |
| 89 | + > |
| 90 | + <svg |
| 91 | + width="11" |
| 92 | + height="11" |
| 93 | + viewBox="0 0 24 24" |
| 94 | + fill="none" |
| 95 | + stroke="currentColor" |
| 96 | + strokeWidth="2.5" |
| 97 | + strokeLinecap="round" |
| 98 | + strokeLinejoin="round" |
| 99 | + > |
| 100 | + <path d="M18 6 6 18" /> |
| 101 | + <path d="m6 6 12 12" /> |
| 102 | + </svg> |
| 103 | + </button> |
| 104 | + </div>, |
| 105 | + container |
| 106 | + ); |
| 107 | +} |
0 commit comments