Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

💄 fix(knowledge-base): 统一排序入口与话题列表交互 (#23130)

Mocha8f671f8a2aa6
diff --git a/locales/en-US/components.json b/locales/en-US/components.json
index 0a9c68e622d..96126c06322 100644
--- a/locales/en-US/components.json
+++ b/locales/en-US/components.json
@@ -80,9 +80,11 @@
"FileManager.search.placeholder": "Search files...",
"FileManager.selection.onlyOwn": "You can only select resources you uploaded.",
"FileManager.sort.asc": "Ascending",
+ "FileManager.sort.by": "Sort by",
"FileManager.sort.custom": "Custom Order",
"FileManager.sort.dateAdded": "Date Added",
"FileManager.sort.desc": "Descending",
+ "FileManager.sort.direction": "Sort direction",
"FileManager.sort.name": "Name",
"FileManager.sort.size": "Size",
"FileManager.source.acceptance": "Acceptance",
diff --git a/locales/zh-CN/components.json b/locales/zh-CN/components.json
index 51af0baaea1..00d6974ea84 100644
--- a/locales/zh-CN/components.json
+++ b/locales/zh-CN/components.json
@@ -80,9 +80,11 @@
"FileManager.search.placeholder": "搜索文件...",
"FileManager.selection.onlyOwn": "只能选择由你上传的资源",
"FileManager.sort.asc": "升序",
+ "FileManager.sort.by": "排序依据",
"FileManager.sort.custom": "自定义排序",
"FileManager.sort.dateAdded": "添加日期",
"FileManager.sort.desc": "降序",
+ "FileManager.sort.direction": "排序方向",
"FileManager.sort.name": "名称",
"FileManager.sort.size": "大小",
"FileManager.source.acceptance": "验收",
diff --git a/packages/locales/src/default/components.ts b/packages/locales/src/default/components.ts
index 0dd6c92d425..6786199b4b7 100644
--- a/packages/locales/src/default/components.ts
+++ b/packages/locales/src/default/components.ts
@@ -91,9 +91,11 @@ export default {
'FileManager.search.placeholder': 'Search files...',
'FileManager.selection.onlyOwn': 'You can only select resources you uploaded.',
'FileManager.sort.asc': 'Ascending',
+ 'FileManager.sort.by': 'Sort by',
'FileManager.sort.custom': 'Custom Order',
'FileManager.sort.dateAdded': 'Date Added',
'FileManager.sort.desc': 'Descending',
+ 'FileManager.sort.direction': 'Sort direction',
'FileManager.sort.name': 'Name',
'FileManager.sort.size': 'Size',
'FileManager.source.acceptance': 'Acceptance',
diff --git a/src/features/ResourceLibrary/Layout/Header/index.tsx b/src/features/ResourceLibrary/Layout/Header/index.tsx
index 44b9b4aa030..5177bbc9b55 100644
--- a/src/features/ResourceLibrary/Layout/Header/index.tsx
+++ b/src/features/ResourceLibrary/Layout/Header/index.tsx
@@ -39,7 +39,7 @@ const Header = memo(() => {
*/}
<Flexbox horizontal align={'center'} gap={4} paddingBlock={'0 8px'} paddingInline={8}>
<LibrarySearchBar />
- <SortDropdown iconOnly />
+ <SortDropdown />
<AddButton iconOnly rootLevel />
</Flexbox>
</>
diff --git a/src/features/ResourceManager/components/Explorer/ToolBar/SortDropdown.tsx b/src/features/ResourceManager/components/Explorer/ToolBar/SortDropdown.tsx
index f268104b43e..0a72a719faa 100644
--- a/src/features/ResourceManager/components/Explorer/ToolBar/SortDropdown.tsx
+++ b/src/features/ResourceManager/components/Explorer/ToolBar/SortDropdown.tsx
@@ -1,43 +1,26 @@
import { ActionIcon, type DropdownItem, DropdownMenu, Icon } from '@lobehub/ui';
-import { Check } from 'lucide-react';
+import { Check, ListFilter } from 'lucide-react';
import { memo } from 'react';
-import ActionIconWithChevron from './ActionIconWithChevron';
import { useSortDropdown } from './useSortDropdown';
-interface SortDropdownProps {
- /**
- * Render only the current sort icon, for the narrow sidebar header, instead
- * of the wider icon + chevron trigger used by the Explorer toolbar.
- */
- iconOnly?: boolean;
-}
-
-const SortDropdown = memo<SortDropdownProps>(({ iconOnly = false }) => {
- const { currentSortIcon, currentSortLabel, menuItems } = useSortDropdown();
+const SortDropdown = memo(() => {
+ const { currentSortLabel, menuItems } = useSortDropdown();
const items = menuItems.map<DropdownItem>((item) => {
if (item.type === 'divider') return item;
- const { checked, icon, ...props } = item;
return {
- ...props,
- extra: checked ? <Icon icon={Check} /> : undefined,
- icon: <Icon icon={icon} />,
+ ...item,
+ children: item.children.map(({ checked, ...props }) => ({
+ ...props,
+ icon: checked ? <Icon icon={Check} /> : <div />,
+ })),
};
});
return (
<DropdownMenu items={items}>
- {iconOnly ? (
- // Match the 32px SearchBar and icon-only "+" Button it sits beside.
- <ActionIcon
- icon={currentSortIcon}
- size={{ blockSize: 32, size: 16 }}
- title={currentSortLabel}
- />
- ) : (
- <ActionIconWithChevron icon={currentSortIcon} title={currentSortLabel} />
- )}
+ <ActionIcon icon={ListFilter} size={'small'} title={currentSortLabel} />
</DropdownMenu>
);
});
diff --git a/src/features/ResourceManager/components/Explorer/ToolBar/useSortDropdown.test.ts b/src/features/ResourceManager/components/Explorer/ToolBar/useSortDropdown.test.ts
index a525bc24b9d..bfe7f33d1e3 100644
--- a/src/features/ResourceManager/components/Explorer/ToolBar/useSortDropdown.test.ts
+++ b/src/features/ResourceManager/components/Explorer/ToolBar/useSortDropdown.test.ts
@@ -1,5 +1,4 @@
import { act, renderHook } from '@testing-library/react';
-import { ArrowDownAZ, CalendarIcon, GripVertical, HardDriveIcon } from 'lucide-react';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { SortType } from '@/types/files';
@@ -27,8 +26,10 @@ const setStore = (sorter: SorterKey, sortType: SortType, libraryId?: string) =>
};
const clickItem = (dropdown: ReturnType<typeof useSortDropdown>, key: string) => {
- const item = dropdown.menuItems.find((item) => item.key === key);
- if (!item || item.type === 'divider') throw new Error(`Missing sort item: ${key}`);
+ const item = dropdown.menuItems
+ .flatMap((group) => (group.type === 'group' ? group.children : []))
+ .find((item) => item.key === key);
+ if (!item) throw new Error(`Missing sort item: ${key}`);
act(() => item.onClick());
};
@@ -43,34 +44,36 @@ describe('useSortDropdown', () => {
setStore('custom', SortType.Asc, 'kb-1');
const { result } = renderHook(useSortDropdown);
- expect(result.current.menuItems.map((item) => item.key)).toEqual([
- 'custom',
- 'name',
- 'createdAt',
- 'size',
- 'sort-direction-divider',
- 'asc',
- 'desc',
- ]);
- expect(result.current.menuItems.slice(0, 4)).toMatchObject([
- { checked: true, icon: GripVertical, label: 'FileManager.sort.custom' },
- { checked: false, icon: ArrowDownAZ, label: 'FileManager.sort.name' },
- { checked: false, icon: CalendarIcon, label: 'FileManager.sort.dateAdded' },
- { checked: false, icon: HardDriveIcon, label: 'FileManager.sort.size' },
+ expect(result.current.menuItems).toMatchObject([
+ {
+ children: [
+ { checked: true, key: 'custom', label: 'FileManager.sort.custom' },
+ { checked: false, key: 'name', label: 'FileManager.sort.name' },
+ { checked: false, key: 'createdAt', label: 'FileManager.sort.dateAdded' },
+ { checked: false, key: 'size', label: 'FileManager.sort.size' },
+ ],
+ label: 'FileManager.sort.by',
+ type: 'group',
+ },
+ { type: 'divider' },
+ {
+ children: [
+ { checked: true, key: 'asc', label: 'FileManager.sort.asc' },
+ { checked: false, key: 'desc', label: 'FileManager.sort.desc' },
+ ],
+ label: 'FileManager.sort.direction',
+ type: 'group',
+ },
]);
});
- it('hides Custom Order outside a library', () => {
+ it('offers name, date and size outside a library', () => {
const { result } = renderHook(useSortDropdown);
+ const fields = result.current.menuItems[0];
- expect(result.current.menuItems.map((item) => item.key)).toEqual([
- 'name',
- 'createdAt',
- 'size',
- 'sort-direction-divider',
- 'asc',
- 'desc',
- ]);
+ expect(fields.type).toBe('group');
+ if (fields.type !== 'group') throw new Error('Missing sort fields');
+ expect(fields.children.map((item) => item.key)).toEqual(['name', 'createdAt', 'size']);
});
it('defaults Name to ascending when it is not the active field', () => {
@@ -81,17 +84,22 @@ describe('useSortDropdown', () => {
expect(store.setSortType).toHaveBeenCalledWith(SortType.Asc);
});
- it.each([
- [SortType.Desc, SortType.Asc],
- [SortType.Asc, SortType.Desc],
- ])('toggles the active field from %s to %s', (initial, expected) => {
- setStore('name', initial);
- const { result } = renderHook(useSortDropdown);
- clickItem(result.current, 'name');
-
- expect(store.setSorter).not.toHaveBeenCalled();
- expect(store.setSortType).toHaveBeenCalledWith(expected);
- });
+ it.each(['name', 'createdAt', 'size', 'custom'] as const)(
+ 'preserves direction when reselecting %s',
+ (key) => {
+ for (const direction of [SortType.Asc, SortType.Desc]) {
+ setStore(key, direction, 'kb-1');
+ store.setSorter.mockClear();
+ store.setSortType.mockClear();
+ const { result, unmount } = renderHook(useSortDropdown);
+ clickItem(result.current, key);
+
+ expect(store.setSorter).not.toHaveBeenCalled();
+ expect(store.setSortType).not.toHaveBeenCalled();
+ unmount();
+ }
+ },
+ );
it.each(['createdAt', 'size'] as const)('defaults %s to descending on first click', (key) => {
setStore('name', SortType.Asc);
@@ -102,8 +110,8 @@ describe('useSortDropdown', () => {
expect(store.setSortType).toHaveBeenCalledWith(SortType.Desc);
});
- it.each(['name', 'custom'] as const)('resets Custom Order to ascending from %s', (key) => {
- setStore(key, SortType.Desc, 'kb-1');
+ it('defaults Custom Order to ascending when switching fields', () => {
+ setStore('name', SortType.Desc, 'kb-1');
const { result } = renderHook(useSortDropdown);
clickItem(result.current, 'custom');
@@ -114,11 +122,6 @@ describe('useSortDropdown', () => {
it.each([SortType.Asc, SortType.Desc])('exposes the %s direction callback', (direction) => {
const { result } = renderHook(useSortDropdown);
- expect(result.current.menuItems.slice(-3)).toMatchObject([
- { key: 'sort-direction-divider', type: 'divider' },
- { checked: false, key: 'asc', label: 'FileManager.sort.asc' },
- { checked: true, key: 'desc', label: 'FileManager.sort.desc' },
- ]);
clickItem(result.current, direction);
expect(store.setSorter).not.toHaveBeenCalled();
@@ -126,13 +129,12 @@ describe('useSortDropdown', () => {
});
it.each([
- ['size', HardDriveIcon, 'FileManager.sort.size'],
- ['custom', GripVertical, 'FileManager.sort.custom'],
- ] as const)('reflects the active %s field and direction in the trigger', (key, icon, label) => {
+ ['size', 'FileManager.sort.size'],
+ ['custom', 'FileManager.sort.custom'],
+ ] as const)('reflects the active %s field and direction in the trigger', (key, label) => {
setStore(key, SortType.Asc, 'kb-1');
const { result, rerender } = renderHook(useSortDropdown);
- expect(result.current.currentSortIcon).toBe(icon);
expect(result.current.currentSortLabel).toBe(`${label} · FileManager.sort.asc`);
store.sortType = SortType.Desc;
@@ -141,11 +143,10 @@ describe('useSortDropdown', () => {
expect(result.current.currentSortLabel).toBe(`${label} · FileManager.sort.desc`);
});
- it('preserves the fallback label and icon when Custom Order is unavailable', () => {
+ it('preserves the fallback label when Custom Order is unavailable', () => {
setStore('custom', SortType.Asc);
const { result } = renderHook(useSortDropdown);
- expect(result.current.currentSortIcon).toBe(ArrowDownAZ);
expect(result.current.currentSortLabel).toBe(
'FileManager.sort.dateAdded · FileManager.sort.asc',
);
diff --git a/src/features/ResourceManager/components/Explorer/ToolBar/useSortDropdown.ts b/src/features/ResourceManager/components/Explorer/ToolBar/useSortDropdown.ts
index 2af6605c9e8..bd4dcc0c053 100644
--- a/src/features/ResourceManager/components/Explorer/ToolBar/useSortDropdown.ts
+++ b/src/features/ResourceManager/components/Explorer/ToolBar/useSortDropdown.ts
@@ -1,12 +1,3 @@
-import {
- ArrowDown,
- ArrowDownAZ,
- ArrowUp,
- CalendarIcon,
- GripVertical,
- HardDriveIcon,
- type LucideIcon,
-} from 'lucide-react';
import { useCallback, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
@@ -15,19 +6,18 @@ import { SortType } from '@/types/files';
type SorterKey = 'custom' | 'name' | 'createdAt' | 'size';
-type SortMenuItem =
+interface SortMenuItem {
+ checked: boolean;
+ key: string;
+ label: string;
+ onClick: () => void;
+}
+
+type SortMenuGroup =
| { key: string; type: 'divider' }
- | {
- checked: boolean;
- icon: LucideIcon;
- key: string;
- label: string;
- onClick: () => void;
- type?: never;
- };
+ | { children: SortMenuItem[]; key: string; label: string; type: 'group' };
interface SortOption {
- icon: LucideIcon;
key: SorterKey;
label: string;
}
@@ -42,15 +32,14 @@ export const useSortDropdown = () => {
const sortOptions = useMemo<SortOption[]>(() => {
const options: SortOption[] = [
- { icon: ArrowDownAZ, key: 'name', label: t('FileManager.sort.name') },
- { icon: CalendarIcon, key: 'createdAt', label: t('FileManager.sort.dateAdded') },
- { icon: HardDriveIcon, key: 'size', label: t('FileManager.sort.size') },
+ { key: 'name', label: t('FileManager.sort.name') },
+ { key: 'createdAt', label: t('FileManager.sort.dateAdded') },
+ { key: 'size', label: t('FileManager.sort.size') },
];
// Custom order only exists where rows carry a server-side sortOrder.
if (libraryId) {
options.unshift({
- icon: GripVertical,
key: 'custom',
label: t('FileManager.sort.custom'),
});
@@ -61,28 +50,18 @@ export const useSortDropdown = () => {
const handleFieldClick = useCallback(
(key: SorterKey) => {
- if (key === 'custom') {
- setSorter('custom');
- setSortType(SortType.Asc);
- return;
- }
-
- if (sorter === key) {
- setSortType(sortType === SortType.Asc ? SortType.Desc : SortType.Asc);
- return;
- }
+ if (sorter === key) return;
setSorter(key);
// Names read naturally A-Z; times and sizes are most useful newest/largest first.
- setSortType(key === 'name' ? SortType.Asc : SortType.Desc);
+ setSortType(key === 'name' || key === 'custom' ? SortType.Asc : SortType.Desc);
},
- [setSorter, setSortType, sorter, sortType],
+ [setSorter, setSortType, sorter],
);
- const menuItems: SortMenuItem[] = useMemo(() => {
+ const menuItems: SortMenuGroup[] = useMemo(() => {
const fieldItems: SortMenuItem[] = sortOptions.map((option) => ({
checked: option.key === sorter,
- icon: option.icon,
key: option.key,
label: option.label,
onClick: () => handleFieldClick(option.key),
@@ -91,21 +70,33 @@ export const useSortDropdown = () => {
const directionItems: SortMenuItem[] = [
{
checked: sortType === SortType.Asc,
- icon: ArrowUp,
key: 'asc',
label: t('FileManager.sort.asc'),
onClick: () => setSortType(SortType.Asc),
},
{
checked: sortType === SortType.Desc,
- icon: ArrowDown,
key: 'desc',
label: t('FileManager.sort.desc'),
onClick: () => setSortType(SortType.Desc),
},
];
- return [...fieldItems, { key: 'sort-direction-divider', type: 'divider' }, ...directionItems];
+ return [
+ {
+ children: fieldItems,
+ key: 'sort-field',
+ label: t('FileManager.sort.by'),
+ type: 'group',
+ },
+ { key: 'sort-direction-divider', type: 'divider' },
+ {
+ children: directionItems,
+ key: 'sort-direction',
+ label: t('FileManager.sort.direction'),
+ type: 'group',
+ },
+ ];
}, [handleFieldClick, setSortType, sortOptions, sorter, sortType, t]);
const activeOption = sortOptions.find((option) => option.key === sorter);
@@ -113,5 +104,5 @@ export const useSortDropdown = () => {
sortType === SortType.Asc ? t('FileManager.sort.asc') : t('FileManager.sort.desc');
const currentSortLabel = `${activeOption?.label || t('FileManager.sort.dateAdded')} · ${directionLabel}`;
- return { currentSortIcon: activeOption?.icon || ArrowDownAZ, currentSortLabel, menuItems };
+ return { currentSortLabel, menuItems };
};