Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions frontend/src/utils/resourceSortPreference.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import assert from 'node:assert/strict'
import test from 'node:test'

// Ways this can go wrong:
// 1. nothing stored yet -> must fall back to the default sort
// 2. a stored value that is no longer a valid option (removed option,
// hand-edited storage) -> must fall back to the default, not leak through
// 3. a saved choice is not read back on the next visit
// 4. a choice made in one workspace shows up after switching to another
// 5. the workspace id is not known yet -> must not read or write a key
// shared by every workspace
// 6. two accounts sharing a browser inherit each other's choice
// 7. the knowledge base, agent and space lists overwrite each other's choice
// 8. storage is unavailable (private mode, disabled, quota) -> must not throw

const store = new Map<string, string>()
let storageBroken = false
Object.defineProperty(globalThis, 'localStorage', {
configurable: true,
value: {
getItem(key: string) {
if (storageBroken) throw new Error('storage disabled')
return store.has(key) ? store.get(key)! : null
},
setItem(key: string, value: string) {
if (storageBroken) throw new Error('storage disabled')
store.set(key, String(value))
},
removeItem(key: string) {
if (storageBroken) throw new Error('storage disabled')
store.delete(key)
},
},
})

const { DEFAULT_RESOURCE_SORT } = await import('./resourceSorting')
const { loadResourceSortPreference, saveResourceSortPreference } = await import('./resourceSortPreference')

function loginAs(userId: string) {
store.set('weknora_user', JSON.stringify({ id: userId }))
}

test.beforeEach(() => {
store.clear()
storageBroken = false
loginAs('u1')
})

test('falls back to the default sort when nothing is stored', () => {
assert.equal(loadResourceSortPreference('knowledge_bases', 10), DEFAULT_RESOURCE_SORT)
})

test('reads back the saved sort on the next visit', () => {
saveResourceSortPreference('knowledge_bases', 10, 'name_asc')
assert.equal(loadResourceSortPreference('knowledge_bases', 10), 'name_asc')
})

test('ignores stored values that are not a known sort option', () => {
for (const raw of ['', 'name', 'NAME_ASC', 'updated_desc ', '{"value":"name_asc"}']) {
store.set('WeKnora_u1_t10_resource_sort_knowledge_bases', raw)
assert.equal(loadResourceSortPreference('knowledge_bases', 10), DEFAULT_RESOURCE_SORT, `raw=${JSON.stringify(raw)}`)
}
})

test('remembers the choice per workspace', () => {
saveResourceSortPreference('knowledge_bases', 10, 'name_asc')
assert.equal(loadResourceSortPreference('knowledge_bases', 20), DEFAULT_RESOURCE_SORT)
saveResourceSortPreference('knowledge_bases', 20, 'created_asc')
assert.equal(loadResourceSortPreference('knowledge_bases', 10), 'name_asc')
assert.equal(loadResourceSortPreference('knowledge_bases', 20), 'created_asc')
})

test('does nothing until the workspace is known', () => {
for (const tenantId of [null, undefined, 0]) {
saveResourceSortPreference('knowledge_bases', tenantId, 'name_asc')
assert.deepEqual([...store.keys()], ['weknora_user'], `tenantId=${tenantId}`)
assert.equal(loadResourceSortPreference('knowledge_bases', tenantId), DEFAULT_RESOURCE_SORT)
}
})

test('keeps each list independent', () => {
saveResourceSortPreference('knowledge_bases', 10, 'name_asc')
saveResourceSortPreference('agents', 10, 'created_asc')
assert.equal(loadResourceSortPreference('knowledge_bases', 10), 'name_asc')
assert.equal(loadResourceSortPreference('agents', 10), 'created_asc')
assert.equal(loadResourceSortPreference('organizations', 10), DEFAULT_RESOURCE_SORT)
})

test('does not share the choice between accounts on the same browser', () => {
saveResourceSortPreference('knowledge_bases', 10, 'name_desc')
loginAs('u2')
assert.equal(loadResourceSortPreference('knowledge_bases', 10), DEFAULT_RESOURCE_SORT)
loginAs('u1')
assert.equal(loadResourceSortPreference('knowledge_bases', 10), 'name_desc')
})

test('degrades to the default when storage is unavailable', () => {
saveResourceSortPreference('knowledge_bases', 10, 'name_asc')
storageBroken = true
const warn = console.warn
console.warn = () => {}
try {
assert.equal(loadResourceSortPreference('knowledge_bases', 10), DEFAULT_RESOURCE_SORT)
assert.doesNotThrow(() => saveResourceSortPreference('knowledge_bases', 10, 'created_desc'))
} finally {
console.warn = warn
}
})
34 changes: 34 additions & 0 deletions frontend/src/utils/resourceSortPreference.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Remembers the sort chosen in the knowledge base, agent and space lists so
* the next visit opens with it instead of the default.
*
* The choice is a per-user, per-workspace UI preference stored in
* localStorage through preferenceStorage
* (WeKnora_${userId}_t${tenantId}_resource_sort_${list}, the same user and
* workspace scoping as recents in useResourcePins): switching workspace
* restores that workspace's own choice, and accounts sharing a browser keep
* their own choice. Each list is remembered separately. Until the workspace
* is known nothing is read or written, so no choice leaks across workspaces.
*/
import { loadPreference, savePreference } from '../composables/preferenceStorage'
import { DEFAULT_RESOURCE_SORT, RESOURCE_SORT_OPTIONS, type ResourceSortValue } from './resourceSorting'

