src/components/CV/TopSection/Copy.js   A
last analyzed

Complexity

Total Complexity 2
Complexity/F 0

Size

Lines of Code 55
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 43
mnd 2
bc 2
fnc 0
dl 0
loc 55
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import React, { useState } from 'react'
2
import PropTypes from 'prop-types'
3
import { withTranslation } from 'react-i18next'
4
import { makeStyles } from '@material-ui/core/styles'
5
import FileCopyOutlinedIcon from '@material-ui/icons/FileCopyOutlined'
6
import Tooltip from '@material-ui/core/Tooltip'
7
import copy from 'copy-to-clipboard'
8
import topStyle from './index.scss'
9
10
const useStyles = makeStyles({
11
  tooltip: {
12
    fontSize: '0.8rem',
13
    textAlign: 'center',
14
  },
15
})
16
17
const Copy = ({ text, title, t }) => {
18
  const [copied, setCopied] = useState(false)
19
  const classes = useStyles()
20
  const b = (
21
    <button
22
      type="button"
23
      className={`${topStyle.svgwrapper} ${topStyle.copy}`}
24
      onClick={() => {
25
        copy(text)
26
        setCopied(true)
27
        setTimeout(() => {
28
          setCopied(false)
29
        }, 2000)
30
      }}
31
    >
32
      <FileCopyOutlinedIcon />
33
    </button>
34
  )
35
  if (title) {
36
    return (
37
      <Tooltip
38
        classes={{ tooltip: classes.tooltip }}
39
        title={copied ? t('Copied') : title}
40
      >
41
        {b}
42
      </Tooltip>
43
    )
44
  }
45
  return b
46
}
47
48
Copy.propTypes = {
49
  text: PropTypes.string.isRequired,
50
  title: PropTypes.string,
51
  t: PropTypes.func.isRequired,
52
}
53
54
export default withTranslation()(Copy)
55