Merge pull request #3601 from overleaf/ta-file-tree-open-doc-null

[ReactFileTree] Fix Possible Entities Not Found Error

GitOrigin-RevId: 15fd8c0ad115cc30427114330e2a6e960fd2e9a2
This commit is contained in:
Miguel Serrano 2021-01-27 10:54:29 +01:00 committed by Copybot
parent ecd42944ad
commit 4955231abf
2 changed files with 11 additions and 2 deletions

View file

@ -130,11 +130,14 @@ export function FileTreeSelectableProvider({
useEffect(() => {
// listen for `editor.openDoc` and selected that doc
function handleOpenDoc(ev) {
dispatch({ type: ACTION_TYPES.SELECT, id: ev.detail })
const found = findInTree(fileTreeData, ev.detail)
if (!found) return
dispatch({ type: ACTION_TYPES.SELECT, id: found.entity._id })
}
window.addEventListener('editor.openDoc', handleOpenDoc)
return () => window.removeEventListener('editor.openDoc', handleOpenDoc)
}, [])
}, [fileTreeData])
return (
<FileTreeSelectableContext.Provider

View file

@ -183,6 +183,12 @@ describe('<FileTreeRoot/>', function() {
screen.getByRole('treeitem', { name: 'main.tex', selected: true })
// entities not found should be ignored
window.dispatchEvent(
new CustomEvent('editor.openDoc', { detail: 'not-an-id' })
)
screen.getByRole('treeitem', { name: 'main.tex', selected: true })
window.dispatchEvent(
new CustomEvent('editor.openDoc', { detail: '789ghi' })
)