file-tree-doc.jsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import PropTypes from 'prop-types'
  2. import { useSelectableEntity } from '../contexts/file-tree-selectable'
  3. import FileTreeItemInner from './file-tree-item/file-tree-item-inner'
  4. import { useTranslation } from 'react-i18next'
  5. import Icon from '../../../shared/components/icon'
  6. import iconTypeFromName from '../util/icon-type-from-name'
  7. import classnames from 'classnames'
  8. import BootstrapVersionSwitcher from '@/features/ui/components/bootstrap-5/bootstrap-version-switcher'
  9. import MaterialIcon from '@/shared/components/material-icon'
  10. function FileTreeDoc({ name, id, isFile, isLinkedFile }) {
  11. const type = isFile ? 'file' : 'doc'
  12. const { isSelected, props: selectableEntityProps } = useSelectableEntity(
  13. id,
  14. type
  15. )
  16. return (
  17. <li
  18. // eslint-disable-next-line jsx-a11y/role-has-required-aria-props
  19. role="treeitem"
  20. // aria-selected is provided in selectableEntityProps
  21. {...selectableEntityProps}
  22. aria-label={name}
  23. tabIndex="0"
  24. >
  25. <FileTreeItemInner
  26. id={id}
  27. name={name}
  28. type={type}
  29. isSelected={isSelected}
  30. icons={<FileTreeIcon isLinkedFile={isLinkedFile} name={name} />}
  31. />
  32. </li>
  33. )
  34. }
  35. FileTreeDoc.propTypes = {
  36. name: PropTypes.string.isRequired,
  37. id: PropTypes.string.isRequired,
  38. isFile: PropTypes.bool,
  39. isLinkedFile: PropTypes.bool,
  40. }
  41. export const FileTreeIcon = ({ isLinkedFile, name }) => {
  42. const { t } = useTranslation()
  43. const className = classnames('file-tree-icon', {
  44. 'linked-file-icon': isLinkedFile,
  45. })
  46. return (
  47. <>
  48. &nbsp;
  49. <BootstrapVersionSwitcher
  50. bs3={
  51. <>
  52. <Icon type={iconTypeFromName(name)} fw className={className} />
  53. {isLinkedFile && (
  54. <Icon
  55. type="external-link-square"
  56. modifier="rotate-180"
  57. className="linked-file-highlight"
  58. accessibilityLabel={t('linked_file')}
  59. />
  60. )}
  61. </>
  62. }
  63. bs5={
  64. <>
  65. <MaterialIcon type={iconTypeFromName(name)} className={className} />
  66. {isLinkedFile && (
  67. <MaterialIcon
  68. type="open_in_new"
  69. modifier="rotate-180"
  70. className="linked-file-highlight"
  71. accessibilityLabel={t('linked_file')}
  72. />
  73. )}
  74. </>
  75. }
  76. />
  77. </>
  78. )
  79. }
  80. FileTreeIcon.propTypes = {
  81. name: PropTypes.string.isRequired,
  82. isLinkedFile: PropTypes.bool,
  83. }
  84. export default FileTreeDoc