-
Notifications
You must be signed in to change notification settings - Fork 12
/
HeaderLogo.jsx
74 lines (68 loc) · 2.39 KB
/
HeaderLogo.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
import React from 'react';
import { useTranslation } from 'react-i18next';
import { Dialog, DialogTitle, DialogContent, DialogActions, Tooltip, Button, IconButton } from '@mui/material'
import { Edit as EditIcon, CloudUpload as UploadIcon } from '@mui/icons-material';
import { last } from 'lodash';
import { currentUserHasAccess } from '../../common/utils';
import ImageUploader from './ImageUploader';
const HeaderLogo = ({ logoURL, onUpload, defaultIcon, isCircle, shrink, className }) => {
const { t } = useTranslation()
const hasAccess = currentUserHasAccess();
const [base64, setBase64] = React.useState(null);
const [open, setOpen] = React.useState(false);
const onLogoUpload = (base64, name) => {
setOpen(false);
setBase64(base64)
onUpload(base64, name)
}
const getExistingLogoName = () => {
if(!logoURL)
return
return last(logoURL.split('/'))
}
let containerClasses = 'logo-container flex-vertical-center'
if(className)
containerClasses += ` ${className}`
else if(shrink)
containerClasses += ' small'
const logo = base64 || logoURL
return (
<React.Fragment>
<div className={containerClasses}>
{
logo ?
<img className='header-logo' src={logo} /> :
defaultIcon
}
{
hasAccess &&
<Tooltip arrow title={logoURL ? t('common.logo.tooltip.edit') : t('common.logo.tooltip.add')}>
<IconButton
onClick={() => setOpen(true)}
className='logo-edit-button'
color='secondary'
size="large">
{
logoURL ?
<EditIcon fontSize='small' color='secondary' /> :
<UploadIcon color='secondary' fontSize='small' />
}
</IconButton>
</Tooltip>
}
</div>
<Dialog onClose={() => setOpen(false)} open={open} fullWidth>
<DialogTitle>{logoURL ? t('common.logo.dialog.title.edit') : t('common.logo.dialog.title.add')}</DialogTitle>
<DialogContent>
<ImageUploader onUpload={onLogoUpload} defaultImg={logoURL} defaultName={getExistingLogoName()} isCircle={isCircle} />
</DialogContent>
<DialogActions>
<Button onClick={() => setOpen(false)}>
{t('common.close')}
</Button>
</DialogActions>
</Dialog>
</React.Fragment>
);
}
export default HeaderLogo;