| 
									
										
										
										
											2024-06-14 08:42:41 +08:00
										 |  |  | 'use client' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import type { RefObject } from 'react' | 
					
						
							|  |  |  | import { createContext, useContext } from 'use-context-selector' | 
					
						
							|  |  |  | import type { | 
					
						
							|  |  |  |   ChatConfig, | 
					
						
							|  |  |  |   ChatItem, | 
					
						
							|  |  |  |   Feedback, | 
					
						
							|  |  |  | } from '../types' | 
					
						
							| 
									
										
										
										
											2024-06-26 04:51:00 -05:00
										 |  |  | import type { ThemeBuilder } from './theme/theme-context' | 
					
						
							| 
									
										
										
										
											2024-06-14 08:42:41 +08:00
										 |  |  | import type { | 
					
						
							|  |  |  |   AppConversationData, | 
					
						
							|  |  |  |   AppData, | 
					
						
							|  |  |  |   AppMeta, | 
					
						
							|  |  |  |   ConversationItem, | 
					
						
							|  |  |  | } from '@/models/share' | 
					
						
							| 
									
										
										
										
											2025-04-06 17:56:08 +08:00
										 |  |  | import { noop } from 'lodash-es' | 
					
						
							| 
									
										
										
										
											2024-06-14 08:42:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-03 14:44:51 +08:00
										 |  |  | export type EmbeddedChatbotContextValue = { | 
					
						
							| 
									
										
										
										
											2025-05-20 12:07:50 +08:00
										 |  |  |   userCanAccess?: boolean | 
					
						
							| 
									
										
										
										
											2024-06-14 08:42:41 +08:00
										 |  |  |   appInfoError?: any | 
					
						
							|  |  |  |   appInfoLoading?: boolean | 
					
						
							|  |  |  |   appMeta?: AppMeta | 
					
						
							|  |  |  |   appData?: AppData | 
					
						
							|  |  |  |   appParams?: ChatConfig | 
					
						
							|  |  |  |   appChatListDataLoading?: boolean | 
					
						
							|  |  |  |   currentConversationId: string | 
					
						
							|  |  |  |   currentConversationItem?: ConversationItem | 
					
						
							|  |  |  |   appPrevChatList: ChatItem[] | 
					
						
							|  |  |  |   pinnedConversationList: AppConversationData['data'] | 
					
						
							|  |  |  |   conversationList: AppConversationData['data'] | 
					
						
							|  |  |  |   newConversationInputs: Record<string, any> | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |   newConversationInputsRef: RefObject<Record<string, any>> | 
					
						
							| 
									
										
										
										
											2024-06-14 08:42:41 +08:00
										 |  |  |   handleNewConversationInputsChange: (v: Record<string, any>) => void | 
					
						
							|  |  |  |   inputsForms: any[] | 
					
						
							|  |  |  |   handleNewConversation: () => void | 
					
						
							| 
									
										
										
										
											2025-03-03 14:44:51 +08:00
										 |  |  |   handleStartChat: (callback?: any) => void | 
					
						
							| 
									
										
										
										
											2024-06-14 08:42:41 +08:00
										 |  |  |   handleChangeConversation: (conversationId: string) => void | 
					
						
							|  |  |  |   handleNewConversationCompleted: (newConversationId: string) => void | 
					
						
							|  |  |  |   chatShouldReloadKey: string | 
					
						
							|  |  |  |   isMobile: boolean | 
					
						
							|  |  |  |   isInstalledApp: boolean | 
					
						
							| 
									
										
										
										
											2025-04-23 22:57:42 +08:00
										 |  |  |   allowResetChat: boolean | 
					
						
							| 
									
										
										
										
											2024-06-14 08:42:41 +08:00
										 |  |  |   appId?: string | 
					
						
							|  |  |  |   handleFeedback: (messageId: string, feedback: Feedback) => void | 
					
						
							|  |  |  |   currentChatInstanceRef: RefObject<{ handleStop: () => void }> | 
					
						
							| 
									
										
										
										
											2024-06-26 04:51:00 -05:00
										 |  |  |   themeBuilder?: ThemeBuilder | 
					
						
							| 
									
										
										
										
											2025-03-13 14:23:41 +08:00
										 |  |  |   clearChatList?: boolean | 
					
						
							|  |  |  |   setClearChatList: (state: boolean) => void | 
					
						
							|  |  |  |   isResponding?: boolean | 
					
						
							|  |  |  |   setIsResponding: (state: boolean) => void, | 
					
						
							| 
									
										
										
										
											2025-03-27 11:58:16 +08:00
										 |  |  |   currentConversationInputs: Record<string, any> | null, | 
					
						
							|  |  |  |   setCurrentConversationInputs: (v: Record<string, any>) => void, | 
					
						
							| 
									
										
										
										
											2025-05-21 13:52:21 +08:00
										 |  |  |   allInputsHidden: boolean | 
					
						
							| 
									
										
										
										
											2025-06-18 14:27:02 +08:00
										 |  |  |   initUserVariables?: { | 
					
						
							|  |  |  |     name?: string | 
					
						
							|  |  |  |     avatar_url?: string | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2024-06-14 08:42:41 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const EmbeddedChatbotContext = createContext<EmbeddedChatbotContextValue>({ | 
					
						
							| 
									
										
										
										
											2025-05-20 12:07:50 +08:00
										 |  |  |   userCanAccess: false, | 
					
						
							| 
									
										
										
										
											2024-06-14 08:42:41 +08:00
										 |  |  |   currentConversationId: '', | 
					
						
							|  |  |  |   appPrevChatList: [], | 
					
						
							|  |  |  |   pinnedConversationList: [], | 
					
						
							|  |  |  |   conversationList: [], | 
					
						
							|  |  |  |   newConversationInputs: {}, | 
					
						
							| 
									
										
										
										
											2024-10-21 10:32:37 +08:00
										 |  |  |   newConversationInputsRef: { current: {} }, | 
					
						
							| 
									
										
										
										
											2025-04-06 17:56:08 +08:00
										 |  |  |   handleNewConversationInputsChange: noop, | 
					
						
							| 
									
										
										
										
											2024-06-14 08:42:41 +08:00
										 |  |  |   inputsForms: [], | 
					
						
							| 
									
										
										
										
											2025-04-06 17:56:08 +08:00
										 |  |  |   handleNewConversation: noop, | 
					
						
							|  |  |  |   handleStartChat: noop, | 
					
						
							|  |  |  |   handleChangeConversation: noop, | 
					
						
							|  |  |  |   handleNewConversationCompleted: noop, | 
					
						
							| 
									
										
										
										
											2024-06-14 08:42:41 +08:00
										 |  |  |   chatShouldReloadKey: '', | 
					
						
							|  |  |  |   isMobile: false, | 
					
						
							|  |  |  |   isInstalledApp: false, | 
					
						
							| 
									
										
										
										
											2025-04-23 22:57:42 +08:00
										 |  |  |   allowResetChat: true, | 
					
						
							| 
									
										
										
										
											2025-04-06 17:56:08 +08:00
										 |  |  |   handleFeedback: noop, | 
					
						
							|  |  |  |   currentChatInstanceRef: { current: { handleStop: noop } }, | 
					
						
							| 
									
										
										
										
											2025-03-13 14:23:41 +08:00
										 |  |  |   clearChatList: false, | 
					
						
							| 
									
										
										
										
											2025-04-06 17:56:08 +08:00
										 |  |  |   setClearChatList: noop, | 
					
						
							| 
									
										
										
										
											2025-03-13 14:23:41 +08:00
										 |  |  |   isResponding: false, | 
					
						
							| 
									
										
										
										
											2025-04-06 17:56:08 +08:00
										 |  |  |   setIsResponding: noop, | 
					
						
							| 
									
										
										
										
											2025-03-27 11:58:16 +08:00
										 |  |  |   currentConversationInputs: {}, | 
					
						
							| 
									
										
										
										
											2025-04-06 17:56:08 +08:00
										 |  |  |   setCurrentConversationInputs: noop, | 
					
						
							| 
									
										
										
										
											2025-05-21 13:52:21 +08:00
										 |  |  |   allInputsHidden: false, | 
					
						
							| 
									
										
										
										
											2025-06-18 14:27:02 +08:00
										 |  |  |   initUserVariables: {}, | 
					
						
							| 
									
										
										
										
											2024-06-14 08:42:41 +08:00
										 |  |  | }) | 
					
						
							|  |  |  | export const useEmbeddedChatbotContext = () => useContext(EmbeddedChatbotContext) |