ChatSidebar.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <template>
  2. <div class="chat-sidebar">
  3. <div class="sidebar-header">
  4. <el-button type="primary" class="new-chat-btn" @click="$emit('new-chat')">
  5. <span>{{ t('pages.chat.newChat') }}</span>
  6. </el-button>
  7. </div>
  8. <div class="sidebar-content" @scroll="handleScroll">
  9. <div class="conversation-list">
  10. <div class="list-title">{{ t('pages.chat.history') }}</div>
  11. <!-- 空状态提示 -->
  12. <div v-if="!conversations || conversations.length === 0" class="empty-history">
  13. {{ t('pages.chat.noHistory') }}
  14. </div>
  15. <div
  16. v-for="conv in conversations"
  17. :key="conv.id"
  18. :class="['conversation-item', { active: activeId === conv.id }]"
  19. @click="$emit('select-conversation', conv.id)"
  20. >
  21. <div class="conv-info">
  22. <div class="conv-title">{{ conv.title || t('pages.chat.newConversation') }}</div>
  23. <div class="conv-time">{{ formatTime(conv.updatedAt) }}</div>
  24. </div>
  25. <!-- 操作菜单 -->
  26. <el-dropdown
  27. class="conv-menu"
  28. trigger="click"
  29. @click.stop
  30. @command="(cmd: string) => $emit('conv-command', cmd, conv.id)"
  31. >
  32. <span class="conv-menu-icon">⋯</span>
  33. <template #dropdown>
  34. <el-dropdown-menu>
  35. <el-dropdown-item command="rename">{{ t('pages.chat.rename') }}</el-dropdown-item>
  36. <el-dropdown-item command="delete" class="delete-item">
  37. {{ t('pages.chat.delete') }}
  38. </el-dropdown-item>
  39. </el-dropdown-menu>
  40. </template>
  41. </el-dropdown>
  42. </div>
  43. <!-- 【新增】加载更多状态提示 -->
  44. <div v-if="isLoadingMore" class="loading-more">
  45. <el-icon class="is-loading">
  46. <Loading />
  47. </el-icon>
  48. <span>{{ t('common.loading') }}</span>
  49. </div>
  50. <div v-else-if="!hasMore && conversations.length > 0" class="no-more">
  51. {{ t('common.noMore') }}
  52. </div>
  53. </div>
  54. </div>
  55. </div>
  56. </template>
  57. <script setup lang="ts">
  58. import { Loading } from '@element-plus/icons-vue'
  59. import { useI18n } from '@/composables/useI18n'
  60. import type { Conversation } from '../types'
  61. const { t } = useI18n()
  62. const props = defineProps<{
  63. conversations: Conversation[]
  64. activeId?: string
  65. hasMore?: boolean
  66. isLoadingMore?: boolean
  67. }>()
  68. const emit = defineEmits<{
  69. (e: 'new-chat'): void
  70. (e: 'select-conversation', convId: string): void
  71. (e: 'conv-command', command: string | number, convId: string): void
  72. (e: 'load-more'): void
  73. }>()
  74. /**
  75. * 格式化时间显示
  76. */
  77. const formatTime = (timestamp: number) => {
  78. if (!timestamp) return ''
  79. const date = new Date(timestamp)
  80. const now = new Date()
  81. const diff = now.getTime() - date.getTime()
  82. const minutes = Math.floor(diff / 60000)
  83. const hours = Math.floor(diff / 3600000)
  84. const days = Math.floor(diff / 86400000)
  85. if (minutes < 1) return t('common.date.justNow')
  86. if (minutes < 60) return t('common.date.minutesAgo', { count: minutes })
  87. if (hours < 24) return t('common.date.hoursAgo', { count: hours })
  88. if (days < 7) return t('common.date.daysAgo', { count: days })
  89. return date.toLocaleDateString()
  90. }
  91. /**
  92. * 处理滚动事件,触底加载
  93. */
  94. const handleScroll = (event: Event) => {
  95. const target = event.target as HTMLElement
  96. // 判断是否滚动到底部 (允许 1px 误差)
  97. if (target.scrollHeight - target.scrollTop <= target.clientHeight + 1) {
  98. if (props.hasMore && !props.isLoadingMore) {
  99. emit('load-more')
  100. }
  101. }
  102. }
  103. </script>
  104. <style lang="less" scoped>
  105. .chat-sidebar {
  106. width: 260px;
  107. border-right: 1px solid var(--border-light);
  108. display: flex;
  109. flex-direction: column;
  110. background: var(--bg-base);
  111. height: 100%;
  112. flex-shrink: 0;
  113. .sidebar-header {
  114. height: 64px;
  115. padding: 16px;
  116. box-sizing: border-box;
  117. border-bottom: 1px solid var(--border-light);
  118. .new-chat-btn {
  119. width: 100%;
  120. display: flex;
  121. align-items: center;
  122. justify-content: center;
  123. }
  124. }
  125. .sidebar-content {
  126. flex: 1;
  127. overflow-y: auto;
  128. padding: 12px 8px;
  129. .conversation-list {
  130. .list-title {
  131. padding: 8px 12px;
  132. font-size: 12px;
  133. color: var(--text-tertiary);
  134. font-weight: 600;
  135. text-transform: uppercase;
  136. letter-spacing: 0.5px;
  137. }
  138. .empty-history {
  139. padding: 20px;
  140. text-align: center;
  141. color: var(--text-tertiary);
  142. font-size: 13px;
  143. }
  144. .conversation-item {
  145. padding: 10px 12px;
  146. margin-bottom: 4px;
  147. border-radius: 8px;
  148. cursor: pointer;
  149. transition: all 0.2s;
  150. position: relative;
  151. display: flex;
  152. align-items: center;
  153. justify-content: space-between;
  154. &:hover {
  155. background: var(--bg-overlay);
  156. .conv-menu {
  157. opacity: 1;
  158. visibility: visible;
  159. }
  160. }
  161. &.active {
  162. background: var(--el-color-primary-light-9);
  163. .conv-title {
  164. color: var(--el-color-primary);
  165. font-weight: 600;
  166. }
  167. .conv-time {
  168. color: var(--el-color-primary-light-3);
  169. }
  170. }
  171. .conv-info {
  172. flex: 1;
  173. overflow: hidden;
  174. margin-right: 8px;
  175. }
  176. .conv-title {
  177. font-size: 14px;
  178. color: var(--text-primary);
  179. overflow: hidden;
  180. text-overflow: ellipsis;
  181. white-space: nowrap;
  182. margin-bottom: 4px;
  183. line-height: 1.4;
  184. }
  185. .conv-time {
  186. font-size: 11px;
  187. color: var(--text-tertiary);
  188. }
  189. .conv-menu {
  190. opacity: 0;
  191. visibility: hidden;
  192. transition:
  193. opacity 0.2s,
  194. visibility 0.2s;
  195. .conv-menu-icon {
  196. display: flex;
  197. align-items: center;
  198. justify-content: center;
  199. width: 24px;
  200. height: 24px;
  201. border-radius: 4px;
  202. color: var(--text-tertiary);
  203. &:hover {
  204. background: rgba(0, 0, 0, 0.05);
  205. color: var(--text-primary);
  206. }
  207. }
  208. }
  209. }
  210. /* 【新增】加载更多样式 */
  211. .loading-more,
  212. .no-more {
  213. padding: 10px;
  214. text-align: center;
  215. font-size: 12px;
  216. color: var(--text-tertiary);
  217. display: flex;
  218. align-items: center;
  219. justify-content: center;
  220. gap: 6px;
  221. }
  222. }
  223. }
  224. }
  225. :deep(.delete-item) {
  226. color: var(--el-color-danger);
  227. &:hover {
  228. background-color: var(--el-color-danger-light-9);
  229. }
  230. }
  231. .sidebar-content::-webkit-scrollbar {
  232. width: 4px;
  233. }
  234. .sidebar-content::-webkit-scrollbar-thumb {
  235. background: transparent;
  236. border-radius: 2px;
  237. }
  238. .sidebar-content:hover::-webkit-scrollbar-thumb {
  239. background: var(--border-light);
  240. }
  241. </style>