1- import React , { createRef , useEffect , useRef , useState } from "react" ;
1+ import React , { createRef , useEffect , useRef , useState , useMemo } from "react" ;
22import { DragDropContext } from "@hello-pangea/dnd" ;
33import { useDispatch , useSelector } from "react-redux" ;
44import { TabPanel , Tabs } from "react-tabs" ;
@@ -32,6 +32,11 @@ const EditorInput = () => {
3232 const isMobile = useMediaQuery ( { query : MOBILE_MEDIA_QUERY } ) ;
3333 const readOnly = useSelector ( ( state ) => state . editor . readOnly ) ;
3434
35+ const [ numberOfComponents , setNumberOfComponents ] = useState (
36+ project ?. components ?. length ,
37+ ) ;
38+ const [ fileNames , setFileNames ] = useState ( ) ;
39+
3540 const { t } = useTranslation ( ) ;
3641
3742 const onDragStart = ( input ) => {
@@ -78,9 +83,6 @@ const EditorInput = () => {
7883 dispatch ( closeFile ( fileName ) ) ;
7984 } ;
8085
81- const [ numberOfComponents , setNumberOfComponents ] = useState (
82- project ?. components ?. length ,
83- ) ;
8486 let tabRefs = useRef ( project ?. components ?. map ( createRef ) ) ;
8587
8688 useEffect ( ( ) => {
@@ -95,19 +97,26 @@ const EditorInput = () => {
9597 } , [ project ?. components ] ) ;
9698
9799 useEffect ( ( ) => {
98- if ( ! project ?. components ) return ;
100+ const newFileNames = project . components . map (
101+ ( file ) => `${ file . name } .${ file . extension } ` ,
102+ ) ;
103+ if ( newFileNames . join ( ) !== fileNames ?. join ( ) ) {
104+ setFileNames ( newFileNames ) ;
105+ }
106+ } , [ fileNames , project . components ] ) ;
107+
108+ useEffect ( ( ) => {
109+ if ( ! fileNames ) return ;
99110
100111 focussedFileIndices . forEach ( ( index , i ) => {
101112 const fileName = openFiles [ i ] [ index ] ;
102- const componentIndex = project . components . findIndex (
103- ( file ) => `${ file . name } .${ file . extension } ` === fileName ,
104- ) ;
113+ const componentIndex = fileNames . findIndex ( ( name ) => name === fileName ) ;
105114 const fileRef = tabRefs . current [ componentIndex ] ;
106115 if ( fileRef && fileRef . current ) {
107116 fileRef . current . parentElement . scrollIntoView ( ) ;
108117 }
109118 } ) ;
110- } , [ focussedFileIndices , openFiles , numberOfComponents , project ] ) ;
119+ } , [ focussedFileIndices , openFiles , numberOfComponents , fileNames ] ) ;
111120
112121 if ( ! project || ! project . components ) {
113122 return null ;
0 commit comments