2021-11-15 11:33:57 -05:00
|
|
|
import {
|
|
|
|
createContext,
|
|
|
|
useContext,
|
|
|
|
useCallback,
|
|
|
|
useMemo,
|
|
|
|
useEffect,
|
2023-10-18 05:14:13 -04:00
|
|
|
Dispatch,
|
|
|
|
SetStateAction,
|
|
|
|
FC,
|
2021-11-15 11:33:57 -05:00
|
|
|
} from 'react'
|
2021-10-08 06:09:41 -04:00
|
|
|
import useScopeValue from '../hooks/use-scope-value'
|
2021-11-15 11:33:57 -05:00
|
|
|
import useDetachLayout from '../hooks/use-detach-layout'
|
2021-10-15 05:39:56 -04:00
|
|
|
import localStorage from '../../infrastructure/local-storage'
|
2021-11-30 09:54:14 -05:00
|
|
|
import getMeta from '../../utils/meta'
|
2024-01-26 04:23:48 -05:00
|
|
|
import { DetachRole } from './detach-context'
|
2023-09-27 05:45:49 -04:00
|
|
|
import { debugConsole } from '@/utils/debugging'
|
2023-10-18 05:14:13 -04:00
|
|
|
import { BinaryFile } from '@/features/file-view/types/binary-file'
|
2023-11-28 06:24:23 -05:00
|
|
|
import useScopeEventEmitter from '@/shared/hooks/use-scope-event-emitter'
|
2024-09-16 04:40:59 -04:00
|
|
|
import useEventListener from '../hooks/use-event-listener'
|
2023-10-18 05:14:13 -04:00
|
|
|
|
|
|
|
export type IdeLayout = 'sideBySide' | 'flat'
|
|
|
|
export type IdeView = 'editor' | 'file' | 'pdf' | 'history'
|
|
|
|
|
|
|
|
type LayoutContextValue = {
|
|
|
|
reattach: () => void
|
|
|
|
detach: () => void
|
|
|
|
detachIsLinked: boolean
|
2024-01-26 04:23:48 -05:00
|
|
|
detachRole: DetachRole
|
2023-10-18 05:14:13 -04:00
|
|
|
changeLayout: (newLayout: IdeLayout, newView?: IdeView) => void
|
2024-01-26 04:23:48 -05:00
|
|
|
view: IdeView | null
|
|
|
|
setView: (view: IdeView | null) => void
|
2023-10-18 05:14:13 -04:00
|
|
|
chatIsOpen: boolean
|
|
|
|
setChatIsOpen: Dispatch<SetStateAction<LayoutContextValue['chatIsOpen']>>
|
|
|
|
reviewPanelOpen: boolean
|
|
|
|
setReviewPanelOpen: Dispatch<
|
|
|
|
SetStateAction<LayoutContextValue['reviewPanelOpen']>
|
|
|
|
>
|
2023-12-08 07:12:58 -05:00
|
|
|
miniReviewPanelVisible: boolean
|
|
|
|
setMiniReviewPanelVisible: Dispatch<
|
|
|
|
SetStateAction<LayoutContextValue['miniReviewPanelVisible']>
|
|
|
|
>
|
2023-10-18 05:14:13 -04:00
|
|
|
leftMenuShown: boolean
|
|
|
|
setLeftMenuShown: Dispatch<
|
|
|
|
SetStateAction<LayoutContextValue['leftMenuShown']>
|
|
|
|
>
|
|
|
|
loadingStyleSheet: boolean
|
|
|
|
setLoadingStyleSheet: Dispatch<
|
|
|
|
SetStateAction<LayoutContextValue['loadingStyleSheet']>
|
|
|
|
>
|
|
|
|
pdfLayout: IdeLayout
|
|
|
|
pdfPreviewOpen: boolean
|
|
|
|
}
|
2021-11-30 09:54:14 -05:00
|
|
|
|
|
|
|
const debugPdfDetach = getMeta('ol-debugPdfDetach')
|
2021-02-23 05:17:41 -05:00
|
|
|
|
2023-10-18 05:14:13 -04:00
|
|
|
export const LayoutContext = createContext<LayoutContextValue | undefined>(
|
|
|
|
undefined
|
|
|
|
)
|
2021-02-23 05:17:41 -05:00
|
|
|
|
2023-10-18 05:14:13 -04:00
|
|
|
function setLayoutInLocalStorage(pdfLayout: IdeLayout) {
|
2021-11-30 09:53:59 -05:00
|
|
|
localStorage.setItem(
|
|
|
|
'pdf.layout',
|
|
|
|
pdfLayout === 'sideBySide' ? 'split' : 'flat'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-10-18 05:14:13 -04:00
|
|
|
export const LayoutProvider: FC = ({ children }) => {
|
2021-10-08 05:51:04 -04:00
|
|
|
// what to show in the "flat" view (editor or pdf)
|
2024-01-26 04:23:48 -05:00
|
|
|
const [view, _setView] = useScopeValue<IdeView | null>('ui.view')
|
2023-10-18 05:14:13 -04:00
|
|
|
const [openFile] = useScopeValue<BinaryFile | null>('openFile')
|
2023-11-28 06:24:23 -05:00
|
|
|
const historyToggleEmitter = useScopeEventEmitter('history:toggle', true)
|
2021-06-16 05:32:38 -04:00
|
|
|
|
2021-05-31 04:25:32 -04:00
|
|
|
const setView = useCallback(
|
2024-01-26 04:23:48 -05:00
|
|
|
(value: IdeView | null) => {
|
2021-06-18 05:29:06 -04:00
|
|
|
_setView(oldValue => {
|
|
|
|
// ensure that the "history:toggle" event is broadcast when switching in or out of history view
|
|
|
|
if (value === 'history' || oldValue === 'history') {
|
2023-11-28 06:24:23 -05:00
|
|
|
historyToggleEmitter()
|
2021-06-18 05:29:06 -04:00
|
|
|
}
|
|
|
|
|
2023-10-18 05:14:13 -04:00
|
|
|
if (value === 'editor' && openFile) {
|
2022-03-15 09:55:17 -04:00
|
|
|
// if a file is currently opened, ensure the view is 'file' instead of
|
|
|
|
// 'editor' when the 'editor' view is requested. This is to ensure
|
|
|
|
// that the entity selected in the file tree is the one visible and
|
2023-10-18 05:14:13 -04:00
|
|
|
// that docs don't take precedence over files.
|
2022-03-15 09:55:17 -04:00
|
|
|
return 'file'
|
|
|
|
}
|
|
|
|
|
2021-06-18 05:29:06 -04:00
|
|
|
return value
|
|
|
|
})
|
2021-05-31 04:25:32 -04:00
|
|
|
},
|
2023-11-28 06:24:23 -05:00
|
|
|
[_setView, openFile, historyToggleEmitter]
|
2021-05-31 04:25:32 -04:00
|
|
|
)
|
|
|
|
|
2021-10-08 05:51:04 -04:00
|
|
|
// whether the chat pane is open
|
2023-10-18 05:14:13 -04:00
|
|
|
const [chatIsOpen, setChatIsOpen] = useScopeValue<boolean>('ui.chatOpen')
|
2021-10-08 05:51:04 -04:00
|
|
|
|
|
|
|
// whether the review pane is open
|
2022-01-10 05:23:05 -05:00
|
|
|
const [reviewPanelOpen, setReviewPanelOpen] =
|
|
|
|
useScopeValue('ui.reviewPanelOpen')
|
2021-10-08 05:51:04 -04:00
|
|
|
|
2023-12-08 07:12:58 -05:00
|
|
|
// whether the review pane is collapsed
|
|
|
|
const [miniReviewPanelVisible, setMiniReviewPanelVisible] =
|
|
|
|
useScopeValue<boolean>('ui.miniReviewPanelVisible')
|
|
|
|
|
2021-10-08 05:51:04 -04:00
|
|
|
// whether the menu pane is open
|
2023-10-18 05:14:13 -04:00
|
|
|
const [leftMenuShown, setLeftMenuShown] =
|
|
|
|
useScopeValue<boolean>('ui.leftMenuShown')
|
2021-10-08 05:51:04 -04:00
|
|
|
|
|
|
|
// whether to display the editor and preview side-by-side or full-width ("flat")
|
2023-10-18 05:14:13 -04:00
|
|
|
const [pdfLayout, setPdfLayout] = useScopeValue<IdeLayout>('ui.pdfLayout')
|
2021-10-08 05:51:04 -04:00
|
|
|
|
2022-11-14 11:48:06 -05:00
|
|
|
// whether stylesheet on theme is loading
|
2023-10-18 05:14:13 -04:00
|
|
|
const [loadingStyleSheet, setLoadingStyleSheet] = useScopeValue<boolean>(
|
2022-11-14 11:48:06 -05:00
|
|
|
'ui.loadingStyleSheet'
|
|
|
|
)
|
|
|
|
|
2021-11-03 09:21:14 -04:00
|
|
|
const changeLayout = useCallback(
|
2023-10-18 05:14:13 -04:00
|
|
|
(newLayout: IdeLayout, newView: IdeView = 'editor') => {
|
2021-11-03 09:21:14 -04:00
|
|
|
setPdfLayout(newLayout)
|
|
|
|
setView(newLayout === 'sideBySide' ? 'editor' : newView)
|
2021-11-30 09:53:59 -05:00
|
|
|
setLayoutInLocalStorage(newLayout)
|
2021-11-03 09:21:14 -04:00
|
|
|
},
|
|
|
|
[setPdfLayout, setView]
|
|
|
|
)
|
|
|
|
|
2021-11-15 11:33:57 -05:00
|
|
|
const {
|
|
|
|
reattach,
|
|
|
|
detach,
|
2021-11-30 09:54:14 -05:00
|
|
|
isLinking: detachIsLinking,
|
|
|
|
isLinked: detachIsLinked,
|
2021-11-15 11:33:57 -05:00
|
|
|
role: detachRole,
|
2022-03-31 07:22:36 -04:00
|
|
|
isRedundant: detachIsRedundant,
|
2021-11-15 11:33:57 -05:00
|
|
|
} = useDetachLayout()
|
|
|
|
|
2022-07-21 04:31:31 -04:00
|
|
|
const pdfPreviewOpen =
|
|
|
|
pdfLayout === 'sideBySide' || view === 'pdf' || detachRole === 'detacher'
|
|
|
|
|
2021-11-15 11:33:57 -05:00
|
|
|
useEffect(() => {
|
2021-11-30 09:54:14 -05:00
|
|
|
if (debugPdfDetach) {
|
2023-09-27 05:45:49 -04:00
|
|
|
debugConsole.warn('Layout Effect', {
|
2022-03-31 07:22:36 -04:00
|
|
|
detachIsRedundant,
|
2021-11-30 09:54:14 -05:00
|
|
|
detachRole,
|
|
|
|
detachIsLinking,
|
|
|
|
detachIsLinked,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (detachRole !== 'detacher') return // not in a PDF detacher layout
|
|
|
|
|
2022-03-31 07:22:36 -04:00
|
|
|
if (detachIsRedundant) {
|
|
|
|
changeLayout('sideBySide')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-30 09:54:14 -05:00
|
|
|
if (detachIsLinking || detachIsLinked) {
|
|
|
|
// the tab is linked to a detached tab (or about to be linked); show
|
|
|
|
// editor only
|
2021-12-02 10:18:38 -05:00
|
|
|
changeLayout('flat', 'editor')
|
2021-11-15 11:33:57 -05:00
|
|
|
}
|
2022-03-31 07:22:36 -04:00
|
|
|
}, [
|
|
|
|
detachIsRedundant,
|
|
|
|
detachRole,
|
|
|
|
detachIsLinking,
|
|
|
|
detachIsLinked,
|
|
|
|
changeLayout,
|
|
|
|
])
|
2021-11-15 11:33:57 -05:00
|
|
|
|
2024-09-16 04:40:59 -04:00
|
|
|
const handleSetReviewPanelOpenEvent = useCallback(
|
|
|
|
(e: Event) => {
|
|
|
|
const event = e as CustomEvent<{ isOpen: boolean }>
|
|
|
|
const { isOpen } = event.detail
|
|
|
|
setReviewPanelOpen(isOpen)
|
|
|
|
},
|
|
|
|
[setReviewPanelOpen]
|
|
|
|
)
|
|
|
|
|
|
|
|
useEventListener('set-review-panel-open', handleSetReviewPanelOpenEvent)
|
|
|
|
|
2023-10-18 05:14:13 -04:00
|
|
|
const value = useMemo<LayoutContextValue>(
|
2021-06-21 06:02:38 -04:00
|
|
|
() => ({
|
2021-11-15 11:33:57 -05:00
|
|
|
reattach,
|
|
|
|
detach,
|
2021-11-30 09:54:14 -05:00
|
|
|
detachIsLinked,
|
2021-11-15 11:33:57 -05:00
|
|
|
detachRole,
|
2021-11-03 09:21:14 -04:00
|
|
|
changeLayout,
|
2021-06-21 06:02:38 -04:00
|
|
|
chatIsOpen,
|
|
|
|
leftMenuShown,
|
|
|
|
pdfLayout,
|
2022-07-21 04:31:31 -04:00
|
|
|
pdfPreviewOpen,
|
2021-10-08 05:51:04 -04:00
|
|
|
reviewPanelOpen,
|
2023-12-08 07:12:58 -05:00
|
|
|
miniReviewPanelVisible,
|
2022-11-14 11:48:06 -05:00
|
|
|
loadingStyleSheet,
|
2021-10-08 05:51:04 -04:00
|
|
|
setChatIsOpen,
|
|
|
|
setLeftMenuShown,
|
|
|
|
setPdfLayout,
|
|
|
|
setReviewPanelOpen,
|
2023-12-08 07:12:58 -05:00
|
|
|
setMiniReviewPanelVisible,
|
2022-11-14 11:48:06 -05:00
|
|
|
setLoadingStyleSheet,
|
2021-10-08 05:51:04 -04:00
|
|
|
setView,
|
|
|
|
view,
|
2021-06-21 06:02:38 -04:00
|
|
|
}),
|
|
|
|
[
|
2021-11-15 11:33:57 -05:00
|
|
|
reattach,
|
|
|
|
detach,
|
2021-11-30 09:54:14 -05:00
|
|
|
detachIsLinked,
|
2021-11-15 11:33:57 -05:00
|
|
|
detachRole,
|
2021-11-03 09:21:14 -04:00
|
|
|
changeLayout,
|
2021-06-21 06:02:38 -04:00
|
|
|
chatIsOpen,
|
|
|
|
leftMenuShown,
|
|
|
|
pdfLayout,
|
2022-07-21 04:31:31 -04:00
|
|
|
pdfPreviewOpen,
|
2021-06-21 06:02:38 -04:00
|
|
|
reviewPanelOpen,
|
2023-12-08 07:12:58 -05:00
|
|
|
miniReviewPanelVisible,
|
2022-11-14 11:48:06 -05:00
|
|
|
loadingStyleSheet,
|
2021-06-21 06:02:38 -04:00
|
|
|
setChatIsOpen,
|
|
|
|
setLeftMenuShown,
|
2021-10-08 05:51:04 -04:00
|
|
|
setPdfLayout,
|
2021-06-21 06:02:38 -04:00
|
|
|
setReviewPanelOpen,
|
2023-12-08 07:12:58 -05:00
|
|
|
setMiniReviewPanelVisible,
|
2022-11-14 11:48:06 -05:00
|
|
|
setLoadingStyleSheet,
|
2021-06-21 06:02:38 -04:00
|
|
|
setView,
|
|
|
|
view,
|
|
|
|
]
|
|
|
|
)
|
2021-02-23 05:17:41 -05:00
|
|
|
|
|
|
|
return (
|
2021-06-16 05:32:38 -04:00
|
|
|
<LayoutContext.Provider value={value}>{children}</LayoutContext.Provider>
|
2021-02-23 05:17:41 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-10-18 05:14:13 -04:00
|
|
|
export function useLayoutContext() {
|
|
|
|
const context = useContext(LayoutContext)
|
|
|
|
if (!context) {
|
|
|
|
throw new Error('useLayoutContext is only available inside LayoutProvider')
|
|
|
|
}
|
|
|
|
return context
|
2021-02-23 05:17:41 -05:00
|
|
|
}
|