file-tree-actionable.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. import React, { createContext, useReducer, useContext } from 'react'
  2. import PropTypes from 'prop-types'
  3. import { mapSeries } from '../../../infrastructure/promise'
  4. import {
  5. syncRename,
  6. syncDelete,
  7. syncMove,
  8. syncCreateEntity
  9. } from '../util/sync-mutation'
  10. import { findInTreeOrThrow } from '../util/find-in-tree'
  11. import { FileTreeMainContext } from './file-tree-main'
  12. import { useFileTreeMutable } from './file-tree-mutable'
  13. import { useFileTreeSelectable } from './file-tree-selectable'
  14. const FileTreeActionableContext = createContext()
  15. const ACTION_TYPES = {
  16. START_RENAME: 'START_RENAME',
  17. START_DELETE: 'START_DELETE',
  18. DELETING: 'DELETING',
  19. START_CREATE_FOLDER: 'START_CREATE_FOLDER',
  20. CREATING_FOLDER: 'CREATING_FOLDER',
  21. CANCEL: 'CANCEL',
  22. CLEAR: 'CLEAR',
  23. ERROR: 'ERROR'
  24. }
  25. const defaultState = {
  26. isDeleting: false,
  27. isRenaming: false,
  28. isCreatingFolder: false,
  29. inFlight: false,
  30. actionedEntities: null,
  31. error: null
  32. }
  33. function fileTreeActionableReadOnlyReducer(state) {
  34. return state
  35. }
  36. function fileTreeActionableReducer(state, action) {
  37. switch (action.type) {
  38. case ACTION_TYPES.START_RENAME:
  39. return { ...defaultState, isRenaming: true }
  40. case ACTION_TYPES.START_DELETE:
  41. return {
  42. ...defaultState,
  43. isDeleting: true,
  44. actionedEntities: action.actionedEntities
  45. }
  46. case ACTION_TYPES.START_CREATE_FOLDER:
  47. return { ...defaultState, isCreatingFolder: true }
  48. case ACTION_TYPES.CREATING_FOLDER:
  49. return { ...defaultState, isCreatingFolder: true, inFlight: true }
  50. case ACTION_TYPES.DELETING:
  51. // keep `actionedEntities` so the entities list remains displayed in the
  52. // delete modal
  53. return {
  54. ...defaultState,
  55. isDeleting: true,
  56. inFlight: true,
  57. actionedEntities: state.actionedEntities
  58. }
  59. case ACTION_TYPES.CLEAR:
  60. return { ...defaultState }
  61. case ACTION_TYPES.CANCEL:
  62. if (state.inFlight) return state
  63. return { ...defaultState }
  64. case ACTION_TYPES.ERROR:
  65. return { ...state, inFlight: false, error: action.error }
  66. default:
  67. throw new Error(`Unknown user action type: ${action.type}`)
  68. }
  69. }
  70. export function FileTreeActionableProvider({ hasWritePermissions, children }) {
  71. const [state, dispatch] = useReducer(
  72. hasWritePermissions
  73. ? fileTreeActionableReducer
  74. : fileTreeActionableReadOnlyReducer,
  75. defaultState
  76. )
  77. return (
  78. <FileTreeActionableContext.Provider value={{ ...state, dispatch }}>
  79. {children}
  80. </FileTreeActionableContext.Provider>
  81. )
  82. }
  83. FileTreeActionableProvider.propTypes = {
  84. hasWritePermissions: PropTypes.bool.isRequired,
  85. children: PropTypes.oneOfType([
  86. PropTypes.arrayOf(PropTypes.node),
  87. PropTypes.node
  88. ]).isRequired
  89. }
  90. export function useFileTreeActionable() {
  91. const {
  92. isDeleting,
  93. isRenaming,
  94. isCreatingFolder,
  95. inFlight,
  96. error,
  97. actionedEntities,
  98. dispatch
  99. } = useContext(FileTreeActionableContext)
  100. const { projectId } = useContext(FileTreeMainContext)
  101. const { fileTreeData, dispatchRename, dispatchMove } = useFileTreeMutable()
  102. const { selectedEntityIds } = useFileTreeSelectable()
  103. function startRenaming() {
  104. dispatch({ type: ACTION_TYPES.START_RENAME })
  105. }
  106. // update the entity with the new name immediately in the tree, but revert to
  107. // the old name if the sync fails
  108. function finishRenaming(newName) {
  109. dispatch({ type: ACTION_TYPES.CLEAR })
  110. const selectedEntityId = Array.from(selectedEntityIds)[0]
  111. const found = findInTreeOrThrow(fileTreeData, selectedEntityId)
  112. const oldName = found.entity.name
  113. dispatchRename(selectedEntityId, newName)
  114. return syncRename(projectId, found.type, found.entity._id, newName).catch(
  115. error => {
  116. dispatch({ type: ACTION_TYPES.ERROR, error })
  117. dispatchRename(selectedEntityId, oldName)
  118. }
  119. )
  120. }
  121. // init deletion flow (this will open the delete modal).
  122. // A copy of the selected entities is set as `actionedEntities` so it is kept
  123. // unchanged as the entities are deleted and the selection is updated
  124. function startDeleting() {
  125. const actionedEntities = Array.from(selectedEntityIds).map(
  126. entityId => findInTreeOrThrow(fileTreeData, entityId).entity
  127. )
  128. dispatch({ type: ACTION_TYPES.START_DELETE, actionedEntities })
  129. }
  130. // deletes entities in serie. Tree will be updated via the socket event
  131. function finishDeleting() {
  132. dispatch({ type: ACTION_TYPES.DELETING })
  133. return mapSeries(Array.from(selectedEntityIds), id => {
  134. const found = findInTreeOrThrow(fileTreeData, id)
  135. return syncDelete(projectId, found.type, found.entity._id).catch(
  136. error => {
  137. // throw unless 404
  138. if (error.message !== '404') {
  139. throw error
  140. }
  141. }
  142. )
  143. })
  144. .then(() => {
  145. dispatch({ type: ACTION_TYPES.CLEAR })
  146. })
  147. .catch(error => {
  148. // set an error and allow user to retry
  149. dispatch({ type: ACTION_TYPES.ERROR, error })
  150. })
  151. }
  152. // moves entities. Tree is updated immediately and data are sync'd after.
  153. function finishMoving(toFolderId, draggedEntityIds) {
  154. draggedEntityIds.forEach(selectedEntityId => {
  155. dispatchMove(selectedEntityId, toFolderId)
  156. })
  157. return mapSeries(Array.from(draggedEntityIds), id => {
  158. const found = findInTreeOrThrow(fileTreeData, id)
  159. return syncMove(projectId, found.type, found.entity._id, toFolderId)
  160. })
  161. }
  162. function startCreatingFolder() {
  163. dispatch({ type: ACTION_TYPES.START_CREATE_FOLDER })
  164. }
  165. function finishCreatingEntity(entity) {
  166. const selectedEntityId = Array.from(selectedEntityIds)[0]
  167. const found = findInTreeOrThrow(fileTreeData, selectedEntityId)
  168. const parentFolderId =
  169. found.type === 'folder' ? found.entity._id : found.parentFolderId
  170. return syncCreateEntity(projectId, parentFolderId, entity)
  171. }
  172. function finishCreatingFolder(name) {
  173. dispatch({ type: ACTION_TYPES.CREATING_FOLDER })
  174. return finishCreatingEntity({ endpoint: 'folder', name })
  175. .then(() => {
  176. dispatch({ type: ACTION_TYPES.CLEAR })
  177. })
  178. .catch(error => {
  179. dispatch({ type: ACTION_TYPES.ERROR, error })
  180. })
  181. }
  182. // bypass React file tree entirely; requesting the Angular new doc or file
  183. // modal instead
  184. function startCreatingDocOrFile() {
  185. const selectedEntityId = Array.from(selectedEntityIds)[0]
  186. const found = findInTreeOrThrow(fileTreeData, selectedEntityId)
  187. const parentFolderId =
  188. found.type === 'folder' ? found.entity._id : found.parentFolderId
  189. window.dispatchEvent(
  190. new CustomEvent('FileTreeReactBridge.openNewDocModal', {
  191. detail: {
  192. mode: 'doc',
  193. parentFolderId
  194. }
  195. })
  196. )
  197. }
  198. function startUploadingDocOrFile() {
  199. const selectedEntityId = Array.from(selectedEntityIds)[0]
  200. const found = findInTreeOrThrow(fileTreeData, selectedEntityId)
  201. const parentFolderId =
  202. found.type === 'folder' ? found.entity._id : found.parentFolderId
  203. window.dispatchEvent(
  204. new CustomEvent('FileTreeReactBridge.openNewDocModal', {
  205. detail: {
  206. mode: 'upload',
  207. parentFolderId
  208. }
  209. })
  210. )
  211. }
  212. function finishCreatingDocOrFile(entity) {
  213. return finishCreatingEntity(entity)
  214. .then(() => {
  215. // dispatch FileTreeReactBridge event to update the Angular modal
  216. window.dispatchEvent(
  217. new CustomEvent('FileTreeReactBridge.openNewFileModal', {
  218. detail: {
  219. done: true
  220. }
  221. })
  222. )
  223. })
  224. .catch(error => {
  225. // dispatch FileTreeReactBridge event to update the Angular modal with
  226. // an error
  227. window.dispatchEvent(
  228. new CustomEvent('FileTreeReactBridge.openNewFileModal', {
  229. detail: {
  230. error: true,
  231. data: error.message
  232. }
  233. })
  234. )
  235. })
  236. }
  237. function finishCreatingDoc(entity) {
  238. entity.endpoint = 'doc'
  239. return finishCreatingDocOrFile(entity)
  240. }
  241. function finishCreatingLinkedFile(entity) {
  242. entity.endpoint = 'linked_file'
  243. return finishCreatingDocOrFile(entity)
  244. }
  245. function cancel() {
  246. dispatch({ type: ACTION_TYPES.CANCEL })
  247. }
  248. return {
  249. canDelete: selectedEntityIds.size > 0,
  250. canRename: selectedEntityIds.size === 1,
  251. canCreate: selectedEntityIds.size === 1,
  252. isDeleting,
  253. isRenaming,
  254. isCreatingFolder,
  255. inFlight,
  256. actionedEntities,
  257. error,
  258. startRenaming,
  259. finishRenaming,
  260. startDeleting,
  261. finishDeleting,
  262. finishMoving,
  263. startCreatingFolder,
  264. finishCreatingFolder,
  265. startCreatingDocOrFile,
  266. startUploadingDocOrFile,
  267. finishCreatingDoc,
  268. finishCreatingLinkedFile,
  269. cancel
  270. }
  271. }