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.

✨ feat(channel): copy a thread's link or id from its menu (#22889)

Arvin Xu6077731830eb
diff --git a/locales/en-US/channel.json b/locales/en-US/channel.json
index 0a4a372402f..0eac215d58f 100644
--- a/locales/en-US/channel.json
+++ b/locales/en-US/channel.json
@@ -42,6 +42,10 @@
"conversationEmpty": "Start the conversation",
"copyId": "Copy ID",
"copyIdSuccess": "Channel ID copied",
+ "copyThreadId": "Copy thread ID",
+ "copyThreadIdSuccess": "Thread ID copied",
+ "copyThreadLink": "Copy link",
+ "copyThreadLinkSuccess": "Thread link copied",
"create": "Create channel",
"create.onlyYou": "Only you for now. Create it and invite members whenever you like.",
"date.yesterday": "Yesterday",
diff --git a/locales/zh-CN/channel.json b/locales/zh-CN/channel.json
index bbe8fe4df8a..eaa77b90936 100644
--- a/locales/zh-CN/channel.json
+++ b/locales/zh-CN/channel.json
@@ -42,6 +42,10 @@
"conversationEmpty": "开始一起讨论吧",
"copyId": "复制 ID",
"copyIdSuccess": "已复制频道 ID",
+ "copyThreadId": "复制线程 ID",
+ "copyThreadIdSuccess": "已复制线程 ID",
+ "copyThreadLink": "复制链接",
+ "copyThreadLinkSuccess": "已复制线程链接",
"create": "创建频道",
"create.onlyYou": "目前只有你。可以先创建,之后随时邀请成员。",
"date.yesterday": "昨天",
diff --git a/packages/locales/src/default/channel.ts b/packages/locales/src/default/channel.ts
index 604b8dcb136..d52391c1657 100644
--- a/packages/locales/src/default/channel.ts
+++ b/packages/locales/src/default/channel.ts
@@ -105,6 +105,10 @@ export default {
'profile.view': 'View profile',
'copyId': 'Copy ID',
'copyIdSuccess': 'Channel ID copied',
+ 'copyThreadId': 'Copy thread ID',
+ 'copyThreadIdSuccess': 'Thread ID copied',
+ 'copyThreadLink': 'Copy link',
+ 'copyThreadLinkSuccess': 'Thread link copied',
'actionFailed': 'Something went wrong. Try again.',
'send': 'Send',
'sendFailed': 'Message was not sent. Your draft is preserved; try again.',
diff --git a/src/features/Channels/useThreadMenu.test.ts b/src/features/Channels/useThreadMenu.test.ts
new file mode 100644
index 00000000000..4966a9dfdad
--- /dev/null
+++ b/src/features/Channels/useThreadMenu.test.ts
@@ -0,0 +1,76 @@
+import type { ChannelListItem, ChannelPublicThread } from '@lobechat/types';
+import { cleanup, renderHook } from '@testing-library/react';
+import { afterEach, describe, expect, it, vi } from 'vitest';
+
+import { useThreadMenu } from './useThreadMenu';
+
+const { copyToClipboard, success, error } = vi.hoisted(() => ({
+ copyToClipboard: vi.fn(),
+ success: vi.fn(),
+ error: vi.fn(),
+}));
+vi.mock('@lobehub/ui', () => ({ copyToClipboard, toast: { error, success } }));
+vi.mock('react-i18next', () => ({ useTranslation: () => ({ t: (key: string) => key }) }));
+vi.mock('@/hooks/useAppOrigin', () => ({ useAppOrigin: () => 'https://app.lobehub.com' }));
+vi.mock('@/business/client/hooks/useActiveWorkspaceSlug', () => ({
+ useActiveWorkspaceSlug: () => 'acme',
+}));
+vi.mock('./useChannelArchive', () => ({
+ useChannelArchive: () => ({ setThreadArchived: vi.fn() }),
+}));
+vi.mock('./useRemoveThread', () => ({ useRemoveThread: () => vi.fn() }));
+
+const channel = {
+ canDelete: false,
+ id: 'chn_abcdefghijkl',
+ projectId: 'prj_1',
+ status: 'active',
+} as ChannelListItem;
+const thread = { id: 'cht_thread1', status: 'active' } as ChannelPublicThread;
+
+const menu = (overrides: Partial<ChannelListItem> = {}) => {
+ const { result } = renderHook(() => useThreadMenu());
+ return result.current({ ...channel, ...overrides }, thread) as any[];
+};
+const item = (items: any[], key: string) => items.find((entry) => entry.key === key);
+
+afterEach(() => {
+ cleanup();
+ vi.clearAllMocks();
+});
+
+describe('useThreadMenu', () => {
+ it('lets a member who cannot manage the thread still copy its link and id', async () => {
+ const items = menu();
+ expect(items.map((entry) => entry.key)).toEqual(['copyThreadLink', 'copyThreadId']);
+
+ await item(items, 'copyThreadLink').onClick();
+ expect(copyToClipboard).toHaveBeenCalledWith(
+ 'https://app.lobehub.com/acme/project/prj_1/channels/chn_abcdefghijkl?thread=cht_thread1&view=page',
+ );
+ expect(success).toHaveBeenCalledWith('copyThreadLinkSuccess');
+
+ await item(items, 'copyThreadId').onClick();
+ expect(copyToClipboard).toHaveBeenLastCalledWith('cht_thread1');
+ expect(success).toHaveBeenLastCalledWith('copyThreadIdSuccess');
+ });
+
+ it('keeps archive and delete after the copy actions for a manager', () => {
+ expect(menu({ canDelete: true }).map((entry) => entry.key ?? entry.type)).toEqual([
+ 'copyThreadLink',
+ 'copyThreadId',
+ 'divider',
+ 'archiveThread',
+ 'divider',
+ 'deleteThread',
+ ]);
+ });
+
+ it('reports a failed copy instead of claiming success', async () => {
+ copyToClipboard.mockRejectedValueOnce(new Error('denied'));
+ vi.spyOn(console, 'error').mockImplementation(() => {});
+ await item(menu(), 'copyThreadId').onClick();
+ expect(success).not.toHaveBeenCalled();
+ expect(error).toHaveBeenCalledWith('actionFailed');
+ });
+});
diff --git a/src/features/Channels/useThreadMenu.ts b/src/features/Channels/useThreadMenu.ts
index 628aee8b88d..1d2a6108abb 100644
--- a/src/features/Channels/useThreadMenu.ts
+++ b/src/features/Channels/useThreadMenu.ts
@@ -1,7 +1,13 @@
+import { CHANNEL_URL } from '@lobechat/const';
import type { ChannelListItem, ChannelPublicThread } from '@lobechat/types';
-import { Archive, ArchiveRestore, Trash } from 'lucide-react';
+import { copyToClipboard, toast } from '@lobehub/ui';
+import { Archive, ArchiveRestore, Copy, Link2, Trash } from 'lucide-react';
import { useTranslation } from 'react-i18next';
+import { useActiveWorkspaceSlug } from '@/business/client/hooks/useActiveWorkspaceSlug';
+import { buildWorkspaceAwarePath } from '@/features/Workspace/workspaceAwarePath';
+import { useAppOrigin } from '@/hooks/useAppOrigin';
+
import { useChannelArchive } from './useChannelArchive';
import { useRemoveThread } from './useRemoveThread';
@@ -10,11 +16,44 @@ export const useThreadMenu = () => {
const { t } = useTranslation('channel');
const removeThread = useRemoveThread();
const { setThreadArchived } = useChannelArchive();
+ const appOrigin = useAppOrigin();
+ const slug = useActiveWorkspaceSlug();
+
+ const copy = async (text: string, success: string) => {
+ try {
+ await copyToClipboard(text);
+ toast.success(success);
+ } catch (error) {
+ console.error('Failed to copy thread', error);
+ toast.error(t('actionFailed'));
+ }
+ };
- // An archived channel is read-only as a whole, its threads included.
- return (channel: ChannelListItem, thread: ChannelPublicThread) =>
- channel.canDelete && channel.status !== 'archived'
+ // Anyone who can see a thread can share it; only managers change it.
+ return (channel: ChannelListItem, thread: ChannelPublicThread) => [
+ {
+ icon: Link2,
+ key: 'copyThreadLink',
+ label: t('copyThreadLink'),
+ onClick: () =>
+ copy(
+ `${appOrigin}${buildWorkspaceAwarePath(
+ CHANNEL_URL(channel.id, thread.id, channel.projectId),
+ slug,
+ )}`,
+ t('copyThreadLinkSuccess'),
+ ),
+ },
+ {
+ icon: Copy,
+ key: 'copyThreadId',
+ label: t('copyThreadId'),
+ onClick: () => copy(thread.id, t('copyThreadIdSuccess')),
+ },
+ // An archived channel is read-only as a whole, its threads included.
+ ...(channel.canDelete && channel.status !== 'archived'
? [
+ { type: 'divider' as const },
thread.status === 'archived'
? {
icon: ArchiveRestore,
@@ -37,5 +76,6 @@ export const useThreadMenu = () => {
onClick: () => removeThread(channel, thread.id),
},
]
- : [];
+ : []),
+ ];
};