1- import { createContext , useCallback , useContext , useEffect , useState } from 'react'
1+ import { useEffect , useState } from 'react'
22import { Box , Button , Layer , Paragraph } from 'grommet'
33import { useTranslation } from 'react-i18next'
44import { Alert , Checkmark , Close , Configure } from 'grommet-icons'
@@ -7,81 +7,32 @@ import { AlertBox } from '../AlertBox'
77import { selectAllowDangerousSetting } from '../SettingsDialog/slice/selectors'
88import { useDispatch , useSelector } from 'react-redux'
99import { settingsActions } from '../SettingsDialog/slice'
10+ import { selectCurrentModal } from './slice/selectors'
11+ import { modalActions } from './slice'
1012
11- interface Modal {
12- title : string
13- description : string
14- handleConfirm : ( ) => void
15-
16- /**
17- * Is this a dangerous operation?
18- *
19- * If marked as such, it will only be possible to execute it if the wallet is configured to run in dangerous mode.
20- *
21- * It also automatically implies a mandatory waiting time of 10 sec, unless specified otherwise.
22- */
23- isDangerous : boolean
24-
25- /**
26- * How long does the user have to wait before he can actually confirm the action?
27- */
28- mustWaitSecs ?: number
29- }
30-
31- interface ModalContainerProps {
32- modal : Modal
33- closeModal : ( ) => void
34- hideModal : ( ) => void
35- }
36-
37- interface ModalContextProps {
38- /**
39- * Show a new modal
40- */
41- launchModal : ( modal : Modal ) => void
42-
43- /**
44- * Close the current modal
45- */
46- closeModal : ( ) => void
47-
48- /**
49- * Hide the current modal (with the intention of showing in again later)
50- */
51- hideModal : ( ) => void
52-
53- /**
54- * Show the previously hidden modal again
55- */
56- showModal : ( ) => void
57- }
58-
59- interface ModalProviderProps {
60- children : React . ReactNode
61- }
62-
63- const ModalContext = createContext < ModalContextProps > ( { } as ModalContextProps )
64-
65- const ModalContainer = ( { modal, closeModal, hideModal } : ModalContainerProps ) => {
13+ const ModalContainer = ( ) => {
6614 const dispatch = useDispatch ( )
6715 const { t } = useTranslation ( )
68- const confirm = useCallback ( ( ) => {
69- modal . handleConfirm ( )
70- closeModal ( )
71- } , [ closeModal , modal ] )
72- const { isDangerous, mustWaitSecs } = modal
16+ const modal = useSelector ( selectCurrentModal )
7317 const allowDangerous = useSelector ( selectAllowDangerousSetting )
18+ const [ secsLeft , setSecsLeft ] = useState ( 0 )
19+ const closeModal = ( ) => dispatch ( modalActions . close ( ) )
20+ const confirm = ( ) => {
21+ modal ?. handleConfirm ( )
22+ dispatch ( modalActions . close ( ) )
23+ }
24+ const { isDangerous, mustWaitSecs } = modal || { }
25+
7426 const forbidden = isDangerous && ! allowDangerous
7527 const waitingTime = forbidden
7628 ? 0 // If the action is forbidden, there is nothing to wait for
7729 : isDangerous
7830 ? mustWaitSecs ?? 10 // For dangerous actions, we require 10 seconds of waiting, unless specified otherwise.
7931 : mustWaitSecs ?? 0 // For normal, non-dangerous operations, just use what was specified
8032
81- const [ secsLeft , setSecsLeft ] = useState ( 0 )
8233 const openSettings = ( ) => {
83- hideModal ( )
84- setTimeout ( ( ) => dispatch ( settingsActions . setOpen ( true ) ) , 100 )
34+ dispatch ( modalActions . stash ( ) )
35+ dispatch ( settingsActions . setOpen ( true ) )
8536 }
8637
8738 useEffect ( ( ) => {
@@ -101,6 +52,7 @@ const ModalContainer = ({ modal, closeModal, hideModal }: ModalContainerProps) =
10152 }
10253 } , [ waitingTime ] )
10354
55+ if ( ! modal ) return null
10456 return (
10557 < Layer modal onEsc = { closeModal } onClickOutside = { closeModal } background = "background-front" >
10658 < Box margin = "medium" >
@@ -148,45 +100,4 @@ const ModalContainer = ({ modal, closeModal, hideModal }: ModalContainerProps) =
148100 )
149101}
150102
151- const ModalProvider = ( props : ModalProviderProps ) => {
152- const [ modal , setModal ] = useState < Modal | null > ( null )
153- const [ hiddenModal , setHiddenModal ] = useState < Modal | null > ( null )
154- const closeModal = useCallback ( ( ) => {
155- setModal ( null )
156- } , [ ] )
157- const hideModal = useCallback ( ( ) => {
158- if ( ! modal ) {
159- throw new Error ( "You can't call hideModal if no model is shown!" )
160- }
161- setHiddenModal ( modal )
162- setModal ( null )
163- } , [ modal ] )
164- const showModal = useCallback ( ( ) => {
165- if ( modal ) {
166- throw new Error ( "You can't call showModal when a modal is already visible!" )
167- }
168- if ( ! hiddenModal ) {
169- return
170- }
171- setModal ( hiddenModal )
172- setHiddenModal ( null )
173- } , [ modal , hiddenModal ] )
174-
175- return (
176- < ModalContext . Provider value = { { closeModal, launchModal : setModal , hideModal, showModal } } >
177- { props . children }
178- { modal && < ModalContainer modal = { modal } closeModal = { closeModal } hideModal = { hideModal } /> }
179- </ ModalContext . Provider >
180- )
181- }
182-
183- const useModal = ( ) => {
184- const context = useContext ( ModalContext )
185- if ( context === undefined ) {
186- throw new Error ( 'useModal must be used within a ModalProvider' )
187- }
188-
189- return context
190- }
191-
192- export { ModalProvider , useModal }
103+ export { ModalContainer }
0 commit comments