Răsfoiți Sursa

Merge pull request #3502 from overleaf/ta-file-tree-select-created

[ReactFileTree] Auto-Select Entities Created by User

GitOrigin-RevId: b45d54cbe124c51be819456b8ea17ddd075b1922
Timothée Alby 5 ani în urmă
părinte
comite
8c7bf5fbd2

+ 24 - 9
services/web/app/src/Features/Editor/EditorController.js

@@ -80,7 +80,8 @@ const EditorController = {
           'reciveNewDoc',
           'reciveNewDoc',
           folder_id,
           folder_id,
           doc,
           doc,
-          source
+          source,
+          user_id
         )
         )
         return callback(err, doc)
         return callback(err, doc)
       }
       }
@@ -124,7 +125,8 @@ const EditorController = {
           folder_id,
           folder_id,
           fileRef,
           fileRef,
           source,
           source,
-          linkedFileData
+          linkedFileData,
+          user_id
         )
         )
         return callback(err, fileRef)
         return callback(err, fileRef)
       }
       }
@@ -157,7 +159,8 @@ const EditorController = {
             'reciveNewDoc',
             'reciveNewDoc',
             folder_id,
             folder_id,
             doc,
             doc,
-            source
+            source,
+            user_id
           )
           )
         }
         }
         return callback(err, doc)
         return callback(err, doc)
@@ -205,7 +208,8 @@ const EditorController = {
           folder_id,
           folder_id,
           newFile,
           newFile,
           source,
           source,
-          linkedFileData
+          linkedFileData,
+          user_id
         )
         )
         return callback(null, newFile)
         return callback(null, newFile)
       }
       }
@@ -243,7 +247,8 @@ const EditorController = {
                 'reciveNewDoc',
                 'reciveNewDoc',
                 lastFolder._id,
                 lastFolder._id,
                 doc,
                 doc,
-                source
+                source,
+                user_id
               )
               )
             }
             }
             return callback()
             return callback()
@@ -295,7 +300,8 @@ const EditorController = {
               lastFolder._id,
               lastFolder._id,
               newFile,
               newFile,
               source,
               source,
-              linkedFileData
+              linkedFileData,
+              user_id
             )
             )
             return callback()
             return callback()
           }
           }
@@ -304,7 +310,7 @@ const EditorController = {
     )
     )
   },
   },
 
 
-  addFolder(project_id, folder_id, folderName, source, callback) {
+  addFolder(project_id, folder_id, folderName, source, userId, callback) {
     if (callback == null) {
     if (callback == null) {
       callback = function(error, folder) {}
       callback = function(error, folder) {}
     }
     }
@@ -328,6 +334,7 @@ const EditorController = {
           project_id,
           project_id,
           folder_id,
           folder_id,
           folder,
           folder,
+          userId,
           function(err) {
           function(err) {
             if (err != null) {
             if (err != null) {
               return callback(err)
               return callback(err)
@@ -677,13 +684,20 @@ const EditorController = {
           project_id,
           project_id,
           folder.parentFolder_id,
           folder.parentFolder_id,
           folder,
           folder,
+          null,
           cb
           cb
         ),
         ),
       callback
       callback
     )
     )
   },
   },
 
 
-  _notifyProjectUsersOfNewFolder(project_id, folder_id, folder, callback) {
+  _notifyProjectUsersOfNewFolder(
+    project_id,
+    folder_id,
+    folder,
+    userId,
+    callback
+  ) {
     if (callback == null) {
     if (callback == null) {
       callback = function(error) {}
       callback = function(error) {}
     }
     }
@@ -691,7 +705,8 @@ const EditorController = {
       project_id,
       project_id,
       'reciveNewFolder',
       'reciveNewFolder',
       folder_id,
       folder_id,
-      folder
+      folder,
+      userId
     )
     )
     return callback()
     return callback()
   }
   }

+ 3 - 1
services/web/app/src/Features/Editor/EditorHttpController.js

@@ -170,6 +170,7 @@ async function addFolder(req, res, next) {
   const projectId = req.params.Project_id
   const projectId = req.params.Project_id
   const { name } = req.body
   const { name } = req.body
   const parentFolderId = req.body.parent_folder_id
   const parentFolderId = req.body.parent_folder_id
+  const userId = AuthenticationController.getLoggedInUserId(req)
   if (!_nameIsAcceptableLength(name)) {
   if (!_nameIsAcceptableLength(name)) {
     return res.sendStatus(400)
     return res.sendStatus(400)
   }
   }
@@ -178,7 +179,8 @@ async function addFolder(req, res, next) {
       projectId,
       projectId,
       parentFolderId,
       parentFolderId,
       name,
       name,
-      'editor'
+      'editor',
+      userId
     )
     )
     res.json(doc)
     res.json(doc)
   } catch (err) {
   } catch (err) {

+ 2 - 1
services/web/app/src/Features/Project/ProjectEntityUpdateHandler.js

@@ -1643,7 +1643,8 @@ const ProjectEntityUpdateHandler = {
                     folder._id,
                     folder._id,
                     fileRef,
                     fileRef,
                     'convertDocToFile',
                     'convertDocToFile',
-                    null
+                    null,
+                    userId
                   )
                   )
                   callback(null, fileRef)
                   callback(null, fileRef)
                 }
                 }

