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(agent-share): Visitors header arrow, desktop selected state, usage icons (#23549)

YuTengjing58bae51c3ec8
diff --git a/locales/en-US/topic.json b/locales/en-US/topic.json
index be4c4bc75c3..7001701dc0e 100644
--- a/locales/en-US/topic.json
+++ b/locales/en-US/topic.json
@@ -305,6 +305,7 @@
"shareVisitorTopics.title": "Visitors",
"shareVisitorTopics.unavailable": "This conversation is no longer available",
"shareVisitorTopics.untitled": "Untitled conversation",
+ "shareVisitorTopics.viewAll": "View all visitor conversations",
"sidebar.collapseAll": "Collapse all groups",
"sidebar.expandAll": "Expand all groups",
"sidebar.title": "Topics",
diff --git a/locales/zh-CN/topic.json b/locales/zh-CN/topic.json
index f6d9157abe9..a3bb6683f6d 100644
--- a/locales/zh-CN/topic.json
+++ b/locales/zh-CN/topic.json
@@ -305,6 +305,7 @@
"shareVisitorTopics.title": "访客",
"shareVisitorTopics.unavailable": "该对话已无法查看",
"shareVisitorTopics.untitled": "未命名对话",
+ "shareVisitorTopics.viewAll": "查看全部访客对话",
"sidebar.collapseAll": "折叠所有分组",
"sidebar.expandAll": "展开所有分组",
"sidebar.title": "话题",
diff --git a/packages/locales/src/default/topic.ts b/packages/locales/src/default/topic.ts
index 7746f977afc..6661674b741 100644
--- a/packages/locales/src/default/topic.ts
+++ b/packages/locales/src/default/topic.ts
@@ -340,6 +340,7 @@ export default {
'shareVisitorTopics.title': 'Visitors',
'shareVisitorTopics.unavailable': 'This conversation is no longer available',
'shareVisitorTopics.untitled': 'Untitled conversation',
+ 'shareVisitorTopics.viewAll': 'View all visitor conversations',
'sidebar.collapseAll': 'Collapse all groups',
'sidebar.expandAll': 'Expand all groups',
'sidebar.title': 'Topics',
diff --git a/src/features/AgentSidebar/Topic/hooks/useTopicNavigation.test.tsx b/src/features/AgentSidebar/Topic/hooks/useTopicNavigation.test.tsx
index 4cf3b113095..f8d6fb67600 100644
--- a/src/features/AgentSidebar/Topic/hooks/useTopicNavigation.test.tsx
+++ b/src/features/AgentSidebar/Topic/hooks/useTopicNavigation.test.tsx
@@ -235,6 +235,18 @@ describe('useTopicNavigation', () => {
expect(pushMock).toHaveBeenCalledWith('/agent/agent-1/topics');
});
+ it('opens the agent topics page on one source when given a trigger', () => {
+ const { result } = renderHook(() => useNavigateToAgentTopics());
+
+ act(() => {
+ result.current('agent-1', 'share');
+ });
+
+ expect(pushMock).toHaveBeenCalledWith('/agent/agent-1/topics', {
+ query: { trigger: 'share' },
+ });
+ });
+
it('leaves the current topic and opens the active agent topics page', () => {
const { result } = renderHook(() => useOpenActiveAgentTopics());
diff --git a/src/features/AgentSidebar/Topic/hooks/useTopicNavigation.ts b/src/features/AgentSidebar/Topic/hooks/useTopicNavigation.ts
index 4858a3728e1..5048621201e 100644
--- a/src/features/AgentSidebar/Topic/hooks/useTopicNavigation.ts
+++ b/src/features/AgentSidebar/Topic/hooks/useTopicNavigation.ts
@@ -3,6 +3,8 @@ import { useCallback, useMemo } from 'react';
import urlJoin from 'url-join';
import { useActiveWorkspaceSlug } from '@/business/client/hooks/useActiveWorkspaceSlug';
+import type { TriggerFilter } from '@/features/AgentTopicManager/types';
+import { TOPICS_TRIGGER_PARAM } from '@/features/AgentTopicManager/urlState';
import { getProjectTopicPath } from '@/features/Projects/Layout/navigation';
import { useFocusTopicPopup } from '@/features/TopicPopupGuard/useTopicPopupsRegistry';
import { buildWorkspaceAwarePath } from '@/features/Workspace/workspaceAwarePath';
@@ -120,7 +122,13 @@ export const useNavigateToAgentTopics = () => {
const router = useQueryRoute();
return useCallback(
- (agentId: string) => router.push(urlJoin('/agent', agentId, 'topics')),
+ /** `trigger` opens the page with only that source picked instead of the default. */
+ (agentId: string, trigger?: TriggerFilter) => {
+ const url = urlJoin('/agent', agentId, 'topics');
+ return trigger
+ ? router.push(url, { query: { [TOPICS_TRIGGER_PARAM]: trigger } })
+ : router.push(url);
+ },
[router],
);
};
diff --git a/src/features/AgentTopicManager/index.tsx b/src/features/AgentTopicManager/index.tsx
index 44f8595e6de..5144bd66cff 100644
--- a/src/features/AgentTopicManager/index.tsx
+++ b/src/features/AgentTopicManager/index.tsx
@@ -8,6 +8,7 @@ import {
import { Flexbox } from '@lobehub/ui';
import { memo, useEffect, useMemo, useRef } from 'react';
import { useTranslation } from 'react-i18next';
+import { useSearchParams } from 'react-router';
import AsyncError from '@/components/AsyncError';
import TopicsSkeleton from '@/components/Skeleton/Topics';
@@ -29,6 +30,7 @@ import Toolbar from './Toolbar';
import TopicGrid from './TopicGrid';
import TopicListView from './TopicListView';
import type { StatusFilter } from './types';
+import { parseTriggerParam, TOPICS_TRIGGER_PARAM } from './urlState';
import { useShareVisitors } from './useShareVisitors';
import {
agentGroupChannels,
@@ -99,6 +101,24 @@ const AgentTopicManager = memo(() => {
reset();
}, [activeAgentId, reset]);
+ // A link that names a source (`?trigger=share` from the sidebar's Visitors section) picks only
+ // that source. Declared after the reset so it lands on top of it, then dropped from the URL so a
+ // later agent switch or reload keeps the page's own filters.
+ const [searchParams, setSearchParams] = useSearchParams();
+ const linkedTrigger = parseTriggerParam(searchParams.get(TOPICS_TRIGGER_PARAM));
+ useEffect(() => {
+ if (!searchParams.has(TOPICS_TRIGGER_PARAM)) return;
+ if (linkedTrigger) setTriggers([linkedTrigger]);
+ setSearchParams(
+ (prev) => {
+ const next = new URLSearchParams(prev);
+ next.delete(TOPICS_TRIGGER_PARAM);
+ return next;
+ },
+ { replace: true },
+ );
+ }, [linkedTrigger, searchParams, setSearchParams, setTriggers]);
+
const { error, isHydrated, isValidating, revalidate } = useFetchAgentTopicsView(true, {
agentId: activeAgentId,
pageSize: PAGE_SIZE,
diff --git a/src/features/AgentTopicManager/urlState.test.ts b/src/features/AgentTopicManager/urlState.test.ts
new file mode 100644
index 00000000000..e21dec0e6c8
--- /dev/null
+++ b/src/features/AgentTopicManager/urlState.test.ts
@@ -0,0 +1,16 @@
+import { describe, expect, it } from 'vitest';
+
+import { parseTriggerParam } from './urlState';
+
+describe('parseTriggerParam', () => {
+ it('accepts a known source', () => {
+ expect(parseTriggerParam('share')).toBe('share');
+ expect(parseTriggerParam('chat')).toBe('chat');
+ });
+
+ it('ignores a missing or unknown source', () => {
+ expect(parseTriggerParam(null)).toBeUndefined();
+ expect(parseTriggerParam('')).toBeUndefined();
+ expect(parseTriggerParam('visitor')).toBeUndefined();
+ });
+});
diff --git a/src/features/AgentTopicManager/urlState.ts b/src/features/AgentTopicManager/urlState.ts
new file mode 100644
index 00000000000..ce697889931
--- /dev/null
+++ b/src/features/AgentTopicManager/urlState.ts
@@ -0,0 +1,22 @@
+import type { TriggerFilter } from './types';
+
+/**
+ * Query parameter that opens the Topics page with one source picked, e.g.
+ * `/agent/:aid/topics?trigger=share` from the sidebar's Visitors section. The page applies it
+ * once and drops it from the URL, so later filter changes are not overridden by a stale link.
+ */
+export const TOPICS_TRIGGER_PARAM = 'trigger';
+
+const TRIGGERS = new Set<TriggerFilter>([
+ 'api',
+ 'bot',
+ 'channel',
+ 'chat',
+ 'eval',
+ 'share',
+ 'task',
+ 'webhook',
+]);
+
+export const parseTriggerParam = (value: string | null): TriggerFilter | undefined =>
+ value && TRIGGERS.has(value as TriggerFilter) ? (value as TriggerFilter) : undefined;
diff --git a/src/features/ShareVisitorTopics/SidebarSection.tsx b/src/features/ShareVisitorTopics/SidebarSection.tsx
index db81becc20b..323cd1e8f0a 100644
--- a/src/features/ShareVisitorTopics/SidebarSection.tsx
+++ b/src/features/ShareVisitorTopics/SidebarSection.tsx
@@ -7,22 +7,28 @@ import {
AccordionItem,
AccordionPanel,
AccordionRoot,
+ accordionStyles,
AccordionTrigger,
+ ActionIcon,
+ cx,
Flexbox,
Text,
} from '@lobehub/ui';
import { cssVar } from '@lobehub/ui/stylex/cssVar.stylex';
-import { useMemo, useState } from 'react';
+import { ArrowRight } from 'lucide-react';
+import { type MouseEvent, useState } from 'react';
import { useTranslation } from 'react-i18next';
-import { useLocation } from 'react-router';
import { useActiveWorkspaceSlug } from '@/business/client/hooks/useActiveWorkspaceSlug';
import UserAvatar from '@/components/UserAvatar';
+import { useNavigateToAgentTopics } from '@/features/AgentSidebar/Topic/hooks/useTopicNavigation';
import NavItem from '@/features/NavPanel/components/NavItem';
import SectionTitle, { useSectionTriggerStyle } from '@/features/NavPanel/components/SectionTitle';
import { useWorkspaceAwareNavigate } from '@/features/Workspace/useWorkspaceAwareNavigate';
import { buildWorkspaceAwarePath } from '@/features/Workspace/workspaceAwarePath';
+import { isMobileSurface } from '@/utils/mobileSurface';
+import { useActiveVisitorTopic } from './useActiveVisitorTopic';
import { useShareVisitorTopics } from './useShareVisitorTopics';
const COUNT_STYLE = { fontVariantNumeric: 'tabular-nums' } as const;
@@ -87,8 +93,6 @@ const VisitorGroup = ({
);
};
-const VISITOR_TOPIC_PATH = /\/visitor\/([^/]+)\/?$/;
-
interface ShareVisitorTopicsSectionProps {
agentId: string;
itemKey: string;
@@ -102,16 +106,8 @@ const ShareVisitorTopicsSection = ({ agentId, itemKey }: ShareVisitorTopicsSecti
const { t } = useTranslation('topic');
const sectionTriggerStyle = useSectionTriggerStyle();
const groups = useShareVisitorTopics(agentId);
- const { pathname } = useLocation();
- const activeTopicId = VISITOR_TOPIC_PATH.exec(pathname)?.[1];
- const activeVisitorId = useMemo(
- () =>
- activeTopicId
- ? groups.find((group) => group.topics.some((topic) => topic.id === activeTopicId))?.visitor
- .id
- : undefined,
- [activeTopicId, groups],
- );
+ const navigateToAgentTopics = useNavigateToAgentTopics();
+ const { activeTopicId, activeVisitorId } = useActiveVisitorTopic(groups);
const [expanded, setExpanded] = useState<string[]>([]);
const [revealedVisitorId, setRevealedVisitorId] = useState<string>();
@@ -135,6 +131,26 @@ const ShareVisitorTopicsSection = ({ agentId, itemKey }: ShareVisitorTopicsSecti
</Text>
</Flexbox>
</AccordionTrigger>
+ {/* Same hover arrow as the Topics section, opening the Topics page on the Share source. */}
+ <div
+ className={cx(
+ 'accordion-action',
+ accordionStyles.action,
+ accordionStyles.actionBorderless,
+ isMobileSurface() && accordionStyles.actionAlwaysVisible,
+ )}
+ >
+ <ActionIcon
+ icon={ArrowRight}
+ size={'small'}
+ title={t('shareVisitorTopics.viewAll')}
+ onClick={(e: MouseEvent) => {
+ // Stop the click from toggling the accordion header.
+ e.stopPropagation();
+ navigateToAgentTopics(agentId, 'share');
+ }}
+ />
+ </div>
</AccordionHeader>
<AccordionPanel contentStyle={{ padding: 0 }}>
<AccordionRoot
diff --git a/src/features/ShareVisitorTopics/TopicView.tsx b/src/features/ShareVisitorTopics/TopicView.tsx
index 0bdf7f46437..fc0a5443da6 100644
--- a/src/features/ShareVisitorTopics/TopicView.tsx
+++ b/src/features/ShareVisitorTopics/TopicView.tsx
@@ -1,9 +1,9 @@
'use client';
import type { ConversationContext } from '@lobechat/types';
-import { formatPrice, formatTokenNumber } from '@lobechat/utils/format';
+import { formatTokenNumber, formatUsd } from '@lobechat/utils/format';
import { Center, Flexbox, Icon, Tag, Text } from '@lobehub/ui';
-import { CircleDollarSign, Zap } from 'lucide-react';
+import { CoinsIcon } from 'lucide-react';
import { useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { useParams } from 'react-router';
@@ -16,7 +16,11 @@ import NavHeader from '@/features/NavHeader';
import { useShareVisitorTopic } from './useShareVisitorTopics';
-/** Counts and amounts are compared across topics, so digits keep the same width. */
+/**
+ * Counts and amounts are compared across topics, so digits keep the same width. Tokens and cost
+ * read the way each reply's usage footer below shows them: a coins icon before the token count,
+ * and a bare dollar amount (`formatUsd`) for the cost.
+ */
const NUMBER_STYLE = { fontVariantNumeric: 'tabular-nums' } as const;
const renderMessage = (index: number, id: string) => (
@@ -79,19 +83,16 @@ const ShareVisitorTopicView = () => {
<Flexbox horizontal align={'center'} gap={12} style={NUMBER_STYLE}>
{topic.totalTokens > 0 && (
<Flexbox horizontal align={'center'} gap={3} title={`${topic.totalTokens} tokens`}>
- <Icon icon={Zap} size={12} />
+ <Icon icon={CoinsIcon} size={12} />
<Text fontSize={12} type={'secondary'}>
{formatTokenNumber(topic.totalTokens)}
</Text>
</Flexbox>
)}
{topic.totalCost > 0 && (
- <Flexbox horizontal align={'center'} gap={3} title={`$${topic.totalCost.toFixed(4)}`}>
- <Icon icon={CircleDollarSign} size={12} />
- <Text fontSize={12} type={'secondary'}>
- {formatPrice(topic.totalCost, 2)}
- </Text>
- </Flexbox>
+ <Text fontSize={12} type={'secondary'}>
+ {formatUsd(topic.totalCost)}
+ </Text>
)}
<Tag title={t('shareVisitorTopics.readOnlyHint')}>
{t('shareVisitorTopics.readOnly')}
diff --git a/src/features/ShareVisitorTopics/useActiveVisitorTopic.test.ts b/src/features/ShareVisitorTopics/useActiveVisitorTopic.test.ts
new file mode 100644
index 00000000000..a7daa7c844b
--- /dev/null
+++ b/src/features/ShareVisitorTopics/useActiveVisitorTopic.test.ts
@@ -0,0 +1,53 @@
+import type { ShareVisitorTopicGroup } from '@lobechat/types';
+import { renderHook } from '@testing-library/react';
+import { createElement, type ReactNode } from 'react';
+import { MemoryRouter } from 'react-router';
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+import { useActiveVisitorTopic } from './useActiveVisitorTopic';
+
+const activePathnameMock = vi.hoisted(() => vi.fn());
+
+vi.mock('@/hooks/useActiveLocation', () => ({
+ useActiveLocation: () => ({ hash: '', pathname: activePathnameMock(), search: '' }),
+}));
+
+const groups = [
+ {
+ topics: [{ id: 'tpc_a' }, { id: 'tpc_b' }],
+ visitor: { avatar: null, id: 'user_alice', name: 'Alice' },
+ },
+ {
+ topics: [{ id: 'tpc_c' }],
+ visitor: { avatar: null, id: 'user_bob', name: 'Bob' },
+ },
+] as unknown as ShareVisitorTopicGroup[];
+
+/**
+ * The sidebar's own router stays on the Agent root, as on desktop, where the sidebar renders
+ * outside the tab that shows the visitor topic.
+ */
+const wrapper = ({ children }: { children: ReactNode }) =>
+ createElement(MemoryRouter, { initialEntries: ['/agent/agt_1'] }, children);
+
+describe('useActiveVisitorTopic', () => {
+ beforeEach(() => {
+ activePathnameMock.mockReset();
+ });
+
+ it('follows the active tab URL rather than the sidebar router', () => {
+ activePathnameMock.mockReturnValue('/agent/agt_1/visitor/tpc_b');
+
+ const { result } = renderHook(() => useActiveVisitorTopic(groups), { wrapper });
+
+ expect(result.current).toEqual({ activeTopicId: 'tpc_b', activeVisitorId: 'user_alice' });
+ });
+
+ it('finds no topic when the tab shows something else', () => {
+ activePathnameMock.mockReturnValue('/agent/agt_1/tpc_own');
+
+ const { result } = renderHook(() => useActiveVisitorTopic(groups), { wrapper });
+
+ expect(result.current).toEqual({ activeTopicId: undefined, activeVisitorId: undefined });
+ });
+});
diff --git a/src/features/ShareVisitorTopics/useActiveVisitorTopic.ts b/src/features/ShareVisitorTopics/useActiveVisitorTopic.ts
new file mode 100644
index 00000000000..2e4afff1a24
--- /dev/null
+++ b/src/features/ShareVisitorTopics/useActiveVisitorTopic.ts
@@ -0,0 +1,28 @@
+import type { ShareVisitorTopicGroup } from '@lobechat/types';
+import { useMemo } from 'react';
+
+import { useActiveLocation } from '@/hooks/useActiveLocation';
+
+const VISITOR_TOPIC_PATH = /\/visitor\/([^/]+)\/?$/;
+
+/**
+ * The visitor topic open beside the sidebar, and the visitor it belongs to.
+ *
+ * Reads the active tab's URL, not the router the sidebar renders in: on desktop the sidebar sits
+ * outside the tab's router, where `useLocation` never sees `/visitor/:topicId` and no row would
+ * show as selected.
+ */
+export const useActiveVisitorTopic = (groups: ShareVisitorTopicGroup[]) => {
+ const { pathname } = useActiveLocation();
+ const activeTopicId = VISITOR_TOPIC_PATH.exec(pathname)?.[1];
+ const activeVisitorId = useMemo(
+ () =>
+ activeTopicId
+ ? groups.find((group) => group.topics.some((topic) => topic.id === activeTopicId))?.visitor
+ .id
+ : undefined,
+ [activeTopicId, groups],
+ );
+
+ return { activeTopicId, activeVisitorId };
+};