context.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. ChatItemInTree,
  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. appPrevChatTree: ChatItemInTree[]
  27. pinnedConversationList: AppConversationData['data']
  28. conversationList: AppConversationData['data']
  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: (callback?: any) => void
  35. handleChangeConversation: (conversationId: string) => void
  36. handlePinConversation: (conversationId: string) => void
  37. handleUnpinConversation: (conversationId: string) => void
  38. handleDeleteConversation: (conversationId: string, callback: Callback) => void
  39. conversationRenaming: boolean
  40. handleRenameConversation: (conversationId: string, newName: string, callback: Callback) => void
  41. handleNewConversationCompleted: (newConversationId: string) => void
  42. chatShouldReloadKey: string
  43. isMobile: boolean
  44. isInstalledApp: boolean
  45. appId?: string
  46. handleFeedback: (messageId: string, feedback: Feedback) => void
  47. currentChatInstanceRef: RefObject<{ handleStop: () => void }>
  48. themeBuilder?: ThemeBuilder
  49. sidebarCollapseState?: boolean
  50. handleSidebarCollapse: (state: boolean) => void
  51. }
  52. export const ChatWithHistoryContext = createContext<ChatWithHistoryContextValue>({
  53. currentConversationId: '',
  54. appPrevChatTree: [],
  55. pinnedConversationList: [],
  56. conversationList: [],
  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. sidebarCollapseState: false,
  76. handleSidebarCollapse: () => {},
  77. })
  78. export const useChatWithHistoryContext = () => useContext(ChatWithHistoryContext)