1
- import React , { memo , useState , useEffect } from 'react' ;
1
+ import React , { memo , useState , useEffect , useRef } from 'react' ;
2
2
import Modal from 'react-modal' ;
3
3
import {
4
4
FaTimes ,
@@ -10,8 +10,8 @@ import {
10
10
FaMicrophoneAltSlash ,
11
11
} from 'react-icons/fa' ;
12
12
import { MdZoomOutMap } from 'react-icons/md' ;
13
- import { useSlides , setMode } from '../../context/slides' ;
14
- import { PresenterProvider , usePresenter , updateCurrentIndex } from '../../context/presenter' ;
13
+ import { useSlides , setMode , updateCurrentIndex , resetState } from '../../context/slides' ;
14
+ import { PresenterProvider , usePresenter } from '../../context/presenter' ;
15
15
import { Controller as PresentationController } from '../../presentationMode/Controller' ; // common and host
16
16
import { Canvas , emitCanvasEvent } from '../Canvas' ;
17
17
import { Timer } from '../Timer' ;
@@ -33,20 +33,17 @@ const Iframe = ({ slideUrl, slideIndex }) => (
33
33
let webrtc = null ;
34
34
let slideUrl = null ;
35
35
let presentationController = null ;
36
- // let presentationApiId = null;
37
36
let recordedTimeline = [ ] ;
38
37
let recordedStartedTime = 0 ;
39
38
let audioUrl = null ;
40
39
41
40
const Host = ( ) => {
42
41
const {
43
- state : { slides } ,
42
+ state : { slides, currentIndex } ,
44
43
dispatch : dispatchSlides ,
45
44
} = useSlides ( ) ;
46
- const {
47
- state : { currentIndex } ,
48
- dispatch : dispatchPresenter ,
49
- } = usePresenter ( ) ;
45
+ const { dispatch : dispatchPresenter } = usePresenter ( ) ;
46
+ const currentIndexRef = useRef ( currentIndex ) ;
50
47
51
48
if ( ! presentationController ) {
52
49
const { origin, pathname } = new URL ( window . location ) ;
@@ -182,6 +179,8 @@ const Host = () => {
182
179
} ;
183
180
184
181
useEffect ( ( ) => {
182
+ dispatchSlides ( resetState ( ) ) ;
183
+
185
184
( async ( ) => {
186
185
try {
187
186
if ( ! presentationController ) {
@@ -194,17 +193,15 @@ const Host = () => {
194
193
} ) ( ) ;
195
194
196
195
const keyboardListener = ( { key } ) => {
197
- const slideLength = slides . length ;
198
-
199
196
if ( key === 'ArrowLeft' ) {
200
- dispatchPresenter ( updateCurrentIndex ( { index : '-' , slideLength } ) ) ;
197
+ dispatchSlides ( updateCurrentIndex ( '-' ) ) ;
198
+ presentationController . changePage ( '-' ) ;
201
199
} else if ( key === 'ArrowRight' ) {
202
- dispatchPresenter ( updateCurrentIndex ( { index : '+' , slideLength } ) ) ;
203
-
204
- // TODO: fix here
205
- // presentationController.changePage(3);
200
+ dispatchSlides ( updateCurrentIndex ( '+' ) ) ;
201
+ presentationController . changePage ( '+' ) ;
206
202
}
207
203
} ;
204
+
208
205
document . addEventListener ( 'keydown' , keyboardListener , false ) ;
209
206
210
207
return ( ) => {
@@ -218,6 +215,11 @@ const Host = () => {
218
215
} ;
219
216
} , [ ] ) ;
220
217
218
+ useEffect ( ( ) => {
219
+ currentIndexRef . current = currentIndex ;
220
+ presentationController . changePage ( currentIndex ) ;
221
+ } , [ currentIndex ] ) ;
222
+
221
223
// prohibit below actions
222
224
// usedAudio && status === 'start'
223
225
// modal, reset
0 commit comments