2024-01-26 04:23:48 -05:00
|
|
|
import { ReactNode, useEffect, useRef } from 'react'
|
2020-11-26 09:22:30 -05:00
|
|
|
import classNames from 'classnames'
|
|
|
|
import scrollIntoViewIfNeeded from 'scroll-into-view-if-needed'
|
|
|
|
|
2024-08-22 11:00:07 -04:00
|
|
|
import { useFileTreeData } from '@/shared/context/file-tree-data-context'
|
2021-03-18 05:52:36 -04:00
|
|
|
import { useFileTreeMainContext } from '../../contexts/file-tree-main'
|
2020-11-26 09:22:30 -05:00
|
|
|
import { useDraggable } from '../../contexts/file-tree-draggable'
|
|
|
|
|
|
|
|
import FileTreeItemName from './file-tree-item-name'
|
|
|
|
import FileTreeItemMenu from './file-tree-item-menu'
|
2021-06-03 09:45:05 -04:00
|
|
|
import { useFileTreeSelectable } from '../../contexts/file-tree-selectable'
|
2023-01-10 10:10:45 -05:00
|
|
|
import { useFileTreeActionable } from '../../contexts/file-tree-actionable'
|
2023-10-25 05:24:58 -04:00
|
|
|
import { useDragDropManager } from 'react-dnd'
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2024-01-26 04:23:48 -05:00
|
|
|
function FileTreeItemInner({
|
|
|
|
id,
|
|
|
|
name,
|
2024-05-08 06:39:18 -04:00
|
|
|
type,
|
2024-01-26 04:23:48 -05:00
|
|
|
isSelected,
|
|
|
|
icons,
|
|
|
|
}: {
|
|
|
|
id: string
|
|
|
|
name: string
|
2024-05-08 06:39:18 -04:00
|
|
|
type: string
|
2024-01-26 04:23:48 -05:00
|
|
|
isSelected: boolean
|
|
|
|
icons?: ReactNode
|
|
|
|
}) {
|
2024-08-22 11:00:07 -04:00
|
|
|
const { fileTreeReadOnly } = useFileTreeData()
|
2022-01-10 10:46:46 -05:00
|
|
|
const { setContextMenuCoords } = useFileTreeMainContext()
|
2023-01-10 10:10:45 -05:00
|
|
|
const { isRenaming } = useFileTreeActionable()
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2021-06-03 09:45:05 -04:00
|
|
|
const { selectedEntityIds } = useFileTreeSelectable()
|
|
|
|
|
|
|
|
const hasMenu =
|
2024-08-22 11:00:07 -04:00
|
|
|
!fileTreeReadOnly && isSelected && selectedEntityIds.size === 1
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2023-10-25 05:24:58 -04:00
|
|
|
const { dragRef, setIsDraggable } = useDraggable(id)
|
|
|
|
|
|
|
|
const dragDropItem = useDragDropManager().getMonitor().getItem()
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2024-01-26 04:23:48 -05:00
|
|
|
const itemRef = useRef<HTMLDivElement | null>(null)
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2020-12-15 05:23:54 -05:00
|
|
|
useEffect(() => {
|
2021-01-08 05:03:18 -05:00
|
|
|
const item = itemRef.current
|
|
|
|
if (isSelected && item) {
|
2021-01-12 06:24:27 -05:00
|
|
|
// we're delaying scrolling due to a race condition with other elements,
|
|
|
|
// mainly the Outline, being resized inside the same panel, causing the
|
|
|
|
// FileTree to have its viewport shrinked after the selected item is
|
|
|
|
// scrolled into the view, hiding it again.
|
|
|
|
// See `left-pane-resize-all` in `file-tree-controller` for more information.
|
2021-01-08 05:03:18 -05:00
|
|
|
setTimeout(() => {
|
|
|
|
if (item) {
|
|
|
|
scrollIntoViewIfNeeded(item, {
|
2021-04-27 03:52:58 -04:00
|
|
|
scrollMode: 'if-needed',
|
2021-01-08 05:03:18 -05:00
|
|
|
})
|
|
|
|
}
|
2021-01-12 06:24:27 -05:00
|
|
|
}, 100)
|
2020-12-15 05:23:54 -05:00
|
|
|
}
|
|
|
|
}, [isSelected, itemRef])
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2024-01-26 04:23:48 -05:00
|
|
|
function handleContextMenu(ev: React.MouseEvent<HTMLDivElement>) {
|
2020-11-26 09:22:30 -05:00
|
|
|
ev.preventDefault()
|
2021-11-09 06:01:08 -05:00
|
|
|
|
2020-11-26 09:22:30 -05:00
|
|
|
setContextMenuCoords({
|
|
|
|
top: ev.pageY,
|
2021-04-27 03:52:58 -04:00
|
|
|
left: ev.pageX,
|
2020-11-26 09:22:30 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={classNames('entity', {
|
2023-10-25 05:24:58 -04:00
|
|
|
'file-tree-entity-dragging': dragDropItem?.draggedEntityIds?.has(id),
|
2020-11-26 09:22:30 -05:00
|
|
|
})}
|
|
|
|
role="presentation"
|
|
|
|
ref={dragRef}
|
2023-01-10 10:10:45 -05:00
|
|
|
draggable={!isRenaming}
|
2020-11-26 09:22:30 -05:00
|
|
|
onContextMenu={handleContextMenu}
|
2024-05-08 06:39:18 -04:00
|
|
|
data-file-id={id}
|
|
|
|
data-file-type={type}
|
2020-11-26 09:22:30 -05:00
|
|
|
>
|
|
|
|
<div
|
|
|
|
className="entity-name entity-name-react"
|
|
|
|
role="presentation"
|
|
|
|
ref={itemRef}
|
|
|
|
>
|
|
|
|
{icons}
|
2021-02-09 04:50:16 -05:00
|
|
|
<FileTreeItemName
|
|
|
|
name={name}
|
|
|
|
isSelected={isSelected}
|
|
|
|
setIsDraggable={setIsDraggable}
|
|
|
|
/>
|
2024-09-25 09:46:02 -04:00
|
|
|
{hasMenu ? <FileTreeItemMenu id={id} name={name} /> : null}
|
2020-11-26 09:22:30 -05:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default FileTreeItemInner
|