|
| 1 | +import { useEffect, useRef, useState, useMemo, useCallback } from "react"; |
| 2 | + |
| 3 | +/* ───────────────────────────────────────────── |
| 4 | + Searchable doc index |
| 5 | + Each entry maps to a section on the DocsPage |
| 6 | + ───────────────────────────────────────────── */ |
| 7 | +export interface SearchEntry { |
| 8 | + id: string; // id of the section element (used for scroll-to) |
| 9 | + title: string; |
| 10 | + description: string; |
| 11 | + category: string; // sidebar group / badge label |
| 12 | + keywords: string[]; // extra tokens for fuzzy matching |
| 13 | +} |
| 14 | + |
| 15 | +const DOC_SEARCH_INDEX: SearchEntry[] = [ |
| 16 | + { |
| 17 | + id: "getting-started", |
| 18 | + title: "Getting Started", |
| 19 | + description: "Quick start guide — API key setup, first prompt, and generating your first diagram.", |
| 20 | + category: "Guide", |
| 21 | + keywords: ["quick start", "begin", "setup", "introduction", "onboarding", "hello"], |
| 22 | + }, |
| 23 | + { |
| 24 | + id: "api-key", |
| 25 | + title: "API Key Setup", |
| 26 | + description: "How to obtain and configure your free Gemini API key for diagram generation.", |
| 27 | + category: "Configuration", |
| 28 | + keywords: ["gemini", "google", "key", "auth", "settings", "token", "credential"], |
| 29 | + }, |
| 30 | + { |
| 31 | + id: "creating-diagrams", |
| 32 | + title: "Creating Diagrams", |
| 33 | + description: "Natural-language prompts, iterative refinement, and conversation examples with Archie.", |
| 34 | + category: "Usage", |
| 35 | + keywords: ["prompt", "generate", "archie", "ai", "conversation", "refine", "chat"], |
| 36 | + }, |
| 37 | + { |
| 38 | + id: "diagram-types", |
| 39 | + title: "Supported Diagram Types", |
| 40 | + description: "All 15 Mermaid diagram types — flowcharts, sequence, ER, Gantt, mindmaps, and more.", |
| 41 | + category: "Reference", |
| 42 | + keywords: [ |
| 43 | + "mermaid", "flowchart", "sequence", "class", "state", "er", "gantt", |
| 44 | + "pie", "git", "mindmap", "timeline", "quadrant", "sankey", "block", "xy", |
| 45 | + ], |
| 46 | + }, |
| 47 | + { |
| 48 | + id: "chart-js", |
| 49 | + title: "Chart.js Support", |
| 50 | + description: "Advanced charts with logarithmic scales, scatter plots, annotations, and custom DSL syntax.", |
| 51 | + category: "Reference", |
| 52 | + keywords: ["chartjs", "chart.js", "scatter", "log", "annotation", "dsl", "dataset", "line chart", "bar chart"], |
| 53 | + }, |
| 54 | + { |
| 55 | + id: "image-input", |
| 56 | + title: "Image Input", |
| 57 | + description: "Upload or paste images — Archie can recreate whiteboard sketches as clean diagrams.", |
| 58 | + category: "Usage", |
| 59 | + keywords: ["upload", "paste", "screenshot", "whiteboard", "image", "photo", "attachment", "clipboard"], |
| 60 | + }, |
| 61 | + { |
| 62 | + id: "code-editing", |
| 63 | + title: "Code Editing", |
| 64 | + description: "View and edit the raw Mermaid or Chart.js code directly in the diagram toolbar.", |
| 65 | + category: "Usage", |
| 66 | + keywords: ["code", "editor", "raw", "mermaid", "syntax", "manual"], |
| 67 | + }, |
| 68 | + { |
| 69 | + id: "export-share", |
| 70 | + title: "Export & Share", |
| 71 | + description: "Export as SVG, PNG, or raw code. Generate shareable links — no sign-up required.", |
| 72 | + category: "Usage", |
| 73 | + keywords: ["svg", "png", "export", "download", "share", "link", "shareable"], |
| 74 | + }, |
| 75 | + { |
| 76 | + id: "history", |
| 77 | + title: "History & Undo", |
| 78 | + description: "Browse, restore, and compare previous diagram versions with auto-save.", |
| 79 | + category: "Usage", |
| 80 | + keywords: ["undo", "redo", "history", "version", "restore", "autosave", "ctrl+z"], |
| 81 | + }, |
| 82 | + { |
| 83 | + id: "keyboard-shortcuts", |
| 84 | + title: "Keyboard Shortcuts", |
| 85 | + description: "All keyboard shortcuts — undo, redo, zoom, fullscreen, and more.", |
| 86 | + category: "Reference", |
| 87 | + keywords: ["shortcut", "hotkey", "keybinding", "ctrl", "keyboard", "accessibility"], |
| 88 | + }, |
| 89 | + { |
| 90 | + id: "mcp-server", |
| 91 | + title: "MCP Server", |
| 92 | + description: "Model Context Protocol server for ChatGPT, Claude, and AI app integrations. Server details, tools, and setup guide.", |
| 93 | + category: "Integration", |
| 94 | + keywords: ["mcp", "model context protocol", "chatgpt", "claude", "plugin", "api", "sse", "tool", "integration", "cursor", "copilot"], |
| 95 | + }, |
| 96 | + { |
| 97 | + id: "privacy", |
| 98 | + title: "Privacy & Security", |
| 99 | + description: "How Diagflo handles your API key, data storage, shared diagrams, and security headers.", |
| 100 | + category: "Security", |
| 101 | + keywords: ["privacy", "security", "localstorage", "headers", "neon", "data", "cors"], |
| 102 | + }, |
| 103 | +]; |
| 104 | + |
| 105 | +/* ───────────── Fuzzy search helper ───────────── */ |
| 106 | +function fuzzyMatch(text: string, query: string): boolean { |
| 107 | + const lower = text.toLowerCase(); |
| 108 | + const q = query.toLowerCase(); |
| 109 | + // Simple substring + token match — fast enough for a small index |
| 110 | + if (lower.includes(q)) return true; |
| 111 | + // Check if all query tokens appear anywhere |
| 112 | + const tokens = q.split(/\s+/).filter(Boolean); |
| 113 | + return tokens.every((t) => lower.includes(t)); |
| 114 | +} |
| 115 | + |
| 116 | +function scoreResult(entry: SearchEntry, query: string): number { |
| 117 | + const q = query.toLowerCase(); |
| 118 | + let score = 0; |
| 119 | + if (entry.title.toLowerCase().startsWith(q)) score += 100; |
| 120 | + if (entry.title.toLowerCase().includes(q)) score += 50; |
| 121 | + if (entry.description.toLowerCase().includes(q)) score += 20; |
| 122 | + if (entry.keywords.some((k) => k.includes(q))) score += 10; |
| 123 | + return score; |
| 124 | +} |
| 125 | + |
| 126 | +/* ───────────── Category badge colors ───────────── */ |
| 127 | +const CATEGORY_COLORS: Record<string, { bg: string; text: string; darkBg: string; darkText: string }> = { |
| 128 | + Guide: { bg: "bg-blue-50", text: "text-blue-600", darkBg: "bg-blue-500/10", darkText: "text-blue-400" }, |
| 129 | + Configuration: { bg: "bg-purple-50", text: "text-purple-600", darkBg: "bg-purple-500/10", darkText: "text-purple-400" }, |
| 130 | + Usage: { bg: "bg-emerald-50", text: "text-emerald-600", darkBg: "bg-emerald-500/10", darkText: "text-emerald-400" }, |
| 131 | + Reference: { bg: "bg-amber-50", text: "text-amber-600", darkBg: "bg-amber-500/10", darkText: "text-amber-400" }, |
| 132 | + Integration: { bg: "bg-cyan-50", text: "text-cyan-600", darkBg: "bg-cyan-500/10", darkText: "text-cyan-400" }, |
| 133 | + Security: { bg: "bg-red-50", text: "text-red-600", darkBg: "bg-red-500/10", darkText: "text-red-400" }, |
| 134 | +}; |
| 135 | + |
| 136 | +/* ─══════════════════════════════════════════════════ |
| 137 | + SuperSearch Component |
| 138 | + ══════════════════════════════════════════════════ */ |
| 139 | + |
| 140 | +interface SuperSearchProps { |
| 141 | + isOpen: boolean; |
| 142 | + onClose: () => void; |
| 143 | +} |
| 144 | + |
| 145 | +export function SuperSearch({ isOpen, onClose }: SuperSearchProps) { |
| 146 | + const [query, setQuery] = useState(""); |
| 147 | + const [selected, setSelected] = useState(0); |
| 148 | + const inputRef = useRef<HTMLInputElement>(null); |
| 149 | + const listRef = useRef<HTMLUListElement>(null); |
| 150 | + const isDark = document.documentElement.classList.contains("dark"); |
| 151 | + |
| 152 | + /* Filtered & ranked results */ |
| 153 | + const results = useMemo(() => { |
| 154 | + if (query.trim().length === 0) return DOC_SEARCH_INDEX; |
| 155 | + const q = query.trim(); |
| 156 | + return DOC_SEARCH_INDEX |
| 157 | + .filter((entry) => { |
| 158 | + const haystack = [entry.title, entry.description, entry.category, ...entry.keywords].join(" "); |
| 159 | + return fuzzyMatch(haystack, q); |
| 160 | + }) |
| 161 | + .sort((a, b) => scoreResult(b, q) - scoreResult(a, q)); |
| 162 | + }, [query]); |
| 163 | + |
| 164 | + /* Reset on open */ |
| 165 | + useEffect(() => { |
| 166 | + if (isOpen) { |
| 167 | + setQuery(""); |
| 168 | + setSelected(0); |
| 169 | + setTimeout(() => inputRef.current?.focus(), 60); |
| 170 | + } |
| 171 | + }, [isOpen]); |
| 172 | + |
| 173 | + /* Keep selected in view */ |
| 174 | + useEffect(() => { |
| 175 | + if (!listRef.current) return; |
| 176 | + const active = listRef.current.children[selected] as HTMLElement | undefined; |
| 177 | + active?.scrollIntoView({ block: "nearest" }); |
| 178 | + }, [selected]); |
| 179 | + |
| 180 | + /* Navigate to section */ |
| 181 | + const navigateTo = useCallback( |
| 182 | + (entry: SearchEntry) => { |
| 183 | + const el = document.getElementById(entry.id); |
| 184 | + if (el) { |
| 185 | + el.scrollIntoView({ behavior: "smooth", block: "start" }); |
| 186 | + // Briefly flash the section |
| 187 | + el.classList.add("ring-2", "ring-orange-500/40", "rounded-xl"); |
| 188 | + setTimeout(() => el.classList.remove("ring-2", "ring-orange-500/40", "rounded-xl"), 1500); |
| 189 | + } |
| 190 | + onClose(); |
| 191 | + }, |
| 192 | + [onClose], |
| 193 | + ); |
| 194 | + |
| 195 | + /* Keyboard handling */ |
| 196 | + useEffect(() => { |
| 197 | + function onKeyDown(e: KeyboardEvent) { |
| 198 | + if (!isOpen) return; |
| 199 | + if (e.key === "Escape") { |
| 200 | + e.preventDefault(); |
| 201 | + onClose(); |
| 202 | + } |
| 203 | + if (e.key === "ArrowDown") { |
| 204 | + e.preventDefault(); |
| 205 | + setSelected((s) => Math.min(s + 1, results.length - 1)); |
| 206 | + } |
| 207 | + if (e.key === "ArrowUp") { |
| 208 | + e.preventDefault(); |
| 209 | + setSelected((s) => Math.max(s - 1, 0)); |
| 210 | + } |
| 211 | + if (e.key === "Enter") { |
| 212 | + e.preventDefault(); |
| 213 | + if (results[selected]) navigateTo(results[selected]); |
| 214 | + } |
| 215 | + } |
| 216 | + window.addEventListener("keydown", onKeyDown); |
| 217 | + return () => window.removeEventListener("keydown", onKeyDown); |
| 218 | + }, [isOpen, selected, results, onClose, navigateTo]); |
| 219 | + |
| 220 | + if (!isOpen) return null; |
| 221 | + |
| 222 | + return ( |
| 223 | + <div |
| 224 | + className="fixed inset-0 z-[200] flex items-start justify-center pt-[12vh] bg-black/50 backdrop-blur-md" |
| 225 | + onClick={(e) => { |
| 226 | + if (e.target === e.currentTarget) onClose(); |
| 227 | + }} |
| 228 | + > |
| 229 | + <div |
| 230 | + className={`w-full max-w-xl rounded-2xl shadow-2xl border overflow-hidden transition-all duration-200 animate-in fade-in slide-in-from-top-4 ${isDark |
| 231 | + ? "bg-[#18181b]/95 border-white/10 shadow-black/60" |
| 232 | + : "bg-white/95 border-black/10 shadow-black/20" |
| 233 | + }`} |
| 234 | + style={{ backdropFilter: "blur(24px)" }} |
| 235 | + > |
| 236 | + {/* ── Search input bar ── */} |
| 237 | + <div className={`flex items-center gap-3 px-5 py-4 border-b ${isDark ? "border-white/5" : "border-black/5"}`}> |
| 238 | + {/* Search icon */} |
| 239 | + <svg className={`w-5 h-5 shrink-0 ${isDark ? "text-white/30" : "text-black/30"}`} fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}> |
| 240 | + <circle cx="11" cy="11" r="7" /> |
| 241 | + <line x1="16.5" y1="16.5" x2="21" y2="21" strokeLinecap="round" /> |
| 242 | + </svg> |
| 243 | + <input |
| 244 | + ref={inputRef} |
| 245 | + className={`flex-1 text-[15px] bg-transparent outline-none placeholder:font-medium ${isDark ? "text-white placeholder:text-white/30" : "text-black placeholder:text-black/30" |
| 246 | + }`} |
| 247 | + placeholder="Search documentation…" |
| 248 | + value={query} |
| 249 | + onChange={(e) => { |
| 250 | + setQuery(e.target.value); |
| 251 | + setSelected(0); |
| 252 | + }} |
| 253 | + aria-label="Search documentation" |
| 254 | + autoComplete="off" |
| 255 | + spellCheck={false} |
| 256 | + /> |
| 257 | + {/* Shortcut badge */} |
| 258 | + <kbd |
| 259 | + className={`hidden sm:inline-flex items-center gap-1 px-2 py-0.5 rounded-md text-[11px] font-mono font-medium ${isDark ? "bg-white/5 text-white/25 border border-white/5" : "bg-gray-100 text-gray-400 border border-gray-200" |
| 260 | + }`} |
| 261 | + > |
| 262 | + Esc |
| 263 | + </kbd> |
| 264 | + </div> |
| 265 | + |
| 266 | + {/* ── Results list ── */} |
| 267 | + <ul |
| 268 | + ref={listRef} |
| 269 | + className={`max-h-[50vh] overflow-y-auto overscroll-contain py-2 ${isDark ? "scrollbar-dark" : "" |
| 270 | + }`} |
| 271 | + role="listbox" |
| 272 | + aria-label="Search results" |
| 273 | + > |
| 274 | + {results.length === 0 && ( |
| 275 | + <li className={`px-5 py-8 text-center ${isDark ? "text-white/30" : "text-black/30"}`}> |
| 276 | + <div className="text-3xl mb-2">🔍</div> |
| 277 | + <div className="text-sm font-medium">No results for "{query}"</div> |
| 278 | + <div className="text-xs mt-1 opacity-60">Try a different search term</div> |
| 279 | + </li> |
| 280 | + )} |
| 281 | + {results.map((entry, i) => { |
| 282 | + const isActive = i === selected; |
| 283 | + const cat = CATEGORY_COLORS[entry.category] ?? CATEGORY_COLORS.Usage; |
| 284 | + return ( |
| 285 | + <li |
| 286 | + key={entry.id} |
| 287 | + role="option" |
| 288 | + aria-selected={isActive} |
| 289 | + className={`mx-2 px-4 py-3 rounded-xl cursor-pointer transition-all duration-150 ${isActive |
| 290 | + ? isDark |
| 291 | + ? "bg-white/[0.06]" |
| 292 | + : "bg-black/[0.04]" |
| 293 | + : "hover:bg-white/[0.03]" |
| 294 | + }`} |
| 295 | + onMouseEnter={() => setSelected(i)} |
| 296 | + onClick={() => navigateTo(entry)} |
| 297 | + > |
| 298 | + <div className="flex items-center gap-3"> |
| 299 | + {/* Title + badge */} |
| 300 | + <span className={`font-semibold text-[14px] ${isDark ? "text-white" : "text-black"}`}> |
| 301 | + {highlightMatch(entry.title, query, isDark)} |
| 302 | + </span> |
| 303 | + <span |
| 304 | + className={`px-2 py-0.5 rounded-full text-[10px] font-semibold uppercase tracking-wider ${isDark ? `${cat.darkBg} ${cat.darkText}` : `${cat.bg} ${cat.text}` |
| 305 | + }`} |
| 306 | + > |
| 307 | + {entry.category} |
| 308 | + </span> |
| 309 | + {/* Right-side arrow when active */} |
| 310 | + {isActive && ( |
| 311 | + <svg className={`ml-auto w-4 h-4 ${isDark ? "text-white/20" : "text-black/20"}`} fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}> |
| 312 | + <path strokeLinecap="round" strokeLinejoin="round" d="M9 5l7 7-7 7" /> |
| 313 | + </svg> |
| 314 | + )} |
| 315 | + </div> |
| 316 | + <p className={`text-[13px] leading-relaxed mt-1 ${isDark ? "text-white/40" : "text-black/40"}`}> |
| 317 | + {highlightMatch(entry.description, query, isDark)} |
| 318 | + </p> |
| 319 | + </li> |
| 320 | + ); |
| 321 | + })} |
| 322 | + </ul> |
| 323 | + |
| 324 | + {/* ── Footer ── */} |
| 325 | + <div className={`flex items-center justify-between px-5 py-2.5 border-t text-[11px] font-medium ${isDark ? "border-white/5 text-white/20" : "border-black/5 text-black/20" |
| 326 | + }`}> |
| 327 | + <div className="flex items-center gap-3"> |
| 328 | + <span className="flex items-center gap-1"> |
| 329 | + <kbd className={`px-1.5 py-0.5 rounded text-[10px] font-mono ${isDark ? "bg-white/5" : "bg-gray-100"}`}>↑</kbd> |
| 330 | + <kbd className={`px-1.5 py-0.5 rounded text-[10px] font-mono ${isDark ? "bg-white/5" : "bg-gray-100"}`}>↓</kbd> |
| 331 | + navigate |
| 332 | + </span> |
| 333 | + <span className="flex items-center gap-1"> |
| 334 | + <kbd className={`px-1.5 py-0.5 rounded text-[10px] font-mono ${isDark ? "bg-white/5" : "bg-gray-100"}`}>↵</kbd> |
| 335 | + open |
| 336 | + </span> |
| 337 | + </div> |
| 338 | + <span>{results.length} result{results.length !== 1 ? "s" : ""}</span> |
| 339 | + </div> |
| 340 | + </div> |
| 341 | + </div> |
| 342 | + ); |
| 343 | +} |
| 344 | + |
| 345 | +/* ───────────── Highlight helper ───────────── */ |
| 346 | +function highlightMatch(text: string, query: string, isDark: boolean): React.ReactNode { |
| 347 | + if (!query.trim()) return text; |
| 348 | + const q = query.trim(); |
| 349 | + const idx = text.toLowerCase().indexOf(q.toLowerCase()); |
| 350 | + if (idx === -1) return text; |
| 351 | + return ( |
| 352 | + <> |
| 353 | + {text.slice(0, idx)} |
| 354 | + <mark className={`rounded px-0.5 ${isDark ? "bg-orange-500/20 text-orange-300" : "bg-orange-100 text-orange-700"}`}> |
| 355 | + {text.slice(idx, idx + q.length)} |
| 356 | + </mark> |
| 357 | + {text.slice(idx + q.length)} |
| 358 | + </> |
| 359 | + ); |
| 360 | +} |
0 commit comments