feat(frontend): introduce KBSwitcherDropdown component for improved knowledge base selection

Added a new KBSwitcherDropdown component to enhance the user experience when selecting knowledge bases. This component displays a list of knowledge bases with the current selection highlighted, allowing users to easily switch between them. Integrated the dropdown into both KnowledgeBase.vue and FAQEntryManager.vue, replacing the previous dropdown implementation for a more streamlined and consistent UI. The new component supports dynamic updates and maintains the order of knowledge bases while ensuring the current selection is always visible at the top.
This commit is contained in:
wizardchen
2026-05-26 10:18:17 +08:00
committed by lyingbug
parent 257766545e
commit 91e4fe8b6b
3 changed files with 178 additions and 26 deletions
@@ -0,0 +1,160 @@
<template>
<t-popup
trigger="click"
placement="bottom-left"
:overlay-style="{ padding: 0 }"
:overlay-inner-style="{ padding: 0 }"
>
<template #content>
<div class="kb-switcher-card">
<div class="kb-switcher-list">
<button
v-for="item in sortedList"
:key="item.id"
type="button"
class="kb-switcher-row"
:class="{ active: item.id === currentKbId }"
@click="handleSelect(item.id)"
>
<t-icon
:name="iconFor(item.type)"
class="kb-switcher-row-icon"
size="16px"
/>
<span class="kb-switcher-row-name" :title="item.name">{{ item.name }}</span>
<t-icon
v-if="item.id === currentKbId"
name="check"
class="kb-switcher-row-check"
size="14px"
/>
</button>
<div v-if="!sortedList.length" class="kb-switcher-empty">
{{ t('common.noData') }}
</div>
</div>
</div>
</template>
<slot />
</t-popup>
</template>
<script setup lang="ts">
import { computed } from 'vue'
import { useI18n } from 'vue-i18n'
interface KBEntry {
id: string
name: string
type?: string
}
const props = defineProps<{
kbList: KBEntry[]
currentKbId: string
}>()
const emit = defineEmits<{
(e: 'select', kbId: string): void
}>()
const { t } = useI18n()
// Sort the list with the current KB pinned to the top so users always
// see "where they are" without scrolling. The rest preserves the
// caller's order (typically "mine first, then shared"); we don't
// re-sort alphabetically because that loses the recency / share-source
// signal embedded in the input order.
const sortedList = computed<KBEntry[]>(() => {
const all = props.kbList || []
const current = all.find((kb) => kb.id === props.currentKbId)
if (!current) return all
return [current, ...all.filter((kb) => kb.id !== props.currentKbId)]
})
const iconFor = (type?: string): string => {
if (type === 'faq') return 'chat-bubble-help'
return 'folder'
}
const handleSelect = (id: string): void => {
if (id === props.currentKbId) return
emit('select', id)
}
</script>
<style scoped lang="less">
.kb-switcher-card {
min-width: 220px;
max-width: 320px;
/* Mirror the info popover's cap so both header surfaces stay inside
the viewport on shorter laptops. */
max-height: min(60vh, 420px);
display: flex;
flex-direction: column;
padding: 6px;
overflow: hidden;
}
.kb-switcher-list {
flex: 1 1 auto;
overflow-y: auto;
display: flex;
flex-direction: column;
gap: 1px;
}
.kb-switcher-row {
display: flex;
align-items: center;
gap: 8px;
padding: 6px 10px;
border: none;
border-radius: 6px;
background: transparent;
color: var(--td-text-color-primary);
font-size: 13px;
line-height: 1.4;
cursor: pointer;
transition: background 0.15s ease, color 0.15s ease;
text-align: left;
&:hover {
background: var(--td-bg-color-secondarycontainer);
}
&.active {
background: var(--td-brand-color-light, rgba(0, 82, 217, 0.08));
color: var(--td-brand-color);
font-weight: 500;
}
}
.kb-switcher-row-icon {
flex: 0 0 auto;
color: var(--td-text-color-placeholder);
.kb-switcher-row.active & {
color: var(--td-brand-color);
}
}
.kb-switcher-row-name {
flex: 1 1 auto;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.kb-switcher-row-check {
flex: 0 0 auto;
color: var(--td-brand-color);
}
.kb-switcher-empty {
padding: 16px;
text-align: center;
font-size: 12px;
color: var(--td-text-color-placeholder);
}
</style>
+9 -13
View File
@@ -6,6 +6,7 @@ import useKnowledgeBase from '@/hooks/useKnowledgeBase';
import { useRoute, useRouter } from 'vue-router';
import EmptyKnowledge from '@/components/empty-knowledge.vue';
import KBInfoPopover from '@/components/KBInfoPopover.vue';
import KBSwitcherDropdown from '@/components/KBSwitcherDropdown.vue';
import { getSessionsList, createSessions, generateSessionsTitle } from "@/api/chat/index";
import { useMenuStore } from '@/stores/menu';
import { useUIStore } from '@/stores/ui';
@@ -1674,14 +1675,6 @@ const handleNavigateToCurrentKB = () => {
router.push(`/platform/knowledge-bases/${kbId.value}`);
};
const knowledgeDropdownOptions = computed(() =>
knowledgeList.value.map((item) => ({
content: item.name,
value: item.id,
prefixIcon: () => h(TIcon, { name: item.type === 'faq' ? 'chat-bubble-help' : 'folder', size: '16px' }),
}))
);
const handleKnowledgeDropdownSelect = (data: { value: string }) => {
if (!data?.value) return;
if (data.value === kbId.value) return;
@@ -1979,10 +1972,13 @@ async function createNewSession(value: string): Promise<void> {
{{ $t('menu.knowledgeBase') }}
</button>
<t-icon name="chevron-right" class="breadcrumb-separator" />
<t-dropdown v-if="knowledgeDropdownOptions.length" :options="knowledgeDropdownOptions" trigger="click"
placement="bottom-left" @click="handleKnowledgeDropdownSelect">
<button type="button" class="breadcrumb-link dropdown" :disabled="!kbId"
@click.stop="handleNavigateToCurrentKB">
<KBSwitcherDropdown
v-if="knowledgeList.length"
:kb-list="knowledgeList"
:current-kb-id="kbId"
@select="(id) => handleKnowledgeDropdownSelect({ value: id })"
>
<button type="button" class="breadcrumb-link dropdown" :disabled="!kbId">
<template v-if="!kbInfo">
<t-skeleton animation="gradient" :row-col="[{ width: '120px', height: '20px' }]" />
</template>
@@ -1991,7 +1987,7 @@ async function createNewSession(value: string): Promise<void> {
<t-icon name="chevron-down" />
</template>
</button>
</t-dropdown>
</KBSwitcherDropdown>
<button v-else type="button" class="breadcrumb-link" :disabled="!kbId" @click="handleNavigateToCurrentKB">
<template v-if="!kbInfo">
<t-skeleton animation="gradient" :row-col="[{ width: '120px', height: '20px' }]" />
@@ -10,10 +10,13 @@
{{ $t('menu.knowledgeBase') }}
</button>
<t-icon name="chevron-right" class="breadcrumb-separator" />
<t-dropdown v-if="knowledgeDropdownOptions.length" :options="knowledgeDropdownOptions" trigger="click"
placement="bottom-left" @click="handleKnowledgeDropdownSelect">
<button type="button" class="breadcrumb-link dropdown" :disabled="!props.kbId"
@click.stop="handleNavigateToCurrentKB">
<KBSwitcherDropdown
v-if="knowledgeList.length"
:kb-list="knowledgeList"
:current-kb-id="props.kbId"
@select="(id) => handleKnowledgeDropdownSelect({ value: id })"
>
<button type="button" class="breadcrumb-link dropdown" :disabled="!props.kbId">
<template v-if="!kbInfo">
<t-skeleton animation="gradient" :row-col="[{ width: '120px', height: '20px' }]" />
</template>
@@ -22,7 +25,7 @@
<t-icon name="chevron-down" />
</template>
</button>
</t-dropdown>
</KBSwitcherDropdown>
<button v-else type="button" class="breadcrumb-link" :disabled="!props.kbId"
@click="handleNavigateToCurrentKB">
<template v-if="!kbInfo">
@@ -944,6 +947,7 @@ import * as XLSX from 'xlsx'
import Papa from 'papaparse'
import FAQTagTooltip from '@/components/FAQTagTooltip.vue'
import KBInfoPopover from '@/components/KBInfoPopover.vue'
import KBSwitcherDropdown from '@/components/KBSwitcherDropdown.vue'
import { useUIStore } from '@/stores/ui'
interface FAQEntry {
@@ -1150,14 +1154,6 @@ const filteredTags = computed(() => {
const kbInfo = ref<any>(null)
const knowledgeList = ref<Array<{ id: string; name: string; type?: string }>>([])
const knowledgeDropdownOptions = computed(() =>
knowledgeList.value
.map((item) => ({
content: item.name,
value: item.id,
prefixIcon: () => h(TIcon, { name: item.type === 'document' ? 'folder' : 'chat-bubble-help', size: '16px' }),
})),
)
const loadKnowledgeInfo = async (kbId: string) => {
if (!kbId) {