1
- import React , { useState , useRef , useEffect } from 'react'
1
+ import React , { useState , useRef , useEffect , useMemo } from 'react'
2
2
import { makeStyles } from '@material-ui/core'
3
3
import SendRoundedIcon from '@material-ui/icons/SendRounded' ;
4
4
import IconButton from "@material-ui/core/IconButton" ;
@@ -14,12 +14,13 @@ import { useBuffer } from './useBuffer'
14
14
import { useCaret } from './useCaret'
15
15
import { theme } from "@postgres.ai/shared/styles/theme" ;
16
16
import { isMobileDevice } from "../../../utils/utils" ;
17
+ import { useAiBot } from "../hooks" ;
18
+ import { ReadyState } from "react-use-websocket" ;
17
19
18
20
19
21
type Props = {
20
- sendDisabled : boolean
21
- onSend : ( value : string ) => void
22
22
threadId ?: string
23
+ orgId : number | null
23
24
}
24
25
25
26
@@ -74,10 +75,22 @@ const useStyles = makeStyles((theme) => (
74
75
)
75
76
76
77
export const Command = React . memo ( ( props : Props ) => {
77
- const { sendDisabled , onSend , threadId } = props
78
+ const { threadId , orgId } = props
78
79
79
80
const classes = useStyles ( )
80
81
const isMobile = isMobileDevice ( ) ;
82
+
83
+ const {
84
+ error,
85
+ wsReadyState,
86
+ wsLoading,
87
+ loading,
88
+ sendMessage,
89
+ chatVisibility
90
+ } = useAiBot ( ) ;
91
+
92
+ const sendDisabled = error !== null || loading || wsLoading || wsReadyState !== ReadyState . OPEN ;
93
+
81
94
// Handle value.
82
95
const [ value , setValue ] = useState ( '' )
83
96
@@ -90,10 +103,19 @@ export const Command = React.memo((props: Props) => {
90
103
// Input caret.
91
104
const caret = useCaret ( inputRef )
92
105
93
- const triggerSend = ( ) => {
106
+ const onSend = async ( message : string ) => {
107
+ await sendMessage ( {
108
+ content : message ,
109
+ thread_id : threadId || null ,
110
+ org_id : orgId ,
111
+ is_public : chatVisibility === 'public'
112
+ } )
113
+ }
114
+
115
+ const triggerSend = async ( ) => {
94
116
if ( ! value . trim ( ) . length || sendDisabled ) return
95
117
96
- onSend ( value )
118
+ await onSend ( value )
97
119
buffer . addNew ( )
98
120
setValue ( buffer . getCurrent ( ) )
99
121
}
@@ -122,13 +144,13 @@ export const Command = React.memo((props: Props) => {
122
144
}
123
145
}
124
146
125
- const handleKeyDown = ( e : React . KeyboardEvent ) => {
147
+ const handleKeyDown = async ( e : React . KeyboardEvent ) => {
126
148
if ( ! inputRef . current ) return
127
149
128
150
// Trigger to send.
129
151
if ( checkIsSendCmd ( e . nativeEvent ) ) {
130
152
e . preventDefault ( )
131
- triggerSend ( )
153
+ await triggerSend ( )
132
154
return
133
155
}
134
156
@@ -171,7 +193,6 @@ export const Command = React.memo((props: Props) => {
171
193
setValue ( '' )
172
194
} , [ threadId ] ) ;
173
195
174
-
175
196
return (
176
197
< div className = { classes . root } >
177
198
< TextField
0 commit comments