Files
pawol.nu/components/teks/teks-drawer.js
T

434 lines
12 KiB
JavaScript
Raw Normal View History

2022-01-22 03:40:30 +04:00
import {useEffect, useState, forwardRef} from 'react'
import PropTypes from 'prop-types'
import Link from 'next/link'
2021-06-26 12:32:44 +02:00
import {useSession} from 'next-auth/client'
import {
Grid,
Toolbar,
Typography,
AppBar,
CssBaseline,
Drawer,
Hidden,
2021-05-26 02:49:06 +02:00
IconButton,
2021-05-30 11:56:27 +02:00
Box,
2021-06-02 02:24:19 +02:00
useMediaQuery,
2021-06-26 12:34:19 +02:00
Snackbar,
Tooltip,
2022-01-19 06:35:04 +04:00
Zoom
2022-01-19 07:06:26 +04:00
} from '@mui/material'
2022-01-19 07:06:26 +04:00
import KeyboardBackspaceIcon from '@mui/icons-material/KeyboardBackspace'
import ExplicitIcon from '@mui/icons-material/Explicit'
2022-01-19 07:06:26 +04:00
import MenuIcon from '@mui/icons-material/Menu'
2022-01-19 07:06:26 +04:00
import {useTheme, styled} from '@mui/material/styles'
2022-01-19 07:06:26 +04:00
import MuiAlert from '@mui/material/Alert'
import {formatJsonString} from '../../lib/utils/format'
2021-06-26 12:32:44 +02:00
import VweKomante from '../komante/vwe-komante'
import Dekoneksyon from '../sesyon/dekoneksyon'
import DrawerBar from './drawer-bar'
import DenyeTeks from './denye-teks'
2020-12-15 08:50:14 +01:00
import VweKouteAchte from './vwe-koute-achte'
2021-01-12 21:58:08 +01:00
import OkiMizik from './oki-mizik'
2021-06-02 02:24:19 +02:00
import Pataje from './pataje'
2021-07-15 00:59:08 +02:00
import EntegreMizik from './entegre-mizik'
2021-06-02 02:24:19 +02:00
2022-01-19 06:35:04 +04:00
const PREFIX = 'teks-drawer'
2022-01-19 06:35:04 +04:00
const classes = {
tooltip: `${PREFIX}-tooltip`,
root: `${PREFIX}-root`,
drawer: `${PREFIX}-drawer`,
appBar: `${PREFIX}-appBar`,
menuButton: `${PREFIX}-menuButton`,
toolbar: `${PREFIX}-toolbar`,
drawerPaper: `${PREFIX}-drawerPaper`,
content: `${PREFIX}-content`,
list: `${PREFIX}-list`,
form: `${PREFIX}-form`,
text: `${PREFIX}-text`,
gridText: `${PREFIX}-gridText`,
grid: `${PREFIX}-grid`,
koute: `${PREFIX}-koute`,
vwe: `${PREFIX}-vwe`,
pataje: `${PREFIX}-pataje`
}
2022-01-19 06:35:04 +04:00
const Root = styled('div')((
{
theme
}
) => ({
[`&.${classes.root}`]: {
display: 'flex'
},
2022-01-19 06:35:04 +04:00
[`& .${classes.drawer}`]: {
marginTop: '10em',
[theme.breakpoints.up('sm')]: {
width: drawerWidth,
flexShrink: 0
}
},
2022-01-19 06:35:04 +04:00
[`& .${classes.appBar}`]: {
borderTop: '2px solid #303030',
2022-01-22 15:47:13 +04:00
marginTop: '3rem',
[theme.breakpoints.up('sm')]: {
width: `calc(100% - ${drawerWidth}px)`,
marginLeft: drawerWidth
2021-06-26 12:08:31 +02:00
},
zIndex: 1
},
2022-01-19 06:35:04 +04:00
[`& .${classes.menuButton}`]: {
[theme.breakpoints.up('sm')]: {
display: 'none'
}
},
2022-01-19 06:35:04 +04:00
[`& .${classes.toolbar}`]: theme.mixins.toolbar,
[`& .${classes.drawerPaper}`]: {
borderTop: '2px solid #303030',
2022-01-22 15:47:13 +04:00
marginTop: '3rem',
width: drawerWidth
},
2022-01-19 06:35:04 +04:00
[`& .${classes.content}`]: {
flexGrow: 1,
2022-01-22 03:41:38 +04:00
padding: theme.spacing(1)
},
2022-01-19 06:35:04 +04:00
[`& .${classes.list}`]: {
marginBottom: '6em'
},
2022-01-19 06:35:04 +04:00
[`& .${classes.form}`]: {
marginLeft: theme.spacing(1)
},
2022-01-19 06:35:04 +04:00
[`& .${classes.text}`]: {
marginBottom: '0.5em'
},
2022-01-19 06:35:04 +04:00
[`& .${classes.gridText}`]: {
2021-05-27 03:28:40 +02:00
border: '2px solid grey',
borderRadius: '5px',
marginTop: '2em',
2021-05-27 03:28:40 +02:00
padding: '1em'
},
2022-01-19 06:35:04 +04:00
[`& .${classes.grid}`]: {
marginTop: '1em'
2020-12-15 08:50:14 +01:00
},
2022-01-19 06:35:04 +04:00
[`& .${classes.koute}`]: {
2020-12-15 08:50:14 +01:00
position: 'absolute',
2021-06-05 21:23:31 +02:00
right: '40px',
2020-12-15 08:50:14 +01:00
top: '8px',
[theme.breakpoints.up('sm')]: {
top: '10px'
}
},
2022-01-19 06:35:04 +04:00
[`& .${classes.vwe}`]: {
2020-12-15 08:50:14 +01:00
position: 'absolute',
2021-06-05 21:23:31 +02:00
right: '90px',
2020-12-15 08:50:14 +01:00
top: '8px',
[theme.breakpoints.up('sm')]: {
top: '10px'
}
2021-06-02 02:24:19 +02:00
},
2022-01-19 06:35:04 +04:00
[`& .${classes.pataje}`]: {
2021-06-05 21:24:27 +02:00
position: 'fixed',
2022-01-22 15:47:13 +04:00
top: '59px',
2021-06-05 21:24:27 +02:00
left: '110px',
2021-06-26 12:08:31 +02:00
zIndex: 2,
2021-06-02 02:24:19 +02:00
[theme.breakpoints.up('sm')]: {
2022-01-22 15:47:13 +04:00
top: '62px',
2021-06-05 21:24:27 +02:00
left: '340px'
2021-06-02 02:24:19 +02:00
}
}
}))
2022-01-22 03:40:30 +04:00
const Alert = forwardRef(function Alert(props, ref) { // eslint-disable-line func-names
return <MuiAlert ref={ref} elevation={6} variant='filled' {...props} />
})
2022-01-19 06:35:04 +04:00
const drawerWidth = 240
2021-05-27 03:28:40 +02:00
const langToArray = anTeks => {
const langArray = []
if (anTeks && anTeks.tradiksyon) {
const {francais, english, espagnol} = anTeks.tradiksyon
if (francais) {
2021-07-01 06:58:56 +02:00
langArray.push({title: 'Traduction', flag: 'fr', lang: francais})
2021-05-27 03:28:40 +02:00
}
if (english) {
2021-07-01 06:58:56 +02:00
langArray.push({title: 'Translation', flag: 'en', lang: english})
2021-05-27 03:28:40 +02:00
}
if (espagnol) {
2021-07-01 06:58:56 +02:00
langArray.push({title: 'Traducción', flag: 'es', lang: espagnol})
2021-05-27 03:28:40 +02:00
}
}
return langArray
}
2021-05-30 16:59:56 +02:00
const alignTeks = (langArray, isMobile) => {
if (langArray.length > 0 && !isMobile) {
return 'justify'
}
if (langArray.length > 0 && isMobile) {
return 'justify'
}
if (langArray.length === 0 && isMobile) {
return 'justify'
}
if (langArray.length === 0 && !isMobile) {
return 'center'
}
}
2022-01-19 06:35:04 +04:00
const ExplicitTooltip = Tooltip
2021-06-26 12:34:19 +02:00
2021-06-26 12:32:44 +02:00
export default function TeksDrawer({teks, anTeks, komante}) {
const [session] = useSession()
2021-05-30 11:56:27 +02:00
const isMobile = useMediaQuery('(max-width:800px)')
2021-05-27 03:28:40 +02:00
const langArray = langToArray(anTeks)
2022-01-19 06:35:04 +04:00
const theme = useTheme()
2020-12-18 22:21:53 +01:00
const [esMobilOuve, meteEsMobilOuve] = useState(false)
2021-06-02 02:24:19 +02:00
const [open, setOpen] = useState(false)
const [error, setError] = useState('')
const [success, setSuccess] = useState('')
const handleClose = (event, reason) => {
if (reason === 'clickaway') {
return
}
setOpen(false)
setSuccess('')
setError('')
}
const handleDrawerToggle = () => {
2020-12-18 22:21:53 +01:00
meteEsMobilOuve(!esMobilOuve)
}
const container = typeof window === 'undefined' ? undefined : () => window.document.body
2021-06-02 02:24:19 +02:00
useEffect(() => {
if (error || success) {
setOpen(true)
}
}, [error, success, setOpen])
return (
2022-01-19 06:35:04 +04:00
<Root className={classes.root}>
<CssBaseline />
<AppBar position='fixed' className={classes.appBar}>
<Toolbar>
<IconButton
color='inherit'
aria-label='open drawer'
edge='start'
className={classes.menuButton}
2022-01-19 07:06:26 +04:00
size='large'
onClick={handleDrawerToggle}
>
<MenuIcon />
</IconButton>
2020-12-18 22:08:34 +01:00
{anTeks ? (
<>
2022-01-18 09:08:26 +04:00
<Link passHref href='/teks'>
2020-12-17 23:45:22 +01:00
<IconButton aria-label='return' size='medium'>
<KeyboardBackspaceIcon style={{fontSize: '1.5em'}} />
</IconButton>
</Link>
2020-12-18 22:08:34 +01:00
{anTeks.lyen && anTeks.lyen.length > 0 && (
2020-12-15 08:50:14 +01:00
<div className={classes.vwe}>
2020-12-18 22:08:34 +01:00
<VweKouteAchte niVideyo anTeks={anTeks} />
2020-12-15 08:50:14 +01:00
</div>
)}
2020-12-18 22:08:34 +01:00
{anTeks.kouteyAchtey && anTeks.kouteyAchtey.length > 0 && (
2020-12-15 08:50:14 +01:00
<div className={classes.koute}>
2020-12-18 22:08:34 +01:00
<VweKouteAchte niOdyo anTeks={anTeks} />
2020-12-15 08:50:14 +01:00
</div>
)}
</>
) : (
<Typography noWrap variant='h6'>
Dènyé Tèks
</Typography>
)}
</Toolbar>
</AppBar>
<nav className={classes.drawer} aria-label='mailbox folders'>
<Hidden smUp implementation='css'>
<Drawer
container={container}
variant='temporary'
anchor={theme.direction === 'rtl' ? 'right' : 'left'}
2020-12-18 22:21:53 +01:00
open={esMobilOuve}
classes={{
paper: classes.drawerPaper
}}
ModalProps={{
keepMounted: true
}}
onClose={handleDrawerToggle}
>
2020-12-18 22:21:53 +01:00
<DrawerBar meteEsMobilOuve={meteEsMobilOuve} teks={teks} anTeks={anTeks} />
</Drawer>
</Hidden>
2022-01-19 07:06:26 +04:00
<Hidden smDown implementation='css'>
<Drawer
open
classes={{
paper: classes.drawerPaper
}}
variant='permanent'
>
2020-12-18 22:08:34 +01:00
<DrawerBar teks={teks} anTeks={anTeks} />
</Drawer>
</Hidden>
</nav>
<main className={classes.content}>
2020-12-18 22:08:34 +01:00
{anTeks ? (
2020-12-17 23:47:15 +01:00
<>
2021-06-26 12:33:17 +02:00
<div className={classes.pataje}>
<Pataje teks={anTeks} setError={setError} setSuccess={setSuccess} />
</div>
2022-01-22 13:42:09 +04:00
<Box sx={{textAlign: 'center', marginTop: 8}}>
2021-09-25 21:48:35 +02:00
<Typography style={{marginTop: '0.8em'}} variant='h4' display='block'>
<Typography gutterBottom variant='h6'>
{anTeks.awtis.map(a => a.alias).join(', ')}
</Typography>
<Typography variant='h5'>
{anTeks.tit}
{anTeks.eksplisit && (
2022-01-19 06:35:04 +04:00
<ExplicitTooltip
title='Explicit Lyrics'
placement='bottom'
TransitionComponent={Zoom}
classes={{
tooltip: classes.tooltip
}}
>
2021-09-25 21:48:35 +02:00
<ExplicitIcon style={{marginLeft: 10}} color='secondary' />
</ExplicitTooltip>
)}
</Typography>
2022-01-22 03:41:38 +04:00
<Grid container alignItems='center' justifyContent='center'>
{komante && (
<VweKomante komante={komante} teks={anTeks} />
)}
{session && (
<Dekoneksyon tooltipPlacement='bottom' />
)}
</Grid>
2021-05-26 02:49:06 +02:00
</Typography>
2021-07-16 21:09:34 +02:00
{anTeks.user && (
<Typography style={{marginBottom: '1.5em'}} display='block' variant='caption'>
2021-06-02 19:21:54 +02:00
<i>texte soumis par {anTeks.user.username}</i>
2021-07-16 21:09:34 +02:00
</Typography>
)}
2021-05-26 02:49:06 +02:00
</Box>
2021-07-16 21:09:57 +02:00
{(anTeks.okiMizikID || anTeks.kouteyAchtey.length > 0) && (
2022-01-20 22:18:01 +04:00
<Box sx={{textAlign: 'center'}}>
<EntegreMizik anTeks={anTeks} isMobile={isMobile} />
</Box>
2021-07-15 00:59:08 +02:00
)}
2021-01-12 21:58:08 +01:00
{anTeks.okiMizikID && (
<OkiMizik id={anTeks.okiMizikID} />
)}
2022-01-22 03:41:38 +04:00
<Grid container justifyContent='start' spacing={1}>
2021-05-27 03:28:40 +02:00
<Grid item xs={12} md={langArray.length > 0 ? 6 : null}>
<div className={classes.gridText}>
2020-12-17 23:47:15 +01:00
<Typography align='center' className={classes.text} variant='h4'>
2021-05-27 03:28:40 +02:00
Transcription
2020-12-17 23:47:15 +01:00
</Typography>
2021-05-30 16:59:56 +02:00
<Typography paragraph align={alignTeks(langArray, isMobile)} component='span'>
2021-05-27 03:28:40 +02:00
{formatJsonString(anTeks.transkripsyon)}
2020-12-17 23:47:15 +01:00
</Typography>
2021-05-27 03:28:40 +02:00
</div>
</Grid>
2021-07-01 06:58:56 +02:00
{langArray.map(({title, flag, lang}) => (
2021-05-27 03:28:40 +02:00
<Grid key={title} item xs={12} md={6}>
<div className={classes.gridText}>
<Typography align='center' className={classes.text} variant='h4'>
2021-07-01 06:58:56 +02:00
{flag === 'fr' && (
<span>
🇫🇷
</span>
)}
{flag === 'en' && (
<span>
🇬🇧
</span>
)}
{flag === 'es' && (
<span>
🇪🇸
</span>
)} {title}
2021-05-27 03:28:40 +02:00
</Typography>
<Typography paragraph align='justify' component='span'>
{formatJsonString(lang)}
</Typography>
</div>
2020-12-17 23:47:15 +01:00
</Grid>
2021-05-27 03:28:40 +02:00
))}
2020-12-17 23:47:15 +01:00
</Grid>
2021-06-02 02:24:19 +02:00
{success && (
<Snackbar open={open} autoHideDuration={3000} onClose={handleClose}>
<Alert severity='success' onClose={handleClose}>
<strong>{success}</strong>
</Alert>
</Snackbar>
)}
{error && (
<Snackbar open={open} autoHideDuration={3000} onClose={handleClose}>
<Alert severity='error' onClose={handleClose}>
<strong>Une erreur sest produite</strong> : <i>{error.message}</i>
</Alert>
</Snackbar>
)}
2020-12-17 23:47:15 +01:00
</>
) : (
2020-12-18 22:08:34 +01:00
<DenyeTeks {...teks} />
)}
</main>
2022-01-19 06:35:04 +04:00
</Root>
)
}
TeksDrawer.propTypes = {
2020-12-18 22:08:34 +01:00
teks: PropTypes.array.isRequired,
2021-06-26 12:32:44 +02:00
anTeks: PropTypes.object,
komante: PropTypes.array
}
TeksDrawer.defaultProps = {
2021-06-26 12:32:44 +02:00
anTeks: null,
komante: null
}