Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

FE: Messages: Add 'copy to clipboard' button for textareas of a message #721

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import React from 'react';
import EditorViewer from 'components/common/EditorViewer/EditorViewer';
import BytesFormatted from 'components/common/BytesFormatted/BytesFormatted';
import ClipboardIcon from 'components/common/Icons/ClipboardIcon';
import { Button } from 'components/common/Button/Button';
import Flexbox from 'components/common/FlexBox/FlexBox';
import { SchemaType, TopicMessageTimestampTypeEnum } from 'generated-sources';
import { formatTimestamp } from 'lib/dateTimeHelpers';
import useDataSaver from 'lib/hooks/useDataSaver';

import * as S from './MessageContent.styled';

Expand Down Expand Up @@ -43,6 +47,10 @@ const MessageContent: React.FC<MessageContentProps> = ({
}
};

const tabContent = activeTabContent() || '';

const { copyToClipboard } = useDataSaver('topic-message', tabContent);

const handleKeyTabClick = (e: React.MouseEvent) => {
e.preventDefault();
setActiveTab('key');
Expand All @@ -69,30 +77,44 @@ const MessageContent: React.FC<MessageContentProps> = ({
<S.Section>
<S.ContentBox>
<S.Tabs>
<S.Tab
type="button"
$active={activeTab === 'key'}
onClick={handleKeyTabClick}
>
Key
</S.Tab>
<S.Tab
$active={activeTab === 'content'}
type="button"
onClick={handleContentTabClick}
>
Value
</S.Tab>
<S.Tab
$active={activeTab === 'headers'}
type="button"
onClick={handleHeadersTabClick}
>
Headers
</S.Tab>
<Flexbox justifyContent="space-between">
<Flexbox>
<S.Tab
type="button"
$active={activeTab === 'key'}
onClick={handleKeyTabClick}
>
Key
</S.Tab>
<S.Tab
$active={activeTab === 'content'}
type="button"
onClick={handleContentTabClick}
>
Value
</S.Tab>
<S.Tab
$active={activeTab === 'headers'}
type="button"
onClick={handleHeadersTabClick}
>
Headers
</S.Tab>
</Flexbox>
<Flexbox>
<Button
type="button"
buttonSize="M"
buttonType="text"
onClick={copyToClipboard}
>
<ClipboardIcon />
</Button>
</Flexbox>
</Flexbox>
</S.Tabs>
<EditorViewer
data={activeTabContent() || ''}
data={tabContent}
maxLines={28}
schemaType={contentType}
/>
Expand Down
25 changes: 25 additions & 0 deletions frontend/src/components/common/Icons/ClipboardIcon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react';
import styled from 'styled-components';

const ClipboardIcon: React.FC<{ className?: string }> = ({ className }) => (
<svg
width="24"
height="24"
className={className}
viewBox="0 0 16 16"
xmlns="http://www.w3.org/2000/svg"
>
<path
fillRule="evenodd"
clipRule="evenodd"
d="M4 4l1-1h5.414L14 6.586V14l-1 1H5l-1-1V4zm9 3l-3-3H5v10h8V7z"
/>
<path
fillRule="evenodd"
clipRule="evenodd"
d="M3 1L2 2v10l1 1V2h6.414l-1-1H3z"
/>
</svg>
);

export default styled(ClipboardIcon)``;
2 changes: 1 addition & 1 deletion frontend/src/lib/hooks/__tests__/useDataSaver.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('useDataSaver hook', () => {
};
render(<HookWrapper />);
expect(navigator.clipboard.writeText).toHaveBeenCalledWith(
JSON.stringify(content)
JSON.stringify(content, null, '\t')
);
});

Expand Down
4 changes: 3 additions & 1 deletion frontend/src/lib/hooks/useDataSaver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ const useDataSaver = (
const copyToClipboard = () => {
if (navigator.clipboard) {
const str =
typeof data === 'string' ? String(data) : JSON.stringify(data);
typeof data === 'string'
? String(data)
: JSON.stringify(data, null, '\t');
navigator.clipboard.writeText(str);
showSuccessAlert({
id: subject,
Expand Down
Loading