file-tree-action-buttons.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 React from 'react'
  6. import { useCommandProvider } from '@/features/ide-react/hooks/use-command-provider'
  7. import { usePermissionsContext } from '@/features/ide-react/context/permissions-context'
  8. import FileTreeActionButton from './file-tree-action-button'
  9. import { useRailContext } from '../../ide-react/context/rail-context'
  10. export default function FileTreeActionButtons({
  11. fileTreeExpanded,
  12. }: {
  13. fileTreeExpanded: boolean
  14. }) {
  15. const { t } = useTranslation()
  16. const { fileTreeReadOnly } = useFileTreeData()
  17. const { write } = usePermissionsContext()
  18. const { handlePaneCollapse } = useRailContext()
  19. const {
  20. canCreate,
  21. canBulkDelete,
  22. startDeleting,
  23. startCreatingFolder,
  24. startCreatingDocOrFile,
  25. startUploadingDocOrFile,
  26. } = useFileTreeActionable()
  27. useCommandProvider(() => {
  28. if (!canCreate || fileTreeReadOnly || !write) return
  29. return [
  30. {
  31. label: t('new_file'),
  32. id: 'new_file',
  33. handler: ({ location }) => {
  34. eventTracking.sendMB('new-file-click', { location })
  35. startCreatingDocOrFile()
  36. },
  37. },
  38. {
  39. label: t('new_folder'),
  40. id: 'new_folder',
  41. handler: startCreatingFolder,
  42. },
  43. {
  44. label: t('upload_file'),
  45. id: 'upload_file',
  46. handler: ({ location }) => {
  47. eventTracking.sendMB('upload-click', { location })
  48. startUploadingDocOrFile()
  49. },
  50. },
  51. ]
  52. }, [
  53. canCreate,
  54. fileTreeReadOnly,
  55. startCreatingDocOrFile,
  56. t,
  57. startCreatingFolder,
  58. startUploadingDocOrFile,
  59. write,
  60. ])
  61. if (fileTreeReadOnly) return null
  62. const createWithAnalytics = () => {
  63. eventTracking.sendMB('new-file-click', { location: 'toolbar' })
  64. startCreatingDocOrFile()
  65. }
  66. const uploadWithAnalytics = () => {
  67. eventTracking.sendMB('upload-click', { location: 'toolbar' })
  68. startUploadingDocOrFile()
  69. }
  70. return (
  71. <div className="file-tree-toolbar-action-buttons">
  72. {fileTreeExpanded && (
  73. <>
  74. {canCreate && (
  75. <FileTreeActionButton
  76. id="new-file"
  77. description={t('new_file')}
  78. onClick={createWithAnalytics}
  79. iconType="note_add"
  80. />
  81. )}
  82. {canCreate && (
  83. <FileTreeActionButton
  84. id="new-folder"
  85. description={t('new_folder')}
  86. onClick={startCreatingFolder}
  87. iconType="create_new_folder"
  88. />
  89. )}
  90. {canCreate && (
  91. <FileTreeActionButton
  92. id="upload"
  93. description={t('upload')}
  94. onClick={uploadWithAnalytics}
  95. iconType="upload"
  96. />
  97. )}
  98. {canBulkDelete && (
  99. <FileTreeActionButton
  100. id="delete"
  101. description={t('delete')}
  102. onClick={startDeleting}
  103. iconType="delete"
  104. />
  105. )}
  106. </>
  107. )}
  108. <FileTreeActionButton
  109. id="close"
  110. description={t('close')}
  111. onClick={handlePaneCollapse}
  112. iconType="close"
  113. />
  114. </div>
  115. )
  116. }