@@ -17,8 +17,9 @@ import { usePalleteContext } from "@utils/pallete";
1717import { useVariablesContext } from "@utils/variables" ;
1818import { loadProject , saveProject } from "@utils/engineTools" ;
1919import { packProject } from "@utils/packerTools" ;
20- import { backupProject , getRestoredProject } from "@utils/desktopTools" ;
20+ import { backupProject , getRestoredProject , isDesktop } from "@utils/desktopTools" ;
2121import { saveData } from "@utils/persistentTools" ;
22+ import { useUnsavedChangesContext } from "@utils/unsavedChanges" ;
2223import {
2324 getBreadcrumb ,
2425 getWindowTools ,
@@ -78,6 +79,11 @@ const createAppDataContext = () => {
7879 requestPrompt,
7980 } = usePromptContext ( ) ! ;
8081
82+ const {
83+ unsavedChanges,
84+ setUnsavedChanges,
85+ } = useUnsavedChangesContext ( ) ! ;
86+
8187 const [
8288 windowTools ,
8389 setWindowTools ,
@@ -97,6 +103,8 @@ const createAppDataContext = () => {
97103 projectId ,
98104 setProjectId ,
99105 ] = useState < string | null > ( null ) ;
106+
107+ const [ loaded , setLoaded ] = useState < boolean > ( false ) ;
100108
101109 useEffect ( ( ) => {
102110 setWindowTools (
@@ -157,24 +165,31 @@ const createAppDataContext = () => {
157165
158166 useEffect ( ( ) => {
159167 ( async ( ) => {
168+ if ( loaded ) return ;
169+
160170 const existingId = await loadExistingId ( ) ;
161- const restoredProject = await getRestoredProject ( ) ;
162- saveData ( `p:${ existingId } ` , restoredProject ) ;
163171 setProjectId ( existingId ) ;
172+ if ( isDesktop ( ) ) {
173+ const restoredProject = await getRestoredProject ( ) ;
174+ saveData ( `p:${ existingId } ` , restoredProject ) ;
175+ }
164176
165- loadProject (
177+ await loadProject (
166178 existingId ,
167179 overrideNodeSystem ,
168180 overrideVariables ,
169181 openNode ,
170182 removeNode ,
171183 ) ;
184+
185+ setLoaded ( true ) ;
172186 } ) ( ) ;
173187 } , [
174188 openNode ,
175189 overrideNodeSystem ,
176190 overrideVariables ,
177191 removeNode ,
192+ loaded ,
178193 ] ) ;
179194
180195 useEffect ( ( ) => {
@@ -198,6 +213,21 @@ const createAppDataContext = () => {
198213 ] ) ;
199214
200215 useEffect ( ( ) => {
216+ if ( ! loaded ) return ;
217+ if ( ! projectId ) return ;
218+ if ( ! unsavedChanges ) return ;
219+
220+ const project = packProject (
221+ projectId ! ,
222+ nodeSystem ,
223+ entries ,
224+ {
225+ states : statesList ,
226+ customComponents,
227+ }
228+ ) ;
229+
230+ backupProject ( JSON . stringify ( project , null , 2 ) ) ;
201231 saveProject (
202232 projectId ! ,
203233 nodeSystem ,
@@ -207,33 +237,12 @@ const createAppDataContext = () => {
207237 customComponents,
208238 } ,
209239 ) ;
210- } , [
211- projectId ,
212- entries ,
213- nodeSystem ,
214- statesList ,
215- customComponents ,
216- ] ) ;
217-
218- useEffect ( ( ) => {
219- const backupInterval = setInterval ( ( ) => {
220- const project = packProject (
221- projectId ! ,
222- nodeSystem ,
223- entries ,
224- {
225- states : statesList ,
226- customComponents,
227- }
228- ) ;
229-
230- backupProject ( JSON . stringify ( project , null , 2 ) ) ;
231- } , 5000 ) ;
232240
233- return ( ) => {
234- clearInterval ( backupInterval ) ;
235- }
241+ setUnsavedChanges ( false ) ;
236242 } , [
243+ loaded ,
244+ unsavedChanges ,
245+ setUnsavedChanges ,
237246 customComponents ,
238247 entries ,
239248 nodeSystem ,
0 commit comments