export type ResourceSortList = 'knowledge_bases' | 'agents' | 'organizations'

type TenantId = number | string | null | undefined

function preferenceSuffix(list: ResourceSortList, tenantId: TenantId): string | null {
return tenantId ? `t${tenantId}_resource_sort_${list}` : null
}

export function loadResourceSortPreference(list: ResourceSortList, tenantId: TenantId): ResourceSortValue {
const suffix = preferenceSuffix(list, tenantId)
const stored = suffix ? loadPreference(suffix) : null
const option = RESOURCE_SORT_OPTIONS.find(item => item.value === stored)
return option ? option.value : DEFAULT_RESOURCE_SORT
}

export function saveResourceSortPreference(list: ResourceSortList, tenantId: TenantId, value: ResourceSortValue): void {
const suffix = preferenceSuffix(list, tenantId)
if (suffix) savePreference(suffix, value)
}
5 changes: 3 additions & 2 deletions frontend/src/views/agent/AgentList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -771,11 +771,11 @@ import { useListUrlState } from '@/composables/useListUrlState'
import { useResourcePins } from '@/composables/useResourcePins'
import { integrationSectionKey } from '@/config/settingsRoute'
import {
DEFAULT_RESOURCE_SORT,
sortResourcesWithinGroups,
type ResourceSortAccessors,
type ResourceSortValue,
} from '@/utils/resourceSorting'
import { loadResourceSortPreference, saveResourceSortPreference } from '@/utils/resourceSortPreference'

const { t } = useI18n()
const route = useRoute()
Expand All @@ -787,7 +787,8 @@ const uiStore = useUIStore()
const orgStore = useOrganizationStore()
const chatResources = useChatResourcesStore()
const { loaded: modelsReadyLoaded, isReadyForAgent } = useTenantModelReadiness()
const selectedResourceSort = ref<ResourceSortValue>(DEFAULT_RESOURCE_SORT)
const selectedResourceSort = ref<ResourceSortValue>(loadResourceSortPreference('agents', authStore.effectiveTenantId))
watch(selectedResourceSort, value => saveResourceSortPreference('agents', authStore.effectiveTenantId, value))

interface AgentWithUI extends CustomAgent {
showMore?: boolean
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/views/knowledge/KnowledgeBaseList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -742,11 +742,11 @@ import { useI18n } from 'vue-i18n'
import { useListUrlState } from '@/composables/useListUrlState'
import { useResourcePins } from '@/composables/useResourcePins'
import {
DEFAULT_RESOURCE_SORT,
sortResourcesWithinGroups,
type ResourceSortAccessors,
type ResourceSortValue,
} from '@/utils/resourceSorting'
import { loadResourceSortPreference, saveResourceSortPreference } from '@/utils/resourceSortPreference'

const router = useRouter()
const route = useRoute()
Expand All @@ -756,7 +756,8 @@ const { loaded: modelsReadyLoaded, isReadyForDocumentKb } = useTenantModelReadin
const orgStore = useOrganizationStore()
const chatResources = useChatResourcesStore()
const { t } = useI18n()
const selectedResourceSort = ref<ResourceSortValue>(DEFAULT_RESOURCE_SORT)
const selectedResourceSort = ref<ResourceSortValue>(loadResourceSortPreference('knowledge_bases', authStore.effectiveTenantId))
watch(selectedResourceSort, value => saveResourceSortPreference('knowledge_bases', authStore.effectiveTenantId, value))

// 左侧空间选择:默认根据当前角色决定。
// Viewer 在该空间里通常 0 KB owned,"我的"会显示空状态、又把共享 KB 藏起来,
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/views/organization/OrganizationList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -448,11 +448,11 @@ import { matchesResourceQuery } from '@/utils/resourceListSearch'
import ResourceSortControl from '@/components/ResourceSortControl.vue'
import { shouldShowOrgRelationTag } from '@/utils/card-list-badge'
import {
DEFAULT_RESOURCE_SORT,
sortResourcesWithinGroups,
type ResourceSortAccessors,
type ResourceSortValue,
} from '@/utils/resourceSorting'
import { loadResourceSortPreference, saveResourceSortPreference } from '@/utils/resourceSortPreference'

type OrgWithUI = Organization

Expand All @@ -461,7 +461,8 @@ const route = useRoute()
const router = useRouter()
const orgStore = useOrganizationStore()
const authStore = useAuthStore()
const selectedResourceSort = ref<ResourceSortValue>(DEFAULT_RESOURCE_SORT)
const selectedResourceSort = ref<ResourceSortValue>(loadResourceSortPreference('organizations', authStore.effectiveTenantId))
watch(selectedResourceSort, value => saveResourceSortPreference('organizations', authStore.effectiveTenantId, value))

const organizationSortAccessors: ResourceSortAccessors<OrgWithUI> = {
getName: organization => organization.name,
Expand Down
Loading