-
Notifications
You must be signed in to change notification settings - Fork 695
Move Swap essential dapp to iframe #3167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+280
−1,410
Merged
Changes from 6 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
0eec2c3
test custom connector
maxaleks 9d9b0a2
move swap to iframe
maxaleks 20b087d
post-merge fixes
maxaleks a41f4d2
rename prop
maxaleks d099606
update schema
maxaleks 0ca810b
undo unnecessary changes
maxaleks ef6db20
improve url comparison
maxaleks 627783a
fix address change
maxaleks a292f69
add connect wallet message listener
maxaleks 44130de
increase timeout
maxaleks a677b74
upgrade version of dappscout-iframe
maxaleks 301bf7d
upgrade version of dappscout-iframe
maxaleks ef875df
change timeout
maxaleks 72984a6
update version of dappscout-iframe, change swap url
maxaleks 6ba8656
Merge branch 'main' into swap-iframe
maxaleks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| import { Center, chakra } from '@chakra-ui/react'; | ||
| import { DappscoutIframeProvider, useDappscoutIframe } from 'dappscout-iframe'; | ||
| import React, { useCallback, useEffect, useState, useMemo } from 'react'; | ||
|
|
||
| import config from 'configs/app'; | ||
| import essentialDappsChainsConfig from 'configs/essential-dapps-chains'; | ||
| import { ContentLoader } from 'toolkit/components/loaders/ContentLoader'; | ||
|
|
||
| import useMarketplaceWallet from '../marketplace/useMarketplaceWallet'; | ||
|
|
||
| const IFRAME_SANDBOX_ATTRIBUTE = 'allow-forms allow-orientation-lock ' + | ||
| 'allow-pointer-lock allow-popups-to-escape-sandbox ' + | ||
| 'allow-same-origin allow-scripts ' + | ||
| 'allow-top-navigation-by-user-activation allow-popups'; | ||
|
|
||
| const IFRAME_ALLOW_ATTRIBUTE = 'clipboard-read; clipboard-write;'; | ||
|
|
||
| type ContentProps = { | ||
| appUrl?: string; | ||
| address?: string; | ||
| message?: Record<string, unknown>; | ||
| isAdaptiveHeight?: boolean; | ||
| className?: string; | ||
| }; | ||
|
|
||
| const Content = chakra(({ appUrl, address, message, isAdaptiveHeight, className }: ContentProps) => { | ||
| const { iframeRef, isReady } = useDappscoutIframe(); | ||
|
|
||
| const [ iframeKey, setIframeKey ] = useState(0); | ||
| const [ isFrameLoading, setIsFrameLoading ] = useState(true); | ||
| const [ iframeHeight, setIframeHeight ] = useState(0); | ||
|
|
||
| useEffect(() => { | ||
| setIframeKey((key) => key + 1); | ||
| }, [ address ]); | ||
|
|
||
| const handleIframeLoad = useCallback(() => { | ||
| setIsFrameLoading(false); | ||
| }, []); | ||
|
|
||
| useEffect(() => { | ||
| if (!isFrameLoading && message && appUrl) { | ||
| iframeRef?.current?.contentWindow?.postMessage(message, appUrl); | ||
| } | ||
| }, [ isFrameLoading, appUrl, iframeRef, message ]); | ||
|
|
||
| useEffect(() => { | ||
| const handleMessage = (event: MessageEvent) => { | ||
| if (event.origin !== appUrl) { | ||
| return; | ||
| } | ||
| if (event.data?.type === 'window-height' && isAdaptiveHeight) { | ||
| setIframeHeight(Number(event.data.height)); | ||
| } | ||
| }; | ||
|
|
||
| window.addEventListener('message', handleMessage); | ||
| return () => window.removeEventListener('message', handleMessage); | ||
| }, [ appUrl, isAdaptiveHeight ]); | ||
|
|
||
| return ( | ||
| <Center | ||
| flexGrow={ 1 } | ||
| minH={ isAdaptiveHeight ? `${ iframeHeight }px` : undefined } | ||
| minW="100%" | ||
| className={ className } | ||
| > | ||
| { (isFrameLoading) && ( | ||
| <ContentLoader/> | ||
| ) } | ||
|
|
||
| { isReady && ( | ||
| <chakra.iframe | ||
| key={ iframeKey } | ||
| allow={ IFRAME_ALLOW_ATTRIBUTE } | ||
| ref={ iframeRef } | ||
| sandbox={ IFRAME_SANDBOX_ATTRIBUTE } | ||
| h="100%" | ||
| w="100%" | ||
| display={ isFrameLoading ? 'none' : 'block' } | ||
| src={ appUrl } | ||
| title="Marketplace dapp" | ||
| onLoad={ handleIframeLoad } | ||
| background="transparent" | ||
| allowTransparency={ true } | ||
| /> | ||
| ) } | ||
| </Center> | ||
| ); | ||
| }); | ||
|
|
||
| type Props = { | ||
| appId: string; | ||
| appUrl?: string; | ||
| message?: Record<string, unknown>; | ||
| isEssentialDapp?: boolean; | ||
| className?: string; | ||
| }; | ||
|
|
||
| const MarketplaceAppIframe = ({ | ||
| appId, appUrl, message, isEssentialDapp, className, | ||
| }: Props) => { | ||
| const { | ||
| address, | ||
| chainId: connectedChainId, | ||
| sendTransaction, | ||
| signMessage, | ||
| signTypedData, | ||
| switchChain, | ||
| } = useMarketplaceWallet(appId, isEssentialDapp); | ||
|
|
||
| const [ chainId, rpcUrl ] = useMemo(() => { | ||
| let data: [ number?, string? ] = [ Number(config.chain.id), config.chain.rpcUrls[0] ]; | ||
|
|
||
| if (isEssentialDapp) { | ||
| const chainConfig = essentialDappsChainsConfig()?.chains.find( | ||
| (chain) => chain.id === String(connectedChainId), | ||
| ); | ||
| if (chainConfig?.app_config?.chain?.rpcUrls[0]) { | ||
| data = [ connectedChainId, chainConfig.app_config.chain.rpcUrls[0] ]; | ||
| } | ||
| } | ||
|
|
||
| return data; | ||
| }, [ isEssentialDapp, connectedChainId ]); | ||
|
|
||
| return ( | ||
| <DappscoutIframeProvider | ||
| address={ address } | ||
| appUrl={ appUrl } | ||
| chainId={ chainId } | ||
| rpcUrl={ rpcUrl } | ||
| sendTransaction={ sendTransaction } | ||
| signMessage={ signMessage } | ||
| signTypedData={ signTypedData } | ||
| switchChain={ switchChain } | ||
| > | ||
| <Content | ||
| appUrl={ appUrl } | ||
| address={ address } | ||
| message={ message } | ||
| isAdaptiveHeight={ isEssentialDapp } | ||
| className={ className } | ||
| /> | ||
| </DappscoutIframeProvider> | ||
| ); | ||
| }; | ||
|
|
||
| export default chakra(MarketplaceAppIframe); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.