diff --git a/client/src/Providers/MessagesHostContext.tsx b/client/src/Providers/MessagesHostContext.tsx index b99eaf4767..9b484230cc 100644 --- a/client/src/Providers/MessagesHostContext.tsx +++ b/client/src/Providers/MessagesHostContext.tsx @@ -4,12 +4,20 @@ import { useRecoilState } from 'recoil'; import { setDefaultMessagesHost } from '@librechat/chat/react'; import type { ForkEvents, + MessageCopy, MessagesHost, + MessageCopyInput, ForkPreferences, ForkConversation, MessagesScrolling, } from '@librechat/chat/react'; import type { TMessage } from 'librechat-data-provider'; +import { + hasCopyableText, + useNavigateToConvo, + getMessageClipboardSource, + useCopyMessageToClipboard, +} from '~/hooks'; import useConversationSeen from '~/hooks/Conversations/useConversationSeen'; import useMessageScrolling from '~/hooks/Messages/useMessageScrolling'; import MessageIcon from '~/components/Chat/Messages/MessageIcon'; @@ -18,7 +26,6 @@ import { useScreenshot } from '~/hooks/ScreenshotContext'; import { useForkConvoMutation } from '~/data-provider'; import useClockFormat from '~/hooks/useClockFormat'; import { autoScrollAtom } from '~/store/autoScroll'; -import { useNavigateToConvo } from '~/hooks'; import store from '~/store'; function useAppForkPreferences(): ForkPreferences { @@ -65,16 +72,30 @@ function useScrolling(messagesTree: TMessage[] | null | undefined): MessagesScro return { ...scrolling, conversationId: conversation?.conversationId }; } +function useAppMessageCopy({ message, searchResults, variant }: MessageCopyInput): MessageCopy { + const clipboardSource = useMemo(() => getMessageClipboardSource(message), [message]); + const copyToClipboard = useCopyMessageToClipboard({ + ...clipboardSource, + searchResults, + variant: clipboardSource.variant ?? variant, + }); + const canCopy = useMemo( + () => hasCopyableText({ ...clipboardSource, searchResults }), + [clipboardSource, searchResults], + ); + return { copyToClipboard, canCopy }; +} + /** What the message list reads from the app: the viewer's clock format and fork defaults, the - * fork request, its scrolling, screenshot and seen reporting, and the row pieces that still live - * here. Registered as the default when this module loads, so rows read it on every route, the - * shared view included. */ + * fork request, the rich-text copy, its scrolling, screenshot and seen reporting, and the row + * pieces that still live here. Registered as the default when this module loads, so rows read it + * on every route, the shared view included. */ export const appMessagesHost: MessagesHost = { useHour12: useClockFormat, renderFiles: (message) => , useForkPreferences: useAppForkPreferences, useForkConversation: useAppForkConversation, - + useMessageCopy: useAppMessageCopy, useScrolling, useScreenshotTarget: () => useScreenshot().screenshotTargetRef, useAutoScroll: () => useAtomValue(autoScrollAtom), diff --git a/client/src/components/Chat/Messages/HoverButtons.tsx b/client/src/components/Chat/Messages/HoverButtons.tsx index 75f2984f46..92a660a58c 100644 --- a/client/src/components/Chat/Messages/HoverButtons.tsx +++ b/client/src/components/Chat/Messages/HoverButtons.tsx @@ -3,8 +3,8 @@ import { Copy, Check } from 'lucide'; import { useAtomValue } from 'jotai'; import { useRecoilState } from 'recoil'; import { hasEditablePart } from '@librechat/chat'; -import { revealedQueuedTurnFamily } from '@librechat/chat/react'; import { hoverButtonClasses, Fork, Feedback } from '@librechat/chat/components'; +import { useGenerationsByLatest, revealedQueuedTurnFamily } from '@librechat/chat/react'; import { getSpeechText, findMessageById, isUserInitiatedCompaction } from 'librechat-data-provider'; import { Button, @@ -16,8 +16,8 @@ import { } from '@librechat/client'; import type { TConversation, TMessage, TFeedback } from 'librechat-data-provider'; import { useMessagesIsSubmitting, useOptionalMessagesOperations } from '~/Providers'; -import { useGenerationsByLatest, useLocalize } from '~/hooks'; import MessageAudio from './MessageAudio'; +import { useLocalize } from '~/hooks'; import { cn } from '~/utils'; import store from '~/store'; diff --git a/client/src/components/Chat/Messages/SearchMessage.tsx b/client/src/components/Chat/Messages/SearchMessage.tsx index 324b116765..9f0fb32e68 100644 --- a/client/src/components/Chat/Messages/SearchMessage.tsx +++ b/client/src/components/Chat/Messages/SearchMessage.tsx @@ -5,11 +5,11 @@ import { MessageRow, AuthorHeader, getHeaderModelName, + MinimalHoverButtons, getMessageRowWidthClass, } from '@librechat/chat/components'; import type { TMessage } from 'librechat-data-provider'; import type { TMessageProps, TMessageIcon } from '~/common'; -import MinimalHoverButtons from '~/components/Chat/Messages/MinimalHoverButtons'; import { getHeaderPrefixForScreenReader, getMessageAriaLabel } from '~/utils'; import SearchContent, { rendersMarkdownLite } from './Content/SearchContent'; import Icon from '~/components/Chat/Messages/MessageIcon'; diff --git a/client/src/components/Chat/Subagents/SubagentConversation.test.tsx b/client/src/components/Chat/Subagents/SubagentConversation.test.tsx index 2f867e14e2..d7f411bf7d 100644 --- a/client/src/components/Chat/Subagents/SubagentConversation.test.tsx +++ b/client/src/components/Chat/Subagents/SubagentConversation.test.tsx @@ -48,9 +48,9 @@ jest.mock('~/Providers/MessagePartsHostContext', () => { }; }); -jest.mock('~/components/Chat/Messages/MinimalHoverButtons', () => ({ - __esModule: true, - default: ({ message }: { message: TMessage }) => { +jest.mock('@librechat/chat/components', () => ({ + ...jest.requireActual('@librechat/chat/components'), + MinimalHoverButtons: ({ message }: { message: TMessage }) => { mockCopyMessage(message); return