summaryrefslogtreecommitdiff
path: root/blocks/eda-frontend/src/components/SchematicEditor/Header.js
blob: 7744714a4aa9588fc3dc3916c45bd5d4910b2926 (plain)
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import { useEffect, useRef, useState } from 'react'
import { useSelector, useDispatch } from 'react-redux'
import { useHistory, Link as RouterLink } from 'react-router-dom'

import PropTypes from 'prop-types'

import {
  Avatar,
  Button,
  Dialog,
  DialogActions,
  DialogContent,
  DialogContentText,
  DialogTitle,
  Fade,
  FormControlLabel,
  Hidden,
  IconButton,
  Input,
  Link,
  ListItemText,
  Menu,
  MenuItem,
  Snackbar,
  Switch,
  Toolbar,
  Typography
} from '@material-ui/core'
import { deepPurple } from '@material-ui/core/colors'
import { makeStyles } from '@material-ui/core/styles'
import CloseIcon from '@material-ui/icons/Close'
import ShareIcon from '@material-ui/icons/Share'

import { logout } from '../../redux/authSlice'
import { setSchTitle, setSchShared } from '../../redux/saveSchematicSlice'
import logo from '../../static/favicon.ico'
import { getDateTime as getDate, getUppercaseInitial } from '../../utils/GalleryUtils'

const useStyles = makeStyles((theme) => ({
  toolbarTitle: {
    marginRight: theme.spacing(2)
  },
  input: {
    marginLeft: theme.spacing(1),
    width: '200px',
    color: '#595959'
  },
  rightBlock: {
    marginLeft: 'auto',
    marginRight: theme.spacing(2)
  },
  button: {
    marginRight: theme.spacing(0.7)
  },
  small: {
    width: theme.spacing(3.7),
    height: theme.spacing(3.7),
    borderRadius: '10%'
  },
  tools: {
    padding: theme.spacing(1),
    margin: theme.spacing(0, 0.8),
    color: '#262626'
  },
  purple: {
    width: theme.spacing(3.75),
    height: theme.spacing(3.75),
    color: theme.palette.getContrastText(deepPurple[500]),
    backgroundColor: deepPurple[500],
    fontSize: '17px'
  }
}))

// Notification snackbar to give alert messages
function SimpleSnackbar ({ open, close, message }) {
  return (
    <div>
      <Snackbar
        anchorOrigin={{
          vertical: 'bottom',
          horizontal: 'left'
        }}
        open={open}
        autoHideDuration={6000}
        onClose={close}
        message={message}
        action={
          <>
            <IconButton size='small' aria-label='close' color='inherit' onClick={close}>
              <CloseIcon fontSize='small' />
            </IconButton>
          </>
        }
      />
    </div>
  )
}

SimpleSnackbar.propTypes = {
  open: PropTypes.bool,
  close: PropTypes.func,
  message: PropTypes.string
}

