2024-02-05 06:44:05 -05:00
|
|
|
import React, { useEffect, useState } from 'react'
|
2020-11-26 09:22:30 -05:00
|
|
|
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'
|
2024-05-08 06:39:18 -04:00
|
|
|
import { pathInFolder } from '@/features/file-tree/util/path'
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2024-01-26 04:23:48 -05:00
|
|
|
const FileTreeRoot = React.memo<{
|
|
|
|
onSelect: () => void
|
|
|
|
onDelete: () => void
|
|
|
|
onInit: () => void
|
|
|
|
isConnected: boolean
|
|
|
|
setRefProviderEnabled: () => void
|
|
|
|
setStartedFreeTrial: () => void
|
|
|
|
refProviders: Record<string, boolean>
|
|
|
|
}>(function FileTreeRoot({
|
2021-03-18 05:52:36 -04:00
|
|
|
refProviders,
|
|
|
|
setRefProviderEnabled,
|
|
|
|
setStartedFreeTrial,
|
2020-11-26 09:22:30 -05:00
|
|
|
onSelect,
|
2021-01-14 08:57:33 -05:00
|
|
|
onInit,
|
2023-11-02 07:36:04 -04:00
|
|
|
onDelete,
|
2021-04-27 03:52:58 -04:00
|
|
|
isConnected,
|
2020-11-26 09:22:30 -05:00
|
|
|
}) {
|
2024-02-05 06:44:05 -05:00
|
|
|
const [fileTreeContainer, setFileTreeContainer] =
|
|
|
|
useState<HTMLDivElement | null>(null)
|
2024-01-26 04:23:48 -05:00
|
|
|
const { _id: projectId } = useProjectContext()
|
2022-01-10 10:47:01 -05:00
|
|
|
const { fileTreeData } = useFileTreeData()
|
2023-12-15 04:19:42 -05:00
|
|
|
const isReady = Boolean(projectId && fileTreeData)
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2024-05-08 06:39:18 -04:00
|
|
|
useEffect(() => {
|
|
|
|
if (fileTreeContainer) {
|
|
|
|
const listener = (event: DragEvent) => {
|
|
|
|
if (event.dataTransfer) {
|
|
|
|
// store the dragged entity in dataTransfer
|
|
|
|
const { dataset } = event.target as HTMLDivElement
|
|
|
|
if (
|
|
|
|
dataset.fileId &&
|
|
|
|
dataset.fileType &&
|
|
|
|
dataset.fileType !== 'folder'
|
|
|
|
) {
|
|
|
|
event.dataTransfer.setData(
|
|
|
|
'application/x-overleaf-file-id',
|
|
|
|
dataset.fileId
|
|
|
|
)
|
|
|
|
|
|
|
|
const filePath = pathInFolder(fileTreeData, dataset.fileId)
|
|
|
|
if (filePath) {
|
|
|
|
event.dataTransfer.setData(
|
|
|
|
'application/x-overleaf-file-path',
|
|
|
|
filePath
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fileTreeContainer.addEventListener('dragstart', listener)
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
fileTreeContainer.removeEventListener('dragstart', listener)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [fileTreeContainer, fileTreeData])
|
|
|
|
|
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 (
|
2024-02-09 06:27:26 -05:00
|
|
|
<div
|
|
|
|
className="file-tree"
|
|
|
|
data-testid="file-tree"
|
|
|
|
ref={setFileTreeContainer}
|
|
|
|
>
|
2024-02-05 06:44:05 -05:00
|
|
|
{fileTreeContainer && (
|
|
|
|
<FileTreeContext
|
|
|
|
refProviders={refProviders}
|
|
|
|
setRefProviderEnabled={setRefProviderEnabled}
|
|
|
|
setStartedFreeTrial={setStartedFreeTrial}
|
|
|
|
onSelect={onSelect}
|
|
|
|
fileTreeContainer={fileTreeContainer}
|
|
|
|
>
|
|
|
|
{isConnected ? null : <div className="disconnected-overlay" />}
|
|
|
|
<FileTreeToolbar />
|
|
|
|
<FileTreeContextMenu />
|
|
|
|
<FileTreeInner>
|
|
|
|
<FileTreeRootFolder onDelete={onDelete} />
|
|
|
|
</FileTreeInner>
|
|
|
|
<FileTreeModalDelete />
|
|
|
|
<FileTreeModalCreateFile />
|
|
|
|
<FileTreeModalCreateFolder />
|
|
|
|
<FileTreeModalError />
|
|
|
|
</FileTreeContext>
|
|
|
|
)}
|
|
|
|
</div>
|
2020-11-26 09:22:30 -05:00
|
|
|
)
|
2021-06-21 06:02:38 -04:00
|
|
|
})
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2024-01-26 04:23:48 -05:00
|
|
|
function FileTreeRootFolder({ onDelete }: { onDelete: () => void }) {
|
2023-11-02 07:36:04 -04:00
|
|
|
useFileTreeSocketListener(onDelete)
|
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,
|
|
|
|
}),
|
|
|
|
}}
|
2024-01-26 04:23:48 -05:00
|
|
|
dropRef={dropRef as any}
|
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
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default withErrorBoundary(FileTreeRoot, FileTreeError)
|