Passed
Push — master ( 68d566...33bc6b )
by Tom
02:09 queued 11s
created

src/components/ProjectList/Icon.js   A

Complexity

Total Complexity 4
Complexity/F 0

Size

Lines of Code 104
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 86
mnd 4
bc 4
fnc 0
dl 0
loc 104
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 0
1
import React from 'react'
2
import PropTypes from 'prop-types'
3
import { makeStyles } from '@material-ui/core/styles'
4
import Tooltip from '@material-ui/core/Tooltip'
5
import HomeIcon from '@material-ui/icons/Home'
6
import WarningIcon from '@material-ui/icons/Warning'
7
import projectListStyle from './index.scss'
8
import stackIconList from './stackIcons'
9
10
const useStyles = makeStyles({
11
  tooltip: {
12
    fontSize: '0.8rem',
13
    textAlign: 'center',
14
  },
15
})
16
17
const iconList = {
18
  home: {
19
    name: 'Home',
20
    icon: HomeIcon,
21
    color: '#008c38',
22
  },
23
  warning: {
24
    name: 'Warning',
25
    icon: WarningIcon,
26
    color: 'red',
27
  },
28
  ...stackIconList,
29
}
30
31
const Icon = ({ type, url, title, size }) => {
32
  let realSize
33
  switch (size) {
34
    case 'small':
35
      realSize = 18
36
      break
37
    case 'middle':
38
      realSize = 25
39
      break
40
    case 'large':
41
      realSize = 32
42
      break
43
    default:
44
      realSize = size
45
  }
46
47
  const classes = useStyles()
48
49
  let typeLower = type.toLowerCase()
50
  typeLower = typeLower in iconList ? typeLower : 'warning'
51
52
  const style = {
53
    width: realSize,
54
    height: realSize,
55
  }
56
  const TypeIcon = iconList[typeLower].icon
57
  const TypeIconRendered = (
58
    <TypeIcon
59
      style={{
60
        ...style,
61
        fontSize: realSize,
62
        color: iconList[typeLower].color,
63
      }}
64
    />
65
  )
66
67
  const anchor = url ? (
68
    <a
69
      href={url}
70
      className={projectListStyle.svgwrapper}
71
      target="_blank"
72
      rel="noopener noreferrer"
73
      style={style}
74
    >
75
      {TypeIconRendered}
76
    </a>
77
  ) : (
78
    <span className={projectListStyle.svgwrapper} style={style}>
79
      {TypeIconRendered}
80
    </span>
81
  )
82
  if (title) {
83
    return (
84
      <Tooltip classes={{ tooltip: classes.tooltip }} title={title}>
85
        {anchor}
86
      </Tooltip>
87
    )
88
  }
89
  return anchor
90
}
91
92
Icon.defaultProps = {
93
  size: 'middle',
94
}
95
96
Icon.propTypes = {
97
  type: PropTypes.string.isRequired,
98
  url: PropTypes.string,
99
  title: PropTypes.string,
100
  size: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
101
}
102
103
export default Icon
104