import classNames from 'classnames'
import { memo, useCallback, useMemo } from 'react'
import { useDetachCompileContext as useCompileContext } from '../../../shared/context/detach-compile-context'
import { useLayoutContext } from '../../../shared/context/layout-context'
import { useTranslation } from 'react-i18next'
import * as eventTracking from '../../../infrastructure/event-tracking'
import OLTooltip from '@/features/ui/components/ol/ol-tooltip'
import OLButton from '@/features/ui/components/ol/ol-button'
import MaterialIcon from '@/shared/components/material-icon'
import { Spinner } from 'react-bootstrap'
import { Placement } from 'react-bootstrap/types'
import useSynctex from '../hooks/use-synctex'
import { useFeatureFlag } from '@/shared/context/split-test-context'
const GoToCodeButton = memo(function GoToCodeButton({
syncToCode,
syncToCodeInFlight,
isDetachLayout,
}: {
syncToCode: ({ visualOffset }: { visualOffset: number }) => void
syncToCodeInFlight: boolean
isDetachLayout?: boolean
}) {
const { t } = useTranslation()
const buttonClasses = classNames('synctex-control', {
'detach-synctex-control': !!isDetachLayout,
})
let buttonIcon = null
if (syncToCodeInFlight) {
buttonIcon = (
)
} else if (!isDetachLayout) {
buttonIcon = (
)
}
const syncToCodeWithButton = useCallback(() => {
eventTracking.sendMB('jump-to-location', {
direction: 'pdf-location-in-code',
method: 'arrow',
})
syncToCode({ visualOffset: 72 })
}, [syncToCode])
const overlayProps = useMemo(
() => ({
placement: (isDetachLayout ? 'bottom' : 'right') as Placement,
}),
[isDetachLayout]
)
return (
{buttonIcon}
{isDetachLayout ? {t('show_in_code')} : ''}
)
})
const GoToPdfButton = memo(function GoToPdfButton({
syncToPdf,
syncToPdfInFlight,
isDetachLayout,
canSyncToPdf,
}: {
syncToPdf: () => void
syncToPdfInFlight: boolean
canSyncToPdf: boolean
isDetachLayout?: boolean
}) {
const { t } = useTranslation()
const tooltipPlacement = isDetachLayout ? 'bottom' : 'right'
const buttonClasses = classNames('synctex-control', {
'detach-synctex-control': !!isDetachLayout,
})
let buttonIcon = null
if (syncToPdfInFlight) {
buttonIcon = (
)
} else if (!isDetachLayout) {
buttonIcon = (
)
}
return (
{buttonIcon}
{isDetachLayout ? {t('show_in_pdf')} : ''}
)
})
function PdfSynctexControls() {
const { detachRole } = useLayoutContext()
const { pdfUrl, pdfViewer, position } = useCompileContext()
const {
syncToCode,
syncToPdf,
syncToCodeInFlight,
syncToPdfInFlight,
canSyncToPdf,
} = useSynctex()
const visualPreviewEnabled = useFeatureFlag('visual-preview')
if (visualPreviewEnabled) {
return null
}
if (!position) {
return null
}
if (!pdfUrl || pdfViewer === 'native') {
return null
}
if (detachRole === 'detacher') {
return (
)
} else if (detachRole === 'detached') {
return (
)
} else {
return (
<>
>
)
}
}
export default memo(PdfSynctexControls)