Repository navigation
feat(chatbot): redesign chat input into pill layout and refine UI spacing #1239
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,15 +5,15 @@ import { marked } from 'marked'; | |
| import React, { useEffect, useRef, useState } from 'react'; | ||
| import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'; | ||
| import { oneLight } from 'react-syntax-highlighter/dist/esm/styles/prism'; | ||
| import { Button, Container, Form, GridRow, Header, Icon } from 'semantic-ui-react'; | ||
| import { Container, Header, Icon } from 'semantic-ui-react'; | ||
| import { Grid } from 'semantic-ui-react'; | ||
|
|
||
| import { useEnvironment } from '../../hooks'; | ||
| import { Document } from '../../types'; | ||
|
|
||
| /** Render chatbot markdown safely for dangerouslySetInnerHTML. */ | ||
| export function renderChatMarkdown(markdown: string): string { | ||
| const parsed = marked.parse(markdown, { async: false }); | ||
| export function renderChatMarkdown(markdown: string, options?: { breaks?: boolean }): string { | ||
| const parsed = marked.parse(markdown, { async: false, breaks: options?.breaks ?? false }); | ||
| return DOMPurify.sanitize(String(parsed), { USE_PROFILES: { html: true } }); | ||
| } | ||
|
|
||
|
|
@@ -40,6 +40,40 @@ export const Chatbot = () => { | |
| const [chat, setChat] = useState<ChatState>(DEFAULT_CHAT_STATE); | ||
| const [user, setUser] = useState(''); | ||
| const [modelName, setModelName] = useState<string>(''); | ||
| const textareaRef = useRef<HTMLTextAreaElement | null>(null); | ||
|
|
||
| function growTextarea() { | ||
| const el = textareaRef.current; | ||
| if (!el) return; | ||
| const container = messagesContainerRef.current; | ||
| const wasNearBottom = container | ||
| ? container.scrollHeight - container.scrollTop - container.clientHeight < 64 | ||
| : false; | ||
|
|
||
| el.style.height = 'auto'; | ||
| const newHeight = Math.min(el.scrollHeight, 200); | ||
| el.style.height = `${newHeight}px`; | ||
|
|
||
| const inputContainer = el.closest('.chat-input') as HTMLElement | null; | ||
| if (inputContainer) { | ||
| const surface = inputContainer.closest('.chat-surface') as HTMLElement | null; | ||
| if (surface) { | ||
| surface.style.setProperty('--composer-height', `${inputContainer.offsetHeight}px`); | ||
| } | ||
| } | ||
|
|
||
| if (container) { | ||
| if (wasNearBottom) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep the transcript still when the composer does not resize. If a reader scrolls 40px up and adds a character to a draft that still fits on one line,
|
||
| container.scrollTop = container.scrollHeight; | ||
| setShowScrollToBottom(false); | ||
|
Comment on lines
+67
to
+68
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use instant scrolling to compensate for composer growth. If a long paste expands the composer while the transcript is at the bottom, assigning
|
||
| } else { | ||
| const threshold = 64; | ||
| const isNearBottom = | ||
| container.scrollHeight - container.scrollTop - container.clientHeight < threshold; | ||
| setShowScrollToBottom(!isNearBottom); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function getModelDisplayName(modelName: string): string { | ||
| if (!modelName) { | ||
|
|
@@ -99,7 +133,6 @@ export const Chatbot = () => { | |
| .catch((error) => { | ||
| console.error('Error checking if user is logged in:', error); | ||
| setError(error instanceof Error ? error.message : 'Network error checking login status'); | ||
| setLoading(false); | ||
| }); | ||
| } | ||
|
|
||
|
|
@@ -109,7 +142,7 @@ export const Chatbot = () => { | |
| } | ||
| }, []); | ||
|
|
||
| function processResponse(response: string) { | ||
| function processResponse(response: string, isUser = false) { | ||
| const responses = response.split('```'); | ||
| const res: React.ReactElement[] = []; | ||
|
|
||
|
|
@@ -119,7 +152,7 @@ export const Chatbot = () => { | |
| <p | ||
| key={i} | ||
| dangerouslySetInnerHTML={{ | ||
| __html: renderChatMarkdown(txt), | ||
| __html: renderChatMarkdown(txt, { breaks: isUser }), | ||
| }} | ||
| /> | ||
| ); | ||
|
|
@@ -136,12 +169,23 @@ export const Chatbot = () => { | |
| } | ||
|
|
||
| async function onSubmit() { | ||
| if (!chat.term.trim()) return; | ||
| if (loading || !chat.term.trim()) return; | ||
| shouldForceScrollRef.current = true; | ||
|
|
||
| const currentTerm = chat.term; | ||
| setChat({ ...chat, term: '' }); | ||
| setLoading(true); | ||
| if (textareaRef.current) { | ||
| textareaRef.current.style.height = 'auto'; | ||
| const inputContainer = textareaRef.current.closest('.chat-input') as HTMLElement | null; | ||
| if (inputContainer) { | ||
| const surface = inputContainer.closest('.chat-surface') as HTMLElement | null; | ||
| if (surface) { | ||
| surface.style.setProperty('--composer-height', `${inputContainer.offsetHeight}px`); | ||
| } | ||
| } | ||
| textareaRef.current.focus(); | ||
| } | ||
|
|
||
| setChatMessages((prev) => [ | ||
| ...prev, | ||
|
|
@@ -253,7 +297,7 @@ export const Chatbot = () => { | |
| <span className="message-timestamp">{m.timestamp}</span> | ||
| </div> | ||
|
|
||
| <div className="message-body">{processResponse(m.message)}</div> | ||
| <div className="message-body">{processResponse(m.message, m.role === 'user')}</div> | ||
|
|
||
| {m.data && m.data.length > 0 && ( | ||
| <div className="references"> | ||
|
|
@@ -300,17 +344,48 @@ export const Chatbot = () => { | |
| <Icon name="arrow down" className="scroll-icon" /> | ||
| </button> | ||
| )} | ||
| <Form className="chat-input" size="large" onSubmit={onSubmit}> | ||
| <Form.Input | ||
| fluid | ||
| <div className="chat-input"> | ||
| <textarea | ||
| ref={textareaRef} | ||
| className="chat-textarea" | ||
| rows={1} | ||
| value={chat.term} | ||
| onChange={(e) => setChat({ ...chat, term: e.target.value })} | ||
| placeholder="Type your infosec question here…" | ||
| placeholder="Ask anything about infosec standards…" | ||
| onChange={(e) => { | ||
| setChat({ ...chat, term: e.target.value }); | ||
| growTextarea(); | ||
| }} | ||
| onKeyDown={(e) => { | ||
| if (e.nativeEvent.isComposing || e.keyCode === 229) { | ||
| return; | ||
| } | ||
| if (e.key === 'Enter' && !e.shiftKey) { | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| e.preventDefault(); | ||
| if (!loading) { | ||
| onSubmit(); | ||
| } | ||
| } | ||
| }} | ||
| /> | ||
| <Button primary fluid size="small"> | ||
| <Icon name="send" /> Ask | ||
| </Button> | ||
| </Form> | ||
| <button | ||
| className="chat-send-btn" | ||
| onClick={onSubmit} | ||
| disabled={loading || !chat.term.trim()} | ||
| aria-label="Send message" | ||
| > | ||
| <svg | ||
| viewBox="0 0 24 24" | ||
| fill="none" | ||
| stroke="currentColor" | ||
| strokeWidth="2.2" | ||
| strokeLinecap="round" | ||
| strokeLinejoin="round" | ||
| > | ||
| <line x1="22" y1="2" x2="11" y2="13" /> | ||
| <polygon points="22 2 15 22 11 13 2 9 22 2" /> | ||
| </svg> | ||
| </button> | ||
| </div> | ||
| </div> | ||
| </div> | ||
|
|
||
|
|
||
Large diffs are not rendered by default.
Uh oh!
There was an error while loading. Please reload this page.