1
1
import { type PlayerRef } from '@remotion/player' ;
2
+ import { useBlocker } from 'react-router-dom' ;
2
3
3
4
import { AudioPlayer } from '~/bundles/common/components/audio-player/audio-player.js' ;
4
5
import {
@@ -30,6 +31,10 @@ import {
30
31
} from '~/bundles/common/hooks/hooks.js' ;
31
32
import { IconName } from '~/bundles/common/icons/icons.js' ;
32
33
import { notificationService } from '~/bundles/common/services/services.js' ;
34
+ import {
35
+ UnsavedWarningContent ,
36
+ WarningContent ,
37
+ } from '~/bundles/studio/components/warning-modal/components/components.js' ;
33
38
34
39
import {
35
40
PlayerControls ,
@@ -81,6 +86,8 @@ const Studio: React.FC = () => {
81
86
scriptPlayer,
82
87
isVideoScriptsGenerationReady,
83
88
isVideoScriptsGenerationPending,
89
+ isDraftSaved,
90
+ isSubmitToRender,
84
91
} = useAppSelector ( ( { studio } ) => studio ) ;
85
92
86
93
const playerReference = useRef < PlayerRef > ( null ) ;
@@ -131,7 +138,6 @@ const Studio: React.FC = () => {
131
138
message : NotificationMessage . VIDEO_SUBMITTED ,
132
139
title : NotificationTitle . VIDEO_SUBMITTED ,
133
140
} ) ;
134
- navigate ( AppRoute . ROOT ) ;
135
141
} )
136
142
. catch ( ( ) => {
137
143
notificationService . error ( {
@@ -140,7 +146,7 @@ const Studio: React.FC = () => {
140
146
title : NotificationTitle . VIDEO_SUBMIT_FAILED ,
141
147
} ) ;
142
148
} ) ;
143
- } , [ dispatch , navigate , scenes , scripts ] ) ;
149
+ } , [ dispatch , scenes , scripts ] ) ;
144
150
145
151
const handleSubmit = useCallback ( ( ) => {
146
152
if ( scenesExceedScripts ( scenes , scripts ) ) {
@@ -242,6 +248,26 @@ const Studio: React.FC = () => {
242
248
243
249
const { isPlaying, url } = scriptPlayer ;
244
250
251
+ const blocker = useBlocker (
252
+ ( { currentLocation, nextLocation } ) =>
253
+ ! ( isDraftSaved || isSubmitToRender ) &&
254
+ currentLocation . pathname !== nextLocation . pathname ,
255
+ ) ;
256
+
257
+ const handleCloseUnsavedChangesModal = useCallback ( ( ) => {
258
+ blocker . reset ?.( ) ;
259
+ } , [ blocker ] ) ;
260
+
261
+ const handleSubmitUnsavedChangesModal = useCallback ( ( ) => {
262
+ blocker . proceed ?.( ) ;
263
+ } , [ blocker ] ) ;
264
+
265
+ useEffect ( ( ) => {
266
+ if ( isSubmitToRender ) {
267
+ navigate ( AppRoute . ROOT ) ;
268
+ }
269
+ } , [ navigate , isSubmitToRender ] ) ;
270
+
245
271
return (
246
272
< >
247
273
< Overlay isOpen = { isVideoScriptsGenerationPending } >
@@ -256,11 +282,21 @@ const Studio: React.FC = () => {
256
282
overflowY = { 'hidden' }
257
283
className = { styles [ 'scrollableContainer' ] }
258
284
>
285
+ < WarningModal isOpen = { isModalOpen } onClose = { handleCloseModal } >
286
+ < WarningContent
287
+ onCancel = { handleCloseModal }
288
+ onSubmit = { handleConfirmSubmit }
289
+ />
290
+ </ WarningModal >
259
291
< WarningModal
260
- isOpen = { isModalOpen }
261
- onClose = { handleCloseModal }
262
- onSubmit = { handleConfirmSubmit }
263
- />
292
+ isOpen = { blocker . state === 'blocked' }
293
+ onClose = { handleCloseUnsavedChangesModal }
294
+ >
295
+ < UnsavedWarningContent
296
+ onCancel = { handleCloseUnsavedChangesModal }
297
+ onSubmit = { handleSubmitUnsavedChangesModal }
298
+ />
299
+ </ WarningModal >
264
300
< Header
265
301
center = {
266
302
< Button
0 commit comments