context.tsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. newConversationInputsRef: RefObject<Record<string, any>>
  31. handleNewConversationInputsChange: (v: Record<string, any>) => void
  32. inputsForms: any[]
  33. handleNewConversation: () => void
  34. handleStartChat: () => void
  35. handleChangeConversation: (conversationId: string) => void
  36. handleNewConversationCompleted: (newConversationId: string) => void
  37. chatShouldReloadKey: string
  38. isMobile: boolean
  39. isInstalledApp: boolean
  40. appId?: string
  41. handleFeedback: (messageId: string, feedback: Feedback) => void
  42. currentChatInstanceRef: RefObject<{ handleStop: () => void }>
  43. themeBuilder?: ThemeBuilder
  44. }
  45. export const EmbeddedChatbotContext = createContext<EmbeddedChatbotContextValue>({
  46. currentConversationId: '',
  47. appPrevChatList: [],
  48. pinnedConversationList: [],
  49. conversationList: [],
  50. showConfigPanelBeforeChat: false,
  51. newConversationInputs: {},
  52. newConversationInputsRef: { current: {} },
  53. handleNewConversationInputsChange: () => {},
  54. inputsForms: [],
  55. handleNewConversation: () => {},
  56. handleStartChat: () => {},
  57. handleChangeConversation: () => {},
  58. handleNewConversationCompleted: () => {},
  59. chatShouldReloadKey: '',
  60. isMobile: false,
  61. isInstalledApp: false,
  62. handleFeedback: () => {},
  63. currentChatInstanceRef: { current: { handleStop: () => {} } },
  64. })
  65. export const useEmbeddedChatbotContext = () => useContext(EmbeddedChatbotContext)