2024-05-20 04:17:09 +04:00
|
|
|
|
'use client'
|
|
|
|
|
|
|
|
|
|
|
|
import PropTypes from 'prop-types'
|
2026-01-04 13:06:24 +04:00
|
|
|
|
import {useEffect, useState, useMemo} from 'react'
|
2024-05-20 04:17:09 +04:00
|
|
|
|
import {signOut} from 'next-auth/react'
|
|
|
|
|
|
import {useRouter} from 'next/navigation'
|
|
|
|
|
|
import Box from '@mui/material/Box'
|
|
|
|
|
|
import Stack from '@mui/material/Stack'
|
|
|
|
|
|
import Fab from '@mui/material/Fab'
|
2024-05-20 14:43:29 +04:00
|
|
|
|
import {styled} from '@mui/material/styles'
|
|
|
|
|
|
import Tooltip, {tooltipClasses} from '@mui/material/Tooltip'
|
2024-05-20 04:17:09 +04:00
|
|
|
|
import LogoutIcon from '@mui/icons-material/Logout'
|
|
|
|
|
|
import LoginIcon from '@mui/icons-material/Login'
|
|
|
|
|
|
import PersonAddIcon from '@mui/icons-material/PersonAdd'
|
2025-01-04 19:10:21 +04:00
|
|
|
|
import {createDirectus, realtime, staticToken} from '@directus/sdk'
|
2024-05-20 16:46:25 +04:00
|
|
|
|
import ConfirmationAlert from './confirmation-alert.js'
|
2024-05-20 04:17:09 +04:00
|
|
|
|
|
2026-01-04 13:06:24 +04:00
|
|
|
|
const apiUrl = process.env.DIRECTUS_API_URL || process.env.NEXT_PUBLIC_DIRECTUS_API_URL
|
2026-01-24 13:22:35 +04:00
|
|
|
|
const disableWebSocket = process.env.NEXT_PUBLIC_DISABLE_WEBSOCKET === 'true'
|
2025-01-04 19:10:21 +04:00
|
|
|
|
|
2024-05-20 14:43:29 +04:00
|
|
|
|
const LightTooltip = styled(({className, ...props}) => (
|
|
|
|
|
|
<Tooltip {...props} classes={{popper: className}} />
|
|
|
|
|
|
))(({theme}) => ({
|
|
|
|
|
|
[`& .${tooltipClasses.tooltip}`]: {
|
|
|
|
|
|
backgroundColor: theme.palette.common.white,
|
|
|
|
|
|
color: 'rgba(0, 0, 0, 0.87)',
|
|
|
|
|
|
boxShadow: theme.shadows[1],
|
|
|
|
|
|
fontSize: 15,
|
|
|
|
|
|
},
|
|
|
|
|
|
}))
|
|
|
|
|
|
|
2024-09-15 18:04:04 +04:00
|
|
|
|
export default function Sign({session, navButton}) {
|
2024-05-20 04:17:09 +04:00
|
|
|
|
const router = useRouter()
|
2024-05-20 16:46:25 +04:00
|
|
|
|
const [isOpen, setIsOpen] = useState(false)
|
|
|
|
|
|
|
|
|
|
|
|
const handleSignout = () => {
|
|
|
|
|
|
setIsOpen(false)
|
|
|
|
|
|
signOut()
|
|
|
|
|
|
}
|
2024-05-20 04:17:09 +04:00
|
|
|
|
|
2025-01-04 19:10:21 +04:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
let cleanup = () => {}
|
|
|
|
|
|
|
2026-01-24 13:22:35 +04:00
|
|
|
|
if (disableWebSocket) {
|
|
|
|
|
|
return () => cleanup()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-04 13:06:24 +04:00
|
|
|
|
if (session?.user?.accessToken) {
|
2025-01-04 19:10:21 +04:00
|
|
|
|
(async () => {
|
|
|
|
|
|
try {
|
2026-01-04 13:06:24 +04:00
|
|
|
|
console.log('Creating WebSocket client with token...')
|
|
|
|
|
|
|
|
|
|
|
|
// Create client with static token
|
|
|
|
|
|
const client = createDirectus(apiUrl)
|
|
|
|
|
|
.with(staticToken(session.user.accessToken))
|
|
|
|
|
|
.with(realtime())
|
2025-01-04 19:10:21 +04:00
|
|
|
|
|
2026-01-04 13:06:24 +04:00
|
|
|
|
console.log('Connecting to WebSocket...')
|
|
|
|
|
|
await client.connect()
|
|
|
|
|
|
|
|
|
|
|
|
client.onWebSocket('open', () => {
|
|
|
|
|
|
console.log({event: 'onopen', message: 'WebSocket connection opened'})
|
2025-01-04 19:10:21 +04:00
|
|
|
|
})
|
|
|
|
|
|
|
2026-01-04 13:06:24 +04:00
|
|
|
|
client.onWebSocket('message', message => {
|
|
|
|
|
|
console.log({event: 'onmessage', message})
|
|
|
|
|
|
|
|
|
|
|
|
// Once authenticated, subscribe
|
2025-01-04 19:10:21 +04:00
|
|
|
|
if (message.type === 'auth' && message.status === 'ok') {
|
2026-01-04 13:06:24 +04:00
|
|
|
|
console.log('WebSocket authenticated successfully!')
|
|
|
|
|
|
|
|
|
|
|
|
// Subscribe to version changes
|
|
|
|
|
|
;(async () => {
|
|
|
|
|
|
const {subscription} = await client.subscribe('directus_versions', {
|
|
|
|
|
|
event: 'create',
|
|
|
|
|
|
query: {
|
|
|
|
|
|
fields: ['*'],
|
|
|
|
|
|
filter: {
|
|
|
|
|
|
collection: {
|
|
|
|
|
|
_eq: 'titres'
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
for await (const item of subscription) {
|
|
|
|
|
|
console.log('New version created:', item)
|
|
|
|
|
|
}
|
|
|
|
|
|
})()
|
2025-01-04 19:10:21 +04:00
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2026-01-04 13:06:24 +04:00
|
|
|
|
client.onWebSocket('close', () => {
|
|
|
|
|
|
console.log({event: 'onclose', message: 'WebSocket connection closed'})
|
2025-01-04 19:10:21 +04:00
|
|
|
|
})
|
|
|
|
|
|
|
2026-01-04 13:06:24 +04:00
|
|
|
|
client.onWebSocket('error', error => {
|
2025-01-04 19:10:21 +04:00
|
|
|
|
console.log({event: 'onerror', error})
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
cleanup = () => {
|
2026-01-04 13:06:24 +04:00
|
|
|
|
console.log('Disconnecting WebSocket...')
|
|
|
|
|
|
client.disconnect()
|
2025-01-04 19:10:21 +04:00
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('WebSocket connection error:', error)
|
|
|
|
|
|
}
|
|
|
|
|
|
})()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return () => cleanup()
|
2026-01-04 13:06:24 +04:00
|
|
|
|
}, [session?.user?.accessToken])
|
2025-01-04 19:10:21 +04:00
|
|
|
|
|
2024-05-20 04:17:09 +04:00
|
|
|
|
return (
|
2024-05-20 16:46:25 +04:00
|
|
|
|
<>
|
2024-06-20 14:06:07 +04:00
|
|
|
|
<Box sx={{display: 'flex', justifyContent: session ? 'start' : 'center', marginTop: 2}}>
|
2024-05-20 16:46:25 +04:00
|
|
|
|
{session ? (
|
2024-06-20 14:06:07 +04:00
|
|
|
|
<Stack direction='row' spacing={2}>
|
|
|
|
|
|
<LightTooltip title='Se déconnecter' placement='right'>
|
|
|
|
|
|
<Fab size='large' color='error' onClick={() => setIsOpen(true)}>
|
|
|
|
|
|
<LogoutIcon fontSize='large' />
|
|
|
|
|
|
</Fab>
|
|
|
|
|
|
</LightTooltip>
|
2024-09-15 18:04:04 +04:00
|
|
|
|
<LightTooltip title={navButton.title} placement='right'>
|
|
|
|
|
|
<Fab sx={{mr: 3}} size='large' color={navButton.color} onClick={() => router.push(navButton.path)}>
|
|
|
|
|
|
{navButton.icon}
|
2024-06-20 14:06:07 +04:00
|
|
|
|
</Fab>
|
|
|
|
|
|
</LightTooltip>
|
|
|
|
|
|
</Stack>
|
2024-05-20 16:46:25 +04:00
|
|
|
|
) : (
|
|
|
|
|
|
<Stack direction='row' spacing={2}>
|
|
|
|
|
|
<LightTooltip title='Se connecter' placement='left'>
|
|
|
|
|
|
<Fab size='large' color='success' onClick={() => router.push('/login')}>
|
|
|
|
|
|
<LoginIcon fontSize='large' />
|
|
|
|
|
|
</Fab>
|
|
|
|
|
|
</LightTooltip>
|
|
|
|
|
|
<LightTooltip title='S’enregistrer' placement='right'>
|
|
|
|
|
|
<Fab size='large' color='success' onClick={() => router.push('/register')}>
|
|
|
|
|
|
<PersonAddIcon fontSize='large' />
|
|
|
|
|
|
</Fab>
|
|
|
|
|
|
</LightTooltip>
|
|
|
|
|
|
</Stack>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</Box>
|
|
|
|
|
|
<ConfirmationAlert
|
|
|
|
|
|
title='Se déconnecter'
|
|
|
|
|
|
description='Vous êtes sur le point de vous déconnecter. Voulez-vous continuer ?'
|
|
|
|
|
|
isOpen={isOpen}
|
|
|
|
|
|
setIsOpen={setIsOpen}
|
|
|
|
|
|
handleConfirmation={handleSignout}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</>
|
2024-05-20 04:17:09 +04:00
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Sign.propTypes = {
|
2024-09-15 18:04:04 +04:00
|
|
|
|
session: PropTypes.object,
|
|
|
|
|
|
navButton: PropTypes.object.isRequired
|
2024-05-20 04:17:09 +04:00
|
|
|
|
}
|