You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

context.tsx 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. import { noop } from 'lodash-es'
  18. export type ChatWithHistoryContextValue = {
  19. appInfoError?: any
  20. appInfoLoading?: boolean
  21. appMeta?: AppMeta
  22. appData?: AppData
  23. appParams?: ChatConfig
  24. appChatListDataLoading?: boolean
  25. currentConversationId: string
  26. currentConversationItem?: ConversationItem
  27. appPrevChatTree: ChatItemInTree[]
  28. pinnedConversationList: AppConversationData['data']
  29. conversationList: AppConversationData['data']
  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: (callback?: any) => 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. sidebarCollapseState?: boolean
  51. handleSidebarCollapse: (state: boolean) => void
  52. clearChatList?: boolean
  53. setClearChatList: (state: boolean) => void
  54. isResponding?: boolean
  55. setIsResponding: (state: boolean) => void,
  56. currentConversationInputs: Record<string, any> | null,
  57. setCurrentConversationInputs: (v: Record<string, any>) => void,
  58. }
  59. export const ChatWithHistoryContext = createContext<ChatWithHistoryContextValue>({
  60. currentConversationId: '',
  61. appPrevChatTree: [],
  62. pinnedConversationList: [],
  63. conversationList: [],
  64. newConversationInputs: {},
  65. newConversationInputsRef: { current: {} },
  66. handleNewConversationInputsChange: noop,
  67. inputsForms: [],
  68. handleNewConversation: noop,
  69. handleStartChat: noop,
  70. handleChangeConversation: noop,
  71. handlePinConversation: noop,
  72. handleUnpinConversation: noop,
  73. handleDeleteConversation: noop,
  74. conversationRenaming: false,
  75. handleRenameConversation: noop,
  76. handleNewConversationCompleted: noop,
  77. chatShouldReloadKey: '',
  78. isMobile: false,
  79. isInstalledApp: false,
  80. handleFeedback: noop,
  81. currentChatInstanceRef: { current: { handleStop: noop } },
  82. sidebarCollapseState: false,
  83. handleSidebarCollapse: noop,
  84. clearChatList: false,
  85. setClearChatList: noop,
  86. isResponding: false,
  87. setIsResponding: noop,
  88. currentConversationInputs: {},
  89. setCurrentConversationInputs: noop,
  90. })
  91. export const useChatWithHistoryContext = () => useContext(ChatWithHistoryContext)