Merge pull request #18174 from overleaf/jdt-file-name-context

[Web] add selectedFileName to context in editor (fileTreeActionable)

GitOrigin-RevId: 53bc1a9692cbd6626a44ae8f0cd8ac68d6ce69ae
This commit is contained in:
Jimmy Domagala-Tang 2024-05-07 07:38:39 -07:00 committed by Copybot
parent 72b53fe8db
commit a40c593a21
2 changed files with 14 additions and 17 deletions

View file

@ -1,30 +1,14 @@
import { useCallback, useMemo } from 'react'
import { useCallback } from 'react'
import { useTranslation } from 'react-i18next'
import * as eventTracking from '../../../../infrastructure/event-tracking'
import { useProjectContext } from '@/shared/context/project-context'
import { MenuItem } from 'react-bootstrap'
import { useFileTreeActionable } from '../../contexts/file-tree-actionable'
import { useFileTreeData } from '@/shared/context/file-tree-data-context'
import { useFileTreeSelectable } from '../../contexts/file-tree-selectable'
import { findInTree } from '../../util/find-in-tree'
function FileTreeItemMenuItems() {
const { t } = useTranslation()
const { fileTreeData } = useFileTreeData()
const { selectedEntityIds } = useFileTreeSelectable()
// return the name of the selected file or doc if there is only one selected
const selectedFileName = useMemo(() => {
if (selectedEntityIds.size === 1) {
const [selectedEntityId] = selectedEntityIds
const selectedEntity = findInTree(fileTreeData, selectedEntityId)
return selectedEntity?.entity?.name
}
return null
}, [fileTreeData, selectedEntityIds])
const {
canRename,
canDelete,
@ -35,6 +19,7 @@ function FileTreeItemMenuItems() {
startCreatingDocOrFile,
startUploadingDocOrFile,
downloadPath,
selectedFileName,
} = useFileTreeActionable()
const { owner } = useProjectContext()

View file

@ -58,6 +58,7 @@ const FileTreeActionableContext = createContext<
canRename: boolean
canCreate: boolean
parentFolderId: string
selectedFileName: string | null
isDuplicate: (parentFolderId: string, name: string) => boolean
startRenaming: any
finishRenaming: any
@ -386,6 +387,16 @@ export const FileTreeActionableProvider: FC<{
)
}, [fileTreeData, selectedEntityIds, isRootFolderSelected])
// return the name of the selected file or doc if there is only one selected
const selectedFileName = useMemo(() => {
if (selectedEntityIds.size === 1) {
const [selectedEntityId] = selectedEntityIds
const selectedEntity = findInTree(fileTreeData, selectedEntityId)
return selectedEntity?.entity?.name
}
return null
}, [fileTreeData, selectedEntityIds])
const finishCreatingEntity = useCallback(
entity => {
const error = validateCreate(fileTreeData, parentFolderId, entity)
@ -498,6 +509,7 @@ export const FileTreeActionableProvider: FC<{
canCreate: selectedEntityIds.size < 2,
...state,
parentFolderId,
selectedFileName,
isDuplicate,
startRenaming,
finishRenaming,