context.tsx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. 'use client'
  2. import type { RefObject } from 'react'
  3. import { createContext, useContext } from 'use-context-selector'
  4. import type {
  5. ChatConfig,
  6. ChatItem,
  7. Feedback,
  8. } from '../types'
  9. import type { ThemeBuilder } from './theme/theme-context'
  10. import type {
  11. AppConversationData,
  12. AppData,
  13. AppMeta,
  14. ConversationItem,
  15. } from '@/models/share'
  16. export type EmbeddedChatbotContextValue = {
  17. appInfoError?: any
  18. appInfoLoading?: boolean
  19. appMeta?: AppMeta
  20. appData?: AppData
  21. appParams?: ChatConfig
  22. appChatListDataLoading?: boolean
  23. currentConversationId: string
  24. currentConversationItem?: ConversationItem
  25. appPrevChatList: ChatItem[]
  26. pinnedConversationList: AppConversationData['data']
  27. conversationList: AppConversationData['data']
  28. showConfigPanelBeforeChat: boolean
  29. newConversationInputs: Record<string, any>
  30. handleNewConversationInputsChange: (v: Record<string, any>) => void
  31. inputsForms: any[]
  32. handleNewConversation: () => void
  33. handleStartChat: () => void
  34. handleChangeConversation: (conversationId: string) => void
  35. handleNewConversationCompleted: (newConversationId: string) => void
  36. chatShouldReloadKey: string
  37. isMobile: boolean
  38. isInstalledApp: boolean
  39. appId?: string
  40. handleFeedback: (messageId: string, feedback: Feedback) => void
  41. currentChatInstanceRef: RefObject<{ handleStop: () => void }>
  42. themeBuilder?: ThemeBuilder
  43. }
  44. export const EmbeddedChatbotContext = createContext<EmbeddedChatbotContextValue>({
  45. currentConversationId: '',
  46. appPrevChatList: [],
  47. pinnedConversationList: [],
  48. conversationList: [],
  49. showConfigPanelBeforeChat: false,
  50. newConversationInputs: {},
  51. handleNewConversationInputsChange: () => {},
  52. inputsForms: [],
  53. handleNewConversation: () => {},
  54. handleStartChat: () => {},
  55. handleChangeConversation: () => {},
  56. handleNewConversationCompleted: () => {},
  57. chatShouldReloadKey: '',
  58. isMobile: false,
  59. isInstalledApp: false,
  60. handleFeedback: () => {},
  61. currentChatInstanceRef: { current: { handleStop: () => {} } },
  62. })
  63. export const useEmbeddedChatbotContext = () => useContext(EmbeddedChatbotContext)