-
Notifications
You must be signed in to change notification settings - Fork 118
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
[DRAFT] Multi chain adapter #500
Draft
0xmaayan
wants to merge
7
commits into
main
Choose a base branch
from
multi_chain
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+7,276
−209
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cc42a00
introduce multi chain swap components
0xmaayan 01d5583
Fix CSS
blakezimmerman 21bbbfe
Change postcss to CommonJS
blakezimmerman db610e5
only one wallet connection
0xmaayan 33e61e5
remove unneeded folders
0xmaayan c823561
Introduce cross chain adapter
0xmaayan 62764a4
add signInWith support
0xmaayan 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 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
221 changes: 221 additions & 0 deletions
221
apps/nextjs-example/src/app/swap/components/walletSelector/WalletItem.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,221 @@ | ||
"use client"; | ||
|
||
import { AdapterWallet } from "@aptos-labs/wallet-adapter-aggregator-core"; | ||
import { useCrossChainWallet } from "@aptos-labs/cross-chain-react"; | ||
import { Slot } from "@radix-ui/react-slot"; | ||
import { | ||
cloneElement, | ||
createContext, | ||
forwardRef, | ||
isValidElement, | ||
ReactNode, | ||
useCallback, | ||
useContext, | ||
} from "react"; | ||
import { useToast } from "@/components/ui/use-toast"; | ||
|
||
export interface WalletItemProps extends HeadlessComponentProps { | ||
/** The wallet option to be displayed. */ | ||
wallet: AdapterWallet; | ||
/** A callback to be invoked when the wallet is connected. */ | ||
onConnect?: () => void; | ||
/** A callback to be invoked when the wallet is signed in. */ | ||
onSignInWith?: () => void; | ||
} | ||
|
||
function useWalletItemContext(displayName: string) { | ||
const context = useContext(WalletItemContext); | ||
|
||
if (!context) { | ||
throw new Error(`\`${displayName}\` must be used within \`WalletItem\``); | ||
} | ||
|
||
return context; | ||
} | ||
|
||
const WalletItemContext = createContext<{ | ||
wallet: AdapterWallet; | ||
connectWallet: () => void; | ||
signInWithWallet: () => void; | ||
} | null>(null); | ||
|
||
const Root = forwardRef<HTMLDivElement, WalletItemProps>( | ||
({ wallet, onConnect, className, asChild, children, onSignInWith }, ref) => { | ||
const { connect, signInWith } = useCrossChainWallet(); | ||
const { toast } = useToast(); | ||
const connectWallet = useCallback(async () => { | ||
await connect(wallet); | ||
onConnect?.(); | ||
}, [wallet, onConnect]); | ||
|
||
const signInWithWallet = useCallback(async () => { | ||
await signInWith(wallet); | ||
onSignInWith?.(); | ||
}, [wallet, onSignInWith]); | ||
|
||
// const isWalletReady = | ||
// wallet.readyState === WalletReadyState.Installed || | ||
// wallet.readyState === WalletReadyState.Loadable; | ||
|
||
// const mobileSupport = | ||
// "deeplinkProvider" in wallet && wallet.deeplinkProvider; | ||
|
||
// if (!isWalletReady && isRedirectable() && !mobileSupport) return null; | ||
|
||
const Component = asChild ? Slot : "div"; | ||
|
||
return ( | ||
<WalletItemContext.Provider | ||
value={{ wallet, connectWallet, signInWithWallet }} | ||
> | ||
<Component ref={ref} className={className}> | ||
{children} | ||
</Component> | ||
</WalletItemContext.Provider> | ||
); | ||
} | ||
); | ||
Root.displayName = "WalletItem"; | ||
|
||
const Icon = createHeadlessComponent( | ||
"WalletItem.Icon", | ||
"img", | ||
(displayName) => { | ||
const context = useWalletItemContext(displayName); | ||
|
||
return { | ||
src: context.wallet.icon, | ||
alt: `${context.wallet.name} icon`, | ||
}; | ||
} | ||
); | ||
|
||
const Name = createHeadlessComponent( | ||
"WalletItem.Name", | ||
"div", | ||
(displayName) => { | ||
const context = useWalletItemContext(displayName); | ||
|
||
return { | ||
children: context.wallet.name, | ||
}; | ||
} | ||
); | ||
|
||
const ConnectButton = createHeadlessComponent( | ||
"WalletItem.ConnectButton", | ||
"button", | ||
(displayName) => { | ||
const context = useWalletItemContext(displayName); | ||
|
||
return { | ||
onClick: context.connectWallet, | ||
children: "Connect", | ||
}; | ||
} | ||
); | ||
|
||
const SignInWithButton = createHeadlessComponent( | ||
"WalletItem.SignInWithButton", | ||
"button", | ||
(displayName) => { | ||
const context = useWalletItemContext(displayName); | ||
|
||
return { | ||
onClick: context.signInWithWallet, | ||
children: "Sign In", | ||
}; | ||
} | ||
); | ||
|
||
const InstallLink = createHeadlessComponent( | ||
"WalletItem.InstallLink", | ||
"a", | ||
(displayName) => { | ||
const context = useWalletItemContext(displayName); | ||
|
||
return { | ||
href: context.wallet.url, | ||
target: "_blank", | ||
rel: "noopener noreferrer", | ||
children: "Install", | ||
}; | ||
} | ||
); | ||
|
||
/** A headless component for rendering a wallet option's name, icon, and either connect button or install link. */ | ||
export const WalletItem = Object.assign(Root, { | ||
Icon, | ||
Name, | ||
ConnectButton, | ||
InstallLink, | ||
SignInWithButton, | ||
}); | ||
|
||
export interface HeadlessComponentProps { | ||
/** A class name for styling the element. */ | ||
className?: string; | ||
/** | ||
* Whether to render as the child element instead of the default element provided. | ||
* All props will be merged into the child element. | ||
*/ | ||
asChild?: boolean; | ||
children?: ReactNode; | ||
} | ||
|
||
/** | ||
* Gets an HTML element type from its tag name | ||
* @example | ||
* HTMLElementFromTag<"img"> // resolved type: HTMLImageElement | ||
*/ | ||
type HTMLElementFromTag<T extends keyof JSX.IntrinsicElements> = | ||
JSX.IntrinsicElements[T] extends React.ClassAttributes<infer Element> | ||
? Element | ||
: HTMLElement; | ||
|
||
export function createHeadlessComponent< | ||
TElement extends keyof JSX.IntrinsicElements, | ||
>( | ||
displayName: string, | ||
elementType: TElement, | ||
props?: | ||
| JSX.IntrinsicElements[TElement] | ||
| ((displayName: string) => JSX.IntrinsicElements[TElement]) | ||
) { | ||
const component = forwardRef< | ||
HTMLElementFromTag<TElement>, | ||
HeadlessComponentProps | ||
>(({ className, asChild, children }, ref) => { | ||
const Component = asChild ? Slot : elementType; | ||
|
||
const { children: defaultChildren, ...resolvedProps } = | ||
typeof props === "function" ? props(displayName) : (props ?? {}); | ||
const resolvedChildren = | ||
/** | ||
* Use props' default children if no children are set in the component element's children and when asChild is true. | ||
*/ | ||
asChild && isValidElement(children) && !children.props.children | ||
? cloneElement(children, {}, defaultChildren) | ||
: (children ?? defaultChildren); | ||
|
||
return ( | ||
/** | ||
* Due to the complexity of the types at play, TypeScript reports the | ||
* following error for our JSX below: | ||
* | ||
* `Expression produces a union type that is too complex to represent.` | ||
* | ||
* We can safely ignore this error and retain accurate return types for | ||
* consumers of this function. The only drawback is that type-checking is | ||
* ignored for the JSX block below. | ||
*/ | ||
// @ts-expect-error | ||
<Component ref={ref} className={className} {...resolvedProps}> | ||
{resolvedChildren} | ||
</Component> | ||
); | ||
}); | ||
component.displayName = displayName; | ||
|
||
return component; | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when starting nextjs build locally
next start
, it does not supportexport
anymore