file-tree-toolbar.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { useTranslation } from 'react-i18next'
  2. import * as eventTracking from '../../../infrastructure/event-tracking'
  3. import { useFileTreeActionable } from '../contexts/file-tree-actionable'
  4. import { useFileTreeData } from '@/shared/context/file-tree-data-context'
  5. import OLTooltip from '@/features/ui/components/ol/ol-tooltip'
  6. import MaterialIcon from '@/shared/components/material-icon'
  7. import OLButtonToolbar from '@/features/ui/components/ol/ol-button-toolbar'
  8. import importOverleafModules from '../../../../macros/import-overleaf-module.macro'
  9. import React, { ElementType } from 'react'
  10. const fileTreeToolbarComponents = importOverleafModules(
  11. 'fileTreeToolbarComponents'
  12. ) as { import: { default: ElementType }; path: string }[]
  13. function FileTreeToolbar() {
  14. const { fileTreeReadOnly } = useFileTreeData()
  15. const { t } = useTranslation()
  16. if (fileTreeReadOnly) return null
  17. return (
  18. <OLButtonToolbar
  19. className="toolbar toolbar-filetree"
  20. aria-label={t('project_files')}
  21. >
  22. <FileTreeToolbarLeft />
  23. <FileTreeToolbarRight />
  24. </OLButtonToolbar>
  25. )
  26. }
  27. function FileTreeToolbarLeft() {
  28. const { t } = useTranslation()
  29. const {
  30. canCreate,
  31. startCreatingFolder,
  32. startCreatingDocOrFile,
  33. startUploadingDocOrFile,
  34. } = useFileTreeActionable()
  35. const createWithAnalytics = () => {
  36. eventTracking.sendMB('new-file-click', { location: 'toolbar' })
  37. startCreatingDocOrFile()
  38. }
  39. const uploadWithAnalytics = () => {
  40. eventTracking.sendMB('upload-click', { location: 'toolbar' })
  41. startUploadingDocOrFile()
  42. }
  43. if (!canCreate) return null
  44. return (
  45. <div className="toolbar-left">
  46. <OLTooltip
  47. id="new-file"
  48. description={t('new_file')}
  49. overlayProps={{ placement: 'bottom' }}
  50. >
  51. <button className="btn" onClick={createWithAnalytics}>
  52. <MaterialIcon type="description" accessibilityLabel={t('new_file')} />
  53. </button>
  54. </OLTooltip>
  55. <OLTooltip
  56. id="new-folder"
  57. description={t('new_folder')}
  58. overlayProps={{ placement: 'bottom' }}
  59. >
  60. <button className="btn" onClick={startCreatingFolder} tabIndex={-1}>
  61. <MaterialIcon type="folder" accessibilityLabel={t('new_folder')} />
  62. </button>
  63. </OLTooltip>
  64. <OLTooltip
  65. id="upload"
  66. description={t('upload')}
  67. overlayProps={{ placement: 'bottom' }}
  68. >
  69. <button className="btn" onClick={uploadWithAnalytics} tabIndex={-1}>
  70. <MaterialIcon type="upload" accessibilityLabel={t('upload')} />
  71. </button>
  72. </OLTooltip>
  73. </div>
  74. )
  75. }
  76. function FileTreeToolbarRight() {
  77. const { t } = useTranslation()
  78. const { canRename, canDelete, startRenaming, startDeleting } =
  79. useFileTreeActionable()
  80. return (
  81. <div className="toolbar-right">
  82. {fileTreeToolbarComponents.map(
  83. ({ import: { default: Component }, path }) => (
  84. <Component key={path} />
  85. )
  86. )}
  87. {canRename ? (
  88. <OLTooltip
  89. id="rename"
  90. description={t('rename')}
  91. overlayProps={{ placement: 'bottom' }}
  92. >
  93. <button className="btn" onClick={startRenaming} tabIndex={-1}>
  94. <MaterialIcon type="edit" accessibilityLabel={t('rename')} />
  95. </button>
  96. </OLTooltip>
  97. ) : null}
  98. {canDelete ? (
  99. <OLTooltip
  100. id="delete"
  101. description={t('delete')}
  102. overlayProps={{ placement: 'bottom' }}
  103. >
  104. <button className="btn" onClick={startDeleting} tabIndex={-1}>
  105. <MaterialIcon type="delete" accessibilityLabel={t('delete')} />
  106. </button>
  107. </OLTooltip>
  108. ) : null}
  109. </div>
  110. )
  111. }
  112. export default FileTreeToolbar