+ 2 - 1
services/web/app/src/Features/Uploads/FileSystemImportManager.js

@@ -71,7 +71,8 @@ async function addFolder(userId, projectId, folderId, name, path, replace) {
     projectId,
     projectId,
     folderId,
     folderId,
     name,
     name,
-    'upload'
+    'upload',
+    userId
   )
   )
   await addFolderContents(userId, projectId, newFolder._id, path, replace)
   await addFolderContents(userId, projectId, newFolder._id, path, replace)
   return newFolder
   return newFolder

+ 5 - 0
services/web/frontend/js/features/file-tree/contexts/file-tree-selectable.js

@@ -154,12 +154,17 @@ export function useSelectableEntity(id) {
 export function useFileTreeSelectable() {
 export function useFileTreeSelectable() {
   const { selectedEntityIds, dispatch } = useContext(FileTreeSelectableContext)
   const { selectedEntityIds, dispatch } = useContext(FileTreeSelectableContext)
 
 
+  function select(id) {
+    dispatch({ type: ACTION_TYPES.SELECT, id })
+  }
+
   function unselect(id) {
   function unselect(id) {
     dispatch({ type: ACTION_TYPES.UNSELECT, id })
     dispatch({ type: ACTION_TYPES.UNSELECT, id })
   }
   }
 
 
   return {
   return {
     selectedEntityIds,
     selectedEntityIds,
+    select,
     unselect
     unselect
   }
   }
 }
 }

+ 26 - 8
services/web/frontend/js/features/file-tree/hooks/file-tree-socket-listener.js

@@ -1,4 +1,4 @@
-import { useEffect } from 'react'
+import { useCallback, useEffect } from 'react'
 
 
 import { useFileTreeMutable } from '../contexts/file-tree-mutable'
 import { useFileTreeMutable } from '../contexts/file-tree-mutable'
 import { useFileTreeSelectable } from '../contexts/file-tree-selectable'
 import { useFileTreeSelectable } from '../contexts/file-tree-selectable'
@@ -12,9 +12,18 @@ export function useFileTreeSocketListener() {
     dispatchCreateDoc,
     dispatchCreateDoc,
     dispatchCreateFile
     dispatchCreateFile
   } = useFileTreeMutable()
   } = useFileTreeMutable()
-  const { unselect } = useFileTreeSelectable()
+  const { select, unselect } = useFileTreeSelectable()
   const socket = window._ide && window._ide.socket
   const socket = window._ide && window._ide.socket
 
 
+  const selectEntityIfCreatedByUser = useCallback(
+    (entityId, userId) => {
+      if (window.user && window.user.id && window.user.id === userId) {
+        select(entityId)
+      }
+    },
+    [select]
+  )
+
   useEffect(() => {
   useEffect(() => {
     function handleDispatchRename(entityId, name) {
     function handleDispatchRename(entityId, name) {
       dispatchRename(entityId, name)
       dispatchRename(entityId, name)
@@ -48,34 +57,43 @@ export function useFileTreeSocketListener() {
   }, [socket, dispatchMove])
   }, [socket, dispatchMove])
 
 
   useEffect(() => {
   useEffect(() => {
-    function handleDispatchCreateFolder(parentFolderId, folder) {
+    function handleDispatchCreateFolder(parentFolderId, folder, userId) {
       dispatchCreateFolder(parentFolderId, folder)
       dispatchCreateFolder(parentFolderId, folder)
+      selectEntityIfCreatedByUser(folder._id, userId)
     }
     }
     if (socket) socket.on('reciveNewFolder', handleDispatchCreateFolder)
     if (socket) socket.on('reciveNewFolder', handleDispatchCreateFolder)
     return () => {
     return () => {
       if (socket)
       if (socket)
         socket.removeListener('reciveNewFolder', handleDispatchCreateFolder)
         socket.removeListener('reciveNewFolder', handleDispatchCreateFolder)
     }
     }
-  }, [socket, dispatchCreateFolder])
+  }, [socket, dispatchCreateFolder, selectEntityIfCreatedByUser])
 
 
   useEffect(() => {
   useEffect(() => {
-    function handleDispatchCreateDoc(parentFolderId, doc) {
+    function handleDispatchCreateDoc(parentFolderId, doc, _source, userId) {
       dispatchCreateDoc(parentFolderId, doc)
       dispatchCreateDoc(parentFolderId, doc)
+      selectEntityIfCreatedByUser(doc._id, userId)
     }
     }
     if (socket) socket.on('reciveNewDoc', handleDispatchCreateDoc)
     if (socket) socket.on('reciveNewDoc', handleDispatchCreateDoc)
     return () => {
     return () => {
       if (socket) socket.removeListener('reciveNewDoc', handleDispatchCreateDoc)
       if (socket) socket.removeListener('reciveNewDoc', handleDispatchCreateDoc)
     }
     }
-  }, [socket, dispatchCreateDoc])
+  }, [socket, dispatchCreateDoc, selectEntityIfCreatedByUser])
 
 
   useEffect(() => {
   useEffect(() => {
-    function handleDispatchCreateFile(parentFolderId, file) {
+    function handleDispatchCreateFile(
+      parentFolderId,
+      file,
+      _source,
+      _linkedFileData,
+      userId
+    ) {
       dispatchCreateFile(parentFolderId, file)
       dispatchCreateFile(parentFolderId, file)
+      selectEntityIfCreatedByUser(file._id, userId)
     }
     }
     if (socket) socket.on('reciveNewFile', handleDispatchCreateFile)
     if (socket) socket.on('reciveNewFile', handleDispatchCreateFile)
     return () => {
     return () => {
       if (socket)
       if (socket)
         socket.removeListener('reciveNewFile', handleDispatchCreateFile)
         socket.removeListener('reciveNewFile', handleDispatchCreateFile)
     }
     }
-  }, [socket, dispatchCreateFile])
+  }, [socket, dispatchCreateFile, selectEntityIfCreatedByUser])
 }
 }

+ 18 - 11
services/web/test/unit/src/Editor/EditorControllerTests.js

@@ -22,11 +22,13 @@ const modulePath = require('path').join(
 )
 )
 const MockClient = require('../helpers/MockClient')
 const MockClient = require('../helpers/MockClient')
 const assert = require('assert')
 const assert = require('assert')
