1
- import PropTypes from 'prop-types' ;
2
1
import React , { useEffect , useRef , useState } from 'react' ;
3
- import { useLocation , Prompt } from 'react-router-dom' ;
2
+ import { useLocation , Prompt , useParams } from 'react-router-dom' ;
4
3
import { useDispatch , useSelector } from 'react-redux' ;
5
4
import { useTranslation } from 'react-i18next' ;
6
5
import { Helmet } from 'react-helmet' ;
@@ -12,13 +11,12 @@ import PreviewFrame from '../components/PreviewFrame';
12
11
import Console from '../components/Console' ;
13
12
import Toast from '../components/Toast' ;
14
13
import { updateFileContent } from '../actions/files' ;
15
- import { setPreviousPath , stopSketch } from '../actions/ide' ;
14
+ import { stopSketch } from '../actions/ide' ;
16
15
import {
17
16
autosaveProject ,
18
17
clearPersistedState ,
19
18
getProject
20
19
} from '../actions/project' ;
21
- import { selectActiveFile } from '../selectors/files' ;
22
20
import { getIsUserOwner } from '../selectors/users' ;
23
21
import RootPage from '../../../components/RootPage' ;
24
22
import Header from '../components/Header' ;
@@ -70,15 +68,16 @@ function WarnIfUnsavedChanges() {
70
68
71
69
export const CmControllerContext = React . createContext ( { } ) ;
72
70
73
- const IDEView = ( props ) => {
71
+ const IDEView = ( ) => {
74
72
const ide = useSelector ( ( state ) => state . ide ) ;
75
- const selectedFile = useSelector ( selectActiveFile ) ;
76
73
const preferences = useSelector ( ( state ) => state . preferences ) ;
77
74
const project = useSelector ( ( state ) => state . project ) ;
78
75
const isUserOwner = useSelector ( getIsUserOwner ) ;
79
76
const dispatch = useDispatch ( ) ;
80
77
const { t } = useTranslation ( ) ;
81
78
79
+ const params = useParams ( ) ;
80
+
82
81
const [ consoleSize , setConsoleSize ] = useState ( 150 ) ;
83
82
const [ sidebarSize , setSidebarSize ] = useState ( 160 ) ;
84
83
const [ isOverlayVisible , setIsOverlayVisible ] = useState ( true ) ;
@@ -87,11 +86,6 @@ const IDEView = (props) => {
87
86
88
87
const autosaveIntervalRef = useRef ( null ) ;
89
88
90
- const location = useLocation ( ) ;
91
-
92
- const prevFileNameRef = useRef ( selectedFile . name ) ;
93
- const locationRef = useRef ( location . pathname ) ;
94
-
95
89
const syncFileContent = ( ) => {
96
90
const file = cmRef . current . getContent ( ) ;
97
91
dispatch ( updateFileContent ( file . id , file . content ) ) ;
@@ -101,13 +95,14 @@ const IDEView = (props) => {
101
95
dispatch ( clearPersistedState ( ) ) ;
102
96
103
97
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 ) ) ;
109
104
}
110
- } , [ ] ) ;
105
+ } , [ dispatch , params , project . id ] ) ;
111
106
112
107
const autosaveAllowed = isUserOwner && project . id && preferences . autosave ;
113
108
const shouldAutosave = autosaveAllowed && ide . unsavedChanges ;
@@ -123,16 +118,15 @@ const IDEView = (props) => {
123
118
}
124
119
125
120
if ( shouldAutosave ) {
126
- autosaveIntervalRef . current = setTimeout ( handleAutosave , 10000 ) ;
121
+ autosaveIntervalRef . current = setTimeout ( handleAutosave , 5000 ) ;
127
122
}
128
- prevFileNameRef . current = selectedFile . name ;
129
123
130
124
return ( ) => {
131
125
if ( autosaveIntervalRef . current ) {
132
126
clearTimeout ( autosaveIntervalRef . current ) ;
133
127
}
134
128
} ;
135
- } , [ shouldAutosave , dispatch ] ) ;
129
+ } , [ shouldAutosave , dispatch ] ) ;
136
130
137
131
return (
138
132
< RootPage >
@@ -249,12 +243,4 @@ const IDEView = (props) => {
249
243
) ;
250
244
} ;
251
245
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
-
260
246
export default IDEView ;
0 commit comments