file-tree-toolbar.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { useTranslation } from 'react-i18next'
  2. import * as eventTracking from '../../../infrastructure/event-tracking'
  3. import { Button } from 'react-bootstrap'
  4. import Tooltip from '../../../shared/components/tooltip'
  5. import Icon from '../../../shared/components/icon'
  6. import { useEditorContext } from '../../../shared/context/editor-context'
  7. import { useFileTreeActionable } from '../contexts/file-tree-actionable'
  8. function FileTreeToolbar() {
  9. const { permissionsLevel } = useEditorContext()
  10. if (permissionsLevel === 'readOnly') return null
  11. return (
  12. <div className="toolbar toolbar-filetree">
  13. <FileTreeToolbarLeft />
  14. <FileTreeToolbarRight />
  15. </div>
  16. )
  17. }
  18. function FileTreeToolbarLeft() {
  19. const { t } = useTranslation()
  20. const {
  21. canCreate,
  22. startCreatingFolder,
  23. startCreatingDocOrFile,
  24. startUploadingDocOrFile,
  25. } = useFileTreeActionable()
  26. const createWithAnalytics = () => {
  27. eventTracking.sendMB('new-file-click', { location: 'toolbar' })
  28. startCreatingDocOrFile()
  29. }
  30. const uploadWithAnalytics = () => {
  31. eventTracking.sendMB('upload-click', { location: 'toolbar' })
  32. startUploadingDocOrFile()
  33. }
  34. if (!canCreate) return null
  35. return (
  36. <div className="toolbar-left">
  37. <Tooltip
  38. id="new-file"
  39. description={t('new_file')}
  40. overlayProps={{ placement: 'bottom' }}
  41. >
  42. <Button onClick={createWithAnalytics} bsStyle={null}>
  43. <Icon type="file" fw accessibilityLabel={t('new_file')} />
  44. </Button>
  45. </Tooltip>
  46. <Tooltip
  47. id="new-folder"
  48. description={t('new_folder')}
  49. overlayProps={{ placement: 'bottom' }}
  50. >
  51. <Button onClick={startCreatingFolder} bsStyle={null}>
  52. <Icon type="folder" fw accessibilityLabel={t('new_folder')} />
  53. </Button>
  54. </Tooltip>
  55. <Tooltip
  56. id="upload"
  57. description={t('upload')}
  58. overlayProps={{ placement: 'bottom' }}
  59. >
  60. <Button onClick={uploadWithAnalytics}>
  61. <Icon type="upload" fw accessibilityLabel={t('upload')} />
  62. </Button>
  63. </Tooltip>
  64. </div>
  65. )
  66. }
  67. function FileTreeToolbarRight() {
  68. const { t } = useTranslation()
  69. const { canRename, canDelete, startRenaming, startDeleting } =
  70. useFileTreeActionable()
  71. if (!canRename && !canDelete) {
  72. return null
  73. }
  74. return (
  75. <div className="toolbar-right">
  76. {canRename ? (
  77. <Tooltip
  78. id="rename"
  79. description={t('rename')}
  80. overlayProps={{ placement: 'bottom' }}
  81. >
  82. <Button onClick={startRenaming}>
  83. <Icon type="pencil" fw accessibilityLabel={t('rename')} />
  84. </Button>
  85. </Tooltip>
  86. ) : null}
  87. {canDelete ? (
  88. <Tooltip
  89. id="delete"
  90. description={t('delete')}
  91. overlayProps={{ placement: 'bottom' }}
  92. >
  93. <Button onClick={startDeleting}>
  94. <Icon type="trash-o" fw accessibilityLabel={t('delete')} />
  95. </Button>
  96. </Tooltip>
  97. ) : null}
  98. </div>
  99. )
  100. }
  101. export default FileTreeToolbar