Skip to content

Commit

Permalink
fix(webview): show copy button in web clients (#7310)
Browse files Browse the repository at this point in the history
CLOSE
https://linear.app/sourcegraph/issue/CODY-5193/cody-web-copy-button-is-missing-for-code-blocks

This commit refactors the UI for code blocks in chat messages to improve
the placement of action buttons (copy, apply, insert) and fixes the
issue where copy button is not showing up on web.

The cause of the issue is due to the logic where actionsContainer will
only be displayed when previewContainer is available, a regression from
#7217.

The fix is to always append available actionsContainer, but also insert
the previewContainer before that when available.

Key changes:

- Modified CSS styles in `ChatMessageContent.module.css` to adjust
padding, borders, and background colors for code blocks and buttons.
- Updated `ChatMessageContent.tsx` to insert the preview container
(containing file info) before the code block and the actions container
after the code block.
- Updated `create-buttons.ts` to use the default `buttonsContainer`
style instead of `buttonsContainerTransparent` for both preview and
action button containers.

## Test plan

<!-- Required. See
https://docs-legacy.sourcegraph.com/dev/background-information/testing_principles.
-->

Run Cody Web and confirmed the copy button is now showing up:

<img width="2056" alt="image"
src="https://github.com/user-attachments/assets/a3b0b21c-33c4-4c72-b62f-e858c8309b17"
/>

Added storybook to cover this component:

<img width="1714" alt="image"
src="https://github.com/user-attachments/assets/92c35899-ae84-4006-8f09-dc8272d53db1"
/>
  • Loading branch information
abeatrix authored Mar 4, 2025
1 parent d669832 commit 0b00474
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,13 @@
display: flex;
align-items: center;
justify-content: space-between;
padding: 8px;
border: 1px solid var(--vscode-editor-foreground);
padding: calc(var(--spacing) * 0.5);
border-radius: 4px;
background: transparent;
border-top: none;
}

.buttons-container-transparent {
composes: buttons-container;
border: none;
padding: 4px 0;
}

