Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prend en compte le statut de la session collaborative #1168

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 19 additions & 23 deletions front/src/components/collaborative/CollaborativeEditorStatus.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import PropTypes from 'prop-types'
import React, { useCallback } from 'react'
import { StopCircle } from 'react-feather'
import { useTranslation } from 'react-i18next'
import { shallowEqual, useSelector } from 'react-redux'
import { useHistory } from 'react-router-dom'
import { useMutation } from '../../hooks/graphql.js'
import CollaborativeEditorWebSocketStatus from './CollaborativeEditorWebSocketStatus.jsx'
Expand All @@ -22,21 +21,12 @@ import styles from './CollaborativeEditorStatus.module.scss'
export default function CollaborativeEditorStatus({
articleId,
websocketStatus,
collaborativeSessionCreatorId,
collaborativeSessionState,
}) {
const { t } = useTranslation()
const mutation = useMutation()
const { setToast } = useToasts()
const history = useHistory()
const activeUser = useSelector(
(state) => ({
_id: state.activeUser._id,
email: state.activeUser.email,
displayName: state.activeUser.displayName,
username: state.activeUser.username,
}),
shallowEqual
)

const {
visible: collaborativeSessionEndVisible,
Expand Down Expand Up @@ -74,18 +64,24 @@ export default function CollaborativeEditorStatus({
<CollaborativeEditorWriters />
</div>
<div className={styles.status}>
<CollaborativeEditorWebSocketStatus status={websocketStatus} />
<CollaborativeEditorWebSocketStatus
status={websocketStatus}
state={collaborativeSessionState}
/>
</div>
<Button
className={clsx(styles.button)}
type="error"
ghost
auto
scale={0.4}
onClick={handleConfirmCollaborativeSessionEnd}
>
<StopCircle /> End collaborative session
</Button>
{websocketStatus === 'connected' &&
collaborativeSessionState === 'started' && (
<Button
className={clsx(styles.button)}
type="error"
ghost
auto
scale={0.4}
onClick={handleConfirmCollaborativeSessionEnd}
>
<StopCircle /> End collaborative session
</Button>
)}
</div>
<GeistModal
width="35rem"
Expand Down Expand Up @@ -114,5 +110,5 @@ export default function CollaborativeEditorStatus({
CollaborativeEditorStatus.propTypes = {
articleId: PropTypes.string.isRequired,
websocketStatus: PropTypes.string.isRequired,
collaborativeSessionCreatorId: PropTypes.string.isRequired,
collaborativeSessionState: PropTypes.string.isRequired,
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import React from 'react'
import PropTypes from 'prop-types'
import { Dot, Loading } from '@geist-ui/core'
import { Dot } from '@geist-ui/core'
import { Loader } from 'react-feather'

import styles from './CollaborativeEditorWebSocketStatus.module.scss'

export default function CollaborativeEditorWebSocketStatus({ status }) {
export default function CollaborativeEditorWebSocketStatus({ status, state }) {
if (state !== 'started') {
return (
<Dot type="warning" className={styles.dot}>
Connecting
<Loader className={styles.loadingIndicator} />
</Dot>
)
}
return (
<>
{status === 'connected' && (
Expand All @@ -19,13 +28,15 @@ export default function CollaborativeEditorWebSocketStatus({ status }) {
)}
{status === 'connecting' && (
<Dot type="warning" className={styles.dot}>
Connecting <Loading />
Connecting
<Loader className={styles.loadingIndicator} />
</Dot>
)}
</>
)
}

CollaborativeEditorWebSocketStatus.propTypes = {
state: PropTypes.string.isRequired,
status: PropTypes.string.isRequired,
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
.dot {
font-size: 0.8rem !important;

:global {
.label {
display: flex;
gap: 0.25rem;
}
}
}

.loadingIndicator {
height: 1em;
width: 1em;
animation: rotation 2.5s;
animation-iteration-count: infinite;
animation-timing-function: linear;
flex-shrink: 0;
}

@keyframes rotation {
from {
transform: rotate(0deg);
}
to {
transform: rotate(359deg);
}
}
39 changes: 21 additions & 18 deletions front/src/components/collaborative/CollaborativeTextEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export default function CollaborativeTextEditor({
const connectingRef = useRef(false)
const [dynamicStyles, setDynamicStyles] = useState('')
const [websocketStatus, setWebsocketStatus] = useState('')
const [collaborativeSessionState, setCollaborativeSessionState] = useState('')
const [yText, setYText] = useState(null)
const [awareness, setAwareness] = useState(null)
const { websocketEndpoint } = useSelector(
Expand All @@ -75,7 +76,9 @@ export default function CollaborativeTextEditor({
const options = useMemo(
() => ({
automaticLayout: true,
readOnly: websocketStatus !== 'connected',
readOnly:
websocketStatus !== 'connected' ||
collaborativeSessionState !== 'started',
contextmenu: websocketStatus === 'connected',
autoClosingBrackets: 'never',
wordBasedSuggestions: false,
Expand Down Expand Up @@ -143,8 +146,18 @@ export default function CollaborativeTextEditor({
[setWebsocketStatus]
)

const handleEditorDidMount = useCallback(
(editor) => {
editorRef.current = editor
new MonacoBinding(yText, editor.getModel(), new Set([editor]), awareness)
},
[yText, awareness]
)

useEffect(() => {
if (connectingRef.current) return
if (connectingRef.current) {
return
}
connectingRef.current = true
const {
awareness,
Expand All @@ -163,29 +176,19 @@ export default function CollaborativeTextEditor({
handleUpdateArticleStructureAndStats({ text: yText.toString() })
})
yState.observe(function () {
setCollaborativeSessionState(yState.toString())
onCollaborativeSessionStateUpdated({ state: yState.toString() })
})
setAwareness(awareness)
setYText(yText)
return () => {
try {
awareness.destroy()
wsProvider.disconnect()
wsProvider.destroy()
} catch (err) {
// try to disconnect...
}
connectingRef.current = false
awareness.destroy()
wsProvider.disconnect()
wsProvider.destroy()
}
}, [collaborativeSessionId, websocketEndpoint, writerInfo])

const handleEditorDidMount = useCallback(
(editor) => {
editorRef.current = editor
new MonacoBinding(yText, editor.getModel(), new Set([editor]), awareness)
},
[yText, awareness]
)

useEffect(() => {
const line = editorCursorPosition.lineNumber
const editor = editorRef.current
Expand All @@ -204,6 +207,7 @@ export default function CollaborativeTextEditor({
<style>{dynamicStyles}</style>
<CollaborativeEditorStatus
articleId={articleId}
collaborativeSessionState={collaborativeSessionState}
websocketStatus={websocketStatus}
collaborativeSessionCreatorId={collaborativeSessionCreatorId}
/>
Expand All @@ -212,7 +216,6 @@ export default function CollaborativeTextEditor({
className={styles.editor}
defaultLanguage="markdown"
onMount={handleEditorDidMount}
on
/>
</>
)
Expand Down
Loading