-
Notifications
You must be signed in to change notification settings - Fork 37
/
index.jsx
216 lines (201 loc) · 5.91 KB
/
index.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import React, { useEffect, useState } from 'react'
import cx from 'classnames'
import PropTypes from 'prop-types'
import get from 'lodash/get'
import AppIcon from '../AppIcon'
import Badge from '../Badge'
import Icon from '../Icon'
import IconCheckAnimated from '../Icons/IconCheckAnimated'
import InfosBadge from '../InfosBadge'
import Spinner from '../Spinner'
import SvgIconCrossAnimated from '../Icons/IconCrossAnimated'
import Typography from '../Typography'
import iconOut from '../Icons/LinkOut'
import iconPlus from '../Icons/Plus'
import iconWarning from '../Icons/WarningCircle'
import { makeStyles } from '../styles'
import { nameToColor } from '../Avatar'
import CozyTheme, { useCozyTheme } from '../CozyTheme'
import { color } from './constants.json'
import styles from './styles.styl'
const useStyles = makeStyles(theme => ({
name: {
color,
width: '5.5rem',
textAlign: 'center',
fontSize: '0.875rem',
lineHeight: '1.188rem',
margin: '0.5rem 0.25rem 0 0.25rem',
textShadow: theme.textShadows[1],
lineClamp: '2',
boxOrient: 'vertical',
display: '-webkit-box',
height: '2.375rem',
[theme.breakpoints.down('sm')]: {
width: '3.75rem',
fontSize: '0.6875rem',
lineHeight: '1rem',
margin: '0.25rem 0.25rem 0 0.25rem',
height: '2rem'
}
},
letter: {
color,
margin: 'auto'
},
shadow: {
boxShadow: theme.shadows[2]
},
errorIcon: {
fill: 'var(--errorColor)',
backgroundColor: 'var(--primaryContrastTextColor)',
borderRadius: '1rem'
},
tileWrapper: {
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
width: '6rem',
[theme.breakpoints.down('sm')]: {
width: '4.25rem'
}
}
}))
export const SquareAppIcon = ({
name,
variant,
IconContent,
...appIconProps
}) => {
const theme = useCozyTheme()
const classes = useStyles()
const appName =
name || get(appIconProps, 'app.name') || get(appIconProps, 'app') || ''
const letter = appName[0] || ''
const prevVariant = React.useRef(variant)
const [animationState, setAnimationState] = useState()
const handleAnimationEnd = e => {
if (e.animationName.includes('end')) setAnimationState()
}
useEffect(() => {
const curr = prevVariant.current
if (curr === 'loading' && variant === 'error') setAnimationState('failed')
if (curr === 'loading' && (variant === 'default' || variant === undefined))
setAnimationState('success')
prevVariant.current = variant
}, [variant])
const squareTheme = ['add', 'ghost'].includes(variant) ? theme : 'normal'
return (
<div data-testid="square-app-icon" className={cx(classes.tileWrapper)}>
<CozyTheme variant={squareTheme}>
<InfosBadge
badgeContent={
variant === 'shortcut' ? <Icon size="10" icon={iconOut} /> : null
}
overlap="rectangular"
invisible={variant !== 'shortcut'}
>
{['default', 'loading', 'error'].includes(variant) && (
<Spinner className={cx(styles['SquareAppIcon-spinner'], 'u-m-0')} />
)}
<Badge
className={cx(
styles['SquareAppIcon-wrapper'],
styles[`SquareAppIcon-wrapper-${variant}`],
{
[`${styles['SquareAppIcon-wrapper-fx']}`]: [
'ghost',
'add'
].includes(variant),
[classes.shadow]: !['add', 'ghost'].includes(variant)
}
)}
badgeContent={
variant === 'error' ? (
<Icon
size="16"
className={cx(classes.errorIcon)}
icon={iconWarning}
/>
) : (
''
)
}
color={variant === 'error' ? 'error' : undefined}
withBorder={false}
size="large"
overlap="rectangular"
style={
variant === 'shortcut' && !IconContent
? { backgroundColor: nameToColor(name) }
: null
}
>
{variant === 'shortcut' && !IconContent ? (
<Typography
className={classes.letter}
variant="h2"
align="center"
>
{letter.toUpperCase()}
</Typography>
) : (
<div className={styles['SquareAppIcon-icon-container']}>
<div
className={cx(
styles['onEnd'],
{ [styles['isSuccess']]: animationState === 'success' },
{ [styles['isFailed']]: animationState === 'failed' }
)}
onAnimationEnd={handleAnimationEnd}
>
{animationState && (
<Icon
size="32"
icon={
animationState === 'success'
? IconCheckAnimated
: SvgIconCrossAnimated
}
/>
)}
</div>
{variant === 'add' ? (
<Icon icon={iconPlus} color={color} />
) : IconContent ? (
IconContent
) : (
<AppIcon {...appIconProps} />
)}
</div>
)}
</Badge>
</InfosBadge>
</CozyTheme>
<Typography
className={cx(classes.name, 'u-spacellipsis')}
variant="h6"
align="center"
>
{appName}
</Typography>
</div>
)
}
SquareAppIcon.propTypes = {
name: PropTypes.string,
variant: PropTypes.oneOf([
'default',
'loading',
'ghost',
'maintenance',
'error',
'add',
'shortcut'
]),
IconContent: PropTypes.node
}
SquareAppIcon.defaultProps = {
variant: 'default'
}
export default SquareAppIcon