|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect, useState } from "react"; |
| 4 | +import { createPortal } from "react-dom"; |
| 5 | + |
| 6 | +const cards = [ |
| 7 | + { |
| 8 | + icon: ( |
| 9 | + <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"> |
| 10 | + <path d="M21.21 15.89A10 10 0 1 1 8 2.83" /> |
| 11 | + <path d="M22 12A10 10 0 0 0 12 2v10z" /> |
| 12 | + </svg> |
| 13 | + ), |
| 14 | + title: "Generative UI", |
| 15 | + description: |
| 16 | + "AI generates interactive charts, visualizations, and rich components directly in the conversation.", |
| 17 | + }, |
| 18 | + { |
| 19 | + icon: ( |
| 20 | + <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"> |
| 21 | + <rect x="2" y="3" width="20" height="14" rx="2" ry="2" /> |
| 22 | + <line x1="8" y1="21" x2="16" y2="21" /> |
| 23 | + <line x1="12" y1="17" x2="12" y2="21" /> |
| 24 | + </svg> |
| 25 | + ), |
| 26 | + title: "Interactive Widgets", |
| 27 | + description: |
| 28 | + "Complex HTML/JS visualizations run in sandboxed iframes — try asking for an animation or diagram.", |
| 29 | + }, |
| 30 | + { |
| 31 | + icon: ( |
| 32 | + <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"> |
| 33 | + <polygon points="12 2 2 7 12 12 22 7 12 2" /> |
| 34 | + <polyline points="2 17 12 22 22 17" /> |
| 35 | + <polyline points="2 12 12 17 22 12" /> |
| 36 | + </svg> |
| 37 | + ), |
| 38 | + title: "Visualize Anything", |
| 39 | + description: |
| 40 | + "Ask for algorithm visualizations, 3D animations, diagrams, or any interactive visual explanation.", |
| 41 | + }, |
| 42 | +]; |
| 43 | + |
| 44 | +function ExplainerCards() { |
| 45 | + return ( |
| 46 | + <div className="explainer-cards"> |
| 47 | + {cards.map((card) => ( |
| 48 | + <div key={card.title} className="explainer-card"> |
| 49 | + <div style={{ display: "flex", alignItems: "center", gap: "8px" }}> |
| 50 | + <div className="explainer-card-icon"> |
| 51 | + {card.icon} |
| 52 | + </div> |
| 53 | + <span |
| 54 | + style={{ |
| 55 | + fontSize: "13px", |
| 56 | + fontWeight: 600, |
| 57 | + color: "var(--text-primary)", |
| 58 | + }} |
| 59 | + > |
| 60 | + {card.title} |
| 61 | + </span> |
| 62 | + </div> |
| 63 | + <p |
| 64 | + style={{ |
| 65 | + fontSize: "12px", |
| 66 | + lineHeight: 1.5, |
| 67 | + color: "var(--text-secondary)", |
| 68 | + margin: 0, |
| 69 | + }} |
| 70 | + > |
| 71 | + {card.description} |
| 72 | + </p> |
| 73 | + </div> |
| 74 | + ))} |
| 75 | + </div> |
| 76 | + ); |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * Portal that injects ExplainerCards into the CopilotKit welcome screen. |
| 81 | + * Inserts a wrapper div inside the welcome screen's main content area, |
| 82 | + * positioned before the suggestion pills. Auto-removes when the welcome |
| 83 | + * screen disappears (user sends a message). |
| 84 | + */ |
| 85 | +export function ExplainerCardsPortal() { |
| 86 | + const [portalTarget, setPortalTarget] = useState<HTMLElement | null>(null); |
| 87 | + |
| 88 | + useEffect(() => { |
| 89 | + const WELCOME_SELECTOR = '[data-testid="copilot-welcome-screen"]'; |
| 90 | + const PORTAL_ID = "explainer-cards-portal"; |
| 91 | + |
| 92 | + const tryAttach = () => { |
| 93 | + const welcomeScreen = document.querySelector<HTMLElement>(WELCOME_SELECTOR); |
| 94 | + if (!welcomeScreen) { |
| 95 | + setPortalTarget(null); |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + // Reuse existing portal container if present |
| 100 | + let portal = document.getElementById(PORTAL_ID); |
| 101 | + if (portal) { |
| 102 | + setPortalTarget(portal); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + // Insert portal container inside the welcome screen's main content div, |
| 107 | + // before the suggestions row |
| 108 | + const mainContent = welcomeScreen.children[0] as HTMLElement | undefined; |
| 109 | + if (!mainContent) return; |
| 110 | + |
| 111 | + portal = document.createElement("div"); |
| 112 | + portal.id = PORTAL_ID; |
| 113 | + portal.style.width = "100%"; |
| 114 | + |
| 115 | + // Insert before the last child (suggestions row) |
| 116 | + const suggestionsRow = mainContent.lastElementChild; |
| 117 | + if (suggestionsRow) { |
| 118 | + mainContent.insertBefore(portal, suggestionsRow); |
| 119 | + } else { |
| 120 | + mainContent.appendChild(portal); |
| 121 | + } |
| 122 | + |
| 123 | + setPortalTarget(portal); |
| 124 | + }; |
| 125 | + |
| 126 | + tryAttach(); |
| 127 | + |
| 128 | + const observer = new MutationObserver(() => { |
| 129 | + const welcomeScreen = document.querySelector<HTMLElement>(WELCOME_SELECTOR); |
| 130 | + if (!welcomeScreen) { |
| 131 | + // Welcome screen removed (chat started) — clean up |
| 132 | + const stale = document.getElementById(PORTAL_ID); |
| 133 | + if (stale) stale.remove(); |
| 134 | + setPortalTarget(null); |
| 135 | + } else if (!document.getElementById(PORTAL_ID)) { |
| 136 | + // Welcome screen appeared but no portal yet |
| 137 | + tryAttach(); |
| 138 | + } |
| 139 | + }); |
| 140 | + |
| 141 | + observer.observe(document.body, { childList: true, subtree: true }); |
| 142 | + return () => observer.disconnect(); |
| 143 | + }, []); |
| 144 | + |
| 145 | + if (!portalTarget) return null; |
| 146 | + |
| 147 | + return createPortal(<ExplainerCards />, portalTarget); |
| 148 | +} |
0 commit comments