file-tree-toolbar.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. function FileTreeToolbar() {
  12. const { t } = useTranslation()
  13. const { fileTreeExpanded, toggleFileTreeExpanded } = useCollapsibleFileTree()
  14. return (
  15. <div className="file-tree-toolbar">
  16. <button
  17. className="file-tree-expand-collapse-button"
  18. onClick={toggleFileTreeExpanded}
  19. aria-label={
  20. fileTreeExpanded ? t('hide_file_tree') : t('show_file_tree')
  21. }
  22. >
  23. <MaterialIcon
  24. type={
  25. fileTreeExpanded ? 'keyboard_arrow_down' : 'keyboard_arrow_right'
  26. }
  27. />
  28. <h4>{t('file_tree')}</h4>
  29. </button>
  30. <FileTreeActionButtons />
  31. </div>
  32. )
  33. }
  34. function FileTreeActionButtons() {
  35. const { t } = useTranslation()
  36. const { fileTreeReadOnly } = useFileTreeData()
  37. const {
  38. canCreate,
  39. startCreatingFolder,
  40. startCreatingDocOrFile,
  41. startUploadingDocOrFile,
  42. } = useFileTreeActionable()
  43. if (!canCreate || fileTreeReadOnly) return null
  44. const createWithAnalytics = () => {
  45. eventTracking.sendMB('new-file-click', { location: 'toolbar' })
  46. startCreatingDocOrFile()
  47. }
  48. const uploadWithAnalytics = () => {
  49. eventTracking.sendMB('upload-click', { location: 'toolbar' })
  50. startUploadingDocOrFile()
  51. }
  52. return (
  53. <div className="file-tree-toolbar-action-buttons">
  54. <FileTreeActionButton
  55. id="new-file"
  56. description={t('new_file')}
  57. onClick={createWithAnalytics}
  58. iconType="note_add"
  59. />
  60. <FileTreeActionButton
  61. id="new-folder"
  62. description={t('new_folder')}
  63. onClick={startCreatingFolder}
  64. iconType="create_new_folder"
  65. />
  66. <FileTreeActionButton
  67. id="upload"
  68. description={t('upload')}
  69. onClick={uploadWithAnalytics}
  70. iconType="upload_file"
  71. />
  72. </div>
  73. )
  74. }
  75. function FileTreeActionButton({
  76. id,
  77. description,
  78. onClick,
  79. iconType,
  80. }: {
  81. id: string
  82. description: string
  83. onClick: () => void
  84. iconType: AvailableUnfilledIcon
  85. }) {
  86. return (
  87. <OLTooltip
  88. id={id}
  89. description={description}
  90. overlayProps={{ placement: 'bottom' }}
  91. >
  92. <button className="btn file-tree-toolbar-action-button" onClick={onClick}>
  93. <MaterialIcon
  94. unfilled
  95. type={iconType}
  96. accessibilityLabel={description}
  97. />
  98. </button>
  99. </OLTooltip>
  100. )
  101. }
  102. export default FileTreeToolbar