From b586e1796e5db0935cfd011a24b86657e4e1b86f Mon Sep 17 00:00:00 2001 From: Pleasure1234 <3196812536@qq.com> Date: Fri, 31 Oct 2025 03:21:10 +0000 Subject: [PATCH] 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. --- .../home/Tabs/hooks/useUnifiedGrouping.ts | 28 +++++++++++++++++-- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/renderer/src/pages/home/Tabs/hooks/useUnifiedGrouping.ts b/src/renderer/src/pages/home/Tabs/hooks/useUnifiedGrouping.ts index 8ea07f57e1..90d2601f63 100644 --- a/src/renderer/src/pages/home/Tabs/hooks/useUnifiedGrouping.ts +++ b/src/renderer/src/pages/home/Tabs/hooks/useUnifiedGrouping.ts @@ -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() @@ -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[]) => {