fix(frontend): dedupe KB list to stop empty render with ≥2 knowledge bases
The "All" scope of the knowledge-base list renders every card with `:key="kb.id"`. The same knowledge base can appear more than once in the source data — owned *and* shared back to the caller, or shared into their view through two different organizations (distinct share_id, identical knowledge_base.id). Duplicate `v-for` keys corrupt Vue's virtual-DOM patch and the list renders empty/partial. A single KB never collides, so the page works with one KB and blanks once there are two or more — the exact behavior reported in #795. Extract the merge into a pure, unit-tested helper (mergeAllScopeKnowledgeBases) that de-duplicates by knowledge-base id — owned rows win over shared duplicates, and repeated shares of one KB keep the most-privileged permission — while preserving the existing pinned → mine → teammate → shared(editable-first) ordering. Tests use the repo's existing node:test convention; a `test` script is added so `npm test` discovers them (it also wires up the two previously orphaned *.test.ts files). Fixes #795 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
committed by
lyingbug
parent
22349055ec
commit
bbf7ac29bf
@@ -778,6 +778,7 @@ import { useUIStore } from '@/stores/ui'
|
||||
import { useAuthStore } from '@/stores/auth'
|
||||
import { useOrganizationStore } from '@/stores/organization'
|
||||
import { listOrganizationSharedKnowledgeBases, type SharedKnowledgeBase, type OrganizationSharedKnowledgeBaseItem, type SourceFromAgentInfo } from '@/api/organization'
|
||||
import { mergeAllScopeKnowledgeBases, type OwnedKnowledgeBase, type SharedKnowledgeBaseLike } from './kbListMerge'
|
||||
import KnowledgeBaseEditorModal from './KnowledgeBaseEditorModal.vue'
|
||||
import ShareKnowledgeBaseDialog from '@/components/ShareKnowledgeBaseDialog.vue'
|
||||
import ListSpaceSidebar from '@/components/ListSpaceSidebar.vue'
|
||||
@@ -1153,50 +1154,18 @@ const filteredKnowledgeBases = computed(() => {
|
||||
if (spaceSelection.value !== 'all') {
|
||||
return []
|
||||
}
|
||||
const result: Array<(KB & { isMine: true }) | (SharedKnowledgeBase['knowledge_base'] & { isMine: false; permission: string; shared_at: string; share_id: string } & any)> = []
|
||||
// 本租户的 KB 分三段渲染:①任何人创建但被当前用户置顶的→「已置顶」组;
|
||||
// ②我创建的非置顶;③同事创建的非置顶(contributor 视图下挂在「本空间 ·
|
||||
// 仅查看」标题下)。置顶现在是 per-user 维度,必须跨创建者优先级地上浮,
|
||||
// 否则同事创建但我置顶的 KB 会被错误地沉到底部。
|
||||
const pinned: KB[] = []
|
||||
const ownMine: KB[] = []
|
||||
const teammateMine: KB[] = []
|
||||
kbs.value.forEach(kb => {
|
||||
if (kb.is_pinned) pinned.push(kb)
|
||||
else if (isMyKb(kb)) ownMine.push(kb)
|
||||
else teammateMine.push(kb)
|
||||
})
|
||||
pinned.sort((a, b) => {
|
||||
const at = a.pinned_at ? Date.parse(a.pinned_at as string) : 0
|
||||
const bt = b.pinned_at ? Date.parse(b.pinned_at as string) : 0
|
||||
return bt - at
|
||||
})
|
||||
pinned.forEach(kb => result.push({ ...kb, isMine: true as const }))
|
||||
ownMine.forEach(kb => result.push({ ...kb, isMine: true as const }))
|
||||
teammateMine.forEach(kb => result.push({ ...kb, isMine: true as const }))
|
||||
// 共享区按 permission 排序:可编辑(admin/editor)在前,仅查看(viewer)在后。
|
||||
// 即便当前角色不显示分组标题,排序也保留——展示更可预测,并且让分组开关切换
|
||||
// 时不会引起卡片顺序跳变。
|
||||
const sortedShared = [...sharedKbs.value].sort((a, b) => {
|
||||
const aE = isSharedKbEditable(a.permission) ? 0 : 1
|
||||
const bE = isSharedKbEditable(b.permission) ? 0 : 1
|
||||
return aE - bE
|
||||
})
|
||||
sortedShared.forEach(shared => {
|
||||
const kb = shared.knowledge_base
|
||||
if (!kb) return
|
||||
result.push({
|
||||
...kb,
|
||||
isMine: false as const,
|
||||
permission: shared.permission,
|
||||
shared_at: shared.shared_at,
|
||||
share_id: shared.share_id,
|
||||
org_name: shared.org_name,
|
||||
knowledge_count: kb.knowledge_count,
|
||||
chunk_count: kb.chunk_count,
|
||||
} as any)
|
||||
})
|
||||
return result
|
||||
// The "All" scope merges own + shared KBs. The card template keys each
|
||||
// row by `kb.id`, so the same KB surfacing twice — owned *and* shared
|
||||
// back, or shared into the caller's view through two different orgs —
|
||||
// produced duplicate `v-for` keys and blanked the list once there were
|
||||
// ≥2 entries (#795). mergeAllScopeKnowledgeBases de-duplicates by KB id
|
||||
// (owned wins; most-privileged share kept) while preserving the existing
|
||||
// pinned → mine → teammate → shared(editable-first) ordering.
|
||||
return mergeAllScopeKnowledgeBases(
|
||||
kbs.value as unknown as OwnedKnowledgeBase[],
|
||||
sharedKbs.value as unknown as SharedKnowledgeBaseLike[],
|
||||
authStore.user?.id,
|
||||
) as unknown as Array<(KB & { isMine: true }) | (SharedKnowledgeBase['knowledge_base'] & { isMine: false; permission: string; shared_at: string; share_id: string } & any)>
|
||||
})
|
||||
|
||||
const showKbListEmpty = computed(() => {
|
||||
|
||||
Reference in New Issue
Block a user