Skip to content
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
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions apps/nextjs-example/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ const isProd = process.env.NODE_ENV === "production";

/** @type {import('next').NextConfig} */
const nextConfig = {
output: "export",
output: "npx serve@latest out",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this for?

Copy link
Collaborator Author

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 support export anymore

reactStrictMode: true,
transpilePackages: ["wallet-adapter-react", "wallet-adapter-plugin"],
assetPrefix: isProd ? "/aptos-wallet-adapter" : "",
basePath: isProd ? "/aptos-wallet-adapter" : "",
assetPrefix: isProd ? "/aptos-wallet-adapter/nextjs-example-testing" : "",
basePath: isProd ? "/aptos-wallet-adapter/nextjs-example-testing" : "",
webpack: (config) => {
config.resolve.fallback = { "@solana/web3.js": false };
return config;
Expand Down
2 changes: 2 additions & 0 deletions apps/nextjs-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"@aptos-labs/wallet-adapter-mui-design": "workspace:*",
"@aptos-labs/wallet-adapter-react": "workspace:*",
"@aptos-labs/wallet-standard": "^0.3.0",
"@aptos-labs/wallet-adapter-aggregator-core": "workspace:*",
"@aptos-labs/cross-chain-react": "workspace:*",
"@radix-ui/react-collapsible": "^1.0.3",
"@radix-ui/react-dialog": "^1.0.5",
"@radix-ui/react-dropdown-menu": "^2.0.6",
Expand Down
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;
}
Loading
Loading