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(resource): guard sidebar Home highlight on child routes (#20552)

Innei804d7794628b
diff --git a/src/features/ResourceHome/Layout/Header/CategoryMenu.tsx b/src/features/ResourceHome/Layout/Header/CategoryMenu.tsx
index ac319d202e7..f0044c2e438 100644
--- a/src/features/ResourceHome/Layout/Header/CategoryMenu.tsx
+++ b/src/features/ResourceHome/Layout/Header/CategoryMenu.tsx
@@ -23,6 +23,8 @@ import { useWorkspaceAwareNavigate } from '@/features/Workspace/useWorkspaceAwar
import { useActiveLocation } from '@/hooks/useActiveLocation';
import { FilesTabs } from '@/types/files';
+import { isResourceCategoryActive } from './categoryActive';
+
interface CategoryItem {
icon: LucideIcon;
isBusiness?: boolean;
@@ -37,11 +39,6 @@ const CategoryMenu = memo(() => {
const navigate = useWorkspaceAwareNavigate();
const businessCategories = useBusinessResourceCategories();
const location = useActiveLocation();
- // In Work-gallery mode (/resource/works) no file category is selected, so
- // suppress the category highlight — otherwise "Home" reads as active
- // alongside the active Work entry. Match by suffix: the pathname may carry a
- // workspace prefix.
- const worksActive = location.pathname.endsWith('/resource/works');
// Two groups below the fixed Home entry: file-based categories (uploaded
// files by type) and LobeHub business entities (pages, works, webpages),
@@ -127,11 +124,7 @@ const CategoryMenu = memo(() => {
{groups.map((group, groupIndex) => (
<Flexbox gap={1} key={groupIndex}>
{group.map((item) => {
- const isActive =
- item.key === 'works'
- ? worksActive
- : !worksActive &&
- (item.isBusiness ? location.pathname === item.url : activeKey === item.key);
+ const isActive = isResourceCategoryActive(item, activeKey, location.pathname);
return (
<Link
key={item.key}
diff --git a/src/features/ResourceHome/Layout/Header/categoryActive.test.ts b/src/features/ResourceHome/Layout/Header/categoryActive.test.ts
new file mode 100644
index 00000000000..426c4e2dcbf
--- /dev/null
+++ b/src/features/ResourceHome/Layout/Header/categoryActive.test.ts
@@ -0,0 +1,55 @@
+import { describe, expect, it } from 'vitest';
+
+import { FilesTabs } from '@/types/files';
+
+import { isResourceCategoryActive } from './categoryActive';
+
+const home = { key: FilesTabs.Home, url: '/resource' };
+
+describe('isResourceCategoryActive', () => {
+ it.each(['/resource', '/resource/', '/team/resource', '/team/resource/'])(
+ 'highlights Home on the resource dashboard at %s',
+ (pathname) => {
+ expect(isResourceCategoryActive(home, FilesTabs.Home, pathname)).toBe(true);
+ },
+ );
+
+ it.each([
+ '/resource/page',
+ '/resource/page/',
+ '/resource/images',
+ '/resource/all',
+ '/resource/works',
+ '/resource/works/',
+ '/resource/library/library-1',
+ '/resource/library/library-1/permission',
+ '/team/resource/page',
+ '/team/resource/library/library-1',
+ ])('does not highlight Home on %s when the store still selects Home', (pathname) => {
+ expect(isResourceCategoryActive(home, FilesTabs.Home, pathname)).toBe(false);
+ });
+
+ it('preserves the selected file category', () => {
+ expect(
+ isResourceCategoryActive(
+ { key: FilesTabs.Pages, url: '/resource/page' },
+ FilesTabs.Pages,
+ '/resource/page',
+ ),
+ ).toBe(true);
+ });
+
+ it('highlights Works and suppresses the stored file category on the gallery', () => {
+ const pathname = '/team/resource/works';
+ expect(
+ isResourceCategoryActive({ key: 'works', url: '/resource/works' }, FilesTabs.Home, pathname),
+ ).toBe(true);
+ expect(
+ isResourceCategoryActive(
+ { key: FilesTabs.Pages, url: '/resource/page' },
+ FilesTabs.Pages,
+ pathname,
+ ),
+ ).toBe(false);
+ });
+});
diff --git a/src/features/ResourceHome/Layout/Header/categoryActive.ts b/src/features/ResourceHome/Layout/Header/categoryActive.ts
new file mode 100644
index 00000000000..b4f122cbb9c
--- /dev/null
+++ b/src/features/ResourceHome/Layout/Header/categoryActive.ts
@@ -0,0 +1,22 @@
+import { FilesTabs } from '@/types/files';
+
+interface ResourceCategoryItem {
+ isBusiness?: boolean;
+ key: string;
+ url: string;
+}
+
+export const isResourceCategoryActive = (
+ item: ResourceCategoryItem,
+ activeKey: string,
+ pathname: string,
+): boolean => {
+ const worksActive = pathname.endsWith('/resource/works');
+
+ if (item.key === 'works') return worksActive;
+ if (item.key === FilesTabs.Home) {
+ return activeKey === FilesTabs.Home && /\/resource\/?$/.test(pathname);
+ }
+
+ return !worksActive && (item.isBusiness ? pathname === item.url : activeKey === item.key);
+};