mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-24 18:50:56 +08:00
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:
parent
fa2ec69fa9
commit
b586e1796e
@ -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[]) => {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user