Files
overleaf-cep/services/web/frontend/js/features/file-tree/components/file-tree-action-buttons.tsx
T
Mathias JakobsenandDavinder Singh 1acb00407e [web] Disable various editor buttons during network stall (#34247)
Co-authored-by: Davinder Singh <davibaweja@gmail.com>
GitOrigin-RevId: 9e169f3591cfff2fdae63b461b946fde4611f2d7
2026-09-17 11:25:49 +00:00

133 lines
3.8 KiB
TypeScript

import { useTranslation } from 'react-i18next'
import * as eventTracking from '../../../infrastructure/event-tracking'
import { useFileTreeActionable } from '@/features/file-tree/contexts/file-tree-actionable'
import { useFileTreeData } from '@/shared/context/file-tree-data-context'
import React from 'react'
import { useCommandProvider } from '@/features/ide-react/hooks/use-command-provider'
import { usePermissionsContext } from '@/features/ide-react/context/permissions-context'
import FileTreeActionButton from './file-tree-action-button'
import { useRailContext } from '../../ide-react/context/rail-context'
import useIsNetworkStalled from '@/features/ide-react/hooks/use-is-network-stalled'
export default function FileTreeActionButtons({
fileTreeExpanded,
}: {
fileTreeExpanded: boolean
}) {
const { t } = useTranslation()
const { fileTreeReadOnly } = useFileTreeData()
const { write } = usePermissionsContext()
const { handlePaneCollapse } = useRailContext()
const isDisabledDueToNetworkStall = useIsNetworkStalled()
const {
canCreate,
canBulkDelete,
startDeleting,
startCreatingFolder,
startCreatingDocOrFile,
startUploadingDocOrFile,
} = useFileTreeActionable()
useCommandProvider(() => {
if (!canCreate || fileTreeReadOnly || !write) return
return [
{
label: t('new_file'),
id: 'new_file',
handler: ({ location }) => {
eventTracking.sendMB('new-file-click', { location })
startCreatingDocOrFile()
},
disabled: isDisabledDueToNetworkStall,
},
{
label: t('new_folder'),
id: 'new_folder',
handler: startCreatingFolder,
disabled: isDisabledDueToNetworkStall,
},
{
label: t('upload_file'),
id: 'upload_file',
handler: ({ location }) => {
eventTracking.sendMB('upload-click', { location })
startUploadingDocOrFile()
},
disabled: isDisabledDueToNetworkStall,
},
]
}, [
canCreate,
fileTreeReadOnly,
startCreatingDocOrFile,
t,
startCreatingFolder,
startUploadingDocOrFile,
write,
isDisabledDueToNetworkStall,
])
if (fileTreeReadOnly) return null
const createWithAnalytics = () => {
eventTracking.sendMB('new-file-click', { location: 'toolbar' })
startCreatingDocOrFile()
}
const uploadWithAnalytics = () => {
eventTracking.sendMB('upload-click', { location: 'toolbar' })
startUploadingDocOrFile()
}
return (
<div className="file-tree-toolbar-action-buttons">
{fileTreeExpanded && (
<>
{canCreate && (
<FileTreeActionButton
id="new-file"
description={t('new_file')}
onClick={createWithAnalytics}
iconType="note_add"
disabled={isDisabledDueToNetworkStall}
/>
)}
{canCreate && (
<FileTreeActionButton
id="new-folder"
description={t('new_folder')}
onClick={startCreatingFolder}
iconType="create_new_folder"
disabled={isDisabledDueToNetworkStall}
/>
)}
{canCreate && (
<FileTreeActionButton
id="upload"
description={t('upload')}
onClick={uploadWithAnalytics}
iconType="upload"
disabled={isDisabledDueToNetworkStall}
/>
)}
{canBulkDelete && (
<FileTreeActionButton
id="delete"
description={t('delete')}
onClick={startDeleting}
iconType="delete"
/>
)}
</>
)}
<FileTreeActionButton
id="close"
description={t('close')}
onClick={handlePaneCollapse}
iconType="close"
/>
</div>
)
}