|
| 1 | +import React, {useState, useEffect, useRef, useCallback} from 'react'; |
| 2 | + |
| 3 | +// Quick-start prompts shown before the user types. |
| 4 | +const SUGGESTIONS = [ |
| 5 | + {icon: 'ph-lock-key', q: 'How do I add JWT authentication?'}, |
| 6 | + {icon: 'ph-globe', q: 'How do I enable CORS?'}, |
| 7 | + {icon: 'ph-link', q: 'How do I bind a JSON request body?'}, |
| 8 | + {icon: 'ph-folder-open', q: 'How do I serve static files?'}, |
| 9 | +]; |
| 10 | + |
| 11 | +// Demo answer used by the prototype. To make this real, replace `streamAnswer` |
| 12 | +// with a call to your RAG provider, e.g. kapa.ai: |
| 13 | +// const r = await fetch('https://api.kapa.ai/query/v1/projects/<id>/chat/', |
| 14 | +// {method:'POST', headers:{'X-API-KEY': KEY,'Content-Type':'application/json'}, |
| 15 | +// body: JSON.stringify({query})}); |
| 16 | +// then stream r.body. The UI below is provider-agnostic. |
| 17 | +const DEMO_ANSWER = |
| 18 | +`To add JWT authentication, use Echo's built-in <strong>JWT middleware</strong>. Register it on the routes (or group) you want to protect: |
| 19 | +
|
| 20 | +<pre><code>import echojwt "github.com/labstack/echo-jwt/v4" |
| 21 | +
|
| 22 | +e.Use(echojwt.WithConfig(echojwt.Config{ |
| 23 | + SigningKey: []byte("your-secret"), |
| 24 | +}))</code></pre> |
| 25 | +
|
| 26 | +Requests must then send <code>Authorization: Bearer <token></code>. Inside a handler, read claims via <code>c.Get("user")</code>. For login, issue a token with <strong>github.com/golang-jwt/jwt/v5</strong>.`; |
| 27 | + |
| 28 | +const SOURCES = [ |
| 29 | + 'Guide › Middleware › JWT', |
| 30 | + 'Cookbook › JWT Authentication', |
| 31 | + 'API › echo-jwt', |
| 32 | +]; |
| 33 | + |
| 34 | +export default function AskEcho() { |
| 35 | + const [open, setOpen] = useState(false); |
| 36 | + const [query, setQuery] = useState(''); |
| 37 | + const [answer, setAnswer] = useState(''); |
| 38 | + const [thinking, setThinking] = useState(false); |
| 39 | + const [done, setDone] = useState(false); |
| 40 | + const inputRef = useRef(null); |
| 41 | + const timer = useRef(null); |
| 42 | + |
| 43 | + const reset = () => { |
| 44 | + setQuery(''); setAnswer(''); setThinking(false); setDone(false); |
| 45 | + if (timer.current) clearInterval(timer.current); |
| 46 | + }; |
| 47 | + const close = useCallback(() => { setOpen(false); reset(); }, []); |
| 48 | + const openPalette = useCallback(() => { reset(); setOpen(true); }, []); |
| 49 | + |
| 50 | + useEffect(() => { |
| 51 | + const onKey = (e) => { |
| 52 | + if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === 'k') { |
| 53 | + e.preventDefault(); |
| 54 | + setOpen((o) => { if (o) reset(); return !o; }); |
| 55 | + } |
| 56 | + if (e.key === 'Escape') close(); |
| 57 | + }; |
| 58 | + const onOpen = () => openPalette(); |
| 59 | + window.addEventListener('keydown', onKey); |
| 60 | + window.addEventListener('ask-echo-open', onOpen); |
| 61 | + return () => { |
| 62 | + window.removeEventListener('keydown', onKey); |
| 63 | + window.removeEventListener('ask-echo-open', onOpen); |
| 64 | + if (timer.current) clearInterval(timer.current); |
| 65 | + }; |
| 66 | + }, [close, openPalette]); |
| 67 | + |
| 68 | + useEffect(() => { |
| 69 | + if (open && inputRef.current) setTimeout(() => inputRef.current.focus(), 40); |
| 70 | + }, [open]); |
| 71 | + |
| 72 | + // Typewriter stream of the (demo) answer. |
| 73 | + const ask = (q) => { |
| 74 | + setQuery(q); setThinking(true); setAnswer(''); setDone(false); |
| 75 | + if (timer.current) clearInterval(timer.current); |
| 76 | + setTimeout(() => { |
| 77 | + setThinking(false); |
| 78 | + let i = 0; |
| 79 | + timer.current = setInterval(() => { |
| 80 | + i += 5; |
| 81 | + setAnswer(DEMO_ANSWER.slice(0, i)); |
| 82 | + if (i >= DEMO_ANSWER.length) { |
| 83 | + clearInterval(timer.current); |
| 84 | + setAnswer(DEMO_ANSWER); |
| 85 | + setDone(true); |
| 86 | + } |
| 87 | + }, 12); |
| 88 | + }, 450); |
| 89 | + }; |
| 90 | + |
| 91 | + if (!open) { |
| 92 | + return ( |
| 93 | + <button className="ask-fab" onClick={openPalette} aria-label="Ask Echo"> |
| 94 | + <i className="ph ph-sparkle" /> Ask Echo <kbd>⌘K</kbd> |
| 95 | + </button> |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + return ( |
| 100 | + <div className="ask-overlay" onMouseDown={(e) => { if (e.target === e.currentTarget) close(); }}> |
| 101 | + <div className="ask-palette" role="dialog" aria-label="Ask Echo"> |
| 102 | + <div className="ask-top"> |
| 103 | + <i className="ph ph-sparkle ask-spark" /> |
| 104 | + <input |
| 105 | + ref={inputRef} |
| 106 | + className="ask-input" |
| 107 | + placeholder="Ask Echo a question…" |
| 108 | + value={query} |
| 109 | + onChange={(e) => setQuery(e.target.value)} |
| 110 | + onKeyDown={(e) => { if (e.key === 'Enter' && query.trim()) ask(query.trim()); }} |
| 111 | + autoComplete="off" |
| 112 | + /> |
| 113 | + <span className="ask-esc">ESC</span> |
| 114 | + </div> |
| 115 | + |
| 116 | + <div className="ask-body"> |
| 117 | + {!answer && !thinking && ( |
| 118 | + <div className="ask-suggest"> |
| 119 | + {SUGGESTIONS.map((s) => ( |
| 120 | + <button key={s.q} className="ask-s" onClick={() => ask(s.q)}> |
| 121 | + <i className={`ph ${s.icon}`} /> {s.q} |
| 122 | + <span className="ask-k">↵</span> |
| 123 | + </button> |
| 124 | + ))} |
| 125 | + </div> |
| 126 | + )} |
| 127 | + |
| 128 | + {thinking && ( |
| 129 | + <div className="ask-badge"><span className="ask-dot" /> Ask Echo is thinking…</div> |
| 130 | + )} |
| 131 | + |
| 132 | + {answer && ( |
| 133 | + <> |
| 134 | + <div className="ask-badge"><span className="ask-dot" /> Ask Echo</div> |
| 135 | + <div |
| 136 | + className="ask-answer" |
| 137 | + dangerouslySetInnerHTML={{ __html: answer + (done ? '' : '<span class="ask-cursor"></span>') }} |
| 138 | + /> |
| 139 | + {done && ( |
| 140 | + <div className="ask-sources"> |
| 141 | + <h5>Sources</h5> |
| 142 | + {SOURCES.map((s, i) => ( |
| 143 | + <div className="ask-src" key={s}><span className="ask-n">{i + 1}</span> {s}</div> |
| 144 | + ))} |
| 145 | + </div> |
| 146 | + )} |
| 147 | + </> |
| 148 | + )} |
| 149 | + </div> |
| 150 | + |
| 151 | + <div className="ask-foot"> |
| 152 | + <span><b>↵</b> ask</span><span><b>esc</b> close</span> |
| 153 | + <span style={{marginLeft: 'auto'}}>Powered by <b>Ask Echo</b> · answers in your language</span> |
| 154 | + </div> |
| 155 | + </div> |
| 156 | + </div> |
| 157 | + ); |
| 158 | +} |
0 commit comments