@@ -30,8 +30,7 @@ import useAsModal from '../../../components/useAsModal';
30
30
import { PreferencesIcon } from '../../../common/icons' ;
31
31
import Dropdown from '../../../components/Dropdown' ;
32
32
33
- const isUserOwner = ( { project, user } ) =>
34
- project . owner && project . owner . id === user . id ;
33
+ import { useEffectWithComparison } from '../../../utils/custom-hooks' ;
35
34
36
35
const withChangeDot = ( title , unsavedChanges = false ) => (
37
36
< span >
@@ -67,47 +66,40 @@ const getNatOptions = (username = undefined) =>
67
66
) ;
68
67
69
68
70
- const asd = ( props , prevProps ) => {
71
- if ( isUserOwner ( this . props ) && this . props . project . id ) {
72
- if (
73
- props . preferences . autosave &&
74
- props . ide . unsavedChanges &&
75
- ! props . ide . justOpenedProject
76
- ) {
77
- if (
78
- props . selectedFile . name === prevProps . selectedFile . name &&
79
- props . selectedFile . content !== prevProps . selectedFile . content
80
- ) {
81
- if ( this . autosaveInterval ) {
82
- clearTimeout ( this . autosaveInterval ) ;
69
+ const isUserOwner = ( { project, user } ) =>
70
+ project && project . owner && project . owner . id === user . id ;
71
+
72
+ const autosave = ( autosaveInterval , setAutosaveInterval ) => ( props , prevProps ) => {
73
+ const {
74
+ autosaveProject, preferences, ide, selectedFile : file , project
75
+ } = props ;
76
+
77
+ const { selectedFile : oldFile } = prevProps ;
78
+
79
+ if ( isUserOwner ( props ) && project . id ) {
80
+ if ( preferences . autosave && ide . unsavedChanges && ! ide . justOpenedProject ) {
81
+ if ( file . name === oldFile . name && file . content !== oldFile . content ) {
82
+ if ( autosaveInterval ) {
83
+ clearTimeout ( autosaveInterval ) ;
83
84
}
84
85
console . log ( 'will save project in 20 seconds' ) ;
85
- this . autosaveInterval = setTimeout ( props . autosaveProject , 20000 ) ;
86
+ setAutosaveInterval ( setTimeout ( autosaveProject , 20000 ) ) ;
86
87
}
87
- } else if ( this . autosaveInterval && ! props . preferences . autosave ) {
88
- clearTimeout ( this . autosaveInterval ) ;
89
- this . autosaveInterval = null ;
88
+ } else if ( autosaveInterval && ! preferences . autosave ) {
89
+ clearTimeout ( autosaveInterval ) ;
90
+ setAutosaveInterval ( null ) ;
90
91
}
91
- } else if ( this . autosaveInterval ) {
92
- clearTimeout ( this . autosaveInterval ) ;
93
- this . autosaveInterval = null ;
92
+ } else if ( autosaveInterval ) {
93
+ clearTimeout ( autosaveInterval ) ;
94
+ setAutosaveInterval ( null ) ;
94
95
}
95
96
} ;
96
97
97
- const useEffectWithComparison = ( fn , props ) => {
98
- const [ prevProps , update ] = useState ( { } ) ;
99
-
100
- return useEffect ( ( ) => {
101
- fn ( props , prevProps ) ;
102
- update ( props ) ;
103
- } , Object . values ( props ) ) ;
104
- } ;
105
-
106
98
107
99
const MobileIDEView = ( props ) => {
108
100
const {
109
- ide, project, selectedFile, user, params, unsavedChanges, collapseConsole,
110
- stopSketch, startSketch, getProject, clearPersistedState,
101
+ ide, preferences , project, selectedFile, user, params, unsavedChanges, collapseConsole,
102
+ stopSketch, startSketch, getProject, clearPersistedState, autosaveProject
111
103
} = props ;
112
104
113
105
const [ tmController , setTmController ] = useState ( null ) ; // eslint-disable-line
@@ -141,8 +133,11 @@ const MobileIDEView = (props) => {
141
133
} , [ params , project , username ] ) ;
142
134
143
135
144
- // useEffectWithComparison(() => alert('haha'), { consoleIsExpanded });
145
-
136
+ // TODO: This behavior could move to <Editor />
137
+ const [ autosaveInterval , setAutosaveInterval ] = useState ( null ) ;
138
+ useEffectWithComparison ( autosave ( autosaveInterval , setAutosaveInterval ) , {
139
+ autosaveProject, preferences, ide, selectedFile
140
+ } ) ;
146
141
147
142
return (
148
143
< Screen fullscreen >
@@ -184,6 +179,9 @@ MobileIDEView.propTypes = {
184
179
consoleIsExpanded : PropTypes . bool . isRequired ,
185
180
} ) . isRequired ,
186
181
182
+ preferences : PropTypes . shape ( {
183
+ } ) . isRequired ,
184
+
187
185
project : PropTypes . shape ( {
188
186
id : PropTypes . string ,
189
187
name : PropTypes . string . isRequired ,
@@ -218,6 +216,7 @@ MobileIDEView.propTypes = {
218
216
getProject : PropTypes . func . isRequired ,
219
217
clearPersistedState : PropTypes . func . isRequired ,
220
218
collapseConsole : PropTypes . func . isRequired ,
219
+ autosaveProject : PropTypes . func . isRequired ,
221
220
} ;
222
221
223
222
function mapStateToProps ( state ) {
0 commit comments