1- import PropTypes from 'prop-types' ;
21import React , { useEffect , useRef , useState } from 'react' ;
3- import { useLocation , Prompt } from 'react-router-dom' ;
2+ import { useLocation , Prompt , useParams } from 'react-router-dom' ;
43import { useDispatch , useSelector } from 'react-redux' ;
54import { useTranslation } from 'react-i18next' ;
65import { Helmet } from 'react-helmet' ;
@@ -12,13 +11,12 @@ import PreviewFrame from '../components/PreviewFrame';
1211import Console from '../components/Console' ;
1312import Toast from '../components/Toast' ;
1413import { updateFileContent } from '../actions/files' ;
15- import { setPreviousPath , stopSketch } from '../actions/ide' ;
14+ import { stopSketch } from '../actions/ide' ;
1615import {
1716 autosaveProject ,
1817 clearPersistedState ,
1918 getProject
2019} from '../actions/project' ;
21- import { selectActiveFile } from '../selectors/files' ;
2220import { getIsUserOwner } from '../selectors/users' ;
2321import RootPage from '../../../components/RootPage' ;
2422import Header from '../components/Header' ;
@@ -70,15 +68,16 @@ function WarnIfUnsavedChanges() {
7068
7169export const CmControllerContext = React . createContext ( { } ) ;
7270
73- const IDEView = ( props ) => {
71+ const IDEView = ( ) => {
7472 const ide = useSelector ( ( state ) => state . ide ) ;
75- const selectedFile = useSelector ( selectActiveFile ) ;
7673 const preferences = useSelector ( ( state ) => state . preferences ) ;
7774 const project = useSelector ( ( state ) => state . project ) ;
7875 const isUserOwner = useSelector ( getIsUserOwner ) ;
7976 const dispatch = useDispatch ( ) ;
8077 const { t } = useTranslation ( ) ;
8178
79+ const params = useParams ( ) ;
80+
8281 const [ consoleSize , setConsoleSize ] = useState ( 150 ) ;
8382 const [ sidebarSize , setSidebarSize ] = useState ( 160 ) ;
8483 const [ isOverlayVisible , setIsOverlayVisible ] = useState ( true ) ;
@@ -87,11 +86,6 @@ const IDEView = (props) => {
8786
8887 const autosaveIntervalRef = useRef ( null ) ;
8988
90- const location = useLocation ( ) ;
91-
92- const prevFileNameRef = useRef ( selectedFile . name ) ;
93- const locationRef = useRef ( location . pathname ) ;
94-
9589 const syncFileContent = ( ) => {
9690 const file = cmRef . current . getContent ( ) ;
9791 dispatch ( updateFileContent ( file . id , file . content ) ) ;
@@ -101,13 +95,14 @@ const IDEView = (props) => {
10195 dispatch ( clearPersistedState ( ) ) ;
10296
10397 dispatch ( stopSketch ( ) ) ;
104- if ( props . params . project_id ) {
105- const { project_id : id , username } = props . params ;
106- if ( id !== project . id ) {
107- dispatch ( getProject ( id , username ) ) ;
108- }
98+ } , [ dispatch ] ) ;
99+
100+ useEffect ( ( ) => {
101+ const { project_id : id , username } = params ;
102+ if ( id && project . id !== id ) {
103+ dispatch ( getProject ( id , username ) ) ;
109104 }
110- } , [ ] ) ;
105+ } , [ dispatch , params , project . id ] ) ;
111106
112107 const autosaveAllowed = isUserOwner && project . id && preferences . autosave ;
113108 const shouldAutosave = autosaveAllowed && ide . unsavedChanges ;
@@ -123,16 +118,15 @@ const IDEView = (props) => {
123118 }
124119
125120 if ( shouldAutosave ) {
126- autosaveIntervalRef . current = setTimeout ( handleAutosave , 10000 ) ;
121+ autosaveIntervalRef . current = setTimeout ( handleAutosave , 5000 ) ;
127122 }
128- prevFileNameRef . current = selectedFile . name ;
129123
130124 return ( ) => {
131125 if ( autosaveIntervalRef . current ) {
132126 clearTimeout ( autosaveIntervalRef . current ) ;
133127 }
134128 } ;
135- } , [ shouldAutosave , dispatch ] ) ;
129+ } , [ shouldAutosave , dispatch ] ) ;
136130
137131 return (
138132 < RootPage >
@@ -249,12 +243,4 @@ const IDEView = (props) => {
249243 ) ;
250244} ;
251245
252- IDEView . propTypes = {
253- params : PropTypes . shape ( {
254- project_id : PropTypes . string ,
255- username : PropTypes . string ,
256- reset_password_token : PropTypes . string
257- } ) . isRequired
258- } ;
259-
260246export default IDEView ;
0 commit comments