1+ import React , { MutableRefObject , useEffect , useRef , useState } from "react" ;
12import { StyledHeaderButton } from "Styles/headers/HeaderMenu.styles" ;
23import { Entry , useError } from "jderobot-ide-interface" ;
34import {
@@ -9,10 +10,8 @@ import {
910} from "Helpers/utils" ;
1011import { CommsManager , states } from "jderobot-commsmanager" ;
1112import JSZip from "jszip" ;
12- import { useEffect , useRef , useState } from "react" ;
1313import commons from "../../common.zip" ;
1414import { useAcademyTheme } from "Contexts/AcademyThemeContext" ;
15- import React from "react" ;
1615
1716import PlayArrowRoundedIcon from "@mui/icons-material/PlayArrowRounded" ;
1817import PauseRoundedIcon from "@mui/icons-material/PauseRounded" ;
@@ -22,19 +21,17 @@ import { getFileList, getHelperFileList } from "Api";
2221const PlayPauseButton = ( {
2322 project,
2423 supportedLanguages,
25- connectManager,
24+ userRef,
25+ entrypointRef,
2626} : {
2727 project : string ;
2828 supportedLanguages : string [ ] ;
29- connectManager : (
30- desiredState ?: string ,
31- callback ?: ( ) => void
32- ) => Promise < void > ;
29+ userRef : MutableRefObject < string | undefined > ;
30+ entrypointRef : MutableRefObject < Entry | undefined > ;
3331} ) => {
3432 const theme = useAcademyTheme ( ) ;
35- const { warning, error, info , close } = useError ( ) ;
33+ const { warning, error } = useError ( ) ;
3634 const filesRef = useRef < Entry [ ] > ( [ ] ) ;
37- const entrypointRef = useRef < Entry | undefined > ( undefined ) ;
3835 const runningFilesRef = useRef < JSZip > ( JSZip ) ;
3936 const runningEntrypointRef = useRef < Entry | undefined > ( undefined ) ;
4037 const runningContentRef = useRef < string | undefined > ( undefined ) ;
@@ -55,24 +52,15 @@ const PlayPauseButton = ({
5552 }
5653 } ;
5754
58- const updateCurrent = ( e : unknown ) => {
59- const T = CustomEvent < { detail : { file ?: Entry } } > ;
60- if ( e instanceof T ) {
61- entrypointRef . current = e . detail . file ;
62- }
63- } ;
64-
6555 useEffect ( ( ) => {
6656 subscribe ( "autoSaveCompleted" , ( ) => {
6757 updateCode ( true ) ;
6858 } ) ;
6959 subscribe ( "CommsManagerStateChange" , updateState ) ;
70- subscribe ( "currentFile" , updateCurrent ) ;
7160
7261 return ( ) => {
7362 unsubscribe ( "autoSaveCompleted" , ( ) => { } ) ;
7463 unsubscribe ( "CommsManagerStateChange" , ( ) => { } ) ;
75- unsubscribe ( "currentFile" , ( ) => { } ) ;
7664 } ;
7765 } , [ ] ) ;
7866
@@ -134,22 +122,12 @@ const PlayPauseButton = ({
134122
135123 // App handling
136124
137- const onAppStateChange = async ( save ?: boolean ) => {
125+ const onAppStateChange = async ( save ?: boolean ) : Promise < void > => {
138126 const manager = CommsManager . getInstance ( ) ;
139127 const state = manager . getState ( ) ;
140128
141129 setLoading ( true ) ;
142130
143- if ( state === states . IDLE ) {
144- info ( "Connecting with the Robotics Backend ..." ) ;
145- connectManager ( states . TOOLS_READY , ( ) => {
146- setLoading ( false ) ;
147- close ( ) ;
148- onAppStateChange ( ) ;
149- } ) ;
150- return ;
151- }
152-
153131 if ( state === states . WORLD_READY || state === states . CONNECTED ) {
154132 console . error ( "Simulation is not ready!" ) ;
155133 warning (
@@ -197,16 +175,20 @@ const PlayPauseButton = ({
197175 }
198176
199177 if ( ! isCodeUpdatedRef . current ) {
200- return setTimeout ( onAppStateChange , 100 , true ) ;
178+ setTimeout ( onAppStateChange , 100 , true ) ;
179+ return ;
201180 }
202181
203- const files = await getFileList ( project ) ;
182+ const files = await getFileList ( project , userRef . current ) ;
204183 filesRef . current = JSON . parse ( files ) ;
205- const userZip = await loadFiles ( entrypointRef . current , JSON . parse ( files ) ) ;
184+ const userZip = await loadFiles (
185+ entrypointRef . current ,
186+ filesRef . current ,
187+ userRef . current
188+ ) ;
206189
207190 if ( state === states . PAUSED ) {
208191 const sameZips = await compareZips ( userZip , runningFilesRef . current ) ;
209-
210192 if ( sameZips && runningEntrypointRef . current === entrypointRef . current ) {
211193 try {
212194 await manager . resume ( ) ;
@@ -283,10 +265,10 @@ const PlayPauseButton = ({
283265 }
284266 }
285267
286- async function loadFiles ( entrypoint : Entry , files : Entry [ ] ) {
268+ async function loadFiles ( entrypoint : Entry , files : Entry [ ] , user ?: string ) {
287269 const zip = new JSZip ( ) ;
288270
289- await zipCodeFiles ( zip , files , project ) ;
271+ await zipCodeFiles ( zip , files , project , user ) ;
290272
291273 zip . files [ entrypoint . path ] . _data . then (
292274 ( value : string ) => ( runningContentRef . current = value )
0 commit comments