fix: sort grouped items by saved tags order from Redux (#11065)

Updated useUnifiedGrouping to sort grouped items by the tags order saved in the Redux store, falling back to untagged first. This improves consistency with user-defined tag ordering.
This commit is contained in:
Pleasure1234 2025-10-31 03:21:10 +00:00 committed by GitHub
parent fa2ec69fa9
commit b586e1796e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,4 +1,5 @@
import { useAppDispatch } from '@renderer/store'
import { createSelector } from '@reduxjs/toolkit'
import { RootState, useAppDispatch, useAppSelector } from '@renderer/store'
import { setUnifiedListOrder } from '@renderer/store/assistants'
import { AgentEntity, Assistant } from '@renderer/types'
import { useCallback, useMemo } from 'react'
@ -21,6 +22,13 @@ export const useUnifiedGrouping = (options: UseUnifiedGroupingOptions) => {
const { t } = useTranslation()
const dispatch = useAppDispatch()
// Selector to get tagsOrder from Redux store
const selectTagsOrder = useMemo(
() => createSelector([(state: RootState) => state.assistants], (assistants) => assistants.tagsOrder ?? []),
[]
)
const savedTagsOrder = useAppSelector(selectTagsOrder)
// Group unified items by tags
const groupedUnifiedItems = useMemo(() => {
const groups = new Map<string, UnifiedItem[]>()
@ -45,16 +53,30 @@ export const useUnifiedGrouping = (options: UseUnifiedGroupingOptions) => {
}
})
// Sort groups: untagged first, then tagged groups
// Sort groups: untagged first, then by savedTagsOrder
const untaggedKey = t('assistants.tags.untagged')
const sortedGroups = Array.from(groups.entries()).sort(([tagA], [tagB]) => {
if (tagA === untaggedKey) return -1
if (tagB === untaggedKey) return 1
if (savedTagsOrder.length > 0) {
const indexA = savedTagsOrder.indexOf(tagA)
const indexB = savedTagsOrder.indexOf(tagB)
if (indexA !== -1 && indexB !== -1) {
return indexA - indexB
}
if (indexA !== -1) return -1
if (indexB !== -1) return 1
}
return 0
})
return sortedGroups.map(([tag, items]) => ({ tag, items }))
}, [unifiedItems, t])
}, [unifiedItems, t, savedTagsOrder])
const handleUnifiedGroupReorder = useCallback(
(tag: string, newGroupList: UnifiedItem[]) => {