-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: render mobile pairing correctly
- Loading branch information
Showing
4 changed files
with
38 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,55 @@ | ||
import { useState } from 'react' | ||
import { Dialog, DialogContent, DialogTitle, IconButton } from '@mui/material' | ||
import { createRoot } from 'react-dom/client' | ||
import CloseIcon from '@mui/icons-material/Close' | ||
import PairingQRCode from '@/components/common/PairingDetails/PairingQRCode' | ||
import PairingDescription from '@/components/common/PairingDetails/PairingDescription' | ||
import { StoreHydrator } from '@/store' | ||
import { AppProviders } from '@/pages/_app' | ||
import { PAIRING_MODULE_LABEL } from '@/services/pairing/module' | ||
import css from './styles.module.css' | ||
import PairingDeprecationWarning from '@/components/common/PairingDetails/PairingDeprecationWarning' | ||
import ExternalStore from '@/services/ExternalStore' | ||
|
||
const WRAPPER_ID = 'safe-mobile-qr-modal' | ||
const QR_CODE_SIZE = 200 | ||
const { useStore: useCloseCallback, setStore: setCloseCallback } = new ExternalStore<() => void>() | ||
|
||
const renderWrapper = () => { | ||
if (typeof document === 'undefined') { | ||
return | ||
} | ||
|
||
const wrapper = document.createElement('div') | ||
wrapper.setAttribute('id', WRAPPER_ID) | ||
|
||
document.body.appendChild(wrapper) | ||
|
||
return wrapper | ||
} | ||
|
||
const open = (uri: string, cb: () => void) => { | ||
const wrapper = renderWrapper() | ||
|
||
if (!wrapper) { | ||
return | ||
} | ||
|
||
const root = createRoot(wrapper) | ||
|
||
root.render(<Modal uri={uri} cb={cb} />) | ||
export const open = (cb: () => void) => { | ||
setCloseCallback(() => cb) | ||
} | ||
|
||
const close = () => { | ||
if (typeof document === 'undefined') { | ||
return | ||
} | ||
|
||
const wrapper = document.getElementById(WRAPPER_ID) | ||
if (wrapper) { | ||
document.body.removeChild(wrapper) | ||
document.body.style.overflow = '' | ||
} | ||
export const close = () => { | ||
setCloseCallback(undefined) | ||
} | ||
|
||
const Modal = ({ uri, cb }: { uri: string; cb: () => void }) => { | ||
const [open, setOpen] = useState(true) | ||
const QRModal = () => { | ||
const closeCallback = useCloseCallback() | ||
const open = !!closeCallback | ||
|
||
const handleClose = () => { | ||
cb() | ||
setOpen(false) | ||
closeCallback?.() | ||
setCloseCallback(undefined) | ||
close() | ||
} | ||
|
||
if (!open) return null | ||
|
||
return ( | ||
<StoreHydrator> | ||
<AppProviders> | ||
<Dialog open={open} onClose={handleClose} disablePortal className={css.modal}> | ||
<DialogTitle sx={{ m: 0, p: 2, display: 'flex', justifyContent: 'space-between' }}> | ||
{PAIRING_MODULE_LABEL} | ||
<IconButton | ||
onClick={handleClose} | ||
size="small" | ||
sx={{ | ||
color: 'border.main', | ||
}} | ||
> | ||
<CloseIcon /> | ||
</IconButton> | ||
</DialogTitle> | ||
<DialogContent sx={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 1 }}> | ||
<PairingDeprecationWarning /> | ||
<PairingQRCode size={QR_CODE_SIZE} /> | ||
<br /> | ||
<PairingDescription /> | ||
</DialogContent> | ||
</Dialog> | ||
</AppProviders> | ||
</StoreHydrator> | ||
<Dialog open={open} onClose={handleClose} disablePortal sx={{ zIndex: 100000 }}> | ||
<DialogTitle sx={{ m: 0, p: 2, display: 'flex', justifyContent: 'space-between' }}> | ||
{PAIRING_MODULE_LABEL} | ||
<IconButton | ||
onClick={handleClose} | ||
size="small" | ||
sx={{ | ||
color: 'border.main', | ||
}} | ||
> | ||
<CloseIcon /> | ||
</IconButton> | ||
</DialogTitle> | ||
<DialogContent sx={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 1 }}> | ||
<PairingDeprecationWarning /> | ||
<PairingQRCode size={200} /> | ||
<br /> | ||
<PairingDescription /> | ||
</DialogContent> | ||
</Dialog> | ||
) | ||
} | ||
|
||
const QRModal = { | ||
open, | ||
close, | ||
} | ||
|
||
export default QRModal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.