Skip to content

Commit 16d9fec

Browse files
tang-jijosStorer
authored andcommitted
Enable user input while response is being processed
- Enable user input while response is being processed - Incorporate Send/Stop button functionality with the 'Enter' key
1 parent a62cf9c commit 16d9fec

File tree

6 files changed

+49
-21
lines changed

6 files changed

+49
-21
lines changed
1.7 KB
Loading
-24.7 KB
Loading
1.61 KB
Loading
1.14 KB
Loading

src/components/ConversationCard/index.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ function ConversationCard(props) {
382382
updateAnswer(e, false, 'error')
383383
}
384384
}}
385+
port={port}
385386
/>
386387
</div>
387388
)

src/components/InputBox/index.jsx

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import PropTypes from 'prop-types'
33
import { updateRefHeight } from '../../utils'
44
import { useTranslation } from 'react-i18next'
55

6-
export function InputBox({ onSubmit, enabled }) {
6+
export function InputBox({ onSubmit, enabled, port }) {
77
const { t } = useTranslation()
88
const [value, setValue] = useState('')
99
const inputRef = useRef(null)
@@ -16,37 +16,64 @@ export function InputBox({ onSubmit, enabled }) {
1616
if (enabled) inputRef.current.focus()
1717
}, [enabled])
1818

19-
const onKeyDown = (e) => {
19+
const handleKeyDownOrClick = (e) => {
2020
e.stopPropagation()
21-
if (e.keyCode === 13 && e.shiftKey === false) {
22-
e.preventDefault()
23-
if (!value) return
24-
onSubmit(value)
25-
setValue('')
21+
if (e.type === 'click' || (e.keyCode === 13 && e.shiftKey === false)) {
22+
if (enabled) {
23+
e.preventDefault()
24+
if (!value) return
25+
onSubmit(value)
26+
setValue('')
27+
} else {
28+
e.preventDefault()
29+
port.postMessage({ stop: true })
30+
}
2631
}
2732
}
2833

2934
return (
30-
<textarea
31-
dir="auto"
32-
ref={inputRef}
33-
disabled={!enabled}
34-
className="interact-input"
35-
placeholder={
36-
enabled
37-
? t('Type your question here\nEnter to send, shift + enter to break line')
38-
: t('Wait for the answer to finish and then continue here')
39-
}
40-
value={value}
41-
onChange={(e) => setValue(e.target.value)}
42-
onKeyDown={onKeyDown}
43-
/>
35+
<div style={{ position: 'relative' }}>
36+
<textarea
37+
dir="auto"
38+
ref={inputRef}
39+
disabled={false}
40+
className="interact-input"
41+
placeholder={
42+
enabled
43+
? t('Type your question here\nEnter to send\nShift + enter to break line')
44+
: t('Type your question here\nEnter to stop generating\nShift + enter to break line')
45+
}
46+
value={value}
47+
onChange={(e) => setValue(e.target.value)}
48+
onKeyDown={handleKeyDownOrClick}
49+
/>
50+
<button
51+
style={{
52+
position: 'absolute',
53+
top: '50%',
54+
right: '8px',
55+
transform: 'translateY(-50%)',
56+
backgroundColor: enabled ? '#30a14e' : '#cf222e',
57+
color: 'white',
58+
border: '1px solid',
59+
borderRadius: '6px',
60+
borderColor: 'rgba(31,35,40,0.15)',
61+
padding: '0.2em 0.8em',
62+
fontSize: '1.2em',
63+
boxShadow: '0 1px 0 rgba(31,35,40,0.1)',
64+
}}
65+
onClick={handleKeyDownOrClick}
66+
>
67+
{enabled ? t('Send') : t('Stop')}
68+
</button>
69+
</div>
4470
)
4571
}
4672

4773
InputBox.propTypes = {
4874
onSubmit: PropTypes.func.isRequired,
4975
enabled: PropTypes.bool,
76+
port: PropTypes.func.isRequired,
5077
}
5178

5279
export default InputBox

0 commit comments

Comments
 (0)