index.tsx 4.9 KB

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