function Header () {
  const history = useHistory()
  const classes = useStyles()
  const isAuthenticated = useSelector(state => state.auth.isAuthenticated)
  const user = useSelector(state => state.auth.user)
  const details = useSelector(state => state.saveSchematic.details)
  const isSaved = useSelector(state => state.saveSchematic.isSaved)
  const isShared = useSelector(state => state.saveSchematic.isShared)
  const title = useSelector(state => state.saveSchematic.title)
  const [anchorEl, setAnchorEl] = useState(null)

  const dispatch = useDispatch()

  const handleClick = (event) => {
    setAnchorEl(event.currentTarget)
  }

  const handleClose = () => {
    setAnchorEl(null)
  }

  const titleHandler = (e) => {
    dispatch(setSchTitle(`${e.target.value}`))
  }

  // handle notification snackbar open and close with message
  const [snacOpen, setSnacOpen] = useState(false)
  const [message, setMessage] = useState('')

  const handleSnacClick = () => {
    setSnacOpen(true)
  }

  const handleSnacClose = (event, reason) => {
    if (reason === 'clickaway') {
      return
    }
    setSnacOpen(false)
  }

  // handle schematic Share Dialog box
  const [openShare, setShareOpen] = useState(false)

  const handleShareOpen = () => {
    setShareOpen(true)
  }

  const handleShareClose = () => {
    setShareOpen(false)
  }

  // change saved schematic share status
  const [shared, setShared] = useState(isShared)

  useEffect(() => {
    setShared(isShared)
  }, [isShared])

  const handleShareChange = (event) => {
    setShared(event.target.checked)
    dispatch(setSchShared(event.target.checked))
  }

  const handleShare = () => {
    if (isAuthenticated !== true) {
      setMessage('You are not Logged In')
      handleSnacClick()
    } else if (isSaved !== true) {
      setMessage('You have not saved the circuit')
      handleSnacClick()
    } else {
      handleShareOpen()
    }
  }

  // handle Copy Share Url
  const textAreaRef = useRef(null)

  function copyToClipboard (e) {
    textAreaRef.current.select()
    document.execCommand('copy')
    e.target.focus()
    setMessage('Copied Successfully!')
    handleSnacClick()
  }

  const link = process.env.REACT_APP_NAME
  const altImage = process.env.REACT_APP_NAME + ' logo'
  const typography1 = 'Share Your ' + process.env.REACT_APP_DIAGRAM_NAME
  const typography2 = 'My ' + process.env.REACT_APP_DIAGRAMS_NAME
  return (
    <Toolbar variant='dense' color='default'>
      <SimpleSnackbar open={snacOpen} close={handleSnacClose} message={message} />

      {/* Display logo */}
      <IconButton edge='start' className={classes.button} color='primary'>
        <Avatar alt={altImage} src={logo} className={classes.small} />
      </IconButton>
      <Typography
        variant='h6'
        color='inherit'
        noWrap
        className={classes.toolbarTitle}
      >
        <Link color='inherit' target='_blank' component={RouterLink} to='/'>
          {link}
        </Link>
      </Typography>

      {/* Input field for schematic title */}
      <Hidden xsDown>
        <Input
          className={classes.input}
          color='secondary'
          value={title === 'Untitled' ? 'Untitled' : title}
          onChange={titleHandler}
          inputProps={{ 'aria-label': 'SchematicTitle' }}
        />
      </Hidden>

      {/* Display last saved and shared option for saved schematics */}
      {isAuthenticated === true
        ? <>
          {(isSaved === true && details.save_time !== undefined)
            ? <Typography
              variant='body2'
              style={{ margin: '0px 15px 0px auto', paddingTop: '5px', color: '#8c8c8c' }}
            >
              Last Saved : {getDate(details.save_time)} {/* Display last saved status for saved schematics */}
            </Typography>
            : <></>}
          <Button
            size='small'
            variant={shared !== true ? 'outlined' : 'contained'}
            color='primary'
            className={isSaved === true && details.save_time !== undefined ? classes.button : classes.rightBlock}
            startIcon={<ShareIcon />}
            onClick={handleShare}
          >
            <Hidden xsDown>Share</Hidden>
          </Button>
        </>
        : <></>}

      {/* Share dialog box to get share url */}
      <Dialog
        open={openShare}
        onClose={handleShareClose}
        aria-labelledby='share-dialog-title'
        aria-describedby='share-dialog-description'
      >
        <DialogTitle id='share-dialog-title'>{typography1}</DialogTitle>
        <DialogContent>
          <DialogContentText id='share-dialog-description'>
            <FormControlLabel
              control={<Switch checked={shared} onChange={handleShareChange} name='shared' />}
              label=': Sharing On'
            />
          </DialogContentText>
          <DialogContentText id='share-dialog-description'>
            {shared === true
              ? <input
                ref={textAreaRef}
                value={`${window.location.origin}/#/editor?id=${details.save_id}`}
                readOnly
              />
              : <> Turn On sharing </>}
          </DialogContentText>

        </DialogContent>
        <DialogActions>
          {shared === true && document.queryCommandSupported('copy')
            ? <Button onClick={copyToClipboard} color='primary' autoFocus>
              Copy url
            </Button>
            : <></>}
          <Button onClick={handleShareClose} color='primary' autoFocus>
            close
          </Button>
        </DialogActions>
      </Dialog>

      {/* Display login option or user menu as per authenticated status */}
      {
        (!isAuthenticated
          ? <Button
            size='small'
            component={RouterLink}
            to='/login'
            style={{ marginLeft: 'auto' }}
            color='primary'
            variant='outlined'
          >
            Login
          </Button>
          : <>
            <IconButton
              edge='start'
              color='primary'
              aria-controls='simple-menu'
              aria-haspopup='true'
              onClick={handleClick}
            >
              <Avatar className={classes.purple}>
                {getUppercaseInitial(user.username)}
              </Avatar>
            </IconButton>
            <Menu
              id='simple-menu'
              anchorEl={anchorEl}
              keepMounted
              open={Boolean(anchorEl)}
              onClose={handleClose}
              TransitionComponent={Fade}
              style={{ marginTop: '25px' }}
            >
              <MenuItem
                target='_blank'
                component={RouterLink}
                to='/dashboard'
                onClick={handleClose}
              >
                <ListItemText primary={user.username} secondary={user.email} />
              </MenuItem>
              <MenuItem
                target='_blank'
                component={RouterLink}
                to='/dashboard/schematics'
                onClick={handleClose}
              >
                {typography2}
              </MenuItem>
              <MenuItem onClick={() => {
                dispatch(logout(history))
              }}
              >
                Logout
              </MenuItem>
            </Menu>
          </>
        )
      }
    </Toolbar>
  )
}

export default Header