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.

💄 style(channel): keep long messages expanded and move collapse to the more menu (#22618)

Arvin Xu7acdda12c586
diff --git a/locales/en-US/channel.json b/locales/en-US/channel.json
index b10200b8cb6..6f96f014905 100644
--- a/locales/en-US/channel.json
+++ b/locales/en-US/channel.json
@@ -118,8 +118,10 @@
"mentionInvite.failed": "Couldn't add {{name}} to the channel. Try again.",
"mentionInvite.failedReason": "Couldn't add {{name}} to the channel: {{reason}}",
"mentionInvite.section": "Not in this channel · mention to add",
+ "message.collapse": "Collapse message",
"message.copied": "Copied",
"message.copy": "Copy text",
+ "message.expand": "Expand message",
"moveToTrash": "Move channel to trash",
"moveToTrashConfirm": "Move “{{title}}” to trash? Replies will stop. You can restore the channel and its history from Trash. Agents and files will be kept.",
"moveToTrashSuccess": "Channel moved to trash",
diff --git a/locales/zh-CN/channel.json b/locales/zh-CN/channel.json
index ff397703e33..c71b04230ab 100644
--- a/locales/zh-CN/channel.json
+++ b/locales/zh-CN/channel.json
@@ -118,8 +118,10 @@
"mentionInvite.failed": "未能将 {{name}} 加入频道,请重试。",
"mentionInvite.failedReason": "未能将 {{name}} 加入频道:{{reason}}",
"mentionInvite.section": "不在频道中 · @ 后将加入",
+ "message.collapse": "收起消息",
"message.copied": "已复制",
"message.copy": "复制文本",
+ "message.expand": "展开消息",
"moveToTrash": "移至回收站",
"moveToTrashConfirm": "将「{{title}}」移至回收站?进行中的回复会停止,你可以在回收站恢复频道和历史消息。Agent 和文件将保留。",
"moveToTrashSuccess": "频道已移至回收站",
diff --git a/packages/locales/src/default/channel.ts b/packages/locales/src/default/channel.ts
index 037f5abee81..828cb01200e 100644
--- a/packages/locales/src/default/channel.ts
+++ b/packages/locales/src/default/channel.ts
@@ -156,7 +156,9 @@ export default {
'activity.two': '{{names}} are replying…',
'activity.typing': 'typing…',
'message.copy': 'Copy text',
+ 'message.collapse': 'Collapse message',
'message.copied': 'Copied',
+ 'message.expand': 'Expand message',
'topic.open': 'Open topic',
'topic.title': 'Topic',
'topic.titleNamed': "{{name}}'s topic",
diff --git a/src/components/CollapsibleContent/index.tsx b/src/components/CollapsibleContent/index.tsx
index 48edb573706..c8637544ea6 100644
--- a/src/components/CollapsibleContent/index.tsx
+++ b/src/components/CollapsibleContent/index.tsx
@@ -50,6 +50,8 @@ interface CollapsibleContentProps {
children: ReactNode;
/** Controlled collapsed state. Omit to let the component own it. Pass it when something outside the toggle expands the content — an editor gaining focus, for instance. */
collapsed?: boolean;
+ /** Keeps the inline toggle for expanding only, for hosts that offer collapsing from their own menu. */
+ hideCollapseToggle?: boolean;
/** Upper bound for the collapsed height; the effective clamp also honours the viewport. */
maxHeight?: number;
/** Fires on toggle with the next collapsed state. Required for the controlled mode; also useful as a plain notification in the uncontrolled one. */
@@ -78,6 +80,7 @@ const CollapsibleContent = memo<CollapsibleContentProps>(
({
children,
collapsed: collapsedProp,
+ hideCollapseToggle = false,
maxHeight: maxHeightLimit = DEFAULT_MAX_HEIGHT,
onCollapsedChange,
onOverflowChange,
@@ -134,7 +137,7 @@ const CollapsibleContent = memo<CollapsibleContentProps>(
>
{children}
</div>
- {shouldCollapse && (
+ {shouldCollapse && (collapsed || !hideCollapseToggle) && (
<div {...stylex.props(styles.toggleWrapper)}>
<button
aria-expanded={!collapsed}
diff --git a/src/features/Channels/ChannelMessage.test.tsx b/src/features/Channels/ChannelMessage.test.tsx
index 2362a00e5a7..0c8ba4ee43e 100644
--- a/src/features/Channels/ChannelMessage.test.tsx
+++ b/src/features/Channels/ChannelMessage.test.tsx
@@ -2,8 +2,8 @@
* @vitest-environment happy-dom
*/
import type { ChannelDetail, ChannelPublicMessage } from '@lobechat/types';
-import { render, screen } from '@testing-library/react';
-import { createElement, type ReactNode } from 'react';
+import { fireEvent, render, screen } from '@testing-library/react';
+import { act, createElement, type ReactNode } from 'react';
import { describe, expect, it, vi } from 'vitest';
import EntityLinkElement from '@/features/EntityLink/element';
@@ -53,6 +53,24 @@ vi.mock('@/features/AgentProfileCard/AgentProfilePopup', () => ({
children,
),
}));
+vi.mock('@lobehub/ui', async (importOriginal) => ({
+ ...(await importOriginal<Record<string, unknown>>()),
+ DropdownMenu: ({ items }: { items: { key: string; label: string; onClick: () => void }[] }) =>
+ createElement(
+ 'div',
+ {},
+ items.map((item) =>
+ createElement('button', { key: item.key, onClick: item.onClick }, item.label),
+ ),
+ ),
+}));
+const collapsibleProps = vi.hoisted(() => ({ current: undefined as any }));
+vi.mock('@/components/CollapsibleContent', () => ({
+ default: (props: { children: ReactNode }) => {
+ collapsibleProps.current = props;
+ return props.children;
+ },
+}));
const markdownProps = vi.hoisted(() => ({ current: undefined as any }));
vi.mock('@/features/Conversation/Markdown', () => ({
default: (props: unknown) => {
@@ -139,3 +157,24 @@ describe('ChannelMessage links', () => {
expect(markdownProps.current.rehypePlugins).toContain(EntityLinkElement.rehypePlugin);
});
});
+
+describe('ChannelMessage collapse', () => {
+ const longMessage = {
+ ...message({ avatar: null, id: 'agt_1', name: 'Sanjo', type: 'agent' }),
+ content: 'A long reply',
+ };
+
+ it('shows a long message in full and only offers collapsing from the more menu', () => {
+ render(<ChannelMessage detail={detail} message={longMessage} />);
+
+ expect(collapsibleProps.current.collapsed).toBe(false);
+ expect(collapsibleProps.current.hideCollapseToggle).toBe(true);
+ expect(screen.queryByText('message.collapse')).toBeNull();
+
+ act(() => collapsibleProps.current.onOverflowChange(true));
+ fireEvent.click(screen.getByText('message.collapse'));
+
+ expect(collapsibleProps.current.collapsed).toBe(true);
+ expect(screen.getByText('message.expand')).toBeTruthy();
+ });
+});
diff --git a/src/features/Channels/ChannelMessage.tsx b/src/features/Channels/ChannelMessage.tsx
index 71f8c23fc74..3b3ff19d512 100644
--- a/src/features/Channels/ChannelMessage.tsx
+++ b/src/features/Channels/ChannelMessage.tsx
@@ -11,9 +11,17 @@ import {
toast,
} from '@lobehub/ui';
import * as stylex from '@stylexjs/stylex';
-import { Copy, GitBranch, MessagesSquare, MoreHorizontal, Reply } from 'lucide-react';
+import {
+ ChevronsDownUp,
+ ChevronsUpDown,
+ Copy,
+ GitBranch,
+ MessagesSquare,
+ MoreHorizontal,
+ Reply,
+} from 'lucide-react';
import type { ReactNode } from 'react';
-import { useMemo } from 'react';
+import { useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import CollapsibleContent from '@/components/CollapsibleContent';
@@ -131,6 +139,11 @@ export const ChannelMessage = ({
? detail.members.find((member) => member.agentId === agentAuthorId)
: undefined;
const sourceTopicId = message.sourceTopicId;
+ // A direct conversation is a chat with one Agent; folding its answers only adds a click.
+ const collapsible = !detail.channel.agentId;
+ // Long messages stay open by default; folding one is an explicit choice from "more".
+ const [collapsed, setCollapsed] = useState(false);
+ const [overflowing, setOverflowing] = useState(false);
const moreItems = [
...(agentAuthorId && sourceTopicId && onOpenTopic
? [
@@ -161,10 +174,18 @@ export const ChannelMessage = ({
},
]
: []),
+ ...(collapsible && overflowing
+ ? [
+ {
+ icon: collapsed ? ChevronsUpDown : ChevronsDownUp,
+ key: 'toggleCollapse',
+ label: t(collapsed ? 'message.expand' : 'message.collapse'),
+ onClick: () => setCollapsed(!collapsed),
+ },
+ ]
+ : []),
];
const hasToolbar = !!onReply || canBranch || moreItems.length > 0;
- // A direct conversation is a chat with one Agent; folding its answers only adds a click.
- const collapsible = !detail.channel.agentId;
const mentionNames = detail.members
.filter((member) => message.mentions.includes(member.id))
.map((member) => memberName(member, ''))
@@ -266,7 +287,14 @@ export const ChannelMessage = ({
)}
{message.content &&
(collapsible ? (
- <CollapsibleContent key={message.id} maxHeight={160}>
+ <CollapsibleContent
+ hideCollapseToggle
+ collapsed={collapsed}
+ key={message.id}
+ maxHeight={160}
+ onCollapsedChange={setCollapsed}
+ onOverflowChange={setOverflowing}
+ >
<MessageText content={message.content} names={mentionNames} />
</CollapsibleContent>
) : (