context.tsx 2.0 KB

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