context.tsx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. newConversationInputs: Record<string, any>
  29. newConversationInputsRef: RefObject<Record<string, any>>
  30. handleNewConversationInputsChange: (v: Record<string, any>) => void
  31. inputsForms: any[]
  32. handleNewConversation: () => void
  33. handleStartChat: (callback?: any) => 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. clearChatList?: boolean
  44. setClearChatList: (state: boolean) => void
  45. isResponding?: boolean
  46. setIsResponding: (state: boolean) => void,
  47. }
  48. export const EmbeddedChatbotContext = createContext<EmbeddedChatbotContextValue>({
  49. currentConversationId: '',
  50. appPrevChatList: [],
  51. pinnedConversationList: [],
  52. conversationList: [],
  53. newConversationInputs: {},
  54. newConversationInputsRef: { current: {} },
  55. handleNewConversationInputsChange: () => {},
  56. inputsForms: [],
  57. handleNewConversation: () => {},
  58. handleStartChat: () => {},
  59. handleChangeConversation: () => {},
  60. handleNewConversationCompleted: () => {},
  61. chatShouldReloadKey: '',
  62. isMobile: false,
  63. isInstalledApp: false,
  64. handleFeedback: () => {},
  65. currentChatInstanceRef: { current: { handleStop: () => {} } },
  66. clearChatList: false,
  67. setClearChatList: () => {},
  68. isResponding: false,
  69. setIsResponding: () => {},
  70. })
  71. export const useEmbeddedChatbotContext = () => useContext(EmbeddedChatbotContext)