index.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import {
  2. useCallback,
  3. useState,
  4. } from 'react'
  5. import { useTranslation } from 'react-i18next'
  6. import {
  7. RiEditBoxLine,
  8. RiExpandRightLine,
  9. RiLayoutLeft2Line,
  10. } from '@remixicon/react'
  11. import { useChatWithHistoryContext } from '../context'
  12. import AppIcon from '@/app/components/base/app-icon'
  13. import ActionButton from '@/app/components/base/action-button'
  14. import Button from '@/app/components/base/button'
  15. import List from '@/app/components/base/chat/chat-with-history/sidebar/list'
  16. import MenuDropdown from '@/app/components/share/text-generation/menu-dropdown'
  17. import Confirm from '@/app/components/base/confirm'
  18. import RenameModal from '@/app/components/base/chat/chat-with-history/sidebar/rename-modal'
  19. import LogoSite from '@/app/components/base/logo/logo-site'
  20. import type { ConversationItem } from '@/models/share'
  21. import cn from '@/utils/classnames'
  22. type Props = {
  23. isPanel?: boolean
  24. }
  25. const Sidebar = ({ isPanel }: Props) => {
  26. const { t } = useTranslation()
  27. const {
  28. appData,
  29. handleNewConversation,
  30. pinnedConversationList,
  31. conversationList,
  32. currentConversationId,
  33. handleChangeConversation,
  34. handlePinConversation,
  35. handleUnpinConversation,
  36. conversationRenaming,
  37. handleRenameConversation,
  38. handleDeleteConversation,
  39. sidebarCollapseState,
  40. handleSidebarCollapse,
  41. isMobile,
  42. } = useChatWithHistoryContext()
  43. const isSidebarCollapsed = sidebarCollapseState
  44. const [showConfirm, setShowConfirm] = useState<ConversationItem | null>(null)
  45. const [showRename, setShowRename] = useState<ConversationItem | null>(null)
  46. const handleOperate = useCallback((type: string, item: ConversationItem) => {
  47. if (type === 'pin')
  48. handlePinConversation(item.id)
  49. if (type === 'unpin')
  50. handleUnpinConversation(item.id)
  51. if (type === 'delete')
  52. setShowConfirm(item)
  53. if (type === 'rename')
  54. setShowRename(item)
  55. }, [handlePinConversation, handleUnpinConversation])
  56. const handleCancelConfirm = useCallback(() => {
  57. setShowConfirm(null)
  58. }, [])
  59. const handleDelete = useCallback(() => {
  60. if (showConfirm)
  61. handleDeleteConversation(showConfirm.id, { onSuccess: handleCancelConfirm })
  62. }, [showConfirm, handleDeleteConversation, handleCancelConfirm])
  63. const handleCancelRename = useCallback(() => {
  64. setShowRename(null)
  65. }, [])
  66. const handleRename = useCallback((newName: string) => {
  67. if (showRename)
  68. handleRenameConversation(showRename.id, newName, { onSuccess: handleCancelRename })
  69. }, [showRename, handleRenameConversation, handleCancelRename])
  70. return (
  71. <div className={cn(
  72. 'grow flex flex-col',
  73. isPanel && 'rounded-xl bg-components-panel-bg border-[0.5px] border-components-panel-border-subtle shadow-lg',
  74. )}>
  75. <div className={cn(
  76. 'shrink-0 flex items-center gap-3 p-3 pr-2',
  77. )}>
  78. <div className='shrink-0'>
  79. <AppIcon
  80. size='large'
  81. iconType={appData?.site.icon_type}
  82. icon={appData?.site.icon}
  83. background={appData?.site.icon_background}
  84. imageUrl={appData?.site.icon_url}
  85. />
  86. </div>
  87. <div className={cn('grow text-text-secondary system-md-semibold truncate')}>{appData?.site.title}</div>
  88. {!isMobile && isSidebarCollapsed && (
  89. <ActionButton size='l' onClick={() => handleSidebarCollapse(false)}>
  90. <RiExpandRightLine className='w-[18px] h-[18px]' />
  91. </ActionButton>
  92. )}
  93. {!isMobile && !isSidebarCollapsed && (
  94. <ActionButton size='l' onClick={() => handleSidebarCollapse(true)}>
  95. <RiLayoutLeft2Line className='w-[18px] h-[18px]' />
  96. </ActionButton>
  97. )}
  98. </div>
  99. <div className='shrink-0 px-3 py-4'>
  100. <Button variant='secondary-accent' className='w-full justify-center' onClick={handleNewConversation}>
  101. <RiEditBoxLine className='w-4 h-4 mr-1' />
  102. {t('share.chat.newChat')}
  103. </Button>
  104. </div>
  105. <div className='grow h-0 pt-4 px-3 space-y-2 overflow-y-auto'>
  106. {/* pinned list */}
  107. {!!pinnedConversationList.length && (
  108. <div className='mb-4'>
  109. <List
  110. isPin
  111. title={t('share.chat.pinnedTitle') || ''}
  112. list={pinnedConversationList}
  113. onChangeConversation={handleChangeConversation}
  114. onOperate={handleOperate}
  115. currentConversationId={currentConversationId}
  116. />
  117. </div>
  118. )}
  119. {!!conversationList.length && (
  120. <List
  121. title={(pinnedConversationList.length && t('share.chat.unpinnedTitle')) || ''}
  122. list={conversationList}
  123. onChangeConversation={handleChangeConversation}
  124. onOperate={handleOperate}
  125. currentConversationId={currentConversationId}
  126. />
  127. )}
  128. </div>
  129. <div className='shrink-0 p-3 flex items-center justify-between'>
  130. <MenuDropdown placement='top-start' data={appData?.site} />
  131. {/* powered by */}
  132. <div className='shrink-0'>
  133. {!appData?.custom_config?.remove_webapp_brand && (
  134. <div className={cn(
  135. 'shrink-0 px-2 flex items-center gap-1.5',
  136. )}>
  137. <div className='text-text-tertiary system-2xs-medium-uppercase'>{t('share.chat.poweredBy')}</div>
  138. {appData?.custom_config?.replace_webapp_logo && (
  139. <img src={appData?.custom_config?.replace_webapp_logo} alt='logo' className='block w-auto h-5' />
  140. )}
  141. {!appData?.custom_config?.replace_webapp_logo && (
  142. <LogoSite className='!h-5' />
  143. )}
  144. </div>
  145. )}
  146. </div>
  147. </div>
  148. {!!showConfirm && (
  149. <Confirm
  150. title={t('share.chat.deleteConversation.title')}
  151. content={t('share.chat.deleteConversation.content') || ''}
  152. isShow
  153. onCancel={handleCancelConfirm}
  154. onConfirm={handleDelete}
  155. />
  156. )}
  157. {showRename && (
  158. <RenameModal
  159. isShow
  160. onClose={handleCancelRename}
  161. saveLoading={conversationRenaming}
  162. name={showRename?.name || ''}
  163. onSave={handleRename}
  164. />
  165. )}
  166. </div>
  167. )
  168. }
  169. export default Sidebar