2021-06-23 05:37:08 -04:00
|
|
|
import {
|
2021-03-10 04:11:43 -05:00
|
|
|
createContext,
|
|
|
|
useCallback,
|
|
|
|
useReducer,
|
2021-04-27 03:52:58 -04:00
|
|
|
useContext,
|
2022-01-10 10:47:01 -05:00
|
|
|
useMemo,
|
2022-03-21 10:47:01 -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'
|
2022-01-10 10:47:01 -05:00
|
|
|
import useScopeValue from '../hooks/use-scope-value'
|
2020-11-26 09:22:30 -05:00
|
|
|
import {
|
|
|
|
renameInTree,
|
|
|
|
deleteInTree,
|
|
|
|
moveInTree,
|
2021-04-27 03:52:58 -04:00
|
|
|
createEntityInTree,
|
2022-01-10 10:47:01 -05:00
|
|
|
} from '../../features/file-tree/util/mutate-in-tree'
|
|
|
|
import { countFiles } from '../../features/file-tree/util/count-in-tree'
|
2022-02-15 08:37:23 -05:00
|
|
|
import useDeepCompareEffect from '../../shared/hooks/use-deep-compare-effect'
|
2022-01-10 10:47:01 -05:00
|
|
|
|
|
|
|
const FileTreeDataContext = createContext()
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2022-01-10 10:47:01 -05:00
|
|
|
const fileTreeDataPropType = PropTypes.shape({
|
|
|
|
_id: PropTypes.string.isRequired,
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
docs: PropTypes.array.isRequired,
|
|
|
|
fileRefs: PropTypes.array.isRequired,
|
|
|
|
folders: PropTypes.array.isRequired,
|
|
|
|
})
|
|
|
|
|
|
|
|
FileTreeDataContext.Provider.propTypes = {
|
|
|
|
value: PropTypes.shape({
|
|
|
|
// fileTreeData is the up-to-date representation of the files list, updated
|
|
|
|
// by the file tree
|
|
|
|
fileTreeData: fileTreeDataPropType,
|
|
|
|
hasFolders: PropTypes.bool,
|
|
|
|
}),
|
|
|
|
}
|
2020-11-26 09:22:30 -05:00
|
|
|
|
|
|
|
const ACTION_TYPES = {
|
|
|
|
RENAME: 'RENAME',
|
2021-05-18 06:58:53 -04:00
|
|
|
RESET: 'RESET',
|
2020-11-26 09:22:30 -05:00
|
|
|
DELETE: 'DELETE',
|
|
|
|
MOVE: 'MOVE',
|
2022-01-10 10:46:46 -05:00
|
|
|
CREATE: 'CREATE',
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function fileTreeMutableReducer({ fileTreeData }, action) {
|
|
|
|
switch (action.type) {
|
2021-05-18 06:58:53 -04:00
|
|
|
case ACTION_TYPES.RESET: {
|
|
|
|
const newFileTreeData = action.fileTreeData
|
|
|
|
|
|
|
|
return {
|
|
|
|
fileTreeData: newFileTreeData,
|
|
|
|
fileCount: countFiles(newFileTreeData),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-18 05:52:36 -04:00
|
|
|
case ACTION_TYPES.RENAME: {
|
|
|
|
const newFileTreeData = renameInTree(fileTreeData, action.id, {
|
2021-04-27 03:52:58 -04:00
|
|
|
newName: action.newName,
|
2021-03-18 05:52:36 -04:00
|
|
|
})
|
|
|
|
|
2020-11-26 09:22:30 -05:00
|
|
|
return {
|
2021-03-18 05:52:36 -04:00
|
|
|
fileTreeData: newFileTreeData,
|
2021-04-27 03:52:58 -04:00
|
|
|
fileCount: countFiles(newFileTreeData),
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
2021-03-18 05:52:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
case ACTION_TYPES.DELETE: {
|
|
|
|
const newFileTreeData = deleteInTree(fileTreeData, action.id)
|
|
|
|
|
2020-11-26 09:22:30 -05:00
|
|
|
return {
|
2021-03-18 05:52:36 -04:00
|
|
|
fileTreeData: newFileTreeData,
|
2021-04-27 03:52:58 -04:00
|
|
|
fileCount: countFiles(newFileTreeData),
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
2021-03-18 05:52:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
case ACTION_TYPES.MOVE: {
|
|
|
|
const newFileTreeData = moveInTree(
|
|
|
|
fileTreeData,
|
|
|
|
action.entityId,
|
|
|
|
action.toFolderId
|
|
|
|
)
|
|
|
|
|
2020-11-26 09:22:30 -05:00
|
|
|
return {
|
2021-03-18 05:52:36 -04:00
|
|
|
fileTreeData: newFileTreeData,
|
2021-04-27 03:52:58 -04:00
|
|
|
fileCount: countFiles(newFileTreeData),
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
2021-03-18 05:52:36 -04:00
|
|
|
}
|
|
|
|
|
2022-01-10 10:46:46 -05:00
|
|
|
case ACTION_TYPES.CREATE: {
|
2021-03-18 05:52:36 -04:00
|
|
|
const newFileTreeData = createEntityInTree(
|
|
|
|
fileTreeData,
|
|
|
|
action.parentFolderId,
|
|
|
|
action.entity
|
|
|
|
)
|
|
|
|
|
2020-11-26 09:22:30 -05:00
|
|
|
return {
|
2021-03-18 05:52:36 -04:00
|
|
|
fileTreeData: newFileTreeData,
|
2021-04-27 03:52:58 -04:00
|
|
|
fileCount: countFiles(newFileTreeData),
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
2021-03-18 05:52:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
default: {
|
2020-11-26 09:22:30 -05:00
|
|
|
throw new Error(`Unknown mutable file tree action type: ${action.type}`)
|
2021-03-18 05:52:36 -04:00
|
|
|
}
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 10:47:01 -05:00
|
|
|
const initialState = rootFolder => {
|
|
|
|
const fileTreeData = rootFolder?.[0]
|
|
|
|
return {
|
|
|
|
fileTreeData,
|
|
|
|
fileCount: countFiles(fileTreeData),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useFileTreeData(propTypes) {
|
|
|
|
const context = useContext(FileTreeDataContext)
|
|
|
|
|
|
|
|
if (!context) {
|
|
|
|
throw new Error(
|
|
|
|
'useFileTreeData is only available inside FileTreeDataProvider'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
PropTypes.checkPropTypes(
|
|
|
|
propTypes,
|
|
|
|
context,
|
|
|
|
'data',
|
|
|
|
'FileTreeDataContext.Provider'
|
|
|
|
)
|
|
|
|
|
|
|
|
return context
|
|
|
|
}
|
2021-05-18 06:58:53 -04:00
|
|
|
|
2022-01-10 10:47:01 -05:00
|
|
|
export function FileTreeDataProvider({ children }) {
|
|
|
|
const [project] = useScopeValue('project', true)
|
|
|
|
|
|
|
|
const { rootFolder } = project || {}
|
2022-01-10 10:46:46 -05:00
|
|
|
|
2021-03-18 05:52:36 -04:00
|
|
|
const [{ fileTreeData, fileCount }, dispatch] = useReducer(
|
|
|
|
fileTreeMutableReducer,
|
2021-05-18 06:58:53 -04:00
|
|
|
rootFolder,
|
|
|
|
initialState
|
2021-03-18 05:52:36 -04:00
|
|
|
)
|
2020-11-26 09:22:30 -05:00
|
|
|
|
2022-03-21 10:47:01 -04:00
|
|
|
const [selectedEntities, setSelectedEntities] = useState([])
|
|
|
|
|
2022-02-15 08:37:23 -05:00
|
|
|
useDeepCompareEffect(() => {
|
2021-05-18 06:58:53 -04:00
|
|
|
dispatch({
|
|
|
|
type: ACTION_TYPES.RESET,
|
2022-01-10 10:47:01 -05:00
|
|
|
fileTreeData: rootFolder?.[0],
|
2021-05-18 06:58:53 -04:00
|
|
|
})
|
|
|
|
}, [rootFolder])
|
|
|
|
|
2021-05-04 07:36:22 -04:00
|
|
|
const dispatchCreateFolder = useCallback((parentFolderId, entity) => {
|
|
|
|
entity.type = 'folder'
|
|
|
|
dispatch({
|
2022-01-10 10:46:46 -05:00
|
|
|
type: ACTION_TYPES.CREATE,
|
2021-05-04 07:36:22 -04:00
|
|
|
parentFolderId,
|
|
|
|
entity,
|
|
|
|
})
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const dispatchCreateDoc = useCallback((parentFolderId, entity) => {
|
|
|
|
entity.type = 'doc'
|
|
|
|
dispatch({
|
2022-01-10 10:46:46 -05:00
|
|
|
type: ACTION_TYPES.CREATE,
|
2021-05-04 07:36:22 -04:00
|
|
|
parentFolderId,
|
|
|
|
entity,
|
|
|
|
})
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const dispatchCreateFile = useCallback((parentFolderId, entity) => {
|
|
|
|
entity.type = 'fileRef'
|
|
|
|
dispatch({
|
2022-01-10 10:46:46 -05:00
|
|
|
type: ACTION_TYPES.CREATE,
|
2021-05-04 07:36:22 -04:00
|
|
|
parentFolderId,
|
|
|
|
entity,
|
|
|
|
})
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const dispatchRename = useCallback((id, newName) => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTION_TYPES.RENAME,
|
|
|
|
newName,
|
|
|
|
id,
|
|
|
|
})
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const dispatchDelete = useCallback(id => {
|
|
|
|
dispatch({ type: ACTION_TYPES.DELETE, id })
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const dispatchMove = useCallback((entityId, toFolderId) => {
|
|
|
|
dispatch({ type: ACTION_TYPES.MOVE, entityId, toFolderId })
|
|
|
|
}, [])
|
|
|
|
|
2022-01-10 10:47:01 -05:00
|
|
|
const value = useMemo(() => {
|
|
|
|
return {
|
|
|
|
dispatchCreateDoc,
|
|
|
|
dispatchCreateFile,
|
|
|
|
dispatchCreateFolder,
|
|
|
|
dispatchDelete,
|
|
|
|
dispatchMove,
|
|
|
|
dispatchRename,
|
|
|
|
fileCount,
|
|
|
|
fileTreeData,
|
|
|
|
hasFolders: fileTreeData?.folders.length > 0,
|
2022-03-21 10:47:01 -04:00
|
|
|
selectedEntities,
|
|
|
|
setSelectedEntities,
|
2022-01-10 10:47:01 -05:00
|
|
|
}
|
|
|
|
}, [
|
2021-05-04 07:36:22 -04:00
|
|
|
dispatchCreateDoc,
|
|
|
|
dispatchCreateFile,
|
|
|
|
dispatchCreateFolder,
|
|
|
|
dispatchDelete,
|
|
|
|
dispatchMove,
|
|
|
|
dispatchRename,
|
|
|
|
fileCount,
|
|
|
|
fileTreeData,
|
2022-03-21 10:47:01 -04:00
|
|
|
selectedEntities,
|
|
|
|
setSelectedEntities,
|
2022-01-10 10:47:01 -05:00
|
|
|
])
|
2021-05-04 07:36:22 -04:00
|
|
|
|
2020-11-26 09:22:30 -05:00
|
|
|
return (
|
2022-01-10 10:47:01 -05:00
|
|
|
<FileTreeDataContext.Provider value={value}>
|
2020-11-26 09:22:30 -05:00
|
|
|
{children}
|
2022-01-10 10:47:01 -05:00
|
|
|
</FileTreeDataContext.Provider>
|
2020-11-26 09:22:30 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-01-10 10:47:01 -05:00
|
|
|
FileTreeDataProvider.propTypes = {
|
|
|
|
children: PropTypes.any,
|
2021-03-18 05:52:36 -04:00
|
|
|
}
|