import { DropdownItem, DropdownHeader, DropdownDivider, } from '@/shared/components/dropdown/dropdown-menu' import { IdeLayout, IdeView, useLayoutContext, } from '@/shared/context/layout-context' import React from 'react' import { useTranslation } from 'react-i18next' import { DetachRole } from '@/shared/context/detach-context' import OLSpinner from '@/shared/components/ol/ol-spinner' import { isMac } from '@/shared/utils/os' import { Shortcut } from '@/shared/components/shortcut' import classNames from 'classnames' import { useEditorAnalytics } from '@/shared/hooks/use-editor-analytics' import { useFeatureFlag } from '@/shared/context/split-test-context' import SplitTestBadge from '@/shared/components/split-test-badge' type LayoutOption = | 'sideBySide' | 'editorOnly' | 'pdfOnly' | 'detachedPdf' | 'focusMode' const getActiveLayoutOption = ({ pdfLayout, view, detachRole, }: { pdfLayout: IdeLayout view: IdeView | null detachRole?: DetachRole }): LayoutOption | null => { if (view === 'history') { return null } if (detachRole === 'detacher') { return 'detachedPdf' } if (pdfLayout === 'flat' && (view === 'editor' || view === 'file')) { return 'editorOnly' } if (pdfLayout === 'flat' && view === 'pdf') { return 'pdfOnly' } if (pdfLayout === 'sideBySide') { return 'sideBySide' } return null } const LayoutDropdownItem = ({ active, disabled = false, processing = false, leadingIcon, trailingIcon, onClick, children, }: { active: boolean leadingIcon: React.ReactNode trailingIcon?: React.ReactNode onClick: () => void children: React.ReactNode processing?: boolean disabled?: boolean }) => { if (processing) { leadingIcon = } else if (active) { leadingIcon = 'check' } return ( {children} ) } const shortcuts: Record = isMac ? { editorOnly: ['⌃', '⌘', '←'], pdfOnly: ['⌃', '⌘', '→'], sideBySide: ['⌃', '⌘', '↓'], detachedPdf: ['⌃', '⌘', '↑'], focusMode: ['⌘', '⇧', 'M'], } : { editorOnly: null, pdfOnly: null, sideBySide: null, detachedPdf: null, focusMode: ['⌃', '⇧', 'M'], } export default function ChangeLayoutOptions() { const { detachIsLinked, detachRole, view, pdfLayout, handleChangeLayout, handleDetach, focusMode, setFocusMode, } = useLayoutContext() const { t } = useTranslation() const { sendEvent } = useEditorAnalytics() const focusModeEnabled = useFeatureFlag('focus-mode') const detachable = 'BroadcastChannel' in window const activeLayoutOption = getActiveLayoutOption({ pdfLayout, view, detachRole, }) const waitingForDetachedLink = !detachIsLinked && detachRole === 'detacher' return ( <> {t('layout_options')} handleChangeLayout('sideBySide')} active={activeLayoutOption === 'sideBySide'} leadingIcon="splitscreen_right" trailingIcon={ shortcuts.sideBySide && } > {t('split_view')} handleChangeLayout('flat', 'editor')} active={activeLayoutOption === 'editorOnly'} leadingIcon="edit" trailingIcon={ shortcuts.editorOnly && } > {t('editor_only')} handleChangeLayout('flat', 'pdf')} active={activeLayoutOption === 'pdfOnly'} leadingIcon="picture_as_pdf" trailingIcon={ shortcuts.pdfOnly && } > {t('pdf_only')} handleDetach()} active={activeLayoutOption === 'detachedPdf' && detachIsLinked} disabled={!detachable} leadingIcon="open_in_new" trailingIcon={ shortcuts.detachedPdf && } processing={waitingForDetachedLink} > {t('open_pdf_in_separate_tab')} {focusModeEnabled && ( <> { setFocusMode(!focusMode) sendEvent('focus-mode-toggle', { focusMode: !focusMode }) }} active={focusMode} leadingIcon="crop_free" trailingIcon={ {shortcuts.focusMode && } } > {t('focus_mode')} )} ) }