context.tsx 2.7 KB

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