file-tree-toolbar.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { useTranslation } from 'react-i18next'
  2. import * as eventTracking from '../../../infrastructure/event-tracking'
  3. import Icon from '../../../shared/components/icon'
  4. import { useFileTreeActionable } from '../contexts/file-tree-actionable'
  5. import { useFileTreeData } from '@/shared/context/file-tree-data-context'
  6. import OLTooltip from '@/features/ui/components/ol/ol-tooltip'
  7. import BootstrapVersionSwitcher from '@/features/ui/components/bootstrap-5/bootstrap-version-switcher'
  8. import MaterialIcon from '@/shared/components/material-icon'
  9. import OLButtonToolbar from '@/features/ui/components/ol/ol-button-toolbar'
  10. function FileTreeToolbar() {
  11. const { fileTreeReadOnly } = useFileTreeData()
  12. const { t } = useTranslation()
  13. if (fileTreeReadOnly) return null
  14. return (
  15. <OLButtonToolbar
  16. className="toolbar toolbar-filetree"
  17. aria-label={t('project_files')}
  18. >
  19. <FileTreeToolbarLeft />
  20. <FileTreeToolbarRight />
  21. </OLButtonToolbar>
  22. )
  23. }
  24. function FileTreeToolbarLeft() {
  25. const { t } = useTranslation()
  26. const {
  27. canCreate,
  28. startCreatingFolder,
  29. startCreatingDocOrFile,
  30. startUploadingDocOrFile,
  31. } = useFileTreeActionable()
  32. const createWithAnalytics = () => {
  33. eventTracking.sendMB('new-file-click', { location: 'toolbar' })
  34. startCreatingDocOrFile()
  35. }
  36. const uploadWithAnalytics = () => {
  37. eventTracking.sendMB('upload-click', { location: 'toolbar' })
  38. startUploadingDocOrFile()
  39. }
  40. if (!canCreate) return null
  41. return (
  42. <div className="toolbar-left">
  43. <OLTooltip
  44. id="new-file"
  45. description={t('new_file')}
  46. overlayProps={{ placement: 'bottom' }}
  47. >
  48. <button className="btn" onClick={createWithAnalytics}>
  49. <BootstrapVersionSwitcher
  50. bs5={
  51. <MaterialIcon
  52. type="description"
  53. accessibilityLabel={t('new_file')}
  54. />
  55. }
  56. bs3={<Icon type="file" fw accessibilityLabel={t('new_file')} />}
  57. />
  58. </button>
  59. </OLTooltip>
  60. <OLTooltip
  61. id="new-folder"
  62. description={t('new_folder')}
  63. overlayProps={{ placement: 'bottom' }}
  64. >
  65. <button className="btn" onClick={startCreatingFolder} tabIndex={-1}>
  66. <BootstrapVersionSwitcher
  67. bs5={
  68. <MaterialIcon
  69. type="folder"
  70. accessibilityLabel={t('new_folder')}
  71. />
  72. }
  73. bs3={<Icon type="folder" fw accessibilityLabel={t('new_folder')} />}
  74. />
  75. </button>
  76. </OLTooltip>
  77. <OLTooltip
  78. id="upload"
  79. description={t('upload')}
  80. overlayProps={{ placement: 'bottom' }}
  81. >
  82. <button className="btn" onClick={uploadWithAnalytics} tabIndex={-1}>
  83. <BootstrapVersionSwitcher
  84. bs5={
  85. <MaterialIcon type="upload" accessibilityLabel={t('upload')} />
  86. }
  87. bs3={<Icon type="upload" fw accessibilityLabel={t('upload')} />}
  88. />
  89. </button>
  90. </OLTooltip>
  91. </div>
  92. )
  93. }
  94. function FileTreeToolbarRight() {
  95. const { t } = useTranslation()
  96. const { canRename, canDelete, startRenaming, startDeleting } =
  97. useFileTreeActionable()
  98. if (!canRename && !canDelete) {
  99. return null
  100. }
  101. return (
  102. <div className="toolbar-right">
  103. {canRename ? (
  104. <OLTooltip
  105. id="rename"
  106. description={t('rename')}
  107. overlayProps={{ placement: 'bottom' }}
  108. >
  109. <button className="btn" onClick={startRenaming} tabIndex={-1}>
  110. <BootstrapVersionSwitcher
  111. bs3={<Icon type="pencil" fw accessibilityLabel={t('rename')} />}
  112. bs5={
  113. <MaterialIcon type="edit" accessibilityLabel={t('rename')} />
  114. }
  115. />
  116. </button>
  117. </OLTooltip>
  118. ) : null}
  119. {canDelete ? (
  120. <OLTooltip
  121. id="delete"
  122. description={t('delete')}
  123. overlayProps={{ placement: 'bottom' }}
  124. >
  125. <button className="btn" onClick={startDeleting} tabIndex={-1}>
  126. <BootstrapVersionSwitcher
  127. bs3={<Icon type="trash-o" fw accessibilityLabel={t('delete')} />}
  128. bs5={
  129. <MaterialIcon type="delete" accessibilityLabel={t('delete')} />
  130. }
  131. />
  132. </button>
  133. </OLTooltip>
  134. ) : null}
  135. </div>
  136. )
  137. }
  138. export default FileTreeToolbar