index.tsx 5.4 KB

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