2021-06-23 05:37:08 -04:00
|
|
|
import { useEffect } from 'react'
|
2020-11-26 09:22:30 -05:00
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
import classNames from 'classnames'
|
|
|
|
|
|
|
|
import Icon from '../../../shared/components/icon'
|
2021-01-07 09:22:40 -05:00
|
|
|
import {
|
|
|
|
useFileTreeSelectable,
|
2021-04-27 03:52:58 -04:00
|
|
|
useSelectableEntity,
|
2021-01-07 09:22:40 -05:00
|
|
|
} from '../contexts/file-tree-selectable'
|
2020-11-26 09:22:30 -05:00
|
|
|
import { useDroppable } from '../contexts/file-tree-draggable'
|
|
|
|
|
|
|
|
import FileTreeItemInner from './file-tree-item/file-tree-item-inner'
|
|
|
|
import FileTreeFolderList from './file-tree-folder-list'
|
2021-05-18 06:56:56 -04:00
|
|
|
import usePersistedState from '../../../shared/hooks/use-persisted-state'
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2023-08-18 05:26:22 -04:00
|
|
|
function FileTreeFolder({ name, id, folders, docs, files }) {
|
2020-11-26 09:22:30 -05:00
|
|
|
const { t } = useTranslation()
|
|
|
|
|
2023-10-20 06:17:23 -04:00
|
|
|
const { isSelected, props: selectableEntityProps } = useSelectableEntity(
|
|
|
|
id,
|
|
|
|
'folder'
|
|
|
|
)
|
2021-01-07 09:22:40 -05:00
|
|
|
|
|
|
|
const { selectedEntityParentIds } = useFileTreeSelectable(id)
|
|
|
|
|
|
|
|
const [expanded, setExpanded] = usePersistedState(
|
|
|
|
`folder.${id}.expanded`,
|
|
|
|
false
|
|
|
|
)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (selectedEntityParentIds.has(id)) {
|
|
|
|
setExpanded(true)
|
|
|
|
}
|
|
|
|
}, [id, selectedEntityParentIds, setExpanded])
|
2020-11-26 09:22:30 -05:00
|
|
|
|
|
|
|
function handleExpandCollapseClick() {
|
|
|
|
setExpanded(!expanded)
|
|
|
|
}
|
|
|
|
|
|
|
|
const { isOver: isOverRoot, dropRef: dropRefRoot } = useDroppable(id)
|
|
|
|
const { isOver: isOverList, dropRef: dropRefList } = useDroppable(id)
|
|
|
|
|
|
|
|
const icons = (
|
|
|
|
<>
|
|
|
|
<button
|
|
|
|
onClick={handleExpandCollapseClick}
|
|
|
|
aria-label={expanded ? t('collapse') : t('expand')}
|
|
|
|
>
|
2022-05-11 07:27:09 -04:00
|
|
|
<Icon
|
|
|
|
type={expanded ? 'angle-down' : 'angle-right'}
|
|
|
|
fw
|
|
|
|
className="file-tree-expand-icon"
|
|
|
|
/>
|
2020-11-26 09:22:30 -05:00
|
|
|
</button>
|
2022-05-11 07:27:09 -04:00
|
|
|
<Icon
|
|
|
|
type={expanded ? 'folder-open' : 'folder'}
|
|
|
|
fw
|
|
|
|
className="file-tree-folder-icon"
|
|
|
|
/>
|
2020-11-26 09:22:30 -05:00
|
|
|
</>
|
|
|
|
)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<li
|
2023-05-24 06:05:09 -04:00
|
|
|
// eslint-disable-next-line jsx-a11y/role-has-required-aria-props
|
2020-11-26 09:22:30 -05:00
|
|
|
role="treeitem"
|
2023-05-24 06:05:09 -04:00
|
|
|
// aria-selected is provided in selectableEntityProps
|
2020-11-26 09:22:30 -05:00
|
|
|
{...selectableEntityProps}
|
|
|
|
aria-expanded={expanded}
|
|
|
|
aria-label={name}
|
|
|
|
tabIndex="0"
|
|
|
|
ref={dropRefRoot}
|
|
|
|
className={classNames(selectableEntityProps.className, {
|
2021-04-27 03:52:58 -04:00
|
|
|
'dnd-droppable-hover': isOverRoot || isOverList,
|
2020-11-26 09:22:30 -05:00
|
|
|
})}
|
|
|
|
>
|
|
|
|
<FileTreeItemInner
|
|
|
|
id={id}
|
|
|
|
name={name}
|
2024-05-08 06:39:18 -04:00
|
|
|
type="folder"
|
2020-11-26 09:22:30 -05:00
|
|
|
isSelected={isSelected}
|
|
|
|
icons={icons}
|
|
|
|
/>
|
|
|
|
</li>
|
|
|
|
{expanded ? (
|
|
|
|
<FileTreeFolderList
|
|
|
|
folders={folders}
|
|
|
|
docs={docs}
|
|
|
|
files={files}
|
|
|
|
dropRef={dropRefList}
|
|
|
|
/>
|
|
|
|
) : null}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
FileTreeFolder.propTypes = {
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
id: PropTypes.string.isRequired,
|
|
|
|
folders: PropTypes.array.isRequired,
|
|
|
|
docs: PropTypes.array.isRequired,
|
2021-04-27 03:52:58 -04:00
|
|
|
files: PropTypes.array.isRequired,
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export default FileTreeFolder
|