file-tree-toolbar.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import { useTranslation } from 'react-i18next'
  2. import * as eventTracking from '../../../infrastructure/event-tracking'
  3. import { useFileTreeActionable } from '@/features/file-tree/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, {
  7. AvailableUnfilledIcon,
  8. } from '@/shared/components/material-icon'
  9. import React from 'react'
  10. import useCollapsibleFileTree from '../hooks/use-collapsible-file-tree'
  11. import { useCommandProvider } from '@/features/ide-react/hooks/use-command-provider'
  12. import { usePermissionsContext } from '@/features/ide-react/context/permissions-context'
  13. function FileTreeToolbar() {
  14. const { t } = useTranslation()
  15. const { fileTreeExpanded, toggleFileTreeExpanded } = useCollapsibleFileTree()
  16. return (
  17. <div className="file-tree-toolbar">
  18. <button
  19. className="file-tree-expand-collapse-button"
  20. onClick={toggleFileTreeExpanded}
  21. aria-label={
  22. fileTreeExpanded ? t('hide_file_tree') : t('show_file_tree')
  23. }
  24. >
  25. <MaterialIcon
  26. type={
  27. fileTreeExpanded ? 'keyboard_arrow_down' : 'keyboard_arrow_right'
  28. }
  29. />
  30. <h4>{t('file_tree')}</h4>
  31. </button>
  32. <FileTreeActionButtons />
  33. </div>
  34. )
  35. }
  36. function FileTreeActionButtons() {
  37. const { t } = useTranslation()
  38. const { fileTreeReadOnly } = useFileTreeData()
  39. const { write } = usePermissionsContext()
  40. const {
  41. canCreate,
  42. startCreatingFolder,
  43. startCreatingDocOrFile,
  44. startUploadingDocOrFile,
  45. } = useFileTreeActionable()
  46. useCommandProvider(() => {
  47. if (!canCreate || fileTreeReadOnly || !write) return
  48. return [
  49. {
  50. label: t('new_file'),
  51. id: 'new_file',
  52. handler: ({ location }) => {
  53. eventTracking.sendMB('new-file-click', { location })
  54. startCreatingDocOrFile()
  55. },
  56. },
  57. {
  58. label: t('new_folder'),
  59. id: 'new_folder',
  60. handler: startCreatingFolder,
  61. },
  62. {
  63. label: t('upload_file'),
  64. id: 'upload_file',
  65. handler: ({ location }) => {
  66. eventTracking.sendMB('upload-click', { location })
  67. startUploadingDocOrFile()
  68. },
  69. },
  70. ]
  71. }, [
  72. canCreate,
  73. fileTreeReadOnly,
  74. startCreatingDocOrFile,
  75. t,
  76. startCreatingFolder,
  77. startUploadingDocOrFile,
  78. write,
  79. ])
  80. if (!canCreate || fileTreeReadOnly) return null
  81. const createWithAnalytics = () => {
  82. eventTracking.sendMB('new-file-click', { location: 'toolbar' })
  83. startCreatingDocOrFile()
  84. }
  85. const uploadWithAnalytics = () => {
  86. eventTracking.sendMB('upload-click', { location: 'toolbar' })
  87. startUploadingDocOrFile()
  88. }
  89. return (
  90. <div className="file-tree-toolbar-action-buttons">
  91. <FileTreeActionButton
  92. id="new-file"
  93. description={t('new_file')}
  94. onClick={createWithAnalytics}
  95. iconType="note_add"
  96. />
  97. <FileTreeActionButton
  98. id="new-folder"
  99. description={t('new_folder')}
  100. onClick={startCreatingFolder}
  101. iconType="create_new_folder"
  102. />
  103. <FileTreeActionButton
  104. id="upload"
  105. description={t('upload')}
  106. onClick={uploadWithAnalytics}
  107. iconType="upload_file"
  108. />
  109. </div>
  110. )
  111. }
  112. function FileTreeActionButton({
  113. id,
  114. description,
  115. onClick,
  116. iconType,
  117. }: {
  118. id: string
  119. description: string
  120. onClick: () => void
  121. iconType: AvailableUnfilledIcon
  122. }) {
  123. return (
  124. <OLTooltip
  125. id={id}
  126. description={description}
  127. overlayProps={{ placement: 'bottom' }}
  128. >
  129. <button className="btn file-tree-toolbar-action-button" onClick={onClick}>
  130. <MaterialIcon
  131. unfilled
  132. type={iconType}
  133. accessibilityLabel={description}
  134. />
  135. </button>
  136. </OLTooltip>
  137. )
  138. }
  139. export default FileTreeToolbar