mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-24 18:50:56 +08:00
fix: MessageMenubar copy uses latest content (#6435)
* Fix: MessageMenubar copy uses latest content The 'Copy' button in MessageMenubar was previously using memoized content derived from component props. This could lead to copying stale content if you edited a message (e.g., a code block), saved it, and then immediately clicked 'Copy', because the asynchronous Redux store update might not have completed and propagated to the props yet. This commit modifies the onCopy function in MessageMenubar.tsx to fetch the latest version of the message directly from the Redux store (store.getState().messages.entities[message.id]) at the moment the copy action is performed. This ensures that the most up-to-date content is always copied, resolving the stale content issue. * Chore: Remove unnecessary comments from MessageMenubar Removes a few explanatory comments from the onCopy function in MessageMenubar.tsx that were deemed unnecessary, to keep the code cleaner. The comments originated from an example provided in a previous description. The core logic of the function remains unchanged. --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
This commit is contained in:
parent
cd0329e0cf
commit
d5f6304bbc
@ -8,7 +8,7 @@ import { useMessageOperations, useTopicLoading } from '@renderer/hooks/useMessag
|
||||
import { EVENT_NAMES, EventEmitter } from '@renderer/services/EventService'
|
||||
import { getMessageTitle } from '@renderer/services/MessagesService'
|
||||
import { translateText } from '@renderer/services/TranslateService'
|
||||
import { RootState } from '@renderer/store'
|
||||
import store, { RootState } from '@renderer/store'
|
||||
import { messageBlocksSelectors } from '@renderer/store/messageBlock'
|
||||
import type { Model } from '@renderer/types'
|
||||
import type { Assistant, Topic } from '@renderer/types'
|
||||
@ -90,13 +90,24 @@ const MessageMenubar: FC<Props> = (props) => {
|
||||
const onCopy = useCallback(
|
||||
(e: React.MouseEvent) => {
|
||||
e.stopPropagation()
|
||||
navigator.clipboard.writeText(removeTrailingDoubleSpaces(mainTextContent.trimStart()))
|
||||
|
||||
const currentMessageId = message.id // from props
|
||||
const latestMessageEntity = store.getState().messages.entities[currentMessageId]
|
||||
|
||||
let contentToCopy = ''
|
||||
if (latestMessageEntity) {
|
||||
contentToCopy = getMainTextContent(latestMessageEntity as Message)
|
||||
} else {
|
||||
contentToCopy = getMainTextContent(message)
|
||||
}
|
||||
|
||||
navigator.clipboard.writeText(removeTrailingDoubleSpaces(contentToCopy.trimStart()))
|
||||
|
||||
window.message.success({ content: t('message.copied'), key: 'copy-message' })
|
||||
setCopied(true)
|
||||
setTimeout(() => setCopied(false), 2000)
|
||||
},
|
||||
[mainTextContent, t]
|
||||
[message, t] // message is needed for message.id and as a fallback. t is for translation.
|
||||
)
|
||||
|
||||
const onNewBranch = useCallback(async () => {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user