@@ -30,8 +30,7 @@ import useAsModal from '../../../components/useAsModal';
3030import { PreferencesIcon } from '../../../common/icons' ;
3131import Dropdown from '../../../components/Dropdown' ;
3232
33- const isUserOwner = ( { project, user } ) =>
34- project . owner && project . owner . id === user . id ;
33+ import { useEffectWithComparison } from '../../../utils/custom-hooks' ;
3534
3635const withChangeDot = ( title , unsavedChanges = false ) => (
3736 < span >
@@ -67,47 +66,40 @@ const getNatOptions = (username = undefined) =>
6766 ) ;
6867
6968
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 ) ;
8384 }
8485 console . log ( 'will save project in 20 seconds' ) ;
85- this . autosaveInterval = setTimeout ( props . autosaveProject , 20000 ) ;
86+ setAutosaveInterval ( setTimeout ( autosaveProject , 20000 ) ) ;
8687 }
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 ) ;
9091 }
91- } else if ( this . autosaveInterval ) {
92- clearTimeout ( this . autosaveInterval ) ;
93- this . autosaveInterval = null ;
92+ } else if ( autosaveInterval ) {
93+ clearTimeout ( autosaveInterval ) ;
94+ setAutosaveInterval ( null ) ;
9495 }
9596} ;
9697
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-
10698
10799const MobileIDEView = ( props ) => {
108100 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
111103 } = props ;
112104
113105 const [ tmController , setTmController ] = useState ( null ) ; // eslint-disable-line
@@ -141,8 +133,11 @@ const MobileIDEView = (props) => {
141133 } , [ params , project , username ] ) ;
142134
143135
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+ } ) ;
146141
147142 return (
148143 < Screen fullscreen >
@@ -184,6 +179,9 @@ MobileIDEView.propTypes = {
184179 consoleIsExpanded : PropTypes . bool . isRequired ,
185180 } ) . isRequired ,
186181
182+ preferences : PropTypes . shape ( {
183+ } ) . isRequired ,
184+
187185 project : PropTypes . shape ( {
188186 id : PropTypes . string ,
189187 name : PropTypes . string . isRequired ,
@@ -218,6 +216,7 @@ MobileIDEView.propTypes = {
218216 getProject : PropTypes . func . isRequired ,
219217 clearPersistedState : PropTypes . func . isRequired ,
220218 collapseConsole : PropTypes . func . isRequired ,
219+ autosaveProject : PropTypes . func . isRequired ,
221220} ;
222221
223222function mapStateToProps ( state ) {
0 commit comments