Skip to content

Commit c8bbb69

Browse files
authored
feat(ui): Switch from copy-text-to-clipboard to navigator (#45438)
1 parent 512791c commit c8bbb69

File tree

7 files changed

+53
-38
lines changed

7 files changed

+53
-38
lines changed

jest.config.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ if (
203203
* node_modules, but some packages which use ES6 syntax only NEED to be
204204
* transformed.
205205
*/
206-
const ESM_NODE_MODULES = ['copy-text-to-clipboard'];
206+
const ESM_NODE_MODULES = [];
207207

208208
const config: Config.InitialOptions = {
209209
verbose: false,
@@ -247,7 +247,11 @@ const config: Config.InitialOptions = {
247247
'^.+\\.tsx?$': ['babel-jest', babelConfig as any],
248248
'^.+\\.pegjs?$': '<rootDir>/tests/js/jest-pegjs-transform.js',
249249
},
250-
transformIgnorePatterns: [`/node_modules/(?!${ESM_NODE_MODULES.join('|')})`],
250+
transformIgnorePatterns: [
251+
ESM_NODE_MODULES.length
252+
? `/node_modules/(?!${ESM_NODE_MODULES.join('|')})`
253+
: '/node_modules/',
254+
],
251255

252256
moduleFileExtensions: ['js', 'ts', 'jsx', 'tsx'],
253257
globals: {},

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@
9090
"classnames": "2.3.1",
9191
"color": "^4.2.3",
9292
"compression-webpack-plugin": "10.0.0",
93-
"copy-text-to-clipboard": "3.0.1",
9493
"copy-webpack-plugin": "^11.0.0",
9594
"core-js": "^3.28.0",
9695
"cronstrue": "^2.23.0",

static/app/components/clipboard.tsx

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {cloneElement, isValidElement, useCallback, useEffect, useState} from 'react';
22
import {findDOMNode} from 'react-dom';
3-
import copy from 'copy-text-to-clipboard';
43

54
import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
65
import {t} from 'sentry/locale';
@@ -57,21 +56,20 @@ function Clipboard({
5756
const [element, setElement] = useState<ReturnType<typeof findDOMNode>>();
5857

5958
const handleClick = useCallback(() => {
60-
const copyWasSuccessful = copy(value);
61-
62-
if (!copyWasSuccessful) {
63-
if (!hideMessages) {
64-
addErrorMessage(errorMessage);
65-
}
66-
onError?.();
67-
return;
68-
}
69-
70-
if (!hideMessages) {
71-
addSuccessMessage(successMessage);
72-
}
73-
74-
onSuccess?.();
59+
navigator.clipboard
60+
.writeText(value)
61+
.then(() => {
62+
onSuccess?.();
63+
if (!hideMessages) {
64+
addSuccessMessage(successMessage);
65+
}
66+
})
67+
.catch(() => {
68+
onError?.();
69+
if (!hideMessages) {
70+
addErrorMessage(errorMessage);
71+
}
72+
});
7573
}, [value, onError, onSuccess, errorMessage, successMessage, hideMessages]);
7674

7775
useEffect(() => {

static/app/components/codeSnippet.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import 'prismjs/components/prism-bash.min';
55

66
import {useEffect, useRef, useState} from 'react';
77
import styled from '@emotion/styled';
8-
import copy from 'copy-text-to-clipboard';
98

109
import {Button} from 'sentry/components/button';
1110
import {IconCopy} from 'sentry/icons';
@@ -41,13 +40,15 @@ export function CodeSnippet({
4140
const [tooltipState, setTooltipState] = useState<'copy' | 'copied' | 'error'>('copy');
4241

4342
const handleCopy = () => {
44-
const copied = copy(children);
43+
navigator.clipboard
44+
.writeText(children)
45+
.then(() => {
46+
setTooltipState('copied');
47+
})
48+
.catch(() => {
49+
setTooltipState('error');
50+
});
4551
onCopy?.(children);
46-
if (copied) {
47-
setTooltipState('copied');
48-
} else {
49-
setTooltipState('error');
50-
}
5152
};
5253

5354
const tooltipTitle =

static/app/views/issueDetails/actions/shareModal.tsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import {Fragment, useCallback, useEffect, useRef, useState} from 'react';
22
import styled from '@emotion/styled';
3-
import copy from 'copy-text-to-clipboard';
43

54
import {bulkUpdate} from 'sentry/actionCreators/group';
6-
import {addErrorMessage} from 'sentry/actionCreators/indicator';
5+
import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
76
import {ModalRenderProps} from 'sentry/actionCreators/modal';
87
import AutoSelectText from 'sentry/components/autoSelectText';
98
import {Button} from 'sentry/components/button';
@@ -94,6 +93,17 @@ function ShareIssueModal({
9493

9594
const shareUrl = group?.shareId ? getShareUrl() : null;
9695

96+
const handleCopy = () => {
97+
navigator.clipboard
98+
.writeText(shareUrl!)
99+
.then(() => {
100+
addSuccessMessage(t('Copied to clipboard'));
101+
})
102+
.catch(() => {
103+
addErrorMessage(t('Error copying to clipboard'));
104+
});
105+
};
106+
97107
return (
98108
<Fragment>
99109
<Header closeButton>
@@ -132,7 +142,7 @@ function ShareIssueModal({
132142
size="sm"
133143
onClick={() => {
134144
urlRef.current?.selectText();
135-
copy(shareUrl);
145+
handleCopy();
136146
}}
137147
icon={<IconCopy />}
138148
aria-label={t('Copy to clipboard')}
@@ -155,8 +165,7 @@ function ShareIssueModal({
155165
<Button
156166
priority="primary"
157167
onClick={() => {
158-
urlRef.current?.selectText();
159-
copy(shareUrl);
168+
handleCopy();
160169
closeModal();
161170
}}
162171
>

static/app/views/settings/organizationRelay/modals/form.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import styled from '@emotion/styled';
2-
import copy from 'copy-text-to-clipboard';
32

3+
import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
44
import Textarea from 'sentry/components/forms/controls/textarea';
55
import FieldGroup from 'sentry/components/forms/fieldGroup';
66
import FieldHelp from 'sentry/components/forms/fieldGroup/fieldHelp';
@@ -48,7 +48,16 @@ const Form = ({
4848
}
4949
};
5050

51-
const onCopy = (value: string) => () => copy(value);
51+
const onCopy = (value: string) => () => {
52+
navigator.clipboard
53+
.writeText(value)
54+
.then(() => {
55+
addSuccessMessage(t('Copied to clipboard'));
56+
})
57+
.catch(() => {
58+
addErrorMessage(t('Error copying to clipboard'));
59+
});
60+
};
5261

5362
return (
5463
<form onSubmit={handleSubmit} id="relay-form">

yarn.lock

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4448,11 +4448,6 @@ copy-anything@^2.0.1:
44484448
dependencies:
44494449
is-what "^3.12.0"
44504450

4451-
4452-
version "3.0.1"
4453-
resolved "https://registry.yarnpkg.com/copy-text-to-clipboard/-/copy-text-to-clipboard-3.0.1.tgz#8cbf8f90e0a47f12e4a24743736265d157bce69c"
4454-
integrity sha512-rvVsHrpFcL4F2P8ihsoLdFHmd404+CMg71S756oRSeQgqk51U3kicGdnvfkrxva0xXH92SjGS62B0XIJsbh+9Q==
4455-
44564451
copy-webpack-plugin@^11.0.0:
44574452
version "11.0.0"
44584453
resolved "https://registry.yarnpkg.com/copy-webpack-plugin/-/copy-webpack-plugin-11.0.0.tgz#96d4dbdb5f73d02dd72d0528d1958721ab72e04a"

0 commit comments

Comments
 (0)