-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb4390c
commit 7c03478
Showing
6 changed files
with
192 additions
and
25 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
6 changes: 3 additions & 3 deletions
6
packages/beacon-ui/src/ui/alert/components/alert-content/index.tsx
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,7 +1,7 @@ | ||
import InfoAlert from '../info-alert' | ||
import PairingAlert from '../pairing-alert' | ||
|
||
const AlertContent = ({ title, body, data }: any) => { | ||
return <InfoAlert title={title} body={body} data={data} /> | ||
const AlertContent = () => { | ||
return <PairingAlert /> | ||
} | ||
|
||
export default AlertContent |
14 changes: 14 additions & 0 deletions
14
packages/beacon-ui/src/ui/alert/components/pairing-alert/index.tsx
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import useWallets from '../../hooks/useWallets' | ||
|
||
const PairingAlert = () => { | ||
const wallets = useWallets() | ||
return ( | ||
<h1> | ||
{wallets.map((wallet, i) => ( | ||
<div key={`${i}`}>{wallet.name}</div> | ||
))} | ||
</h1> | ||
) | ||
} | ||
|
||
export default PairingAlert |
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 |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { useCallback, useEffect, useState } from 'react' | ||
import { PostMessageTransport } from '@airgap/beacon-transport-postmessage' | ||
import { | ||
arrangeTopWallets, | ||
MergedWallet, | ||
mergeWallets, | ||
parseWallets, | ||
Wallet | ||
} from 'src/utils/wallets' | ||
import { Extension, NetworkType } from '@airgap/beacon-types' | ||
import { desktopList, extensionList, iOSList, webList } from '../wallet-lists' | ||
import { windowRef } from '@airgap/beacon-core' | ||
|
||
const useWallets = (networkType?: NetworkType, featuredWallets?: string[]) => { | ||
const [wallets, setWallets] = useState<MergedWallet[]>([]) | ||
|
||
useEffect(() => { | ||
PostMessageTransport.getAvailableExtensions().then((extensions) => | ||
setWallets(createWallets(extensions)) | ||
) | ||
const handler = async (event: any): Promise<void> => { | ||
if (event.data === 'extensionsUpdated') { | ||
setWallets(createWallets(await PostMessageTransport.getAvailableExtensions())) | ||
} | ||
} | ||
windowRef.addEventListener('message', handler) | ||
return () => { | ||
windowRef.removeEventListener('message', handler) | ||
} | ||
}, []) | ||
|
||
const createWallets = useCallback((availableExtensions: Extension[]) => { | ||
const wallets: Wallet[] = [ | ||
...desktopList | ||
// This is used for a special case where desktop wallets have inApp browsers. | ||
// In this case, the wallet will act like an extension. This means we have to remove | ||
// the desktop app from the list to make the user experience better. One example of this | ||
// is Infinity Wallet. | ||
.filter( | ||
(wallet) => !availableExtensions.some((extWallet) => wallet.name === extWallet.name) | ||
) | ||
.map((wallet) => { | ||
return { | ||
id: wallet.key, | ||
key: wallet.key, | ||
name: wallet.shortName, | ||
image: wallet.logo, | ||
description: 'Desktop App', | ||
supportedInteractionStandards: wallet.supportedInteractionStandards, | ||
type: 'desktop', | ||
link: wallet.downloadLink, | ||
deepLink: wallet.deepLink | ||
} | ||
}), | ||
...extensionList.map((wallet) => { | ||
return { | ||
id: wallet.id, | ||
key: wallet.key, | ||
name: wallet.shortName, | ||
image: wallet.logo, | ||
description: 'Browser Extension', | ||
supportedInteractionStandards: wallet.supportedInteractionStandards, | ||
type: 'extension', | ||
link: wallet.link | ||
} | ||
}), | ||
...iOSList.map((wallet) => { | ||
return { | ||
id: wallet.key, | ||
key: wallet.key, | ||
name: wallet.shortName, | ||
image: wallet.logo, | ||
description: 'Mobile App', | ||
supportedInteractionStandards: wallet.supportedInteractionStandards, | ||
type: 'ios', | ||
link: wallet.universalLink, | ||
deepLink: wallet.deepLink | ||
} | ||
}), | ||
...webList.map((wallet) => { | ||
const link = networkType ?? NetworkType.MAINNET | ||
return { | ||
id: wallet.key, | ||
key: wallet.key, | ||
name: wallet.shortName, | ||
image: wallet.logo, | ||
description: 'Web App', | ||
supportedInteractionStandards: wallet.supportedInteractionStandards, | ||
type: 'web', | ||
link: link ?? wallet.links.mainnet | ||
} | ||
}), | ||
...availableExtensions | ||
.filter((newExt) => !extensionList.some((ext) => ext.id === newExt.id)) | ||
.map((wallet) => { | ||
return { | ||
id: wallet.id, | ||
key: wallet.id, | ||
name: wallet.shortName ?? wallet.name ?? '', | ||
image: wallet.iconUrl ?? '', | ||
description: 'Browser Extension', | ||
type: 'extension', | ||
link: (wallet as any).link ?? '' | ||
} | ||
}) | ||
] | ||
|
||
// Parse wallet names | ||
const parsedWallets = parseWallets(wallets) | ||
|
||
// Merge wallets by name | ||
const mergedWallets = mergeWallets(parsedWallets) | ||
|
||
// Default selection of featured wallets | ||
const defaultWalletList = ['kukai', 'temple', 'plenty', 'umami'] | ||
|
||
// Sort wallets by top 4 | ||
const arrangedWallets = arrangeTopWallets(mergedWallets, featuredWallets ?? defaultWalletList) | ||
|
||
return arrangedWallets | ||
}, []) | ||
|
||
return wallets | ||
} | ||
export default useWallets |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export class Subject<T> { | ||
private subscribers: ((value: T) => void)[] = [] | ||
|
||
subscribe(callback: (value: T) => void): void { | ||
this.subscribers.push(callback) | ||
} | ||
|
||
next(value: T): void { | ||
this.subscribers.forEach((callback) => setTimeout(() => callback(value), 50)) | ||
} | ||
} |