2023-08-18 05:26:22 -04:00
|
|
|
import React, { useEffect } from 'react'
|
2020-11-26 09:22:30 -05:00
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
|
|
|
|
import withErrorBoundary from '../../../infrastructure/error-boundary'
|
2022-01-10 10:46:46 -05:00
|
|
|
import { useProjectContext } from '../../../shared/context/project-context'
|
2022-01-10 10:47:01 -05:00
|
|
|
import { useFileTreeData } from '../../../shared/context/file-tree-data-context'
|
2020-11-26 09:22:30 -05:00
|
|
|
import FileTreeContext from './file-tree-context'
|
|
|
|
import FileTreeDraggablePreviewLayer from './file-tree-draggable-preview-layer'
|
|
|
|
import FileTreeFolderList from './file-tree-folder-list'
|
|
|
|
import FileTreeToolbar from './file-tree-toolbar'
|
|
|
|
import FileTreeModalDelete from './modals/file-tree-modal-delete'
|
|
|
|
import FileTreeModalCreateFolder from './modals/file-tree-modal-create-folder'
|
2020-12-09 06:55:36 -05:00
|
|
|
import FileTreeModalError from './modals/file-tree-modal-error'
|
2020-11-26 09:22:30 -05:00
|
|
|
import FileTreeContextMenu from './file-tree-context-menu'
|
|
|
|
import FileTreeError from './file-tree-error'
|
|
|
|
|
|
|
|
import { useDroppable } from '../contexts/file-tree-draggable'
|
|
|
|
|
|
|
|
import { useFileTreeSocketListener } from '../hooks/file-tree-socket-listener'
|
2021-03-18 05:52:36 -04:00
|
|
|
import FileTreeModalCreateFile from './modals/file-tree-modal-create-file'
|
2023-08-18 05:26:22 -04:00
|
|
|
import FileTreeInner from './file-tree-inner'
|
2023-10-25 05:24:58 -04:00
|
|
|
import { useDragLayer } from 'react-dnd'
|
|
|
|
import classnames from 'classnames'
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2021-06-21 06:02:38 -04:00
|
|
|
const FileTreeRoot = React.memo(function FileTreeRoot({
|
2021-03-18 05:52:36 -04:00
|
|
|
refProviders,
|
|
|
|
reindexReferences,
|
|
|
|
setRefProviderEnabled,
|
|
|
|
setStartedFreeTrial,
|
2020-11-26 09:22:30 -05:00
|
|
|
onSelect,
|
2021-01-14 08:57:33 -05:00
|
|
|
onInit,
|
2021-04-27 03:52:58 -04:00
|
|
|
isConnected,
|
2020-11-26 09:22:30 -05:00
|
|
|
}) {
|
2022-01-10 10:47:01 -05:00
|
|
|
const { _id: projectId } = useProjectContext(projectContextPropTypes)
|
|
|
|
const { fileTreeData } = useFileTreeData()
|
|
|
|
const isReady = projectId && fileTreeData
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2020-12-15 05:23:54 -05:00
|
|
|
useEffect(() => {
|
|
|
|
if (isReady) onInit()
|
2021-01-07 09:22:19 -05:00
|
|
|
}, [isReady, onInit])
|
2020-11-26 09:22:30 -05:00
|
|
|
if (!isReady) return null
|
|
|
|
|
|
|
|
return (
|
|
|
|
<FileTreeContext
|
2021-03-18 05:52:36 -04:00
|
|
|
refProviders={refProviders}
|
|
|
|
setRefProviderEnabled={setRefProviderEnabled}
|
|
|
|
setStartedFreeTrial={setStartedFreeTrial}
|
|
|
|
reindexReferences={reindexReferences}
|
2020-11-26 09:22:30 -05:00
|
|
|
onSelect={onSelect}
|
|
|
|
>
|
2021-01-14 08:57:33 -05:00
|
|
|
{isConnected ? null : <div className="disconnected-overlay" />}
|
2020-11-26 09:22:30 -05:00
|
|
|
<FileTreeToolbar />
|
|
|
|
<FileTreeContextMenu />
|
2023-08-18 05:26:22 -04:00
|
|
|
<FileTreeInner>
|
|
|
|
<FileTreeRootFolder />
|
|
|
|
</FileTreeInner>
|
2020-11-26 09:22:30 -05:00
|
|
|
<FileTreeModalDelete />
|
2021-04-29 05:11:00 -04:00
|
|
|
<FileTreeModalCreateFile />
|
2020-11-26 09:22:30 -05:00
|
|
|
<FileTreeModalCreateFolder />
|
2020-12-09 06:55:36 -05:00
|
|
|
<FileTreeModalError />
|
2020-11-26 09:22:30 -05:00
|
|
|
</FileTreeContext>
|
|
|
|
)
|
2021-06-21 06:02:38 -04:00
|
|
|
})
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2023-08-18 05:26:22 -04:00
|
|
|
function FileTreeRootFolder() {
|
2020-11-26 09:22:30 -05:00
|
|
|
useFileTreeSocketListener()
|
2022-01-10 10:47:01 -05:00
|
|
|
const { fileTreeData } = useFileTreeData()
|
2020-11-26 09:22:30 -05:00
|
|
|
|
|
|
|
const { isOver, dropRef } = useDroppable(fileTreeData._id)
|
|
|
|
|
2023-10-25 05:24:58 -04:00
|
|
|
const dragLayer = useDragLayer(monitor => ({
|
|
|
|
isDragging: monitor.isDragging(),
|
|
|
|
item: monitor.getItem(),
|
|
|
|
clientOffset: monitor.getClientOffset(),
|
|
|
|
}))
|
|
|
|
|
2020-11-26 09:22:30 -05:00
|
|
|
return (
|
|
|
|
<>
|
2023-10-25 05:24:58 -04:00
|
|
|
<FileTreeDraggablePreviewLayer isOver={isOver} {...dragLayer} />
|
2020-11-26 09:22:30 -05:00
|
|
|
<FileTreeFolderList
|
|
|
|
folders={fileTreeData.folders}
|
|
|
|
docs={fileTreeData.docs}
|
|
|
|
files={fileTreeData.fileRefs}
|
2023-10-25 05:24:58 -04:00
|
|
|
classes={{
|
|
|
|
root: classnames('file-tree-list', {
|
|
|
|
'file-tree-dragging': dragLayer.isDragging,
|
|
|
|
}),
|
|
|
|
}}
|
2020-11-26 09:22:30 -05:00
|
|
|
dropRef={dropRef}
|
2023-08-18 05:26:22 -04:00
|
|
|
dataTestId="file-tree-list-root"
|
2023-09-15 03:13:11 -04:00
|
|
|
/>
|
2020-11-26 09:22:30 -05:00
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
FileTreeRoot.propTypes = {
|
|
|
|
onSelect: PropTypes.func.isRequired,
|
2021-01-14 08:57:33 -05:00
|
|
|
onInit: PropTypes.func.isRequired,
|
2021-03-18 05:52:36 -04:00
|
|
|
isConnected: PropTypes.bool.isRequired,
|
|
|
|
setRefProviderEnabled: PropTypes.func.isRequired,
|
|
|
|
setStartedFreeTrial: PropTypes.func.isRequired,
|
|
|
|
reindexReferences: PropTypes.func.isRequired,
|
2021-04-27 03:52:58 -04:00
|
|
|
refProviders: PropTypes.object.isRequired,
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
|
|
|
|
2022-01-10 10:46:46 -05:00
|
|
|
const projectContextPropTypes = {
|
|
|
|
_id: PropTypes.string.isRequired,
|
|
|
|
}
|
|
|
|
|
2020-11-26 09:22:30 -05:00
|
|
|
export default withErrorBoundary(FileTreeRoot, FileTreeError)
|