context.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. appInfoLoading?: boolean
  18. appMeta?: AppMeta
  19. appData?: AppData
  20. appParams?: ChatConfig
  21. appChatListDataLoading?: boolean
  22. currentConversationId: string
  23. currentConversationItem?: ConversationItem
  24. appPrevChatList: ChatItem[]
  25. pinnedConversationList: AppConversationData['data']
  26. conversationList: AppConversationData['data']
  27. showConfigPanelBeforeChat: boolean
  28. newConversationInputs: Record<string, any>
  29. handleNewConversationInputsChange: (v: Record<string, any>) => void
  30. inputsForms: any[]
  31. handleNewConversation: () => void
  32. handleStartChat: () => void
  33. handleChangeConversation: (conversationId: string) => void
  34. handlePinConversation: (conversationId: string) => void
  35. handleUnpinConversation: (conversationId: string) => void
  36. handleDeleteConversation: (conversationId: string, callback: Callback) => void
  37. conversationRenaming: boolean
  38. handleRenameConversation: (conversationId: string, newName: string, callback: Callback) => void
  39. handleNewConversationCompleted: (newConversationId: string) => void
  40. chatShouldReloadKey: string
  41. isMobile: boolean
  42. isInstalledApp: boolean
  43. appId?: string
  44. handleFeedback: (messageId: string, feedback: Feedback) => void
  45. currentChatInstanceRef: RefObject<{ handleStop: () => void }>
  46. }
  47. export const ChatWithHistoryContext = createContext<ChatWithHistoryContextValue>({
  48. currentConversationId: '',
  49. appPrevChatList: [],
  50. pinnedConversationList: [],
  51. conversationList: [],
  52. showConfigPanelBeforeChat: false,
  53. newConversationInputs: {},
  54. handleNewConversationInputsChange: () => {},
  55. inputsForms: [],
  56. handleNewConversation: () => {},
  57. handleStartChat: () => {},
  58. handleChangeConversation: () => {},
  59. handlePinConversation: () => {},
  60. handleUnpinConversation: () => {},
  61. handleDeleteConversation: () => {},
  62. conversationRenaming: false,
  63. handleRenameConversation: () => {},
  64. handleNewConversationCompleted: () => {},
  65. chatShouldReloadKey: '',
  66. isMobile: false,
  67. isInstalledApp: false,
  68. handleFeedback: () => {},
  69. currentChatInstanceRef: { current: { handleStop: () => {} } },
  70. })
  71. export const useChatWithHistoryContext = () => useContext(ChatWithHistoryContext)