1
- import React , { MouseEventHandler , useEffect , useState } from "react" ;
1
+ import React , { useEffect , useState } from "react" ;
2
2
import { Button , Text , useDisclosure } from "@chakra-ui/react" ;
3
3
import { BeatLoader } from "react-spinners" ;
4
- import { ClickPromptBird } from "./ClickPromptButton " ;
5
- import { ButtonSize , StyledPromptButton } from "./Button.shared " ;
6
- import { LoggingDrawer } from "./LoggingDrawer " ;
4
+ import { StyledPromptButton } from "@/SharedButton " ;
5
+ import { LoggingDrawer } from "@/LoggingDrawer " ;
6
+ import { ClickPromptBird } from "@/ClickPromptBird " ;
7
7
8
8
export type ExecButtonProps = {
9
9
loading ?: boolean ;
10
- onClick ?: MouseEventHandler ;
11
- name : string ;
12
10
text : string ;
13
- size ?: ButtonSize ;
14
11
children ?: React . ReactNode ;
15
12
handleResponse ?: ( response : any ) => void ;
16
13
conversationId ?: number ;
17
14
updateConversationId ?: ( conversationId : number ) => void ;
18
- isLoggedIn : ( ) => Promise < any > ;
19
15
createConversation : ( name ?: string ) => Promise < any > ;
20
16
sendMessage : ( conversageId : number , message : string , name ?: string ) => Promise < any > ;
17
+ isLoggedInApi : ( ) => Promise < any > ;
18
+ changeConversationNameApi : ( conversation_id : number , name : string ) => Promise < any > ;
19
+ createConversationApi : ( name ?: string ) => Promise < any > ;
20
+ getChatsByConversationIdApi : ( conversationId : number ) => Promise < any > ;
21
+ deleteConversationApi : ( conversationId : number ) => Promise < any > ;
22
+ deleteAllConversationsApi : ( ) => Promise < any > ;
23
+ sendMsgWithStreamResApi : ( conversageId : number , message : string , name ?: string ) => Promise < any > ;
24
+ logoutApi : ( ) => Promise < any > ;
21
25
} ;
22
26
23
- function ExecutePromptButton ( props : ExecButtonProps ) {
24
- const [ isLoading , setIsLoading ] = useState ( props . loading ) ;
27
+ export const ExecutePromptButton = ( {
28
+ loading,
29
+ text,
30
+ children,
31
+ handleResponse,
32
+ conversationId,
33
+ updateConversationId,
34
+ createConversation,
35
+ sendMessage,
36
+ isLoggedInApi,
37
+ changeConversationNameApi,
38
+ createConversationApi,
39
+ getChatsByConversationIdApi,
40
+ deleteConversationApi,
41
+ deleteAllConversationsApi,
42
+ sendMsgWithStreamResApi,
43
+ logoutApi,
44
+ } : ExecButtonProps ) => {
45
+ const [ isLoading , setIsLoading ] = useState ( loading ) ;
25
46
const { isOpen, onOpen, onClose } = useDisclosure ( ) ;
26
47
const [ hasLogin , setHasLogin ] = useState ( false ) ;
27
48
28
49
const handleClick = async ( ) => {
29
50
setIsLoading ( true ) ;
30
51
31
52
try {
32
- const isLoggedIn = await props . isLoggedIn ( ) ;
53
+ const isLoggedIn = await isLoggedInApi ( ) ;
33
54
if ( ! isLoggedIn ) {
34
55
onOpen ( ) ;
35
56
setIsLoading ( false ) ;
@@ -40,21 +61,21 @@ function ExecutePromptButton(props: ExecButtonProps) {
40
61
setHasLogin ( false ) ;
41
62
}
42
63
43
- let conversationId = props . conversationId ;
44
- if ( ! props . conversationId ) {
45
- const conversation = await props . createConversation ( ) ;
64
+ let newConversationId = conversationId ;
65
+ if ( ! conversationId ) {
66
+ const conversation = await createConversation ( ) ;
46
67
if ( ! conversation ) {
47
68
return ;
48
69
}
49
70
50
- conversationId = conversation . id as number ;
51
- props . updateConversationId ? props . updateConversationId ( conversationId ) : null ;
71
+ newConversationId = conversation . id as number ;
72
+ updateConversationId ? updateConversationId ( newConversationId ) : null ;
52
73
}
53
74
54
- if ( conversationId ) {
55
- const response : any = await props . sendMessage ( conversationId , props . text ) ;
56
- if ( response && props . handleResponse ) {
57
- props . handleResponse ( response as any ) ;
75
+ if ( newConversationId ) {
76
+ const response : any = await sendMessage ( newConversationId , text ) ;
77
+ if ( response && handleResponse ) {
78
+ handleResponse ( response as any ) ;
58
79
}
59
80
}
60
81
@@ -83,15 +104,27 @@ function ExecutePromptButton(props: ExecButtonProps) {
83
104
< >
84
105
< StyledPromptButton >
85
106
< Button colorScheme = "twitter" className = "bg-blue" onClick = { handleClick } >
86
- { props . children }
107
+ { children }
87
108
{ ! isLoading && < Text > Prompt</ Text > }
88
109
{ isLoading && < BeatLoader size = { 8 } color = "black" /> }
89
110
</ Button >
90
111
< ClickPromptBird />
91
112
</ StyledPromptButton >
92
- { ! hasLogin && LoggingDrawer ( isOpen , handleClose , hasLogin , props , updateLoginStatus ) }
113
+ { ! hasLogin &&
114
+ LoggingDrawer ( {
115
+ isOpen,
116
+ handleClose,
117
+ updateStatus : updateLoginStatus ,
118
+ isLoggedIn : hasLogin ,
119
+ initMessage : text ,
120
+ changeConversationNameApi : changeConversationNameApi ,
121
+ createConversationApi : createConversationApi ,
122
+ getChatsByConversationIdApi : getChatsByConversationIdApi ,
123
+ deleteConversationApi : deleteConversationApi ,
124
+ deleteAllConversationsApi : deleteAllConversationsApi ,
125
+ sendMsgWithStreamResApi : sendMsgWithStreamResApi ,
126
+ logoutApi : logoutApi ,
127
+ } ) }
93
128
</ >
94
129
) ;
95
- }
96
-
97
- export default ExecutePromptButton ;
130
+ } ;
0 commit comments