2022-11-03 10:24:13 -04:00
|
|
|
import { useCallback, useEffect, useRef, useState } from 'react'
|
|
|
|
import { Button } from 'react-bootstrap'
|
|
|
|
import useScopeValue from '../../../shared/hooks/use-scope-value'
|
2022-11-03 11:38:37 -04:00
|
|
|
import getMeta from '../../../utils/meta'
|
2022-12-06 09:56:12 -05:00
|
|
|
import {
|
|
|
|
hasSeenCM6SwitchAwaySurvey,
|
|
|
|
setHasSeenCM6SwitchAwaySurvey,
|
|
|
|
} from '../utils/switch-away-survey'
|
|
|
|
import { sendMB } from '../../../infrastructure/event-tracking'
|
2022-11-03 10:24:13 -04:00
|
|
|
|
|
|
|
type CM6SwitchAwaySurveyState = 'disabled' | 'enabled' | 'shown'
|
|
|
|
|
|
|
|
export default function CM6SwitchAwaySurvey() {
|
|
|
|
const [state, setState] = useState<CM6SwitchAwaySurveyState>('disabled')
|
|
|
|
const [newSourceEditor] = useScopeValue('editor.newSourceEditor')
|
|
|
|
const [richText] = useScopeValue('editor.showRichText')
|
|
|
|
const initialRichTextPreference = useRef<boolean>(richText)
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-12-06 09:56:12 -05:00
|
|
|
// If cm6 is not available, don't show the survey
|
2022-11-03 11:38:37 -04:00
|
|
|
if (!getMeta('ol-showNewSourceEditorOption')) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-06 09:56:12 -05:00
|
|
|
// If the user has previously seen any switch-away survey, then don't show
|
|
|
|
// the current one
|
|
|
|
if (hasSeenCM6SwitchAwaySurvey()) return
|
2022-11-03 10:24:13 -04:00
|
|
|
|
|
|
|
if (initialRichTextPreference.current) {
|
|
|
|
if (!richText && newSourceEditor) {
|
2022-12-06 09:56:12 -05:00
|
|
|
// If user change from rich text to cm6, we remove the rich text
|
|
|
|
// preference so if user use rich text -> cm6 -> ace, we will show the
|
|
|
|
// current survey
|
2022-11-03 10:24:13 -04:00
|
|
|
initialRichTextPreference.current = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the user loaded rich text initially, then don't show the survey
|
|
|
|
// (we are assuming that they will not have used CM6 as much)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-07 09:25:29 -05:00
|
|
|
if (!newSourceEditor && !richText) {
|
2022-11-03 10:24:13 -04:00
|
|
|
setState('enabled')
|
2022-11-07 09:25:29 -05:00
|
|
|
} else {
|
|
|
|
setState('disabled')
|
2022-11-03 10:24:13 -04:00
|
|
|
}
|
|
|
|
}, [newSourceEditor, richText])
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-11-07 09:25:29 -05:00
|
|
|
const handleKeyDown = () => {
|
|
|
|
const TIME_FOR_SURVEY_TO_APPEAR = 3000
|
2022-11-03 10:24:13 -04:00
|
|
|
|
2022-11-07 09:25:29 -05:00
|
|
|
setTimeout(() => {
|
|
|
|
if (state === 'enabled') {
|
2022-11-03 10:24:13 -04:00
|
|
|
setState('shown')
|
2022-12-06 09:56:12 -05:00
|
|
|
setHasSeenCM6SwitchAwaySurvey()
|
2022-11-07 09:25:29 -05:00
|
|
|
}
|
|
|
|
}, TIME_FOR_SURVEY_TO_APPEAR)
|
2022-11-03 10:24:13 -04:00
|
|
|
}
|
2022-11-07 09:25:29 -05:00
|
|
|
|
|
|
|
// can't access the ace editor directly, so add the keydown event
|
|
|
|
// to window
|
|
|
|
window?.addEventListener('keydown', handleKeyDown, { once: true })
|
|
|
|
|
|
|
|
return () => window?.removeEventListener('keydown', handleKeyDown)
|
2022-11-03 10:24:13 -04:00
|
|
|
}, [state])
|
|
|
|
|
|
|
|
const handleClose = useCallback(() => {
|
|
|
|
setState('disabled')
|
|
|
|
}, [])
|
|
|
|
|
2022-12-06 09:56:12 -05:00
|
|
|
const handleFollowLink = useCallback(() => {
|
|
|
|
sendMB('cm6-switch-away-survey')
|
|
|
|
setState('disabled')
|
|
|
|
}, [])
|
|
|
|
|
2022-11-03 10:24:13 -04:00
|
|
|
if (state !== 'shown') {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="alert alert-success cm6-switch-away-survey" role="alert">
|
|
|
|
<Button
|
|
|
|
className="close"
|
|
|
|
data-dismiss="alert"
|
|
|
|
aria-label="Close"
|
|
|
|
onClick={handleClose}
|
|
|
|
>
|
|
|
|
<span aria-hidden="true">×</span>
|
|
|
|
</Button>
|
|
|
|
<div className="warning-content">
|
|
|
|
<div>
|
|
|
|
<div className="warning-text">
|
|
|
|
We noticed that you're still using the{' '}
|
|
|
|
<strong>Source (legacy)</strong> editor.
|
|
|
|
</div>
|
|
|
|
<div className="warning-text">Could you let us know why?</div>
|
|
|
|
</div>
|
|
|
|
<div style={{ display: 'inline-flex' }}>
|
|
|
|
<a
|
2022-12-06 09:56:12 -05:00
|
|
|
href="https://forms.gle/Ygv8gLZ4N8LepQj56"
|
2022-11-03 10:24:13 -04:00
|
|
|
className="btn btn-sm btn-info"
|
|
|
|
target="_blank"
|
|
|
|
rel="noreferrer"
|
2022-12-06 09:56:12 -05:00
|
|
|
onClick={handleFollowLink}
|
2022-11-03 10:24:13 -04:00
|
|
|
>
|
|
|
|
Take survey
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|