try-new-editor-button.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { useCallback, useState } from 'react'
  2. import OLButton from '../../shared/components/ol/ol-button'
  3. import { useTranslation } from 'react-i18next'
  4. import { useSwitchEnableNewEditorState } from '../ide-redesign/hooks/use-switch-enable-new-editor-state'
  5. import MaterialIcon from '@/shared/components/material-icon'
  6. import { useEditorAnalytics } from '@/shared/hooks/use-editor-analytics'
  7. import { useFeatureFlag } from '@/shared/context/split-test-context'
  8. import OldEditorWarningTooltip from '../ide-redesign/components/old-editor-warning-tooltip'
  9. const TryNewEditorButton = () => {
  10. const { t } = useTranslation()
  11. const { loading, setEditorRedesignStatus } = useSwitchEnableNewEditorState()
  12. const { sendEvent } = useEditorAnalytics()
  13. const isNewEditorOptOutStage = useFeatureFlag('editor-redesign-opt-out')
  14. const [buttonElt, setButtonElt] = useState<HTMLButtonElement | null>(null)
  15. const buttonRef = useCallback((node: HTMLButtonElement) => {
  16. if (node !== null) {
  17. setButtonElt(node)
  18. }
  19. }, [])
  20. const onClick = useCallback(() => {
  21. sendEvent('switch-to-new-editor', {
  22. location: 'toolbar',
  23. })
  24. setEditorRedesignStatus(true)
  25. }, [setEditorRedesignStatus, sendEvent])
  26. return (
  27. <div className="d-flex align-items-center">
  28. <OLButton
  29. className="toolbar-experiment-button try-new-editor-button"
  30. onClick={onClick}
  31. size="sm"
  32. variant="secondary"
  33. isLoading={loading}
  34. ref={buttonRef}
  35. >
  36. <MaterialIcon type="fiber_new" />
  37. {isNewEditorOptOutStage
  38. ? t('switch_to_new_look')
  39. : t('try_the_new_editor_design')}
  40. </OLButton>
  41. {isNewEditorOptOutStage && <OldEditorWarningTooltip target={buttonElt} />}
  42. </div>
  43. )
  44. }
  45. export default TryNewEditorButton