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,
|
2024-01-26 04:23:48 -05:00
|
|
|
FC,
|
2021-03-10 04:11:43 -05:00
|
|
|
} from 'react'
|
2020-11-26 09:22:30 -05:00
|
|
|
|
|
|
|
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'
|
2024-01-26 04:23:48 -05:00
|
|
|
import { Folder } from '../../../../../types/folder'
|
|
|
|
|
2024-02-21 06:34:25 -05:00
|
|
|
type DroppedFile = File & {
|
|
|
|
relativePath?: string
|
|
|
|
}
|
|
|
|
|
|
|
|
type DroppedFiles = {
|
|
|
|
files: DroppedFile[]
|
|
|
|
targetFolderId: string
|
|
|
|
}
|
|
|
|
|
2024-01-26 04:23:48 -05:00
|
|
|
const FileTreeActionableContext = createContext<
|
|
|
|
| {
|
|
|
|
isDeleting: boolean
|
|
|
|
isRenaming: boolean
|
|
|
|
isCreatingFile: boolean
|
|
|
|
isCreatingFolder: boolean
|
|
|
|
isMoving: boolean
|
|
|
|
inFlight: boolean
|
|
|
|
actionedEntities: any | null
|
|
|
|
newFileCreateMode: any | null
|
|
|
|
error: any | null
|
|
|
|
canDelete: boolean
|
|
|
|
canRename: boolean
|
|
|
|
canCreate: boolean
|
|
|
|
parentFolderId: string
|
2024-05-07 10:38:39 -04:00
|
|
|
selectedFileName: string | null
|
2024-01-26 04:23:48 -05:00
|
|
|
isDuplicate: (parentFolderId: string, name: string) => boolean
|
|
|
|
startRenaming: any
|
|
|
|
finishRenaming: any
|
|
|
|
startDeleting: any
|
|
|
|
finishDeleting: any
|
|
|
|
finishMoving: any
|
|
|
|
startCreatingFile: any
|
|
|
|
startCreatingFolder: any
|
|
|
|
finishCreatingFolder: any
|
|
|
|
startCreatingDocOrFile: any
|
|
|
|
startUploadingDocOrFile: any
|
|
|
|
finishCreatingDoc: any
|
|
|
|
finishCreatingLinkedFile: any
|
|
|
|
cancel: () => void
|
2024-02-21 06:34:25 -05:00
|
|
|
droppedFiles: { files: File[]; targetFolderId: string } | null
|
|
|
|
setDroppedFiles: (value: DroppedFiles | null) => void
|
2024-01-26 04:23:48 -05:00
|
|
|
downloadPath?: string
|
|
|
|
}
|
|
|
|
| undefined
|
|
|
|
>(undefined)
|
|
|
|
|
|
|
|
/* eslint-disable no-unused-vars */
|
|
|
|
enum ACTION_TYPES {
|
|
|
|
START_RENAME = 'START_RENAME',
|
|
|
|
START_DELETE = 'START_DELETE',
|
|
|
|
DELETING = 'DELETING',
|
|
|
|
START_CREATE_FILE = 'START_CREATE_FILE',
|
|
|
|
START_CREATE_FOLDER = 'START_CREATE_FOLDER',
|
|
|
|
CREATING_FILE = 'CREATING_FILE',
|
|
|
|
CREATING_FOLDER = 'CREATING_FOLDER',
|
|
|
|
MOVING = 'MOVING',
|
|
|
|
CANCEL = 'CANCEL',
|
|
|
|
CLEAR = 'CLEAR',
|
|
|
|
ERROR = 'ERROR',
|
|
|
|
}
|
|
|
|
/* eslint-enable no-unused-vars */
|
|
|
|
|
|
|
|
type State = {
|
|
|
|
isDeleting: boolean
|
|
|
|
isRenaming: boolean
|
|
|
|
isCreatingFile: boolean
|
|
|
|
isCreatingFolder: boolean
|
|
|
|
isMoving: boolean
|
|
|
|
inFlight: boolean
|
|
|
|
actionedEntities: any | null
|
|
|
|
newFileCreateMode: any | null
|
|
|
|
error: unknown | null
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
|
|
|
|
2024-01-26 04:23:48 -05:00
|
|
|
const defaultState: State = {
|
2020-11-26 09:22:30 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-01-26 04:23:48 -05:00
|
|
|
function fileTreeActionableReadOnlyReducer(state: State) {
|
2020-11-26 09:22:30 -05:00
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
2024-01-26 04:23:48 -05:00
|
|
|
type Action =
|
|
|
|
| {
|
|
|
|
type: ACTION_TYPES.START_RENAME
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: ACTION_TYPES.START_DELETE
|
|
|
|
actionedEntities: any | null
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: ACTION_TYPES.START_CREATE_FILE
|
|
|
|
newFileCreateMode: any | null
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: ACTION_TYPES.START_CREATE_FOLDER
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: ACTION_TYPES.CREATING_FILE
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: ACTION_TYPES.CREATING_FOLDER
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: ACTION_TYPES.DELETING
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: ACTION_TYPES.MOVING
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: ACTION_TYPES.CLEAR
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: ACTION_TYPES.CANCEL
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: ACTION_TYPES.ERROR
|
|
|
|
error: unknown
|
|
|
|
}
|
|
|
|
|
|
|
|
function fileTreeActionableReducer(state: State, action: Action) {
|
2020-11-26 09:22:30 -05:00
|
|
|
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:
|
2024-01-26 04:23:48 -05:00
|
|
|
throw new Error(`Unknown user action type: ${(action as Action).type}`)
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-26 04:23:48 -05:00
|
|
|
export const FileTreeActionableProvider: FC<{
|
|
|
|
reindexReferences: () => void
|
|
|
|
}> = ({ reindexReferences, children }) => {
|
|
|
|
const { _id: projectId } = useProjectContext()
|
|
|
|
const { permissionsLevel } = useEditorContext()
|
2022-01-10 10:46:46 -05:00
|
|
|
|
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
|
|
|
|
2024-02-21 06:34:25 -05:00
|
|
|
const [droppedFiles, setDroppedFiles] = useState<DroppedFiles | null>(null)
|
2022-07-21 04:32:34 -04:00
|
|
|
|
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(
|
2024-01-26 04:23:48 -05:00
|
|
|
(newName: string) => {
|
2021-03-10 04:11:43 -05:00
|
|
|
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(
|
2024-01-26 04:23:48 -05:00
|
|
|
(parentFolderId: string, name: string) => {
|
2021-03-18 05:52:36 -04:00
|
|
|
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
|
|
|
|
2024-01-26 04:23:48 -05:00
|
|
|
return (
|
|
|
|
mapSeries(Array.from(selectedEntityIds), id => {
|
|
|
|
const found = findInTreeOrThrow(fileTreeData, id)
|
|
|
|
shouldReindexReferences =
|
|
|
|
shouldReindexReferences || /\.bib$/.test(found.entity.name)
|
|
|
|
return syncDelete(projectId, found.type, found.entity._id).catch(
|
|
|
|
error => {
|
|
|
|
// throw unless 404
|
|
|
|
if (error.info.statusCode !== 404) {
|
|
|
|
throw error
|
|
|
|
}
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
2024-01-26 04:23:48 -05:00
|
|
|
)
|
2020-11-26 09:22:30 -05:00
|
|
|
})
|
2024-01-26 04:23:48 -05:00
|
|
|
// @ts-ignore (TODO: improve mapSeries types)
|
|
|
|
.then(() => {
|
|
|
|
if (shouldReindexReferences) {
|
|
|
|
reindexReferences()
|
|
|
|
}
|
|
|
|
dispatch({ type: ACTION_TYPES.CLEAR })
|
|
|
|
})
|
|
|
|
.catch((error: 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 })
|
|
|
|
|
2023-10-23 06:24:52 -04:00
|
|
|
// find entities and filter out no-ops and nested files
|
2021-03-10 04:11:43 -05:00
|
|
|
const founds = Array.from(draggedEntityIds)
|
|
|
|
.map(draggedEntityId =>
|
|
|
|
findInTreeOrThrow(fileTreeData, draggedEntityId)
|
|
|
|
)
|
2023-10-23 06:24:52 -04:00
|
|
|
.filter(
|
|
|
|
found =>
|
|
|
|
found.parentFolderId !== toFolderId &&
|
|
|
|
!draggedEntityIds.has(found.parentFolderId)
|
|
|
|
)
|
2021-03-10 04:11:43 -05:00
|
|
|
|
|
|
|
// 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
|
2024-01-26 04:23:48 -05:00
|
|
|
const oldParentFolderIds: Record<string, string> = {}
|
2023-07-28 13:34:03 -04:00
|
|
|
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
|
2024-01-26 04:23:48 -05: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])
|
|
|
|
dispatch({ type: ACTION_TYPES.ERROR, error })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// @ts-ignore (TODO: improve mapSeries types)
|
|
|
|
.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
|
|
|
|
2024-05-07 10:38:39 -04:00
|
|
|
// 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])
|
|
|
|
|
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(() => {
|
2024-01-26 04:23:48 -05:00
|
|
|
function handleEvent(event: Event) {
|
2021-06-11 04:40:42 -04:00
|
|
|
dispatch({
|
|
|
|
type: ACTION_TYPES.START_CREATE_FILE,
|
2024-01-26 04:23:48 -05:00
|
|
|
newFileCreateMode: (event as CustomEvent<{ mode: string }>).detail.mode,
|
2021-06-11 04:40:42 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener('file-tree.start-creating', handleEvent)
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener('file-tree.start-creating', handleEvent)
|
|
|
|
}
|
|
|
|
}, [])
|
|
|
|
|
2024-02-21 06:36:00 -05:00
|
|
|
// build the path for downloading a single file or doc
|
2022-08-12 05:54:32 -04:00
|
|
|
const downloadPath = useMemo(() => {
|
|
|
|
if (selectedEntityIds.size === 1) {
|
|
|
|
const [selectedEntityId] = selectedEntityIds
|
|
|
|
const selectedEntity = findInTree(fileTreeData, selectedEntityId)
|
2024-02-21 06:36:00 -05:00
|
|
|
|
2022-08-12 05:54:32 -04:00
|
|
|
if (selectedEntity?.type === 'fileRef') {
|
|
|
|
return `/project/${projectId}/file/${selectedEntityId}`
|
|
|
|
}
|
2024-02-21 06:36:00 -05:00
|
|
|
|
|
|
|
if (selectedEntity?.type === 'doc') {
|
|
|
|
return `/project/${projectId}/doc/${selectedEntityId}/download`
|
|
|
|
}
|
2022-08-12 05:54:32 -04:00
|
|
|
}
|
|
|
|
}, [fileTreeData, projectId, selectedEntityIds])
|
|
|
|
|
2024-01-26 04:23:48 -05:00
|
|
|
// TODO: wrap in useMemo
|
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,
|
2024-05-07 10:38:39 -04:00
|
|
|
selectedFileName,
|
2021-03-18 05:52:36 -04:00
|
|
|
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>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
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(
|
2024-01-26 04:23:48 -05:00
|
|
|
fileTreeData: Folder,
|
|
|
|
selectedEntityIds: Set<string>,
|
|
|
|
isRootFolderSelected: boolean
|
2023-08-18 05:26:22 -04:00
|
|
|
) {
|
|
|
|
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
|
|
|
|
2024-01-26 04:23:48 -05:00
|
|
|
function validateCreate(
|
|
|
|
fileTreeData: Folder,
|
|
|
|
parentFolderId: string,
|
|
|
|
entity: { name: string; endpoint: string }
|
|
|
|
) {
|
2021-03-18 05:52:36 -04:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-26 04:23:48 -05:00
|
|
|
function validateRename(
|
|
|
|
fileTreeData: Folder,
|
|
|
|
found: { parentFolderId: string; path: string; type: string },
|
|
|
|
newName: string
|
|
|
|
) {
|
2021-01-14 08:57:20 -05:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-26 04:23:48 -05:00
|
|
|
function validateMove(
|
|
|
|
fileTreeData: Folder,
|
|
|
|
toFolderId: string,
|
|
|
|
found: { entity: { name: string }; type: string },
|
|
|
|
isMoveToRoot: boolean
|
|
|
|
) {
|
2021-01-14 08:57:20 -05:00
|
|
|
if (!isNameUniqueInFolder(fileTreeData, toFolderId, found.entity.name)) {
|
|
|
|
const error = new DuplicateFilenameMoveError()
|
2024-01-26 04:23:48 -05:00
|
|
|
;(error as DuplicateFilenameMoveError & { entityName: string }).entityName =
|
|
|
|
found.entity.name
|
2021-01-14 08:57:20 -05:00
|
|
|
return error
|
|
|
|
}
|
|
|
|
|
|
|
|
const isFolder = found.type === 'folder'
|
|
|
|
if (isMoveToRoot && !isFolder && isBlockedFilename(found.entity.name)) {
|
|
|
|
return new BlockedFilenameError()
|
|
|
|
}
|
|
|
|
}
|