Skip to content

Commit

Permalink
fix(ui): turn "copy as fetch" into text button (#32858)
Browse files Browse the repository at this point in the history
Turns the "copy as fetch" text with a copy button into a text button, as
discussed in the meeting.


https://github.com/user-attachments/assets/01f72f0b-e3f2-440e-a75d-33385aabeec4
  • Loading branch information
Skn0tt committed Sep 30, 2024
1 parent 4120732 commit aa31467
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
22 changes: 22 additions & 0 deletions packages/trace-viewer/src/ui/copyToClipboard.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Copyright (c) Microsoft Corporation.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

.copy-to-clipboard-text-button {
background-color: var(--vscode-editor-inactiveSelectionBackground);
border: none;
padding: 4px 12px;
cursor: pointer;
}
15 changes: 15 additions & 0 deletions packages/trace-viewer/src/ui/copyToClipboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import * as React from 'react';
import { ToolbarButton } from '@web/components/toolbarButton';
import './copyToClipboard.css';

export const CopyToClipboard: React.FunctionComponent<{
value: string | (() => Promise<string>),
Expand All @@ -34,8 +35,22 @@ export const CopyToClipboard: React.FunctionComponent<{
}, () => {
setIcon('close');
});
}, () => {
setIcon('close');
});

}, [value]);
return <ToolbarButton title={description ? description : 'Copy'} icon={icon} onClick={handleCopy}/>;
};

export const CopyToClipboardTextButton: React.FunctionComponent<{
value: string | (() => Promise<string>),
description: string,
}> = ({ value, description }) => {
const handleCopy = React.useCallback(async () => {
const valueToCopy = typeof value === 'function' ? await value() : value;
await navigator.clipboard.writeText(valueToCopy);
}, [value]);

return <ToolbarButton title={description} onClick={handleCopy} className='copy-to-clipboard-text-button'>{description}</ToolbarButton>;
};
7 changes: 2 additions & 5 deletions packages/trace-viewer/src/ui/networkResourceDetails.css
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,8 @@

.network-request-details-copy {
display: flex;
margin-left: 10px;
}

.network-request-details-copy button {
border-radius: 4px
margin: 8px 10px;
gap: 8px;
}

.network-font-preview {
Expand Down
11 changes: 5 additions & 6 deletions packages/trace-viewer/src/ui/networkResourceDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { TabbedPane } from '@web/components/tabbedPane';
import { CodeMirrorWrapper } from '@web/components/codeMirrorWrapper';
import { ToolbarButton } from '@web/components/toolbarButton';
import { generateCurlCommand, generateFetchCall } from '../third_party/devtools';
import { CopyToClipboard } from './copyToClipboard';
import { CopyToClipboardTextButton } from './copyToClipboard';

export const NetworkResourceDetails: React.FunctionComponent<{
resource: ResourceSnapshot;
Expand Down Expand Up @@ -92,13 +92,12 @@ const RequestTab: React.FunctionComponent<{
</> : null}
<div className='network-request-details-header'>Request Headers</div>
<div className='network-request-details-headers'>{resource.request.headers.map(pair => `${pair.name}: ${pair.value}`).join('\n')}</div>
<div className='network-request-details-header'>Copy Request</div>
<div className='network-request-details-copy'>
As cURL: <CopyToClipboard description='Copy as cURL' value={() => generateCurlCommand(resource)}/>
</div>

<div className='network-request-details-copy'>
As Fetch: <CopyToClipboard description='Copy as Fetch' value={() => generateFetchCall(resource)}/>
<CopyToClipboardTextButton description='Copy as cURL' value={() => generateCurlCommand(resource)} />
<CopyToClipboardTextButton description='Copy as Fetch' value={() => generateFetchCall(resource)} />
</div>

{requestBody && <div className='network-request-details-header'>Request Body</div>}
{requestBody && <CodeMirrorWrapper text={requestBody.text} mimeType={requestBody.mimeType} readOnly lineNumbers={true}/>}
</div>;
Expand Down

0 comments on commit aa31467

Please sign in to comment.