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(nav-rail): open home or the active Channel from the Messages tab (#22439)

Arvin Xu6bc8c70e3dac
diff --git a/src/features/NavRail/index.tsx b/src/features/NavRail/index.tsx
index 2de049b24bc..4b46974f0f1 100644
--- a/src/features/NavRail/index.tsx
+++ b/src/features/NavRail/index.tsx
@@ -1,6 +1,5 @@
'use client';
-import { AGENT_CHAT_URL } from '@lobechat/const';
import { cssVar, Flexbox } from '@lobehub/ui';
import * as stylex from '@stylexjs/stylex';
import {
@@ -17,18 +16,16 @@ import { useTranslation } from 'react-i18next';
import { useActiveWorkspaceSlug } from '@/business/client/hooks/useActiveWorkspaceSlug';
import { isDesktop } from '@/const/version';
-import { resolveInboxAgentRouteId } from '@/features/AgentRoute/useResolvedAgentRouteId';
import { MAC_VIBRANCY_BACKGROUND } from '@/features/Electron/system/macWindowFullscreen';
import InboxButton from '@/features/HomeSidebar/Header/components/InboxButton';
import User from '@/features/HomeSidebar/Header/components/User';
import { useActiveLocation } from '@/hooks/useActiveLocation';
-import { useAgentStore } from '@/store/agent';
-import { builtinAgentSelectors } from '@/store/agent/selectors';
import { useHomeStore } from '@/store/home';
import { homeAgentListSelectors } from '@/store/home/selectors';
import { featureFlagsSelectors, useServerConfigStore } from '@/store/serverConfig';
import { isMacOS } from '@/utils/platform';
+import { ActiveChannelSync, useMessagesUrl } from './messagesTarget';
import MoreMenu from './MoreMenu';
import RailItem from './RailItem';
import { resolveRailTab } from './railTab';
@@ -57,10 +54,8 @@ const NavRail = memo(() => {
const activeTab = resolveRailTab(pathname, activeWorkspaceSlug);
- // Messages opens straight into the Lobe AI conversation, like an IM app's
- // default channel; the conversation list sits in the list column.
- const inboxAgentId = useAgentStore(builtinAgentSelectors.inboxAgentId);
- const messagesUrl = AGENT_CHAT_URL(resolveInboxAgentRouteId(inboxAgentId), false);
+ // Messages returns to the active Channel, or home where the conversation list lives.
+ const { activeChannelId, url: messagesUrl } = useMessagesUrl(pathname, activeWorkspaceSlug);
// Tab order follows the product IA: messages, contacts, docs, tasks, projects.
const topItems = useMemo(
@@ -76,6 +71,7 @@ const NavRail = memo(() => {
return (
<Flexbox className={stylex.props(styles.rail).className} gap={12} style={railBackground}>
+ {activeChannelId && <ActiveChannelSync />}
<User lite />
<Flexbox align={'center'} gap={RAIL_GAP}>
{topItems.map((item) => (
diff --git a/src/features/NavRail/messagesTarget.test.ts b/src/features/NavRail/messagesTarget.test.ts
new file mode 100644
index 00000000000..bee4d8d8a6f
--- /dev/null
+++ b/src/features/NavRail/messagesTarget.test.ts
@@ -0,0 +1,57 @@
+import { act, renderHook } from '@testing-library/react';
+import { beforeEach, describe, expect, it } from 'vitest';
+
+import { useChannelStore } from '@/store/channel';
+
+import { nextActiveChannelId, useMessagesUrl } from './messagesTarget';
+
+describe('nextActiveChannelId', () => {
+ it.each([
+ [undefined, '/channels/chn_1', null, 'chn_1'],
+ ['chn_1', '/lobe-team/channels/chn_2', 'lobe-team', 'chn_2'],
+ // Other pages keep the active Channel.
+ ['chn_1', '/agent/agt_1', null, 'chn_1'],
+ ['chn_1', '/tasks', null, 'chn_1'],
+ // A project's Channel belongs to the Projects tab.
+ ['chn_1', '/project/project-1/channels/chn_9', null, 'chn_1'],
+ // Home clears it.
+ ['chn_1', '/', null, undefined],
+ ['chn_1', '/lobe-team', 'lobe-team', undefined],
+ ])('%s + %s → %s', (previous, pathname, slug, expected) => {
+ expect(nextActiveChannelId(previous, pathname, slug)).toBe(expected);
+ });
+});
+
+describe('useMessagesUrl', () => {
+ beforeEach(() => {
+ useChannelStore.setState({
+ listMap: { all: [{ id: 'chn_1' }] as never },
+ } as never);
+ });
+
+ it('opens home when no Channel is active', () => {
+ const { result } = renderHook(() => useMessagesUrl('/agent/agt_1', null));
+ expect(result.current.url).toBe('/');
+ });
+
+ it('returns to the last opened Channel after leaving it', () => {
+ const { result, rerender } = renderHook(({ pathname }) => useMessagesUrl(pathname, null), {
+ initialProps: { pathname: '/channels/chn_1' },
+ });
+ expect(result.current.url).toBe('/channels/chn_1');
+
+ rerender({ pathname: '/agent/agt_1' });
+ expect(result.current.url).toBe('/channels/chn_1');
+
+ rerender({ pathname: '/' });
+ expect(result.current.url).toBe('/');
+ });
+
+ it('falls back home once the Channel is gone', () => {
+ const { result } = renderHook(() => useMessagesUrl('/channels/chn_1', null));
+ act(() => {
+ useChannelStore.setState({ listMap: { all: [] } } as never);
+ });
+ expect(result.current.url).toBe('/');
+ });
+});
diff --git a/src/features/NavRail/messagesTarget.ts b/src/features/NavRail/messagesTarget.ts
new file mode 100644
index 00000000000..b88cd929da3
--- /dev/null
+++ b/src/features/NavRail/messagesTarget.ts
@@ -0,0 +1,68 @@
+import { CHANNEL_URL } from '@lobechat/const';
+import { memo, useState } from 'react';
+
+import { channelActions, channelSelectors, useChannelStore } from '@/store/channel';
+
+/** Path segments below the active workspace slug, if the pathname carries one. */
+const scopedSegments = (pathname: string, activeWorkspaceSlug: string | null) => {
+ const segments = pathname.split('/').filter(Boolean);
+ return activeWorkspaceSlug && segments[0] === activeWorkspaceSlug ? segments.slice(1) : segments;
+};
+
+/**
+ * The Channel the Messages tab returns to after `pathname` is visited. Opening a Channel makes it
+ * the active one; going back home clears it; any other page keeps the previous one. A project's
+ * Channel belongs to the Projects tab, so it is ignored.
+ */
+export const nextActiveChannelId = (
+ previous: string | undefined,
+ pathname: string,
+ activeWorkspaceSlug: string | null,
+): string | undefined => {
+ const segments = scopedSegments(pathname, activeWorkspaceSlug);
+ if (segments.length === 0) return undefined;
+ if (segments[0] === 'channels' && segments[1]) return segments[1];
+ return previous;
+};
+
+/**
+ * Keeps the Channel list fresh while the rail points at a Channel, so one removed elsewhere (another
+ * tab or device) drops the link back to home instead of opening a missing Channel.
+ */
+export const ActiveChannelSync = memo(() => {
+ channelActions.useFetchList();
+ return null;
+});
+
+ActiveChannelSync.displayName = 'NavRailActiveChannelSync';
+
+/**
+ * Where the Messages tab leads: the active Channel while it still exists, otherwise home, where
+ * the conversation list lives.
+ */
+export const useMessagesUrl = (pathname: string, activeWorkspaceSlug: string | null) => {
+ const [tracked, setTracked] = useState(() => ({
+ channelId: nextActiveChannelId(undefined, pathname, activeWorkspaceSlug),
+ pathname,
+ }));
+ // Derive on navigation during render rather than in an effect, so the link is never a step behind.
+ if (tracked.pathname !== pathname) {
+ setTracked({
+ channelId: nextActiveChannelId(tracked.channelId, pathname, activeWorkspaceSlug),
+ pathname,
+ });
+ }
+ const activeChannelId = tracked.channelId;
+
+ const channelExists = useChannelStore((s) => {
+ if (!activeChannelId) return false;
+ const list = channelSelectors.list(s);
+ // Before the list loads, trust the route the user just opened.
+ return !list || list.some((channel) => channel.id === activeChannelId);
+ });
+
+ return {
+ activeChannelId,
+ url: activeChannelId && channelExists ? CHANNEL_URL(activeChannelId) : '/',
+ };
+};
diff --git a/src/routes/(main)/_layout/index.tsx b/src/routes/(main)/_layout/index.tsx
index 63cdf067a8c..4388f291514 100644
--- a/src/routes/(main)/_layout/index.tsx
+++ b/src/routes/(main)/_layout/index.tsx
@@ -72,7 +72,7 @@ const Layout: FC = () => {
>
<NavPanelShell />
<DesktopLayoutContainer flush>
- {/* `/` stays the home dashboard; the Messages tab opens Lobe AI. */}
+ {/* `/` stays the home dashboard; the Messages tab returns here or to the active Channel. */}
<DesktopHomeLayout>
<DesktopHome />
</DesktopHomeLayout>