1- import React , { MouseEventHandler , useEffect , useState } from "react" ;
1+ import React , { useEffect , useState } from "react" ;
22import { Button , Text , useDisclosure } from "@chakra-ui/react" ;
33import { 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 " ;
77
88export type ExecButtonProps = {
99 loading ?: boolean ;
10- onClick ?: MouseEventHandler ;
11- name : string ;
1210 text : string ;
13- size ?: ButtonSize ;
1411 children ?: React . ReactNode ;
1512 handleResponse ?: ( response : any ) => void ;
1613 conversationId ?: number ;
1714 updateConversationId ?: ( conversationId : number ) => void ;
18- isLoggedIn : ( ) => Promise < any > ;
1915 createConversation : ( name ?: string ) => Promise < any > ;
2016 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 > ;
2125} ;
2226
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 ) ;
2546 const { isOpen, onOpen, onClose } = useDisclosure ( ) ;
2647 const [ hasLogin , setHasLogin ] = useState ( false ) ;
2748
2849 const handleClick = async ( ) => {
2950 setIsLoading ( true ) ;
3051
3152 try {
32- const isLoggedIn = await props . isLoggedIn ( ) ;
53+ const isLoggedIn = await isLoggedInApi ( ) ;
3354 if ( ! isLoggedIn ) {
3455 onOpen ( ) ;
3556 setIsLoading ( false ) ;
@@ -40,21 +61,21 @@ function ExecutePromptButton(props: ExecButtonProps) {
4061 setHasLogin ( false ) ;
4162 }
4263
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 ( ) ;
4667 if ( ! conversation ) {
4768 return ;
4869 }
4970
50- conversationId = conversation . id as number ;
51- props . updateConversationId ? props . updateConversationId ( conversationId ) : null ;
71+ newConversationId = conversation . id as number ;
72+ updateConversationId ? updateConversationId ( newConversationId ) : null ;
5273 }
5374
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 ) ;
5879 }
5980 }
6081
@@ -83,15 +104,27 @@ function ExecutePromptButton(props: ExecButtonProps) {
83104 < >
84105 < StyledPromptButton >
85106 < Button colorScheme = "twitter" className = "bg-blue" onClick = { handleClick } >
86- { props . children }
107+ { children }
87108 { ! isLoading && < Text > Prompt</ Text > }
88109 { isLoading && < BeatLoader size = { 8 } color = "black" /> }
89110 </ Button >
90111 < ClickPromptBird />
91112 </ 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+ } ) }
93128 </ >
94129 ) ;
95- }
96-
97- export default ExecutePromptButton ;
130+ } ;
0 commit comments