Skip to content

Commit

Permalink
fix: Use navigator.clipboard instead of copy-text-to-clipboard
Browse files Browse the repository at this point in the history
copy-text-to-clipboard use deprecated command.exec which does not
work anymore on some desktop browser.
  • Loading branch information
zatteo committed Jan 31, 2024
1 parent e8943fd commit ae42cb9
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 20 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@
"bundlemon": "^1.3.2",
"chart.js": "3.7.1",
"classnames": "^2.2.5",
"copy-text-to-clipboard": "3.2.0",
"cozy-interapp": "^0.5.4",
"date-fns": "^1.28.5",
"filesize": "8.0.7",
Expand Down
9 changes: 4 additions & 5 deletions react/ListItem/ExpandedAttributes/helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import get from 'lodash/get'
import copy from 'copy-text-to-clipboard'

import { formatDate } from '../../Viewer/helpers'

Expand Down Expand Up @@ -68,15 +67,15 @@ export const makeDefaultExpandedAttributes = (doc, expandedAttributes) => {
.filter(x => x)
}

export const copyToClipboard = ({ value, setAlertProps, t }) => () => {
const hasCopied = copy(value)
if (hasCopied) {
export const copyToClipboard = ({ value, setAlertProps, t }) => async () => {
try {
await navigator.clipboard.writeText(value)
setAlertProps({
open: true,
severity: 'success',
message: t(`ListItem.snackbar.copyToClipboard.success`)
})
} else {
} catch (error) {
setAlertProps({
open: true,
severity: 'error',
Expand Down
2 changes: 0 additions & 2 deletions react/ListItem/ExpandedAttributes/helpers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import {
import { I18nContext } from '../../jestLib/I18n'
import en from '../locales/en'

jest.mock('copy-text-to-clipboard', () => ({ copy: jest.fn() }))

const i18nContext = I18nContext({
locale: en
})
Expand Down
9 changes: 4 additions & 5 deletions react/Viewer/Panel/ActionMenuWrapper.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { forwardRef } from 'react'
import PropTypes from 'prop-types'
import copy from 'copy-text-to-clipboard'

import { useAppLinkWithStoreFallback, useClient } from 'cozy-client'

Expand Down Expand Up @@ -40,14 +39,14 @@ const ActionMenuWrapper = forwardRef(({ onClose, file, optionFile }, ref) => {
)
const isAppLinkLoaded = fetchStatus === 'loaded'

const handleCopy = () => {
const hasCopied = copy(value)
if (hasCopied) {
const handleCopy = async () => {
try {
await navigator.clipboard.writeText(value)
showViewerSnackbar(
'success',
t(`Viewer.snackbar.copiedToClipboard.success`)
)
} else {
} catch (error) {
showViewerSnackbar('error', t(`Viewer.snackbar.copiedToClipboard.error`))
}
onClose()
Expand Down
2 changes: 0 additions & 2 deletions react/Viewer/Panel/getPanelBlocks.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ jest.mock('cozy-harvest-lib/dist/components/KonnectorBlock', () => jest.fn())
const block1Component = jest.fn()
const block2Component = jest.fn()

jest.mock('copy-text-to-clipboard', () => ({ copy: jest.fn() }))

describe('getPanelBlocks', () => {
it('should return only blocks with truthy condition', () => {
// with two truthy component
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5909,11 +5909,6 @@ copy-descriptor@^0.1.0:
resolved "https://registry.yarnpkg.com/copy-descriptor/-/copy-descriptor-0.1.1.tgz#676f6eb3c39997c2ee1ac3a924fd6124748f578d"
integrity sha1-Z29us8OZl8LuGsOpJP1hJHSPV40=

[email protected]:
version "3.2.0"
resolved "https://registry.yarnpkg.com/copy-text-to-clipboard/-/copy-text-to-clipboard-3.2.0.tgz#0202b2d9bdae30a49a53f898626dcc3b49ad960b"
integrity sha512-RnJFp1XR/LOBDckxTib5Qjr/PMfkatD0MUCQgdpqS8MdKiNUzBjAQBEN6oUy+jW7LI93BBG3DtMB2KOOKpGs2Q==

copy-text-to-clipboard@^2.1.1:
version "2.2.0"
resolved "https://registry.yarnpkg.com/copy-text-to-clipboard/-/copy-text-to-clipboard-2.2.0.tgz#329dd6daf8c42034c763ace567418401764579ae"
Expand Down

0 comments on commit ae42cb9

Please sign in to comment.