.leftInfo {
.left-info {
display: flex;
align-items: center;
gap: 8px;
Expand All @@ -29,6 +23,7 @@

.stats {
color: var(--vscode-descriptionForeground);
padding: calc(var(--spacing) * 0.5);
}

.buttons {
Expand All @@ -38,26 +33,28 @@
justify-content: space-between;
}

.actionButtons {
.action-buttons {
display: flex;
gap: 0.25rem;
}

.button {
display: flex;
align-items: center;
padding: 3px;
height: 20px;
background-color: transparent;
cursor: pointer;
background: var(--button-icon-background);
border-radius: var(--button-icon-corner-radius);
color: var(--foreground);
}

.button:hover {
background: var(--button-icon-hover-background);
outline: 1px dotted var(--contrast-active-border);
outline-offset: -1px;
}

.button:not(:first-child) {
margin-left: 0.25rem;
}
Expand Down Expand Up @@ -94,6 +91,7 @@
border-radius: var(--button-icon-corner-radius);
color: var(--foreground);
}

.copy-button:hover,
.insert-button:hover {
background: var(--button-icon-hover-background);
Expand All @@ -116,7 +114,7 @@
margin-right: 0.25rem;
}

.fileNameContainer {
.file-name-container {
font-size: 12px;
color: var(--vscode-descriptionForeground);
display: flex;
Expand All @@ -129,7 +127,6 @@
margin-left: auto;
}


.attribution-icon-unavailable {
color: var(--hl-orange);
}
Expand Down Expand Up @@ -188,7 +185,6 @@
padding: calc(var(--spacing) * 0.5);
overflow-x: auto;
border-bottom: none;
margin: 1rem 0;
border-radius: 3px;
border: 1px solid var(--vscode-input-background);
}
Expand All @@ -207,6 +203,7 @@
color: var(--code-foreground);
margin-bottom: 0;
}

body[data-vscode-theme-kind='vscode-light'] .content pre,
body[data-vscode-theme-kind='vscode-light'] .content pre > code {
/* Our syntax highlighter emits colors intended for dark backgrounds only. */
Expand Down Expand Up @@ -258,8 +255,3 @@ body[data-vscode-theme-kind='vscode-light'] .content pre > code {
padding-inline-start: 2rem;
list-style: revert;
}

.file-name-container {
color: var(--vscode-descriptionForeground);
margin-left: auto;
}
169 changes: 169 additions & 0 deletions vscode/webviews/chat/ChatMessageContent/ChatMessageContent.story.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
import { ps } from '@sourcegraph/cody-shared'
import type { Meta, StoryObj } from '@storybook/react'
import { VSCodeWebview } from '../../storybook/VSCodeStoryDecorator'
import { ChatMessageContent } from './ChatMessageContent'

const meta: Meta<typeof ChatMessageContent> = {
title: 'chat/ChatMessageContent',
component: ChatMessageContent,

args: {
displayMarkdown: '# Hello\nThis is a test message',
isMessageLoading: false,
humanMessage: null,
copyButtonOnSubmit: () => console.log('Copy button clicked'),
insertButtonOnSubmit: undefined,
smartApplyEnabled: false,
smartApply: undefined,
guardrails: undefined,
},

decorators: [VSCodeWebview],
}

export default meta

export const Default: StoryObj<typeof meta> = {}

export const WithCodeBlock: StoryObj<typeof meta> = {
args: {
displayMarkdown: 'Code Example\n```javascript\nconsole.log("Hello world");\n```',
},
}

export const WithCodeBlockNoSmartApply: StoryObj<typeof meta> = {
args: {
displayMarkdown: '## Code Example\n```javascript\nconsole.log("Hello world");\n```',
smartApplyEnabled: false,
copyButtonOnSubmit: () => console.log('Copy button clicked'),
insertButtonOnSubmit: () => console.log('Insert button clicked'),
},
name: 'With Code Block - No Smart Apply (Web Client)',
}

export const WithCodeBlockWithSmartApply: StoryObj<typeof meta> = {
args: {
displayMarkdown: '### Code Example\n```javascript\nconsole.log("Hello world");\n```',
smartApplyEnabled: true,
copyButtonOnSubmit: () => console.log('Copy button clicked'),
insertButtonOnSubmit: () => console.log('Insert button clicked'),
smartApply: {
onSubmit: () => console.log('Smart apply submitted'),
onAccept: () => console.log('Smart apply accepted'),
onReject: () => console.log('Smart apply rejected'),
},
},
name: 'With Code Block - With Smart Apply (VS Code)',
}

export const WithMultipleCodeBlocks: StoryObj<typeof meta> = {
args: {
displayMarkdown: `# Multiple Code Blocks
Here's the first code block:
\`\`\`javascript
function hello() {
console.log("Hello world");
}
\`\`\`
And here's the second one:
\`\`\`python
def hello():
print("Hello world")
\`\`\``,
copyButtonOnSubmit: () => console.log('Copy button clicked'),
insertButtonOnSubmit: () => console.log('Insert button clicked'),
},
}

export const SmartApplyPending: StoryObj<typeof meta> = {
args: {
displayMarkdown: '# Working Example\n```javascript\nconsole.log("Hello world");\n```',
smartApplyEnabled: true,
copyButtonOnSubmit: () => console.log('Copy button clicked'),
insertButtonOnSubmit: () => console.log('Insert button clicked'),
smartApply: {
onSubmit: () => console.log('Smart apply submitted'),
onAccept: () => console.log('Smart apply accepted'),
onReject: () => console.log('Smart apply rejected'),
},
humanMessage: {
text: ps`Write a hello world example`,
intent: 'chat',
hasInitialContext: {
repositories: false,
files: false,
},
hasExplicitMentions: false,
rerunWithDifferentContext: () => console.log('Rerun with different context'),
appendAtMention: () => console.log('Append at mention'),
},
},
name: 'Smart Apply - Pending State',
}

export const SmartApplyWorking: StoryObj<typeof meta> = {
render: args => {
// Return the component with state and callbacks set up
return <ChatMessageContent {...args} />
},
args: {
displayMarkdown: '# Working Example\n```javascript\nconsole.log("Hello world");\n```',
smartApplyEnabled: true,
copyButtonOnSubmit: () => console.log('Copy button clicked'),
insertButtonOnSubmit: () => console.log('Insert button clicked'),
smartApply: {
onSubmit: () => console.log('Smart apply submitted'),
onAccept: () => console.log('Smart apply accepted'),
onReject: () => console.log('Smart apply rejected'),
},
humanMessage: {
text: ps`Write a hello world example`,
intent: 'chat',
hasInitialContext: {
repositories: false,
files: false,
},
hasExplicitMentions: false,
rerunWithDifferentContext: () => console.log('Rerun with different context'),
appendAtMention: () => console.log('Append at mention'),
},
},
name: 'Smart Apply - Working State',
}

export const EditIntent: StoryObj<typeof meta> = {
args: {
displayMarkdown:
'# Edit Intent Example\n```javascript:hello.js\n+ console.log("Hello world");\n+ \n- // console.log("Hello world");\n```',
copyButtonOnSubmit: () => console.log('Copy button clicked'),
insertButtonOnSubmit: () => console.log('Insert button clicked'),
smartApplyEnabled: true,
smartApply: undefined,
humanMessage: {
text: ps`Write a hello world example`,
intent: 'edit',
hasInitialContext: {
repositories: false,
files: false,
},
hasExplicitMentions: false,
rerunWithDifferentContext: () => console.log('Rerun with different context'),
appendAtMention: () => console.log('Append at mention'),
},
},
name: 'Edit Intent with Preview',
}

export const Loading: StoryObj<typeof meta> = {
args: {
displayMarkdown: '# Loading...',
isMessageLoading: true,
},
}

export const WithThinkContent: StoryObj<typeof meta> = {
args: {
displayMarkdown: '<think>\nAnalyzing the problem...\n</think>\nHere is the solution.',
},
}
13 changes: 6 additions & 7 deletions vscode/webviews/chat/ChatMessageContent/ChatMessageContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,8 @@ export const ChatMessageContent: React.FunctionComponent<ChatMessageContentProps
// This allows us to intelligently apply code to the suitable file.
const codeElement = preElement.querySelectorAll('code')?.[0]
const fileName = codeElement?.getAttribute('data-file-path') || undefined
// Check if the code element has either 'language-bash' or 'language-shell' class
const isShellCommand =
codeElement?.classList.contains('language-bash') ||
codeElement?.classList.contains('language-shell')
// Check if the code element has either 'language-bash' class
const isShellCommand = codeElement?.classList.contains('language-bash')
const codeBlockName = isShellCommand ? 'command' : fileName

let buttons: HTMLElement
Expand Down Expand Up @@ -193,11 +191,12 @@ export const ChatMessageContent: React.FunctionComponent<ChatMessageContentProps
// Get the preview container and actions container
const previewContainer = buttons.querySelector(`[data-container-type="preview"]`)
const actionsContainer = buttons.querySelector(`[data-container-type="actions"]`)
if (!previewContainer || !actionsContainer) return
if (!actionsContainer) return

// Insert the preview container right before this code block
parent.insertBefore(previewContainer, preElement)

if (previewContainer) {
parent.insertBefore(previewContainer, preElement)
}
// Add the actions container right after this code block
if (preElement.nextSibling) {
parent.insertBefore(actionsContainer, preElement.nextSibling)
Expand Down
Loading

0 comments on commit 0b00474

Please sign in to comment.