2021-06-23 05:37:08 -04:00
|
|
|
import {
|
2021-03-10 04:11:43 -05:00
|
|
|
createContext,
|
|
|
|
useCallback,
|
2021-03-18 05:52:36 -04:00
|
|
|
useMemo,
|
2021-03-10 04:11:43 -05:00
|
|
|
useReducer,
|
2021-04-27 03:52:58 -04:00
|
|
|
useContext,
|
2021-06-11 04:40:42 -04:00
|
|
|
useEffect,
|
2022-07-21 04:32:34 -04:00
|
|
|
useState,
|
2021-03-10 04:11:43 -05:00
|
|
|
} from 'react'
|
2020-11-26 09:22:30 -05:00
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
|
|
|
|
import { mapSeries } from '../../../infrastructure/promise'
|
|
|
|
|
|
|
|
import {
|
|
|
|
syncRename,
|
|
|
|
syncDelete,
|
|
|
|
syncMove,
|
2021-04-27 03:52:58 -04:00
|
|
|
syncCreateEntity,
|
2020-11-26 09:22:30 -05:00
|
|
|
} from '../util/sync-mutation'
|
2021-05-18 06:58:53 -04:00
|
|
|
import { findInTree, findInTreeOrThrow } from '../util/find-in-tree'
|
2020-12-09 06:55:36 -05:00
|
|
|
import { isNameUniqueInFolder } from '../util/is-name-unique-in-folder'
|
2021-01-14 08:57:20 -05:00
|
|
|
import { isBlockedFilename, isCleanFilename } from '../util/safe-path'
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2022-01-10 10:46:46 -05:00
|
|
|
import { useProjectContext } from '../../../shared/context/project-context'
|
|
|
|
import { useEditorContext } from '../../../shared/context/editor-context'
|
2022-01-10 10:47:01 -05:00
|
|
|
import { useFileTreeData } from '../../../shared/context/file-tree-data-context'
|
2020-11-26 09:22:30 -05:00
|
|
|
import { useFileTreeSelectable } from './file-tree-selectable'
|
|
|
|
|
2021-01-14 08:57:20 -05:00
|
|
|
import {
|
|
|
|
InvalidFilenameError,
|
|
|
|
BlockedFilenameError,
|
|
|
|
DuplicateFilenameError,
|
2021-04-27 03:52:58 -04:00
|
|
|
DuplicateFilenameMoveError,
|
2021-01-14 08:57:20 -05:00
|
|
|
} from '../errors'
|
2020-12-09 06:55:36 -05:00
|
|
|
|
2020-11-26 09:22:30 -05:00
|
|
|
const FileTreeActionableContext = createContext()
|
|
|
|
|
|
|
|
const ACTION_TYPES = {
|
|
|
|
START_RENAME: 'START_RENAME',
|
|
|
|
START_DELETE: 'START_DELETE',
|
|
|
|
DELETING: 'DELETING',
|
2021-03-18 05:52:36 -04:00
|
|
|
START_CREATE_FILE: 'START_CREATE_FILE',
|
2020-11-26 09:22:30 -05:00
|
|
|
START_CREATE_FOLDER: 'START_CREATE_FOLDER',
|
2021-03-18 05:52:36 -04:00
|
|
|
CREATING_FILE: 'CREATING_FILE',
|
2020-11-26 09:22:30 -05:00
|
|
|
CREATING_FOLDER: 'CREATING_FOLDER',
|
2021-01-14 08:57:20 -05:00
|
|
|
MOVING: 'MOVING',
|
2020-11-26 09:22:30 -05:00
|
|
|
CANCEL: 'CANCEL',
|
|
|
|
CLEAR: 'CLEAR',
|
2021-04-27 03:52:58 -04:00
|
|
|
ERROR: 'ERROR',
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const defaultState = {
|
|
|
|
isDeleting: false,
|
|
|
|
isRenaming: false,
|
2021-03-18 05:52:36 -04:00
|
|
|
isCreatingFile: false,
|
2020-11-26 09:22:30 -05:00
|
|
|
isCreatingFolder: false,
|
2021-01-14 08:57:20 -05:00
|
|
|
isMoving: false,
|
2020-11-26 09:22:30 -05:00
|
|
|
inFlight: false,
|
|
|
|
actionedEntities: null,
|
2021-03-18 05:52:36 -04:00
|
|
|
newFileCreateMode: null,
|
2021-04-27 03:52:58 -04:00
|
|
|
error: null,
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function fileTreeActionableReadOnlyReducer(state) {
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
|
|
|
function fileTreeActionableReducer(state, action) {
|
|
|
|
switch (action.type) {
|
|
|
|
case ACTION_TYPES.START_RENAME:
|
|
|
|
return { ...defaultState, isRenaming: true }
|
|
|
|
case ACTION_TYPES.START_DELETE:
|
|
|
|
return {
|
|
|
|
...defaultState,
|
|
|
|
isDeleting: true,
|
2021-04-27 03:52:58 -04:00
|
|
|
actionedEntities: action.actionedEntities,
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
2021-03-18 05:52:36 -04:00
|
|
|
case ACTION_TYPES.START_CREATE_FILE:
|
|
|
|
return {
|
|
|
|
...defaultState,
|
|
|
|
isCreatingFile: true,
|
2021-04-27 03:52:58 -04:00
|
|
|
newFileCreateMode: action.newFileCreateMode,
|
2021-03-18 05:52:36 -04:00
|
|
|
}
|
2020-11-26 09:22:30 -05:00
|
|
|
case ACTION_TYPES.START_CREATE_FOLDER:
|
|
|
|
return { ...defaultState, isCreatingFolder: true }
|
2021-03-18 05:52:36 -04:00
|
|
|
case ACTION_TYPES.CREATING_FILE:
|
|
|
|
return {
|
|
|
|
...defaultState,
|
|
|
|
isCreatingFile: true,
|
|
|
|
newFileCreateMode: state.newFileCreateMode,
|
2021-04-27 03:52:58 -04:00
|
|
|
inFlight: true,
|
2021-03-18 05:52:36 -04:00
|
|
|
}
|
2020-11-26 09:22:30 -05:00
|
|
|
case ACTION_TYPES.CREATING_FOLDER:
|
|
|
|
return { ...defaultState, isCreatingFolder: true, inFlight: true }
|
|
|
|
case ACTION_TYPES.DELETING:
|
|
|
|
// keep `actionedEntities` so the entities list remains displayed in the
|
|
|
|
// delete modal
|
|
|
|
return {
|
|
|
|
...defaultState,
|
|
|
|
isDeleting: true,
|
|
|
|
inFlight: true,
|
2021-04-27 03:52:58 -04:00
|
|
|
actionedEntities: state.actionedEntities,
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
2021-01-14 08:57:20 -05:00
|
|
|
case ACTION_TYPES.MOVING:
|
|
|
|
return {
|
|
|
|
...defaultState,
|
|
|
|
isMoving: true,
|
2021-04-27 03:52:58 -04:00
|
|
|
inFlight: true,
|
2021-01-14 08:57:20 -05:00
|
|
|
}
|
2020-11-26 09:22:30 -05:00
|
|
|
case ACTION_TYPES.CLEAR:
|
|
|
|
return { ...defaultState }
|
|
|
|
case ACTION_TYPES.CANCEL:
|
|
|
|
if (state.inFlight) return state
|
|
|
|
return { ...defaultState }
|
|
|
|
case ACTION_TYPES.ERROR:
|
|
|
|
return { ...state, inFlight: false, error: action.error }
|
|
|
|
default:
|
|
|
|
throw new Error(`Unknown user action type: ${action.type}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-28 09:42:31 -04:00
|
|
|
export function FileTreeActionableProvider({ reindexReferences, children }) {
|
2022-01-10 10:46:46 -05:00
|
|
|
const { _id: projectId } = useProjectContext(projectContextPropTypes)
|
|
|
|
const { permissionsLevel } = useEditorContext(editorContextPropTypes)
|
|
|
|
|
2020-11-26 09:22:30 -05:00
|
|
|
const [state, dispatch] = useReducer(
|
2022-01-10 10:46:46 -05:00
|
|
|
permissionsLevel === 'readOnly'
|
|
|
|
? fileTreeActionableReadOnlyReducer
|
|
|
|
: fileTreeActionableReducer,
|
2020-11-26 09:22:30 -05:00
|
|
|
defaultState
|
|
|
|
)
|
|
|
|
|
2022-01-10 10:47:01 -05:00
|
|
|
const { fileTreeData, dispatchRename, dispatchMove } = useFileTreeData()
|
2023-08-18 05:26:22 -04:00
|
|
|
const { selectedEntityIds, isRootFolderSelected } = useFileTreeSelectable()
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2022-07-21 04:32:34 -04:00
|
|
|
const [droppedFiles, setDroppedFiles] = useState(null)
|
|
|
|
|
2021-03-10 04:11:43 -05:00
|
|
|
const startRenaming = useCallback(() => {
|
2020-11-26 09:22:30 -05:00
|
|
|
dispatch({ type: ACTION_TYPES.START_RENAME })
|
2021-05-04 07:36:22 -04:00
|
|
|
}, [])
|
2020-11-26 09:22:30 -05:00
|
|
|
|
|
|
|
// update the entity with the new name immediately in the tree, but revert to
|
|
|
|
// the old name if the sync fails
|
2021-03-10 04:11:43 -05:00
|
|
|
const finishRenaming = useCallback(
|
|
|
|
newName => {
|
|
|
|
const selectedEntityId = Array.from(selectedEntityIds)[0]
|
|
|
|
const found = findInTreeOrThrow(fileTreeData, selectedEntityId)
|
|
|
|
const oldName = found.entity.name
|
|
|
|
if (newName === oldName) {
|
|
|
|
return dispatch({ type: ACTION_TYPES.CLEAR })
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
2021-03-10 04:11:43 -05:00
|
|
|
|
|
|
|
const error = validateRename(fileTreeData, found, newName)
|
|
|
|
if (error) return dispatch({ type: ACTION_TYPES.ERROR, error })
|
|
|
|
|
|
|
|
dispatch({ type: ACTION_TYPES.CLEAR })
|
|
|
|
dispatchRename(selectedEntityId, newName)
|
|
|
|
return syncRename(projectId, found.type, found.entity._id, newName).catch(
|
|
|
|
error => {
|
|
|
|
dispatchRename(selectedEntityId, oldName)
|
|
|
|
// The state from this error action isn't used anywhere right now
|
|
|
|
// but we need to handle the error for linting
|
|
|
|
dispatch({ type: ACTION_TYPES.ERROR, error })
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
2021-05-04 07:36:22 -04:00
|
|
|
[dispatchRename, fileTreeData, projectId, selectedEntityIds]
|
2021-03-10 04:11:43 -05:00
|
|
|
)
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2021-03-18 05:52:36 -04:00
|
|
|
const isDuplicate = useCallback(
|
|
|
|
(parentFolderId, name) => {
|
|
|
|
return !isNameUniqueInFolder(fileTreeData, parentFolderId, name)
|
|
|
|
},
|
|
|
|
[fileTreeData]
|
|
|
|
)
|
|
|
|
|
2020-11-26 09:22:30 -05:00
|
|
|
// init deletion flow (this will open the delete modal).
|
|
|
|
// A copy of the selected entities is set as `actionedEntities` so it is kept
|
|
|
|
// unchanged as the entities are deleted and the selection is updated
|
2021-03-10 04:11:43 -05:00
|
|
|
const startDeleting = useCallback(() => {
|
2020-11-26 09:22:30 -05:00
|
|
|
const actionedEntities = Array.from(selectedEntityIds).map(
|
|
|
|
entityId => findInTreeOrThrow(fileTreeData, entityId).entity
|
|
|
|
)
|
|
|
|
dispatch({ type: ACTION_TYPES.START_DELETE, actionedEntities })
|
2021-05-04 07:36:22 -04:00
|
|
|
}, [fileTreeData, selectedEntityIds])
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2021-05-04 07:36:22 -04:00
|
|
|
// deletes entities in series. Tree will be updated via the socket event
|
2021-03-10 04:11:43 -05:00
|
|
|
const finishDeleting = useCallback(() => {
|
2020-11-26 09:22:30 -05:00
|
|
|
dispatch({ type: ACTION_TYPES.DELETING })
|
2023-09-28 09:42:31 -04:00
|
|
|
let shouldReindexReferences = false
|
2020-11-26 09:22:30 -05:00
|
|
|
|
|
|
|
return mapSeries(Array.from(selectedEntityIds), id => {
|
|
|
|
const found = findInTreeOrThrow(fileTreeData, id)
|
2023-09-28 09:42:31 -04:00
|
|
|
shouldReindexReferences =
|
|
|
|
shouldReindexReferences || /\.bib$/.test(found.entity.name)
|
2020-11-26 09:22:30 -05:00
|
|
|
return syncDelete(projectId, found.type, found.entity._id).catch(
|
|
|
|
error => {
|
|
|
|
// throw unless 404
|
2021-01-14 08:58:02 -05:00
|
|
|
if (error.info.statusCode !== 404) {
|
2020-11-26 09:22:30 -05:00
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.then(() => {
|
2023-09-28 09:42:31 -04:00
|
|
|
if (shouldReindexReferences) {
|
|
|
|
reindexReferences()
|
|
|
|
}
|
2020-11-26 09:22:30 -05:00
|
|
|
dispatch({ type: ACTION_TYPES.CLEAR })
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
// set an error and allow user to retry
|
|
|
|
dispatch({ type: ACTION_TYPES.ERROR, error })
|
|
|
|
})
|
2023-09-28 09:42:31 -04:00
|
|
|
}, [fileTreeData, projectId, selectedEntityIds, reindexReferences])
|
2020-11-26 09:22:30 -05:00
|
|
|
|
|
|
|
// moves entities. Tree is updated immediately and data are sync'd after.
|
2021-03-10 04:11:43 -05:00
|
|
|
const finishMoving = useCallback(
|
|
|
|
(toFolderId, draggedEntityIds) => {
|
|
|
|
dispatch({ type: ACTION_TYPES.MOVING })
|
|
|
|
|
|
|
|
// find entities and filter out no-ops
|
|
|
|
const founds = Array.from(draggedEntityIds)
|
|
|
|
.map(draggedEntityId =>
|
|
|
|
findInTreeOrThrow(fileTreeData, draggedEntityId)
|
|
|
|
)
|
|
|
|
.filter(found => found.parentFolderId !== toFolderId)
|
|
|
|
|
|
|
|
// make sure all entities can be moved, return early otherwise
|
|
|
|
const isMoveToRoot = toFolderId === fileTreeData._id
|
|
|
|
const validationError = founds
|
|
|
|
.map(found =>
|
|
|
|
validateMove(fileTreeData, toFolderId, found, isMoveToRoot)
|
|
|
|
)
|
|
|
|
.find(error => error)
|
|
|
|
if (validationError) {
|
|
|
|
return dispatch({ type: ACTION_TYPES.ERROR, error: validationError })
|
|
|
|
}
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2023-07-28 13:34:03 -04:00
|
|
|
// keep track of old parent folder ids so we can revert entities if sync fails
|
|
|
|
const oldParentFolderIds = {}
|
|
|
|
let isMoveFailed = false
|
|
|
|
|
2021-03-10 04:11:43 -05:00
|
|
|
// dispatch moves immediately
|
2023-07-28 13:34:03 -04:00
|
|
|
founds.forEach(found => {
|
|
|
|
oldParentFolderIds[found.entity._id] = found.parentFolderId
|
|
|
|
dispatchMove(found.entity._id, toFolderId)
|
|
|
|
})
|
2021-03-10 04:11:43 -05:00
|
|
|
|
|
|
|
// sync dispatched moves after
|
2023-07-28 13:34:03 -04:00
|
|
|
return mapSeries(founds, async found => {
|
|
|
|
try {
|
|
|
|
await syncMove(projectId, found.type, found.entity._id, toFolderId)
|
|
|
|
} catch (error) {
|
|
|
|
isMoveFailed = true
|
|
|
|
dispatchMove(found.entity._id, oldParentFolderIds[found.entity._id])
|
2021-03-10 04:11:43 -05:00
|
|
|
dispatch({ type: ACTION_TYPES.ERROR, error })
|
2023-07-28 13:34:03 -04:00
|
|
|
}
|
|
|
|
}).then(() => {
|
|
|
|
if (!isMoveFailed) {
|
|
|
|
dispatch({ type: ACTION_TYPES.CLEAR })
|
|
|
|
}
|
|
|
|
})
|
2021-03-10 04:11:43 -05:00
|
|
|
},
|
2021-05-04 07:36:22 -04:00
|
|
|
[dispatchMove, fileTreeData, projectId]
|
2021-03-10 04:11:43 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const startCreatingFolder = useCallback(() => {
|
2020-11-26 09:22:30 -05:00
|
|
|
dispatch({ type: ACTION_TYPES.START_CREATE_FOLDER })
|
2021-05-04 07:36:22 -04:00
|
|
|
}, [])
|
|
|
|
|
2023-08-18 05:26:22 -04:00
|
|
|
const parentFolderId = useMemo(() => {
|
|
|
|
return getSelectedParentFolderId(
|
|
|
|
fileTreeData,
|
|
|
|
selectedEntityIds,
|
|
|
|
isRootFolderSelected
|
|
|
|
)
|
|
|
|
}, [fileTreeData, selectedEntityIds, isRootFolderSelected])
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2021-03-10 04:11:43 -05:00
|
|
|
const finishCreatingEntity = useCallback(
|
|
|
|
entity => {
|
2021-03-18 05:52:36 -04:00
|
|
|
const error = validateCreate(fileTreeData, parentFolderId, entity)
|
|
|
|
if (error) {
|
|
|
|
return Promise.reject(error)
|
2021-03-10 04:11:43 -05:00
|
|
|
}
|
2021-03-18 05:52:36 -04:00
|
|
|
|
|
|
|
return syncCreateEntity(projectId, parentFolderId, entity)
|
2021-03-10 04:11:43 -05:00
|
|
|
},
|
2021-05-04 07:36:22 -04:00
|
|
|
[fileTreeData, parentFolderId, projectId]
|
2021-03-10 04:11:43 -05:00
|
|
|
)
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2021-03-10 04:11:43 -05:00
|
|
|
const finishCreatingFolder = useCallback(
|
|
|
|
name => {
|
|
|
|
dispatch({ type: ACTION_TYPES.CREATING_FOLDER })
|
|
|
|
return finishCreatingEntity({ endpoint: 'folder', name })
|
|
|
|
.then(() => {
|
|
|
|
dispatch({ type: ACTION_TYPES.CLEAR })
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
dispatch({ type: ACTION_TYPES.ERROR, error })
|
|
|
|
})
|
|
|
|
},
|
2021-05-04 07:36:22 -04:00
|
|
|
[finishCreatingEntity]
|
2021-03-10 04:11:43 -05:00
|
|
|
)
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2021-05-04 07:36:22 -04:00
|
|
|
const startCreatingFile = useCallback(newFileCreateMode => {
|
|
|
|
dispatch({ type: ACTION_TYPES.START_CREATE_FILE, newFileCreateMode })
|
|
|
|
}, [])
|
2021-03-18 05:52:36 -04:00
|
|
|
|
2021-03-10 04:11:43 -05:00
|
|
|
const startCreatingDocOrFile = useCallback(() => {
|
2021-04-29 05:11:00 -04:00
|
|
|
startCreatingFile('doc')
|
|
|
|
}, [startCreatingFile])
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2021-03-10 04:11:43 -05:00
|
|
|
const startUploadingDocOrFile = useCallback(() => {
|
2021-04-29 05:11:00 -04:00
|
|
|
startCreatingFile('upload')
|
|
|
|
}, [startCreatingFile])
|
2021-03-10 04:11:43 -05:00
|
|
|
|
|
|
|
const finishCreatingDocOrFile = useCallback(
|
2021-03-18 05:52:36 -04:00
|
|
|
entity => {
|
|
|
|
dispatch({ type: ACTION_TYPES.CREATING_FILE })
|
|
|
|
|
|
|
|
return finishCreatingEntity(entity)
|
2021-03-10 04:11:43 -05:00
|
|
|
.then(() => {
|
2021-04-29 05:11:00 -04:00
|
|
|
dispatch({ type: ACTION_TYPES.CLEAR })
|
2021-03-10 04:11:43 -05:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2021-04-29 05:11:00 -04:00
|
|
|
dispatch({ type: ACTION_TYPES.ERROR, error })
|
2021-03-18 05:52:36 -04:00
|
|
|
})
|
|
|
|
},
|
2021-05-04 07:36:22 -04:00
|
|
|
[finishCreatingEntity]
|
2021-03-10 04:11:43 -05:00
|
|
|
)
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2021-03-10 04:11:43 -05:00
|
|
|
const finishCreatingDoc = useCallback(
|
|
|
|
entity => {
|
|
|
|
entity.endpoint = 'doc'
|
|
|
|
return finishCreatingDocOrFile(entity)
|
|
|
|
},
|
|
|
|
[finishCreatingDocOrFile]
|
|
|
|
)
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2021-03-10 04:11:43 -05:00
|
|
|
const finishCreatingLinkedFile = useCallback(
|
|
|
|
entity => {
|
|
|
|
entity.endpoint = 'linked_file'
|
|
|
|
return finishCreatingDocOrFile(entity)
|
|
|
|
},
|
|
|
|
[finishCreatingDocOrFile]
|
|
|
|
)
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2021-03-10 04:11:43 -05:00
|
|
|
const cancel = useCallback(() => {
|
2020-11-26 09:22:30 -05:00
|
|
|
dispatch({ type: ACTION_TYPES.CANCEL })
|
2021-05-04 07:36:22 -04:00
|
|
|
}, [])
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2021-06-11 04:40:42 -04:00
|
|
|
// listen for `file-tree.start-creating` events
|
|
|
|
useEffect(() => {
|
|
|
|
function handleEvent(event) {
|
|
|
|
dispatch({
|
|
|
|
type: ACTION_TYPES.START_CREATE_FILE,
|
|
|
|
newFileCreateMode: event.detail.mode,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener('file-tree.start-creating', handleEvent)
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener('file-tree.start-creating', handleEvent)
|
|
|
|
}
|
|
|
|
}, [])
|
|
|
|
|
2022-08-12 05:54:32 -04:00
|
|
|
// build the path for downloading a single file
|
|
|
|
const downloadPath = useMemo(() => {
|
|
|
|
if (selectedEntityIds.size === 1) {
|
|
|
|
const [selectedEntityId] = selectedEntityIds
|
|
|
|
const selectedEntity = findInTree(fileTreeData, selectedEntityId)
|
|
|
|
if (selectedEntity?.type === 'fileRef') {
|
|
|
|
return `/project/${projectId}/file/${selectedEntityId}`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [fileTreeData, projectId, selectedEntityIds])
|
|
|
|
|
2021-05-04 07:36:22 -04:00
|
|
|
const value = {
|
2023-08-18 05:26:22 -04:00
|
|
|
canDelete: selectedEntityIds.size > 0 && !isRootFolderSelected,
|
|
|
|
canRename: selectedEntityIds.size === 1 && !isRootFolderSelected,
|
2020-12-10 06:15:05 -05:00
|
|
|
canCreate: selectedEntityIds.size < 2,
|
2021-05-04 07:36:22 -04:00
|
|
|
...state,
|
2021-03-18 05:52:36 -04:00
|
|
|
parentFolderId,
|
|
|
|
isDuplicate,
|
2020-11-26 09:22:30 -05:00
|
|
|
startRenaming,
|
|
|
|
finishRenaming,
|
|
|
|
startDeleting,
|
|
|
|
finishDeleting,
|
|
|
|
finishMoving,
|
2021-03-18 05:52:36 -04:00
|
|
|
startCreatingFile,
|
2020-11-26 09:22:30 -05:00
|
|
|
startCreatingFolder,
|
|
|
|
finishCreatingFolder,
|
|
|
|
startCreatingDocOrFile,
|
|
|
|
startUploadingDocOrFile,
|
|
|
|
finishCreatingDoc,
|
|
|
|
finishCreatingLinkedFile,
|
2021-04-27 03:52:58 -04:00
|
|
|
cancel,
|
2022-07-21 04:32:34 -04:00
|
|
|
droppedFiles,
|
|
|
|
setDroppedFiles,
|
2022-08-12 05:54:32 -04:00
|
|
|
downloadPath,
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
2021-05-04 07:36:22 -04:00
|
|
|
|
|
|
|
return (
|
|
|
|
<FileTreeActionableContext.Provider value={value}>
|
|
|
|
{children}
|
|
|
|
</FileTreeActionableContext.Provider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
FileTreeActionableProvider.propTypes = {
|
2023-09-28 09:42:31 -04:00
|
|
|
reindexReferences: PropTypes.func.isRequired,
|
2021-05-04 07:36:22 -04:00
|
|
|
children: PropTypes.oneOfType([
|
|
|
|
PropTypes.arrayOf(PropTypes.node),
|
|
|
|
PropTypes.node,
|
|
|
|
]).isRequired,
|
|
|
|
}
|
|
|
|
|
2022-01-10 10:46:46 -05:00
|
|
|
const projectContextPropTypes = {
|
|
|
|
_id: PropTypes.string.isRequired,
|
|
|
|
}
|
|
|
|
|
|
|
|
const editorContextPropTypes = {
|
|
|
|
permissionsLevel: PropTypes.oneOf(['readOnly', 'readAndWrite', 'owner']),
|
|
|
|
}
|
|
|
|
|
2021-05-04 07:36:22 -04:00
|
|
|
export function useFileTreeActionable() {
|
|
|
|
const context = useContext(FileTreeActionableContext)
|
|
|
|
|
|
|
|
if (!context) {
|
|
|
|
throw new Error(
|
|
|
|
'useFileTreeActionable is only available inside FileTreeActionableProvider'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return context
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
2020-12-10 06:15:05 -05:00
|
|
|
|
2023-08-18 05:26:22 -04:00
|
|
|
function getSelectedParentFolderId(
|
|
|
|
fileTreeData,
|
|
|
|
selectedEntityIds,
|
|
|
|
isRootFolderSelected
|
|
|
|
) {
|
|
|
|
if (isRootFolderSelected) {
|
|
|
|
return fileTreeData._id
|
|
|
|
}
|
|
|
|
|
2020-12-10 06:15:05 -05:00
|
|
|
// we expect only one entity to be selected in that case, so we pick the first
|
|
|
|
const selectedEntityId = Array.from(selectedEntityIds)[0]
|
|
|
|
if (!selectedEntityId) {
|
|
|
|
// in some cases no entities are selected. Return the root folder id then.
|
|
|
|
return fileTreeData._id
|
|
|
|
}
|
|
|
|
|
2021-05-18 06:58:53 -04:00
|
|
|
const found = findInTree(fileTreeData, selectedEntityId)
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
// if the entity isn't in the tree, return the root folder id.
|
|
|
|
return fileTreeData._id
|
|
|
|
}
|
|
|
|
|
2020-12-10 06:15:05 -05:00
|
|
|
return found.type === 'folder' ? found.entity._id : found.parentFolderId
|
|
|
|
}
|
2021-01-14 08:57:20 -05:00
|
|
|
|
2021-03-18 05:52:36 -04:00
|
|
|
function validateCreate(fileTreeData, parentFolderId, entity) {
|
|
|
|
if (!isCleanFilename(entity.name)) {
|
|
|
|
return new InvalidFilenameError()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isNameUniqueInFolder(fileTreeData, parentFolderId, entity.name)) {
|
|
|
|
return new DuplicateFilenameError()
|
|
|
|
}
|
|
|
|
|
|
|
|
// check that the name of a file is allowed, if creating in the root folder
|
|
|
|
const isMoveToRoot = parentFolderId === fileTreeData._id
|
|
|
|
const isFolder = entity.endpoint === 'folder'
|
|
|
|
if (isMoveToRoot && !isFolder && isBlockedFilename(entity.name)) {
|
|
|
|
return new BlockedFilenameError()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 08:57:20 -05:00
|
|
|
function validateRename(fileTreeData, found, newName) {
|
|
|
|
if (!isCleanFilename(newName)) {
|
|
|
|
return new InvalidFilenameError()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isNameUniqueInFolder(fileTreeData, found.parentFolderId, newName)) {
|
|
|
|
return new DuplicateFilenameError()
|
|
|
|
}
|
|
|
|
|
|
|
|
const isTopLevel = found.path.length === 1
|
|
|
|
const isFolder = found.type === 'folder'
|
|
|
|
if (isTopLevel && !isFolder && isBlockedFilename(newName)) {
|
|
|
|
return new BlockedFilenameError()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function validateMove(fileTreeData, toFolderId, found, isMoveToRoot) {
|
|
|
|
if (!isNameUniqueInFolder(fileTreeData, toFolderId, found.entity.name)) {
|
|
|
|
const error = new DuplicateFilenameMoveError()
|
|
|
|
error.entityName = found.entity.name
|
|
|
|
return error
|
|
|
|
}
|
|
|
|
|
|
|
|
const isFolder = found.type === 'folder'
|
|
|
|
if (isMoveToRoot && !isFolder && isBlockedFilename(found.entity.name)) {
|
|
|
|
return new BlockedFilenameError()
|
|
|
|
}
|
|
|
|
}
|