+const { ObjectId } = require('mongodb')
 
 
 describe('EditorController', function() {
 describe('EditorController', function() {
   beforeEach(function() {
   beforeEach(function() {
     this.project_id = 'test-project-id'
     this.project_id = 'test-project-id'
     this.source = 'dropbox'
     this.source = 'dropbox'
+    this.user_id = new ObjectId()
 
 
     this.doc = { _id: (this.doc_id = 'test-doc-id') }
     this.doc = { _id: (this.doc_id = 'test-doc-id') }
     this.docName = 'doc.tex'
     this.docName = 'doc.tex'
@@ -112,7 +114,8 @@ describe('EditorController', function() {
           'reciveNewDoc',
           'reciveNewDoc',
           this.folder_id,
           this.folder_id,
           this.doc,
           this.doc,
-          this.source
+          this.source,
+          this.user_id
         )
         )
         .should.equal(true)
         .should.equal(true)
     })
     })
@@ -160,7 +163,8 @@ describe('EditorController', function() {
           this.folder_id,
           this.folder_id,
           this.file,
           this.file,
           this.source,
           this.source,
-          this.linkedFileData
+          this.linkedFileData,
+          this.user_id
         )
         )
         .should.equal(true)
         .should.equal(true)
     })
     })
@@ -225,7 +229,8 @@ describe('EditorController', function() {
             'reciveNewDoc',
             'reciveNewDoc',
             this.folder_id,
             this.folder_id,
             this.doc,
             this.doc,
-            this.source
+            this.source,
+            this.user_id
           )
           )
           .should.equal(true)
           .should.equal(true)
       })
       })
@@ -291,7 +296,8 @@ describe('EditorController', function() {
             this.folder_id,
             this.folder_id,
             this.file,
             this.file,
             this.source,
             this.source,
-            this.linkedFileData
+            this.linkedFileData,
+            this.user_id
           )
           )
           .should.equal(true)
           .should.equal(true)
       })
       })
@@ -343,7 +349,8 @@ describe('EditorController', function() {
             'reciveNewDoc',
             'reciveNewDoc',
             this.folder_id,
             this.folder_id,
             this.doc,
             this.doc,
-            this.source
+            this.source,
+            this.user_id
           )
           )
           .should.equal(true)
           .should.equal(true)
       })
       })
@@ -442,7 +449,8 @@ describe('EditorController', function() {
             this.folder_id,
             this.folder_id,
             this.file,
             this.file,
             this.source,
             this.source,
-            this.linkedFileData
+            this.linkedFileData,
+            this.user_id
           )
           )
           .should.equal(true)
           .should.equal(true)
       })
       })
@@ -502,6 +510,7 @@ describe('EditorController', function() {
         this.folder_id,
         this.folder_id,
         this.folderName,
         this.folderName,
         this.source,
         this.source,
+        this.user_id,
         this.callback
         this.callback
       )
       )
     })
     })
@@ -513,11 +522,9 @@ describe('EditorController', function() {
     })
     })
 
 
     it('should notifyProjectUsersOfNewFolder', function() {
     it('should notifyProjectUsersOfNewFolder', function() {
-      return this.EditorController._notifyProjectUsersOfNewFolder.calledWith(
-        this.project_id,
-        this.folder_id,
-        this.folder
-      )
+      return this.EditorController._notifyProjectUsersOfNewFolder
+        .calledWith(this.project_id, this.folder_id, this.folder, this.user_id)
+        .should.equal(true)
     })
     })
 
 
     it('should return the folder in the callback', function() {
     it('should return the folder in the callback', function() {