| 
									
										
										
										
											2023-06-28 11:16:54 +08:00
										 |  |  | import React, { useEffect, useState } from 'react' | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | import type { FC } from 'react' | 
					
						
							|  |  |  | import { useTranslation } from 'react-i18next' | 
					
						
							|  |  |  | import { | 
					
						
							| 
									
										
										
										
											2023-06-01 23:19:36 +08:00
										 |  |  |   PencilSquareIcon, | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | } from '@heroicons/react/24/outline' | 
					
						
							| 
									
										
										
										
											2023-06-28 11:16:54 +08:00
										 |  |  | import cn from 'classnames' | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | import Button from '../../../base/button' | 
					
						
							| 
									
										
										
										
											2023-06-28 11:16:54 +08:00
										 |  |  | import List from './list' | 
					
						
							| 
									
										
										
										
											2023-05-25 16:59:47 +08:00
										 |  |  | import AppInfo from '@/app/components/share/chat/sidebar/app-info' | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | // import Card from './card'
 | 
					
						
							| 
									
										
										
										
											2023-05-25 16:59:47 +08:00
										 |  |  | import type { ConversationItem, SiteInfo } from '@/models/share' | 
					
						
							|  |  |  | import { fetchConversations } from '@/service/share' | 
					
						
							| 
									
										
										
										
											2023-07-27 13:27:34 +08:00
										 |  |  | import { fetchConversations as fetchUniversalConversations } from '@/service/universal-chat' | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export type ISidebarProps = { | 
					
						
							|  |  |  |   copyRight: string | 
					
						
							|  |  |  |   currentId: string | 
					
						
							|  |  |  |   onCurrentIdChange: (id: string) => void | 
					
						
							|  |  |  |   list: ConversationItem[] | 
					
						
							| 
									
										
										
										
											2023-08-30 17:32:32 +08:00
										 |  |  |   onListChanged: (newList: ConversationItem[]) => void | 
					
						
							| 
									
										
										
										
											2023-06-28 17:53:23 +08:00
										 |  |  |   isClearConversationList: boolean | 
					
						
							| 
									
										
										
										
											2023-06-28 11:16:54 +08:00
										 |  |  |   pinnedList: ConversationItem[] | 
					
						
							| 
									
										
										
										
											2023-08-30 17:32:32 +08:00
										 |  |  |   onPinnedListChanged: (newList: ConversationItem[]) => void | 
					
						
							| 
									
										
										
										
											2023-06-28 17:53:23 +08:00
										 |  |  |   isClearPinnedConversationList: boolean | 
					
						
							| 
									
										
										
										
											2023-05-25 16:59:47 +08:00
										 |  |  |   isInstalledApp: boolean | 
					
						
							|  |  |  |   installedAppId?: string | 
					
						
							| 
									
										
										
										
											2023-07-27 13:27:34 +08:00
										 |  |  |   isUniversalChat?: boolean | 
					
						
							| 
									
										
										
										
											2023-05-25 16:59:47 +08:00
										 |  |  |   siteInfo: SiteInfo | 
					
						
							| 
									
										
										
										
											2023-06-01 23:19:36 +08:00
										 |  |  |   onMoreLoaded: (res: { data: ConversationItem[]; has_more: boolean }) => void | 
					
						
							| 
									
										
										
										
											2023-06-28 11:16:54 +08:00
										 |  |  |   onPinnedMoreLoaded: (res: { data: ConversationItem[]; has_more: boolean }) => void | 
					
						
							| 
									
										
										
										
											2023-05-25 16:59:47 +08:00
										 |  |  |   isNoMore: boolean | 
					
						
							| 
									
										
										
										
											2023-06-28 11:16:54 +08:00
										 |  |  |   isPinnedNoMore: boolean | 
					
						
							|  |  |  |   onPin: (id: string) => void | 
					
						
							|  |  |  |   onUnpin: (id: string) => void | 
					
						
							|  |  |  |   controlUpdateList: number | 
					
						
							|  |  |  |   onDelete: (id: string) => void | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const Sidebar: FC<ISidebarProps> = ({ | 
					
						
							|  |  |  |   copyRight, | 
					
						
							|  |  |  |   currentId, | 
					
						
							|  |  |  |   onCurrentIdChange, | 
					
						
							| 
									
										
										
										
											2023-05-25 16:59:47 +08:00
										 |  |  |   list, | 
					
						
							| 
									
										
										
										
											2023-08-30 17:32:32 +08:00
										 |  |  |   onListChanged, | 
					
						
							| 
									
										
										
										
											2023-06-28 17:53:23 +08:00
										 |  |  |   isClearConversationList, | 
					
						
							| 
									
										
										
										
											2023-06-28 11:16:54 +08:00
										 |  |  |   pinnedList, | 
					
						
							| 
									
										
										
										
											2023-08-30 17:32:32 +08:00
										 |  |  |   onPinnedListChanged, | 
					
						
							| 
									
										
										
										
											2023-06-28 17:53:23 +08:00
										 |  |  |   isClearPinnedConversationList, | 
					
						
							| 
									
										
										
										
											2023-05-25 16:59:47 +08:00
										 |  |  |   isInstalledApp, | 
					
						
							|  |  |  |   installedAppId, | 
					
						
							| 
									
										
										
										
											2023-07-27 13:27:34 +08:00
										 |  |  |   isUniversalChat, | 
					
						
							| 
									
										
										
										
											2023-05-25 16:59:47 +08:00
										 |  |  |   siteInfo, | 
					
						
							|  |  |  |   onMoreLoaded, | 
					
						
							| 
									
										
										
										
											2023-06-28 11:16:54 +08:00
										 |  |  |   onPinnedMoreLoaded, | 
					
						
							| 
									
										
										
										
											2023-05-25 16:59:47 +08:00
										 |  |  |   isNoMore, | 
					
						
							| 
									
										
										
										
											2023-06-28 11:16:54 +08:00
										 |  |  |   isPinnedNoMore, | 
					
						
							|  |  |  |   onPin, | 
					
						
							|  |  |  |   onUnpin, | 
					
						
							|  |  |  |   controlUpdateList, | 
					
						
							|  |  |  |   onDelete, | 
					
						
							| 
									
										
										
										
											2023-05-25 16:59:47 +08:00
										 |  |  | }) => { | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   const { t } = useTranslation() | 
					
						
							| 
									
										
										
										
											2023-06-28 11:16:54 +08:00
										 |  |  |   const [hasPinned, setHasPinned] = useState(false) | 
					
						
							| 
									
										
										
										
											2023-05-25 16:59:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-28 11:16:54 +08:00
										 |  |  |   const checkHasPinned = async () => { | 
					
						
							| 
									
										
										
										
											2023-07-27 13:27:34 +08:00
										 |  |  |     let res: any | 
					
						
							|  |  |  |     if (isUniversalChat) | 
					
						
							|  |  |  |       res = await fetchUniversalConversations(undefined, true) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       res = await fetchConversations(isInstalledApp, installedAppId, undefined, true) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     setHasPinned(res.data.length > 0) | 
					
						
							| 
									
										
										
										
											2023-06-28 11:16:54 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   useEffect(() => { | 
					
						
							|  |  |  |     checkHasPinned() | 
					
						
							|  |  |  |   }, []) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   useEffect(() => { | 
					
						
							|  |  |  |     if (controlUpdateList !== 0) | 
					
						
							|  |  |  |       checkHasPinned() | 
					
						
							|  |  |  |   }, [controlUpdateList]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-27 13:27:34 +08:00
										 |  |  |   const maxListHeight = (isInstalledApp || isUniversalChat) ? 'max-h-[30vh]' : 'max-h-[40vh]' | 
					
						
							| 
									
										
										
										
											2023-05-25 16:59:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |   return ( | 
					
						
							|  |  |  |     <div | 
					
						
							| 
									
										
										
										
											2023-05-25 16:59:47 +08:00
										 |  |  |       className={ | 
					
						
							| 
									
										
										
										
											2023-06-28 11:16:54 +08:00
										 |  |  |         cn( | 
					
						
							| 
									
										
										
										
											2023-08-24 17:57:25 +08:00
										 |  |  |           (isInstalledApp || isUniversalChat) ? 'tablet:h-[calc(100vh_-_74px)]' : '', | 
					
						
							| 
									
										
										
										
											2023-06-01 23:19:36 +08:00
										 |  |  |           'shrink-0 flex flex-col bg-white pc:w-[244px] tablet:w-[192px] mobile:w-[240px]  border-r border-gray-200 mobile:h-screen', | 
					
						
							| 
									
										
										
										
											2023-05-25 16:59:47 +08:00
										 |  |  |         ) | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     > | 
					
						
							| 
									
										
										
										
											2023-05-25 16:59:47 +08:00
										 |  |  |       {isInstalledApp && ( | 
					
						
							|  |  |  |         <AppInfo | 
					
						
							|  |  |  |           className='my-4 px-4' | 
					
						
							|  |  |  |           name={siteInfo.title || ''} | 
					
						
							|  |  |  |           icon={siteInfo.icon || ''} | 
					
						
							|  |  |  |           icon_background={siteInfo.icon_background} | 
					
						
							|  |  |  |         /> | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |       )} | 
					
						
							| 
									
										
										
										
											2023-05-25 16:59:47 +08:00
										 |  |  |       <div className="flex flex-shrink-0 p-4 !pb-0"> | 
					
						
							|  |  |  |         <Button | 
					
						
							|  |  |  |           onClick={() => { onCurrentIdChange('-1') }} | 
					
						
							|  |  |  |           className="group block w-full flex-shrink-0 !justify-start !h-9 text-primary-600 items-center text-sm"> | 
					
						
							|  |  |  |           <PencilSquareIcon className="mr-2 h-4 w-4" /> {t('share.chat.newChat')} | 
					
						
							|  |  |  |         </Button> | 
					
						
							|  |  |  |       </div> | 
					
						
							| 
									
										
										
										
											2023-06-28 17:53:23 +08:00
										 |  |  |       <div className={'flex-grow flex flex-col h-0 overflow-y-auto overflow-x-hidden'}> | 
					
						
							| 
									
										
										
										
											2023-06-28 11:16:54 +08:00
										 |  |  |         {/* pinned list */} | 
					
						
							|  |  |  |         {hasPinned && ( | 
					
						
							| 
									
										
										
										
											2023-06-28 17:53:23 +08:00
										 |  |  |           <div className={cn('mt-4 px-4', list.length === 0 && 'flex flex-col flex-grow')}> | 
					
						
							| 
									
										
										
										
											2023-06-28 11:16:54 +08:00
										 |  |  |             <div className='mb-1.5 leading-[18px] text-xs text-gray-500 font-medium uppercase'>{t('share.chat.pinnedTitle')}</div> | 
					
						
							|  |  |  |             <List | 
					
						
							| 
									
										
										
										
											2023-06-28 17:53:23 +08:00
										 |  |  |               className={cn(list.length > 0 ? maxListHeight : 'flex-grow')} | 
					
						
							| 
									
										
										
										
											2023-06-28 11:16:54 +08:00
										 |  |  |               currentId={currentId} | 
					
						
							|  |  |  |               onCurrentIdChange={onCurrentIdChange} | 
					
						
							|  |  |  |               list={pinnedList} | 
					
						
							| 
									
										
										
										
											2023-08-30 17:32:32 +08:00
										 |  |  |               onListChanged={onPinnedListChanged} | 
					
						
							| 
									
										
										
										
											2023-06-28 17:53:23 +08:00
										 |  |  |               isClearConversationList={isClearPinnedConversationList} | 
					
						
							| 
									
										
										
										
											2023-06-28 11:16:54 +08:00
										 |  |  |               isInstalledApp={isInstalledApp} | 
					
						
							|  |  |  |               installedAppId={installedAppId} | 
					
						
							| 
									
										
										
										
											2023-07-27 13:27:34 +08:00
										 |  |  |               isUniversalChat={isUniversalChat} | 
					
						
							| 
									
										
										
										
											2023-06-28 11:16:54 +08:00
										 |  |  |               onMoreLoaded={onPinnedMoreLoaded} | 
					
						
							|  |  |  |               isNoMore={isPinnedNoMore} | 
					
						
							|  |  |  |               isPinned={true} | 
					
						
							|  |  |  |               onPinChanged={id => onUnpin(id)} | 
					
						
							|  |  |  |               controlUpdate={controlUpdateList + 1} | 
					
						
							|  |  |  |               onDelete={onDelete} | 
					
						
							|  |  |  |             /> | 
					
						
							|  |  |  |           </div> | 
					
						
							|  |  |  |         )} | 
					
						
							|  |  |  |         {/* unpinned list */} | 
					
						
							| 
									
										
										
										
											2023-07-27 13:27:34 +08:00
										 |  |  |         <div className={cn('grow flex flex-col mt-4 px-4', !hasPinned && 'flex flex-col flex-grow')}> | 
					
						
							| 
									
										
										
										
											2023-06-28 17:53:23 +08:00
										 |  |  |           {(hasPinned && list.length > 0) && ( | 
					
						
							| 
									
										
										
										
											2023-06-28 11:16:54 +08:00
										 |  |  |             <div className='mb-1.5 leading-[18px] text-xs text-gray-500 font-medium uppercase'>{t('share.chat.unpinnedTitle')}</div> | 
					
						
							|  |  |  |           )} | 
					
						
							|  |  |  |           <List | 
					
						
							| 
									
										
										
										
											2023-07-27 13:27:34 +08:00
										 |  |  |             className={cn('flex-grow h-0')} | 
					
						
							| 
									
										
										
										
											2023-06-28 11:16:54 +08:00
										 |  |  |             currentId={currentId} | 
					
						
							|  |  |  |             onCurrentIdChange={onCurrentIdChange} | 
					
						
							|  |  |  |             list={list} | 
					
						
							| 
									
										
										
										
											2023-08-30 17:32:32 +08:00
										 |  |  |             onListChanged={onListChanged} | 
					
						
							| 
									
										
										
										
											2023-06-28 17:53:23 +08:00
										 |  |  |             isClearConversationList={isClearConversationList} | 
					
						
							| 
									
										
										
										
											2023-06-28 11:16:54 +08:00
										 |  |  |             isInstalledApp={isInstalledApp} | 
					
						
							|  |  |  |             installedAppId={installedAppId} | 
					
						
							| 
									
										
										
										
											2023-07-27 13:27:34 +08:00
										 |  |  |             isUniversalChat={isUniversalChat} | 
					
						
							| 
									
										
										
										
											2023-06-28 11:16:54 +08:00
										 |  |  |             onMoreLoaded={onMoreLoaded} | 
					
						
							|  |  |  |             isNoMore={isNoMore} | 
					
						
							|  |  |  |             isPinned={false} | 
					
						
							|  |  |  |             onPinChanged={id => onPin(id)} | 
					
						
							|  |  |  |             controlUpdate={controlUpdateList + 1} | 
					
						
							|  |  |  |             onDelete={onDelete} | 
					
						
							|  |  |  |           /> | 
					
						
							|  |  |  |         </div> | 
					
						
							| 
									
										
										
										
											2023-06-28 17:53:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-28 11:16:54 +08:00
										 |  |  |       </div> | 
					
						
							| 
									
										
										
										
											2023-07-27 13:27:34 +08:00
										 |  |  |       {!isUniversalChat && ( | 
					
						
							|  |  |  |         <div className="flex flex-shrink-0 pr-4 pb-4 pl-4"> | 
					
						
							|  |  |  |           <div className="text-gray-400 font-normal text-xs">© {copyRight} {(new Date()).getFullYear()}</div> | 
					
						
							|  |  |  |         </div> | 
					
						
							|  |  |  |       )} | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  |     </div> | 
					
						
							|  |  |  |   ) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default React.memo(Sidebar) |