@@ -101,13 +101,10 @@ const IDEView = (props) => {
101
101
let overlay = null ;
102
102
103
103
const autosaveIntervalRef = useRef ( null ) ;
104
- const prevPropsRef = useRef ( {
105
- selectedFileName : selectedFile . name ,
106
- selectedFileContent : selectedFile . content ,
107
- location : props . location ,
108
- sidebarIsExpanded : ide . sidebarSize ,
109
- project_id : props . params . project_id
110
- } ) ;
104
+
105
+ const prevFileNameRef = useRef ( selectedFile . name ) ;
106
+ const prevFileContentRef = useRef ( selectedFile . content ) ;
107
+ const locationRef = useRef ( props . location ) ;
111
108
112
109
const syncFileContent = ( ) => {
113
110
const file = cmRef . current . getContent ( ) ;
@@ -128,61 +125,50 @@ const IDEView = (props) => {
128
125
129
126
// for setting previous location
130
127
useEffect ( ( ) => {
131
- if ( props . location !== prevPropsRef . current . location ) {
132
- dispatch ( setPreviousPath ( prevPropsRef . current . location . pathname ) ) ;
133
- }
134
-
135
- prevPropsRef . current . location = props . location ;
128
+ dispatch ( setPreviousPath ( locationRef . current . pathname ) ) ;
129
+ console . log ( locationRef . current . pathname ) ;
136
130
} , [ props . location ] ) ;
137
131
138
- // for the sidebar size behaviour
132
+ // for the sidebar size behavior
139
133
useEffect ( ( ) => {
140
- if ( ! ide . sidebarIsExpanded ) {
141
- prevPropsRef . current . sidebarSize = sidebarSize ;
142
- setSidebarSize ( 20 ) ;
143
- }
144
134
if ( ide . sidebarIsExpanded ) {
145
- setSidebarSize (
146
- prevPropsRef . current . sidebarSize > 160
147
- ? prevPropsRef . current . sidebarSize
148
- : 160
149
- ) ;
135
+ setSidebarSize ( ( prev ) => ( prev < 160 ? 160 : sidebarSize ) ) ;
150
136
}
151
137
} , [ ide . sidebarIsExpanded ] ) ;
152
138
153
139
// For autosave
154
140
useEffect ( ( ) => {
155
- if (
156
- isUserOwner &&
157
- project . id &&
158
- preferences . autosave &&
159
- ide . unsavedChanges &&
160
- ! ide . justOpenedProject
161
- ) {
141
+ let autosaveTimeout ;
142
+
143
+ const handleAutosave = ( ) => {
162
144
if (
163
- selectedFile . name === prevPropsRef . current . selectedFileName &&
164
- selectedFile . content !== prevPropsRef . current . selectedFileContent
145
+ isUserOwner &&
146
+ project . id &&
147
+ preferences . autosave &&
148
+ ide . unsavedChanges &&
149
+ ! ide . justOpenedProject &&
150
+ selectedFile . name === prevFileNameRef . current &&
151
+ selectedFile . content !== prevFileContentRef . current
165
152
) {
166
- if ( autosaveIntervalRef . current ) {
167
- clearTimeout ( autosaveIntervalRef . current ) ;
168
- }
169
- autosaveIntervalRef . current = setTimeout (
170
- dispatch ( autosaveProject ( ) ) ,
171
- 20000
172
- ) ;
153
+ dispatch ( autosaveProject ( ) ) ;
173
154
}
174
- } else if ( autosaveIntervalRef . current && ! preferences . autosave ) {
155
+ } ;
156
+
157
+ if ( autosaveIntervalRef . current ) {
175
158
clearTimeout ( autosaveIntervalRef . current ) ;
176
- autosaveIntervalRef . current = null ;
177
159
}
178
160
179
- prevPropsRef . current . selectedFileName = selectedFile . name ;
180
- prevPropsRef . current . selectedFileContent = selectedFile . content ;
161
+ if ( preferences . autosave ) {
162
+ autosaveTimeout = setTimeout ( handleAutosave , 20000 ) ;
163
+ }
164
+
165
+ autosaveIntervalRef . current = autosaveTimeout ;
166
+ prevFileNameRef . current = selectedFile . name ;
167
+ prevFileContentRef . current = selectedFile . content ;
181
168
182
169
return ( ) => {
183
- if ( autosaveIntervalRef . current ) {
184
- clearTimeout ( autosaveIntervalRef . current ) ;
185
- autosaveIntervalRef . current = null ;
170
+ if ( autosaveTimeout ) {
171
+ clearTimeout ( autosaveTimeout ) ;
186
172
}
187
173
} ;
188
174
} , [
@@ -192,7 +178,9 @@ const IDEView = (props) => {
192
178
ide . unsavedChanges ,
193
179
ide . justOpenedProject ,
194
180
selectedFile . name ,
195
- selectedFile . content
181
+ selectedFile . content ,
182
+ dispatch ,
183
+ autosaveProject
196
184
] ) ;
197
185
198
186
return (
@@ -212,7 +200,7 @@ const IDEView = (props) => {
212
200
< main className = "editor-preview-container" >
213
201
< SplitPane
214
202
split = "vertical"
215
- size = { sidebarSize }
203
+ size = { ide . sidebarIsExpanded ? sidebarSize : 20 }
216
204
onChange = { ( size ) => {
217
205
setSidebarSize ( size ) ;
218
206
} }
0 commit comments