2023-10-26 04:57:00 -04:00
|
|
|
import React, { useCallback, useState } from 'react'
|
|
|
|
import TwoColumnMainContent from '@/features/ide-react/components/layout/two-column-main-content'
|
|
|
|
import Editor from '@/features/ide-react/components/editor/editor'
|
|
|
|
import EditorSidebar from '@/features/ide-react/components/editor-sidebar'
|
|
|
|
import customLocalStorage from '@/infrastructure/local-storage'
|
2023-10-27 06:50:57 -04:00
|
|
|
import History from '@/features/ide-react/components/history'
|
|
|
|
import { HistoryProvider } from '@/features/history/context/history-context'
|
2023-10-26 04:57:00 -04:00
|
|
|
import { useProjectContext } from '@/shared/context/project-context'
|
2023-10-27 06:50:57 -04:00
|
|
|
import { useLayoutContext } from '@/shared/context/layout-context'
|
2023-10-26 04:57:00 -04:00
|
|
|
import { FileTreeFindResult } from '@/features/ide-react/types/file-tree'
|
|
|
|
|
|
|
|
type EditorAndSidebarProps = {
|
|
|
|
shouldPersistLayout: boolean
|
|
|
|
leftColumnDefaultSize: number
|
|
|
|
setLeftColumnDefaultSize: React.Dispatch<React.SetStateAction<number>>
|
|
|
|
}
|
|
|
|
|
|
|
|
export function EditorAndSidebar({
|
|
|
|
shouldPersistLayout,
|
|
|
|
leftColumnDefaultSize,
|
|
|
|
setLeftColumnDefaultSize,
|
|
|
|
}: EditorAndSidebarProps) {
|
|
|
|
const [leftColumnIsOpen, setLeftColumnIsOpen] = useState(true)
|
|
|
|
const { rootDocId, _id: projectId } = useProjectContext()
|
2023-10-27 06:50:57 -04:00
|
|
|
const { view } = useLayoutContext()
|
|
|
|
const historyIsOpen = view === 'history'
|
2023-10-26 04:57:00 -04:00
|
|
|
|
|
|
|
const [openDocId, setOpenDocId] = useState(
|
|
|
|
() => customLocalStorage.getItem(`doc.open_id.${projectId}`) || rootDocId
|
|
|
|
)
|
|
|
|
const [fileTreeReady, setFileTreeReady] = useState(false)
|
|
|
|
|
|
|
|
const handleFileTreeInit = useCallback(() => {
|
|
|
|
setFileTreeReady(true)
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const handleFileTreeSelect = useCallback(
|
|
|
|
(selectedEntities: FileTreeFindResult[]) => {
|
|
|
|
const firstDocId =
|
|
|
|
selectedEntities.find(result => result.type === 'doc')?.entity._id ||
|
|
|
|
null
|
|
|
|
setOpenDocId(firstDocId)
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
|
|
|
|
const leftColumnContent = (
|
|
|
|
<EditorSidebar
|
|
|
|
shouldPersistLayout={shouldPersistLayout}
|
|
|
|
onFileTreeInit={handleFileTreeInit}
|
|
|
|
onFileTreeSelect={handleFileTreeSelect}
|
|
|
|
/>
|
|
|
|
)
|
2023-10-27 06:50:57 -04:00
|
|
|
|
2023-10-26 04:57:00 -04:00
|
|
|
const rightColumnContent = (
|
2023-10-27 06:50:57 -04:00
|
|
|
<>
|
|
|
|
{/* Recreate the history context when the history view is toggled */}
|
|
|
|
{historyIsOpen && (
|
|
|
|
<HistoryProvider>
|
|
|
|
<History />
|
|
|
|
</HistoryProvider>
|
|
|
|
)}
|
|
|
|
<Editor
|
|
|
|
shouldPersistLayout={shouldPersistLayout}
|
|
|
|
openDocId={openDocId}
|
|
|
|
fileTreeReady={fileTreeReady}
|
|
|
|
/>
|
|
|
|
</>
|
2023-10-26 04:57:00 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<TwoColumnMainContent
|
|
|
|
leftColumnId="editor-left-column"
|
|
|
|
leftColumnContent={leftColumnContent}
|
|
|
|
leftColumnDefaultSize={leftColumnDefaultSize}
|
|
|
|
setLeftColumnDefaultSize={setLeftColumnDefaultSize}
|
|
|
|
rightColumnContent={rightColumnContent}
|
|
|
|
leftColumnIsOpen={leftColumnIsOpen}
|
|
|
|
setLeftColumnIsOpen={setLeftColumnIsOpen}
|
|
|
|
shouldPersistLayout={shouldPersistLayout}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|