context.tsx 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. clearChatList?: boolean
  52. setClearChatList: (state: boolean) => void
  53. isResponding?: boolean
  54. setIsResponding: (state: boolean) => void,
  55. }
  56. export const ChatWithHistoryContext = createContext<ChatWithHistoryContextValue>({
  57. currentConversationId: '',
  58. appPrevChatTree: [],
  59. pinnedConversationList: [],
  60. conversationList: [],
  61. newConversationInputs: {},
  62. newConversationInputsRef: { current: {} },
  63. handleNewConversationInputsChange: () => {},
  64. inputsForms: [],
  65. handleNewConversation: () => {},
  66. handleStartChat: () => {},
  67. handleChangeConversation: () => {},
  68. handlePinConversation: () => {},
  69. handleUnpinConversation: () => {},
  70. handleDeleteConversation: () => {},
  71. conversationRenaming: false,
  72. handleRenameConversation: () => {},
  73. handleNewConversationCompleted: () => {},
  74. chatShouldReloadKey: '',
  75. isMobile: false,
  76. isInstalledApp: false,
  77. handleFeedback: () => {},
  78. currentChatInstanceRef: { current: { handleStop: () => {} } },
  79. sidebarCollapseState: false,
  80. handleSidebarCollapse: () => {},
  81. clearChatList: false,
  82. setClearChatList: () => {},
  83. isResponding: false,
  84. setIsResponding: () => {},
  85. })
  86. export const useChatWithHistoryContext = () => useContext(ChatWithHistoryContext)