9595 <div class =" sources-sidebar" >
9696 <div class =" sources-header" >
9797 <h4 >搜索源</h4 >
98- <span class =" sources-count" >({{ searchSources.length }})</span >
98+ <span class =" sources-count" >({{ searchStats.completed }}/{{ searchStats.total }})</span >
99+ <span class =" sources-result-tag" v-if =" searchStats.withData > 0" >{{ searchStats.withData }}</span >
99100 </div >
100101 <div class =" sources-list" >
101102 <div
102- v-for =" source in searchSources "
103+ v-for =" source in sourcesWithResults "
103104 :key =" source.key"
104105 class =" source-item"
105106 :class =" { active: activeSource === source.key }"
@@ -371,6 +372,50 @@ export default defineComponent({
371372 return hasMoreFromServer || hasMoreFromLocal;
372373 });
373374
375+ // 过滤有结果的搜索源
376+ const sourcesWithResults = computed (() => {
377+ return searchSources .value .filter (source => {
378+ const results = searchResults .value [source .key ];
379+ // 严格只显示有结果的源
380+ return results && results .length > 0 ;
381+ });
382+ });
383+
384+ // 搜索统计计算属性
385+ const searchStats = computed (() => {
386+ const totalSources = searchSources .value .length ;
387+ let completedSources = 0 ;
388+ let sourcesWithData = 0 ;
389+ let sourcesWithoutData = 0 ;
390+
391+ // 计算已完成搜索的源数量(包括成功和失败的)
392+ searchSources .value .forEach (source => {
393+ const isLoading = loadingStates .value [source .key ];
394+ const hasResults = searchResults .value [source .key ] !== undefined ;
395+ const hasError = errorStates .value [source .key ] !== undefined ;
396+ const resultCount = searchResults .value [source .key ]? .length || 0 ;
397+
398+ // 如果不在加载中,且有结果或有错误,则认为已完成
399+ if (! isLoading && (hasResults || hasError)) {
400+ completedSources++ ;
401+
402+ // 区分有数据和无数据的源
403+ if (resultCount > 0 ) {
404+ sourcesWithData++ ;
405+ } else {
406+ sourcesWithoutData++ ;
407+ }
408+ }
409+ });
410+
411+ return {
412+ completed: completedSources,
413+ total: totalSources,
414+ withData: sourcesWithData,
415+ withoutData: sourcesWithoutData
416+ };
417+ });
418+
374419 // 方法
375420 const loadSearchSources = () => {
376421 try {
@@ -438,10 +483,8 @@ export default defineComponent({
438483 hasMorePages .value = {};
439484 displayedCount .value = pageSize .value ;
440485
441- // 设置第一个源为活跃源
442- if (searchSources .value .length > 0 ) {
443- activeSource .value = searchSources .value [0 ].key ;
444- }
486+ // 重置活跃源,让自动激活逻辑来处理
487+ activeSource .value = ' ' ;
445488
446489 // 并行搜索所有源
447490 const searchPromises = searchSources .value .map (source =>
@@ -856,6 +899,31 @@ export default defineComponent({
856899 watch (activeSource, () => {
857900 updateGlobalStats ();
858901 });
902+
903+ // 监听搜索结果变化,自动激活第一个有结果的源
904+ watch (searchResults, (newResults ) => {
905+ // 只有在有搜索结果且当前没有活跃源或当前活跃源没有结果时才自动切换
906+ if (Object .keys (newResults).length > 0 ) {
907+ // 找到第一个有结果的源
908+ const firstSourceWithResults = sourcesWithResults .value .find (source => {
909+ const results = newResults[source .key ];
910+ return results && results .length > 0 ;
911+ });
912+
913+ // 如果找到有结果的源,且当前没有活跃源或当前活跃源没有结果,则自动切换
914+ if (firstSourceWithResults) {
915+ const currentActiveHasResults = activeSource .value &&
916+ newResults[activeSource .value ] &&
917+ newResults[activeSource .value ].length > 0 ;
918+
919+ // 如果当前没有活跃源,或当前活跃源没有结果,则切换到第一个有结果的源
920+ if (! activeSource .value || ! currentActiveHasResults) {
921+ activeSource .value = firstSourceWithResults .key ;
922+ console .log (` 自动激活第一个有结果的搜索源: ${ firstSourceWithResults .name } ` );
923+ }
924+ }
925+ }
926+ }, { deep: true });
859927
860928 // 组件挂载时初始化
861929 onMounted (() => {
@@ -883,6 +951,7 @@ export default defineComponent({
883951 hasSearched,
884952 showSearchSettings,
885953 searchSources,
954+ sourcesWithResults,
886955 searchResults,
887956 loadingStates,
888957 errorStates,
@@ -896,6 +965,7 @@ export default defineComponent({
896965 suggestions,
897966 showActionRenderer,
898967 currentActionData,
968+ searchStats,
899969 performSearch,
900970 selectSource,
901971 getSourceName,
@@ -1107,6 +1177,16 @@ export default defineComponent({
11071177 font- size: 14px ;
11081178}
11091179
1180+ .sources - result- tag {
1181+ background: #52c41a ;
1182+ color: white;
1183+ font- size: 12px ;
1184+ padding: 2px 8px ;
1185+ border- radius: 12px ;
1186+ font- weight: 500 ;
1187+ margin- left: 8px ;
1188+ }
1189+
11101190.sources - list {
11111191 flex: 1 ;
11121192 overflow- y: auto;
0 commit comments