index.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import React, { useEffect, useState } from 'react'
  2. import type { FC } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import {
  5. PencilSquareIcon,
  6. } from '@heroicons/react/24/outline'
  7. import cn from 'classnames'
  8. import Button from '../../../base/button'
  9. import List from './list'
  10. import AppInfo from '@/app/components/share/chat/sidebar/app-info'
  11. // import Card from './card'
  12. import type { ConversationItem, SiteInfo } from '@/models/share'
  13. import { fetchConversations } from '@/service/share'
  14. import { fetchConversations as fetchUniversalConversations } from '@/service/universal-chat'
  15. export type ISidebarProps = {
  16. copyRight: string
  17. currentId: string
  18. onCurrentIdChange: (id: string) => void
  19. list: ConversationItem[]
  20. isClearConversationList: boolean
  21. pinnedList: ConversationItem[]
  22. isClearPinnedConversationList: boolean
  23. isInstalledApp: boolean
  24. installedAppId?: string
  25. isUniversalChat?: boolean
  26. siteInfo: SiteInfo
  27. onMoreLoaded: (res: { data: ConversationItem[]; has_more: boolean }) => void
  28. onPinnedMoreLoaded: (res: { data: ConversationItem[]; has_more: boolean }) => void
  29. isNoMore: boolean
  30. isPinnedNoMore: boolean
  31. onPin: (id: string) => void
  32. onUnpin: (id: string) => void
  33. controlUpdateList: number
  34. onDelete: (id: string) => void
  35. }
  36. const Sidebar: FC<ISidebarProps> = ({
  37. copyRight,
  38. currentId,
  39. onCurrentIdChange,
  40. list,
  41. isClearConversationList,
  42. pinnedList,
  43. isClearPinnedConversationList,
  44. isInstalledApp,
  45. installedAppId,
  46. isUniversalChat,
  47. siteInfo,
  48. onMoreLoaded,
  49. onPinnedMoreLoaded,
  50. isNoMore,
  51. isPinnedNoMore,
  52. onPin,
  53. onUnpin,
  54. controlUpdateList,
  55. onDelete,
  56. }) => {
  57. const { t } = useTranslation()
  58. const [hasPinned, setHasPinned] = useState(false)
  59. const checkHasPinned = async () => {
  60. let res: any
  61. if (isUniversalChat)
  62. res = await fetchUniversalConversations(undefined, true)
  63. else
  64. res = await fetchConversations(isInstalledApp, installedAppId, undefined, true)
  65. setHasPinned(res.data.length > 0)
  66. }
  67. useEffect(() => {
  68. checkHasPinned()
  69. }, [])
  70. useEffect(() => {
  71. if (controlUpdateList !== 0)
  72. checkHasPinned()
  73. }, [controlUpdateList])
  74. const maxListHeight = (isInstalledApp || isUniversalChat) ? 'max-h-[30vh]' : 'max-h-[40vh]'
  75. return (
  76. <div
  77. className={
  78. cn(
  79. (isInstalledApp || isUniversalChat) ? 'tablet:h-[calc(100vh_-_74px)]' : '',
  80. 'shrink-0 flex flex-col bg-white pc:w-[244px] tablet:w-[192px] mobile:w-[240px] border-r border-gray-200 mobile:h-screen',
  81. )
  82. }
  83. >
  84. {isInstalledApp && (
  85. <AppInfo
  86. className='my-4 px-4'
  87. name={siteInfo.title || ''}
  88. icon={siteInfo.icon || ''}
  89. icon_background={siteInfo.icon_background}
  90. />
  91. )}
  92. <div className="flex flex-shrink-0 p-4 !pb-0">
  93. <Button
  94. onClick={() => { onCurrentIdChange('-1') }}
  95. className="group block w-full flex-shrink-0 !justify-start !h-9 text-primary-600 items-center text-sm">
  96. <PencilSquareIcon className="mr-2 h-4 w-4" /> {t('share.chat.newChat')}
  97. </Button>
  98. </div>
  99. <div className={'flex-grow flex flex-col h-0 overflow-y-auto overflow-x-hidden'}>
  100. {/* pinned list */}
  101. {hasPinned && (
  102. <div className={cn('mt-4 px-4', list.length === 0 && 'flex flex-col flex-grow')}>
  103. <div className='mb-1.5 leading-[18px] text-xs text-gray-500 font-medium uppercase'>{t('share.chat.pinnedTitle')}</div>
  104. <List
  105. className={cn(list.length > 0 ? maxListHeight : 'flex-grow')}
  106. currentId={currentId}
  107. onCurrentIdChange={onCurrentIdChange}
  108. list={pinnedList}
  109. isClearConversationList={isClearPinnedConversationList}
  110. isInstalledApp={isInstalledApp}
  111. installedAppId={installedAppId}
  112. isUniversalChat={isUniversalChat}
  113. onMoreLoaded={onPinnedMoreLoaded}
  114. isNoMore={isPinnedNoMore}
  115. isPinned={true}
  116. onPinChanged={id => onUnpin(id)}
  117. controlUpdate={controlUpdateList + 1}
  118. onDelete={onDelete}
  119. />
  120. </div>
  121. )}
  122. {/* unpinned list */}
  123. <div className={cn('grow flex flex-col mt-4 px-4', !hasPinned && 'flex flex-col flex-grow')}>
  124. {(hasPinned && list.length > 0) && (
  125. <div className='mb-1.5 leading-[18px] text-xs text-gray-500 font-medium uppercase'>{t('share.chat.unpinnedTitle')}</div>
  126. )}
  127. <List
  128. className={cn('flex-grow h-0')}
  129. currentId={currentId}
  130. onCurrentIdChange={onCurrentIdChange}
  131. list={list}
  132. isClearConversationList={isClearConversationList}
  133. isInstalledApp={isInstalledApp}
  134. installedAppId={installedAppId}
  135. isUniversalChat={isUniversalChat}
  136. onMoreLoaded={onMoreLoaded}
  137. isNoMore={isNoMore}
  138. isPinned={false}
  139. onPinChanged={id => onPin(id)}
  140. controlUpdate={controlUpdateList + 1}
  141. onDelete={onDelete}
  142. />
  143. </div>
  144. </div>
  145. {!isUniversalChat && (
  146. <div className="flex flex-shrink-0 pr-4 pb-4 pl-4">
  147. <div className="text-gray-400 font-normal text-xs">© {copyRight} {(new Date()).getFullYear()}</div>
  148. </div>
  149. )}
  150. </div>
  151. )
  152. }
  153. export default React.memo(Sidebar)