2020-11-26 09:22:30 -05:00
|
|
|
import React, { useContext, useEffect, createRef } from 'react'
|
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
import classNames from 'classnames'
|
|
|
|
import scrollIntoViewIfNeeded from 'scroll-into-view-if-needed'
|
|
|
|
|
|
|
|
import { FileTreeMainContext } from '../../contexts/file-tree-main'
|
|
|
|
import { useDraggable } from '../../contexts/file-tree-draggable'
|
|
|
|
|
|
|
|
import FileTreeItemName from './file-tree-item-name'
|
|
|
|
import FileTreeItemMenu from './file-tree-item-menu'
|
|
|
|
|
|
|
|
function FileTreeItemInner({ id, name, isSelected, icons }) {
|
|
|
|
const { hasWritePermissions, setContextMenuCoords } = useContext(
|
|
|
|
FileTreeMainContext
|
|
|
|
)
|
|
|
|
|
|
|
|
const hasMenu = hasWritePermissions && isSelected
|
|
|
|
|
|
|
|
const { isDragging, dragRef } = useDraggable(id)
|
|
|
|
|
|
|
|
const itemRef = createRef()
|
|
|
|
|
2020-12-15 05:23:54 -05:00
|
|
|
useEffect(() => {
|
2021-01-08 05:03:18 -05:00
|
|
|
const item = itemRef.current
|
|
|
|
if (isSelected && item) {
|
|
|
|
// we found scrolling doesn't happen unless a explicit delay is introduced
|
|
|
|
setTimeout(() => {
|
|
|
|
if (item) {
|
|
|
|
scrollIntoViewIfNeeded(item, {
|
|
|
|
scrollMode: 'if-needed'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}, 10)
|
2020-12-15 05:23:54 -05:00
|
|
|
}
|
|
|
|
}, [isSelected, itemRef])
|
2020-11-26 09:22:30 -05:00
|
|
|
|
|
|
|
function handleContextMenu(ev) {
|
|
|
|
ev.preventDefault()
|
|
|
|
setContextMenuCoords({
|
|
|
|
top: ev.pageY,
|
|
|
|
left: ev.pageX
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={classNames('entity', {
|
|
|
|
'dnd-draggable-dragging': isDragging
|
|
|
|
})}
|
|
|
|
role="presentation"
|
|
|
|
ref={dragRef}
|
|
|
|
onContextMenu={handleContextMenu}
|
|
|
|
>
|
|
|
|
<div
|
|
|
|
className="entity-name entity-name-react"
|
|
|
|
role="presentation"
|
|
|
|
ref={itemRef}
|
|
|
|
>
|
|
|
|
{icons}
|
|
|
|
<FileTreeItemName name={name} isSelected={isSelected} />
|
|
|
|
{hasMenu ? <FileTreeItemMenu id={id} /> : null}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
FileTreeItemInner.propTypes = {
|
|
|
|
id: PropTypes.string.isRequired,
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
isSelected: PropTypes.bool.isRequired,
|
|
|
|
icons: PropTypes.node
|
|
|
|
}
|
|
|
|
|
|
|
|
export default FileTreeItemInner
|