ResourceSidebar.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <script setup lang="ts">
  2. import { computed, nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue'
  3. import AppIcon from '@/components/common/AppIcon.vue'
  4. import TeamAvatar from '@/components/common/TeamAvatar.vue'
  5. import AgentAvatar from '@/components/common/AgentAvatar.vue'
  6. import type { SidebarItem } from './types'
  7. const SIDEBAR_SEARCH_EVENT = 'hermes-yaoyao:sidebar-search'
  8. const SIDEBAR_SEARCH_CLOSE_EVENT = 'hermes-yaoyao:sidebar-search-close'
  9. const props = withDefaults(defineProps<{
  10. items: SidebarItem[]
  11. activeId?: string
  12. loading?: boolean
  13. searchable?: boolean
  14. externalSearch?: boolean
  15. search?: string
  16. searchPlaceholder?: string
  17. emptyTitle?: string
  18. emptyDescription?: string
  19. singleLine?: boolean
  20. hasMore?: boolean
  21. loadingMore?: boolean
  22. }>(), {
  23. activeId: '',
  24. loading: false,
  25. searchable: true,
  26. externalSearch: false,
  27. search: '',
  28. searchPlaceholder: '搜索',
  29. emptyTitle: '暂无内容',
  30. emptyDescription: '新建一项开始使用。',
  31. singleLine: false,
  32. hasMore: false,
  33. loadingMore: false,
  34. })
  35. const emit = defineEmits<{
  36. select: [id: string]
  37. search: [value: string]
  38. more: [id: string, event: MouseEvent]
  39. contextMenu: [id: string, event: MouseEvent]
  40. toggle: [id: string]
  41. loadMore: []
  42. }>()
  43. const searchInput = ref<HTMLInputElement | null>(null)
  44. const searchOpen = ref(Boolean(props.search))
  45. const sectionCounts = computed(() => props.items.reduce<Record<string, number>>((counts, item) => {
  46. if (item.section && !item.nested) counts[item.section] = (counts[item.section] ?? 0) + 1
  47. return counts
  48. }, {}))
  49. const sidebarRows = computed(() => props.items.map((item, index) => ({
  50. item,
  51. section: item.section && item.section !== props.items[index - 1]?.section ? item.section : '',
  52. sectionLabel: item.section === '已置顶' ? `已置顶 ${sectionCounts.value[item.section] ?? 0}` : item.section || '',
  53. })))
  54. async function focusSearch() {
  55. if (!props.searchable) return
  56. searchOpen.value = true
  57. await nextTick()
  58. const input = searchInput.value
  59. if (!input || input.offsetParent === null) return
  60. input.focus()
  61. input.select()
  62. }
  63. function handleSearchInput(event: Event) {
  64. const value = (event.target as HTMLInputElement).value
  65. emit('search', value)
  66. if (!value) searchOpen.value = false
  67. }
  68. function closeSearch(clear = false) {
  69. if (clear) emit('search', '')
  70. searchOpen.value = false
  71. document.dispatchEvent(new CustomEvent(SIDEBAR_SEARCH_CLOSE_EVENT))
  72. }
  73. function handleSearchRequest() { if (!props.externalSearch) void focusSearch() }
  74. function handleSearchCloseRequest() { searchOpen.value = false }
  75. function handleListScroll(event: Event) {
  76. const element = event.currentTarget as HTMLElement
  77. if (props.hasMore && !props.loadingMore && element.scrollHeight - element.scrollTop - element.clientHeight < 96) {
  78. emit('loadMore')
  79. }
  80. }
  81. watch(() => props.search, value => {
  82. if (value) searchOpen.value = true
  83. })
  84. onMounted(() => {
  85. document.addEventListener(SIDEBAR_SEARCH_EVENT, handleSearchRequest)
  86. document.addEventListener(SIDEBAR_SEARCH_CLOSE_EVENT, handleSearchCloseRequest)
  87. })
  88. onBeforeUnmount(() => {
  89. document.removeEventListener(SIDEBAR_SEARCH_EVENT, handleSearchRequest)
  90. document.removeEventListener(SIDEBAR_SEARCH_CLOSE_EVENT, handleSearchCloseRequest)
  91. })
  92. defineExpose({ focusSearch })
  93. </script>
  94. <template>
  95. <div class="resource-sidebar">
  96. <Transition name="search-reveal">
  97. <label v-if="searchable && !externalSearch && searchOpen" class="sidebar-search">
  98. <AppIcon name="search" :size="15" />
  99. <input
  100. ref="searchInput"
  101. :value="search"
  102. type="search"
  103. :placeholder="searchPlaceholder"
  104. @input="handleSearchInput"
  105. @keydown.esc.prevent="closeSearch(true)"
  106. @blur="!search && closeSearch()"
  107. />
  108. </label>
  109. </Transition>
  110. <div class="sidebar-list" role="listbox" aria-label="资源列表" @scroll.passive="handleListScroll">
  111. <div v-if="loading && !items.length" class="sidebar-loading" aria-label="正在加载">
  112. <span v-for="n in 7" :key="n" class="sidebar-skeleton" :style="{ opacity: 1 - n * .08 }" />
  113. </div>
  114. <template v-else>
  115. <template v-for="row in sidebarRows" :key="row.item.id">
  116. <div v-if="row.section" class="sidebar-section-label" :class="{ 'sidebar-section-label--pinned': row.section === '已置顶' }" role="presentation"><span>{{ row.sectionLabel }}</span></div>
  117. <div
  118. class="sidebar-item"
  119. :class="{ active: row.item.id === activeId || row.item.active, 'sidebar-item--single-line': singleLine, 'sidebar-item--nested': row.item.nested, 'sidebar-item--topic': row.item.topic, 'sidebar-item--expandable': row.item.expandable }"
  120. role="option"
  121. tabindex="0"
  122. :aria-selected="row.item.id === activeId || row.item.active"
  123. :data-sidebar-id="row.item.id"
  124. @click="emit('select', row.item.id)"
  125. @contextmenu.prevent.stop="emit('contextMenu', row.item.id, $event)"
  126. @keydown.enter.prevent="emit('select', row.item.id)"
  127. @keydown.space.prevent="emit('select', row.item.id)"
  128. >
  129. <button
  130. v-if="row.item.expandable"
  131. class="sidebar-item__expand"
  132. :class="{ 'sidebar-item__expand--collapsed': !row.item.expanded }"
  133. type="button"
  134. :aria-label="`${row.item.expanded ? '收起' : '展开'} ${row.item.title} 的话题`"
  135. :aria-expanded="row.item.expanded"
  136. @click.stop="emit('toggle', row.item.id)"
  137. >
  138. <AppIcon name="chevron-down" :size="13" />
  139. </button>
  140. <span v-if="!singleLine" class="sidebar-item__icon" :class="{ 'sidebar-item__icon--avatar': !row.item.icon }">
  141. <AgentAvatar v-if="row.item.avatarKind === 'agent'" :name="row.item.title" :avatar="row.item.avatar" :state="row.item.avatarState" :size="23" />
  142. <TeamAvatar v-else-if="row.item.avatar !== undefined || row.item.avatarMembers?.length" :name="row.item.title" :avatar="row.item.avatar || ''" :members="row.item.avatarMembers || []" :fallback-key="row.item.avatarFallbackKey || row.item.id" :size="23" />
  143. <AppIcon v-else-if="row.item.icon" :name="row.item.icon" :size="row.item.topic ? 14 : 15" />
  144. <template v-else>{{ row.item.title.slice(0, 1).toUpperCase() }}</template>
  145. <span v-if="row.item.status" class="presence" :class="`presence--${row.item.status}`" />
  146. </span>
  147. <span class="sidebar-item__copy">
  148. <span class="sidebar-item__row">
  149. <strong><AppIcon v-if="row.item.pinned" name="pin-filled" :size="12" />{{ row.item.title }}</strong>
  150. <small v-if="row.item.meta">{{ row.item.meta }}</small>
  151. </span>
  152. <span v-if="!singleLine && row.item.subtitle" class="sidebar-item__row sidebar-item__row--secondary">
  153. <span>{{ row.item.subtitle }}</span>
  154. <b v-if="row.item.unread">{{ row.item.unread > 99 ? '99+' : row.item.unread }}</b>
  155. </span>
  156. </span>
  157. <button v-if="row.item.showMore !== false" class="sidebar-item__more" type="button" aria-label="更多操作" @click.stop="emit('more', row.item.id, $event)">
  158. <AppIcon name="dots" :size="16" />
  159. </button>
  160. </div>
  161. </template>
  162. </template>
  163. <button v-if="hasMore" class="sidebar-load-more" type="button" :disabled="loadingMore" @click="emit('loadMore')">
  164. {{ loadingMore ? '正在加载更多…' : '继续加载会话' }}
  165. </button>
  166. <div v-if="!loading && !items.length" class="sidebar-empty">
  167. <span><AppIcon name="chat" :size="20" /></span>
  168. <strong>{{ emptyTitle }}</strong>
  169. <p>{{ emptyDescription }}</p>
  170. </div>
  171. </div>
  172. </div>
  173. </template>
  174. <style scoped>
  175. .resource-sidebar { display: flex; height: 100%; min-height: 0; flex-direction: column; }
  176. .sidebar-search { display: flex; align-items: center; gap: 8px; min-height: 35px; margin: 0 11px 10px; padding: 0 10px; border: 1px solid var(--line); border-radius: 10px; color: var(--text-muted); background: var(--surface-soft); }
  177. .sidebar-search:focus-within { border-color: var(--line-strong); background: var(--surface-raised); box-shadow: 0 0 0 3px var(--focus-ring); }
  178. .sidebar-search input { min-width: 0; flex: 1; border: 0; outline: 0; background: transparent; color: var(--text-primary); font-size: 13px; }
  179. .sidebar-search input::placeholder { color: var(--text-muted); }
  180. .search-reveal-enter-active, .search-reveal-leave-active { transition: opacity 120ms ease, transform 120ms ease; }
  181. .search-reveal-enter-from, .search-reveal-leave-to { opacity: 0; transform: translateY(-3px); }
  182. .sidebar-list { min-height: 0; flex: 1; overflow: auto; padding: 0 7px 14px; scrollbar-gutter: stable; }
  183. .sidebar-section-label { display: flex; align-items: center; gap: 8px; min-height: 25px; padding: 7px 8px 3px; color: var(--text-muted); font-size: 10px; font-weight: 560; }
  184. .sidebar-section-label::after { height: 1px; flex: 1; background: var(--line); content: ''; }
  185. .sidebar-section-label--pinned { min-height: 28px; padding-top: 9px; color: var(--text-secondary); font-size: 11px; font-weight: 650; }
  186. .sidebar-section-label--pinned::after { background: transparent; }
  187. .sidebar-item { position: relative; display: flex; align-items: center; gap: 7px; width: 100%; min-height: 42px; padding: 4px 7px; border: 0; border-radius: 8px; background: transparent; color: var(--text-primary); cursor: pointer; text-align: left; transition: background-color 120ms ease, color 120ms ease; }
  188. .sidebar-item:hover { background: var(--surface-soft); }
  189. .sidebar-item.active { background: var(--surface-hover); }
  190. .sidebar-item--single-line { min-height: 31px; padding-block: 1px; }
  191. .sidebar-item--single-line .sidebar-item__copy { display: block; }
  192. .sidebar-item--single-line .sidebar-item__row { min-height: 27px; }
  193. .sidebar-item--single-line .sidebar-item__row strong { font-size: 14px; font-weight: 400; }
  194. .sidebar-item--single-line .sidebar-item__more { top: 1px; }
  195. .sidebar-item--nested { min-height: 32px; margin-left: 17px; width: calc(100% - 17px); padding-block: 1px; }
  196. .sidebar-item--nested .sidebar-item__icon { width: 19px; height: 19px; flex-basis: 19px; border-radius: 6px; color: var(--text-muted); }
  197. .sidebar-item--nested .sidebar-item__row { min-height: 25px; }
  198. .sidebar-item--nested .sidebar-item__row strong { font-size: 11.5px; font-weight: 500; }
  199. .sidebar-item--nested .sidebar-item__row--secondary { display: none; }
  200. .sidebar-item--nested .sidebar-item__more { display: none; }
  201. .sidebar-item--topic .sidebar-item__row strong { font-size: 14px; font-weight: 450; }
  202. .sidebar-item__expand { display: grid; width: 18px; height: 24px; flex: 0 0 18px; place-items: center; padding: 0; border: 0; border-radius: 5px; background: transparent; color: var(--text-muted); cursor: pointer; }.sidebar-item__expand:hover, .sidebar-item__expand:focus-visible { outline: 0; background: var(--surface-hover); color: var(--text-primary); }.sidebar-item__expand :deep(.app-icon) { transition: transform 120ms ease; }.sidebar-item__expand--collapsed :deep(.app-icon) { transform: rotate(-90deg); }
  203. .sidebar-item__icon { position: relative; display: grid; place-items: center; width: 23px; height: 23px; flex: 0 0 23px; border: 0; border-radius: 7px; color: var(--text-muted); background: transparent; }
  204. .sidebar-item.active .sidebar-item__icon, .sidebar-item:hover .sidebar-item__icon { color: var(--text-secondary); }
  205. .sidebar-item__icon--avatar { background: var(--surface-hover); color: var(--text-secondary); font-size: 10px; font-weight: 700; }
  206. .presence { position: absolute; right: -2px; bottom: -2px; width: 8px; height: 8px; border: 2px solid var(--surface); border-radius: 50%; background: var(--text-muted); }
  207. .presence--online { background: var(--success); }.presence--working { background: var(--warning); }.presence--offline { background: var(--text-muted); }
  208. .sidebar-item__copy { display: flex; min-width: 0; flex: 1; flex-direction: column; gap: 1px; }
  209. .sidebar-item__row { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-width: 0; }
  210. .sidebar-item__row strong, .sidebar-item__row span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  211. .sidebar-item__row strong { display: flex; min-width: 0; align-items: center; gap: 4px; font-size: 12.5px; font-weight: 570; }
  212. .sidebar-item__row strong :deep(.app-icon) { color: var(--text-muted); }
  213. .sidebar-item__row small { flex: 0 0 auto; color: var(--text-muted); font-size: 9.5px; font-variant-numeric: tabular-nums; }
  214. .sidebar-item__row--secondary { color: var(--text-muted); font-size: 10.5px; line-height: 13px; }
  215. .sidebar-item__row b { display: grid; place-items: center; min-width: 16px; height: 16px; padding: 0 4px; border-radius: 999px; background: var(--accent); color: var(--text-on-solid); font-size: 9px; }
  216. .sidebar-item__more { display: none; position: absolute; right: 5px; top: 7px; place-items: center; width: 28px; height: 28px; padding: 0; border: 0; border-radius: 7px; background: var(--surface-raised); color: var(--text-secondary); cursor: pointer; box-shadow: 0 1px 5px rgba(0,0,0,.07); }
  217. .sidebar-item:hover .sidebar-item__more, .sidebar-item:focus-within .sidebar-item__more { display: grid; }
  218. .sidebar-load-more { display: block; width: calc(100% - 14px); min-height: 32px; margin: 8px 7px 2px; border: 0; border-radius: 8px; background: var(--surface-soft); color: var(--text-secondary); cursor: pointer; font: 11px var(--font-ui); }
  219. .sidebar-load-more:hover:not(:disabled) { background: var(--surface-hover); color: var(--text-primary); }.sidebar-load-more:disabled { cursor: progress; opacity: .7; }
  220. .sidebar-loading { display: flex; flex-direction: column; gap: 4px; padding: 2px; }
  221. .sidebar-skeleton { height: 38px; border-radius: 8px; background: linear-gradient(90deg, var(--surface-soft), var(--surface-hover), var(--surface-soft)); background-size: 200% 100%; animation: shimmer 1.5s linear infinite; }
  222. @keyframes shimmer { to { background-position: -200% 0; } }
  223. .sidebar-empty { display: grid; place-items: center; padding: 45px 22px; color: var(--text-muted); text-align: center; }
  224. .sidebar-empty > span { display: grid; place-items: center; width: 38px; height: 38px; margin-bottom: 10px; border-radius: 12px; background: var(--surface-soft); }
  225. .sidebar-empty strong { color: var(--text-secondary); font-size: 13px; }.sidebar-empty p { margin: 5px 0 0; font-size: 11px; line-height: 1.5; }
  226. @media (prefers-reduced-motion: reduce) { .sidebar-item, .search-reveal-enter-active, .search-reveal-leave-active { transition: none; } }
  227. </style>