context.tsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 {
  11. AppConversationData,
  12. AppData,
  13. AppMeta,
  14. ConversationItem,
  15. } from '@/models/share'
  16. export type ChatWithHistoryContextValue = {
  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. handleNewConversationInputsChange: (v: Record<string, any>) => void
  31. inputsForms: any[]
  32. handleNewConversation: () => void
  33. handleStartChat: () => void
  34. handleChangeConversation: (conversationId: string) => void
  35. handlePinConversation: (conversationId: string) => void
  36. handleUnpinConversation: (conversationId: string) => void
  37. handleDeleteConversation: (conversationId: string, callback: Callback) => void
  38. conversationRenaming: boolean
  39. handleRenameConversation: (conversationId: string, newName: string, callback: Callback) => void
  40. handleNewConversationCompleted: (newConversationId: string) => void
  41. chatShouldReloadKey: string
  42. isMobile: boolean
  43. isInstalledApp: boolean
  44. appId?: string
  45. handleFeedback: (messageId: string, feedback: Feedback) => void
  46. currentChatInstanceRef: RefObject<{ handleStop: () => void }>
  47. }
  48. export const ChatWithHistoryContext = createContext<ChatWithHistoryContextValue>({
  49. currentConversationId: '',
  50. appPrevChatList: [],
  51. pinnedConversationList: [],
  52. conversationList: [],
  53. showConfigPanelBeforeChat: false,
  54. newConversationInputs: {},
  55. handleNewConversationInputsChange: () => {},
  56. inputsForms: [],
  57. handleNewConversation: () => {},
  58. handleStartChat: () => {},
  59. handleChangeConversation: () => {},
  60. handlePinConversation: () => {},
  61. handleUnpinConversation: () => {},
  62. handleDeleteConversation: () => {},
  63. conversationRenaming: false,
  64. handleRenameConversation: () => {},
  65. handleNewConversationCompleted: () => {},
  66. chatShouldReloadKey: '',
  67. isMobile: false,
  68. isInstalledApp: false,
  69. handleFeedback: () => {},
  70. currentChatInstanceRef: { current: { handleStop: () => {} } },
  71. })
  72. export const useChatWithHistoryContext = () => useContext(ChatWithHistoryContext)