Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
0c6b462
improve react & sdk usability
jnsdls Sep 21, 2022
d051b7d
Merge remote-tracking branch 'origin/main' into jd/use-internals
jnsdls Sep 21, 2022
92ce99e
fix the tests based on changed export names etc
jnsdls Sep 21, 2022
719bfbe
fix various small things
jnsdls Sep 21, 2022
8cad050
add additional changeset
jnsdls Sep 21, 2022
3def7fc
Merge branch 'main' into jd/use-internals
jnsdls Sep 21, 2022
6295138
remove turbo from react
jnsdls Sep 21, 2022
4d2ed04
Merge branch 'main' into jd/use-internals
jnsdls Sep 21, 2022
048bb40
fix connectwallet getting stuck in connecting state
jnsdls Sep 21, 2022
ef5e1cb
Merge branch 'jd/use-internals' of github.com:thirdweb-dev/js into jd…
jnsdls Sep 21, 2022
39980c0
properly type useContractMetadata response
jnsdls Sep 21, 2022
40147ad
bring back account and fix some more types
jnsdls Sep 21, 2022
bc2414e
fix __DEV__ shenanigans
jnsdls Sep 21, 2022
aa12bfc
Merge branch 'main' into jd/use-internals
jnsdls Sep 21, 2022
3550309
re-organize a bunch of files
jnsdls Sep 21, 2022
5e66a2a
Merge branch 'jd/use-internals' of github.com:thirdweb-dev/js into jd…
jnsdls Sep 21, 2022
535e754
Merge branch 'main' into jd/use-internals
jnsdls Sep 21, 2022
978b077
skip e2e tests for connect wallet and web3 button for now. they work
jnsdls Sep 22, 2022
f2452da
Merge branch 'jd/use-internals' of github.com:thirdweb-dev/js into jd…
jnsdls Sep 22, 2022
7bedfae
fix various things
jnsdls Sep 22, 2022
623bfb9
fix solana test cases
jnsdls Sep 22, 2022
2decc34
Merge branch 'main' into jd/use-internals
jnsdls Sep 22, 2022
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
5 changes: 5 additions & 0 deletions .changeset/rotten-lions-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@thirdweb-dev/react": patch
---

use internals wherever possible to allow wider usecases with `<ThirdwebSDKProvider>` & add invariants to catch improper use of functionality that requires the full `<ThirdwebProvider` earlier
5 changes: 5 additions & 0 deletions .changeset/sharp-dodos-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@thirdweb-dev/react": patch
---

fix case where `<ConnectWallet />` button would get stuck when user cancels connection
5 changes: 5 additions & 0 deletions .changeset/slimy-dolls-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@thirdweb-dev/sdk": patch
---

Switch naming of exported prebuilt contract types back from `<name>Impl` to `<name>` for easier use
5 changes: 5 additions & 0 deletions .changeset/tiny-trees-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@thirdweb-dev/react": patch
---

allow passing `className` to `<ConnectWallet />` and `<Web3Button />` for possible style overrides
4 changes: 2 additions & 2 deletions packages/auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"eslint-plugin-tsdoc": "^0.2.16",
"ethers": "^5.7.0",
"express": "^4.18.1",
"next": "12.2.0",
"next": "^12.2.0",
"next-auth": "^4.10.3",
"prettier": "^2.7.1",
"typescript": "^4.7.4"
Expand All @@ -69,7 +69,7 @@
"@thirdweb-dev/sdk": "*",
"ethers": ">=5.5.1",
"express": "^4.18.1",
"next": "12.2.0",
"next": "^12.2.0",
"next-auth": "^4.10.3"
},
"peerDependenciesMeta": {
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"prettier": "^2.7.1",
"ts-jest": "^28.0.8",
"ts-node": "^10.7.0",
"tsc": "^2.0.4",
"tsup": "^6.2.3",
"typescript": "^4.7.4"
},
Expand Down Expand Up @@ -81,4 +80,4 @@
"sourcemap": true,
"clean": true
}
}
}
4 changes: 2 additions & 2 deletions packages/eslint-config-thirdweb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"main": "index.js",
"license": "Apache-2.0",
"dependencies": {
"@typescript-eslint/eslint-plugin": "^5.36.2",
"@typescript-eslint/parser": "^5.36.2",
"@typescript-eslint/eslint-plugin": "^5.33.0",
"@typescript-eslint/parser": "^5.33.0",
"eslint-config-next": "^12.0.8",
"eslint-config-prettier": "^8.3.0",
"eslint-config-turbo": "latest",
Expand Down
4 changes: 2 additions & 2 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
"@microsoft/api-documenter": "^7.19.4",
"@microsoft/api-extractor": "^7.29.2",
"@microsoft/tsdoc": "^0.14.1",
"@playwright/experimental-ct-react": "^1.25.1",
"@playwright/test": "^1.25.1",
"@playwright/experimental-ct-react": "1.25.1",
"@playwright/test": "1.25.1",
"@preconstruct/cli": "^2.2.1",
"@thirdweb-dev/sdk": "*",
"@types/color": "^3.0.3",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ConnectWallet } from ".";
import { ThirdwebProvider } from "../..";
import { ThirdwebProvider } from "../../providers/full";
import { test, expect } from "@playwright/experimental-ct-react";

test.use({ viewport: { width: 500, height: 500 } });
Expand All @@ -10,5 +10,6 @@ test("should render the connect wallet button", async ({ mount }) => {
<ConnectWallet />
</ThirdwebProvider>,
);

await expect(component).toContainText("Connect Wallet");
});
61 changes: 32 additions & 29 deletions packages/react/src/components/ConnectWallet/index.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { useThirdwebAuthConfig } from "../../contexts/thirdweb-auth";
import { useBalance } from "../../hooks/async/wallet";
import { LoginConfig, useAuth } from "../../hooks/auth";
import { useMetamask } from "../../hooks/connectors/useMetamask";
import { useAddress } from "../../hooks/useAddress";
import { useChainId } from "../../hooks/useChainId";
import { useConnect } from "../../hooks/useConnect";
import { useDisconnect } from "../../hooks/useDisconnect";
import { useNetwork } from "../../hooks/useNetwork";
import { useConnect } from "../../hooks/wagmi-required/useConnect";
import { useDisconnect } from "../../hooks/wagmi-required/useDisconnect";
import { useNetwork } from "../../hooks/wagmi-required/useNetwork";
import { useAddress, useBalance, useChainId } from "../../hooks/wallet";
import { Portal } from "../../lib/portal";
import { shortenIfAddress } from "../../utils/addresses";
import { useClipboard } from "../hooks/useCopyClipboard";
import { useIsMounted } from "../hooks/useIsMounted";
import { Box } from "../shared/Box";
import { Button } from "../shared/Button";
import { Icon } from "../shared/Icon";
Expand All @@ -30,8 +27,8 @@ import { FiXCircle } from "@react-icons/all-files/fi/FiXCircle";
import { ChainId, LoginOptions, SUPPORTED_CHAIN_ID } from "@thirdweb-dev/sdk";
import * as menu from "@zag-js/menu";
import { normalizeProps, useMachine } from "@zag-js/react";
import React, { useId, useMemo } from "react";
import { Connector } from "wagmi";
import React, { useId } from "react";
import type { Connector } from "wagmi";

const SUPPORTED_CONNECTORS = [
"injected",
Expand Down Expand Up @@ -65,6 +62,7 @@ interface ConnectWalletProps extends ThemeProviderProps {
loginConfig?: LoginConfig;
loginOptional?: boolean;
};
className?: string;
}

let connecting = false;
Expand Down Expand Up @@ -124,15 +122,12 @@ const chainIdToCurrencyMap: Record<
*/
export const ConnectWallet: React.FC<ConnectWalletProps> = ({
auth,
className,
...themeProps
}) => {
const id = useId();
const isMounted = useIsMounted();
const address = useAddress();

const mountedAddress = useMemo(() => {
return isMounted ? address : null;
}, [address, isMounted]);
const walletAddress = useAddress();

const [state, send] = useMachine(
menu.machine({
Expand Down Expand Up @@ -165,24 +160,26 @@ export const ConnectWallet: React.FC<ConnectWalletProps> = ({

const balanceQuery = useBalance();

const { onCopy, hasCopied } = useClipboard(mountedAddress || "");
const { onCopy, hasCopied } = useClipboard(walletAddress || "");

const authConfig = useThirdwebAuthConfig();
const { user, isLoading, login, logout } = useAuth(auth?.loginConfig);

const requiresSignIn = auth?.loginOptional
? false
: !!authConfig?.authUrl && !!mountedAddress && !user?.address;
: !!authConfig?.authUrl && !!walletAddress && !user?.address;

return (
<ThemeProvider {...themeProps}>
<div
style={{
position: "relative",
width: "100%",
}}
>
<Button
style={{ height: "50px", minWidth: "200px" }}
className={className}
style={{ height: "50px", minWidth: "200px", width: "100%" }}
onClick={async (e) => {
if (requiresSignIn) {
e.preventDefault();
Expand All @@ -204,7 +201,7 @@ export const ConnectWallet: React.FC<ConnectWalletProps> = ({
) : (
<FiLock />
)
) : mountedAddress && chainId && chainId in chainIdToCurrencyMap ? (
) : walletAddress && chainId && chainId in chainIdToCurrencyMap ? (
<Icon
boxSize="1.5em"
name={chainIdToCurrencyMap[chainId as SUPPORTED_CHAIN_ID]}
Expand All @@ -225,7 +222,7 @@ export const ConnectWallet: React.FC<ConnectWalletProps> = ({
)
}
>
{mountedAddress ? (
{walletAddress ? (
requiresSignIn ? (
<span style={{ whiteSpace: "nowrap" }}>Sign in</span>
) : (
Expand All @@ -249,7 +246,7 @@ export const ConnectWallet: React.FC<ConnectWalletProps> = ({
)}
</span>
<span style={{ fontSize: "0.9em" }}>
{shortenIfAddress(mountedAddress)}
{shortenIfAddress(walletAddress)}
</span>
</span>
)
Expand All @@ -267,7 +264,7 @@ export const ConnectWallet: React.FC<ConnectWalletProps> = ({
}}
>
<Menu {...api.contentProps}>
{!api.isOpen ? null : mountedAddress ? (
{!api.isOpen ? null : walletAddress ? (
<>
{authConfig?.authUrl && !user?.address && !requiresSignIn ? (
<MenuItem
Expand Down Expand Up @@ -396,10 +393,13 @@ export const ConnectWallet: React.FC<ConnectWalletProps> = ({
})}
onClick={async () => {
if (!connecting) {
connecting = true;
await connectWithMetamask();
connecting = false;
api.close();
try {
connecting = true;
await connectWithMetamask();
api.close();
} finally {
connecting = false;
}
}
}}
leftElement={<Icon boxSize="1.5em" name="metamask" />}
Expand All @@ -421,10 +421,13 @@ export const ConnectWallet: React.FC<ConnectWalletProps> = ({
})}
onClick={async () => {
if (!connecting) {
connecting = true;
await connect(c);
connecting = false;
api.close();
try {
connecting = true;
await connect(c);
api.close();
} finally {
connecting = false;
}
}
}}
leftElement={getIconForConnector(c)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Web3Button } from ".";
import { ThirdwebProvider } from "../..";
import { ThirdwebProvider } from "../../providers/full";
import { test, expect } from "@playwright/experimental-ct-react";

test.use({ viewport: { width: 500, height: 500 } });
Expand Down
22 changes: 14 additions & 8 deletions packages/react/src/components/Web3Button/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { useActiveChainId } from "../../Provider";
import { useContract } from "../../hooks/async/contracts";
import { useAddress } from "../../hooks/useAddress";
import { useChainId } from "../../hooks/useChainId";
import { useNetwork } from "../../hooks/useNetwork";
import { useNetwork } from "../../hooks/wagmi-required/useNetwork";
import { useAddress, useChainId } from "../../hooks/wallet";
import { useSDKChainId } from "../../providers/base";
import {
createCacheKeyWithNetwork,
createContractCacheKey,
Expand All @@ -20,6 +19,7 @@ type ActionFn = (contract: SmartContract) => any;

interface Web3ButtonProps<TActionFn extends ActionFn>
extends ThemeProviderProps {
className?: string;
contractAddress: `0x${string}` | `${string}.eth` | string;

overrides?: CallOverrides;
Expand Down Expand Up @@ -64,11 +64,12 @@ export const Web3Button = <TAction extends ActionFn>({
isDisabled,
children,
action,
className,
...themeProps
}: PropsWithChildren<Web3ButtonProps<TAction>>) => {
const address = useAddress();
const walletChainId = useChainId();
const sdkChainId = useActiveChainId();
const sdkChainId = useSDKChainId();
const [, switchNetwork] = useNetwork();

const queryClient = useQueryClient();
Expand All @@ -87,7 +88,7 @@ export const Web3Button = <TAction extends ActionFn>({
if (switchToChainId) {
if (switchNetwork) {
await switchNetwork(switchToChainId);
await new Promise((resolve) => setTimeout(resolve, 500));
return "__NETWORK_SWITCHED__";
} else {
throw new Error(
"need to switch chain but connected wallet does not support switching",
Expand All @@ -99,10 +100,14 @@ export const Web3Button = <TAction extends ActionFn>({
if (onSubmit) {
onSubmit();
}

return await action(contract);
},
{
onSuccess: (res) => {
if (res === "__NETWORK_SWITCHED__") {
return;
}
if (onSuccess) {
onSuccess(res);
}
Expand All @@ -122,13 +127,14 @@ export const Web3Button = <TAction extends ActionFn>({
},
);
if (!address) {
return <ConnectWallet {...themeProps} />;
return <ConnectWallet className={className} {...themeProps} />;
}

return (
<ThemeProvider {...themeProps}>
<Button
style={{ height: "50px" }}
className={className}
style={{ height: "50px", minWidth: "200px", width: "100%" }}
isLoading={mutation.isLoading || !contract}
onClick={() => mutation.mutate()}
isDisabled={isDisabled}
Expand Down
9 changes: 0 additions & 9 deletions packages/react/src/components/hooks/useIsMounted.ts

This file was deleted.

12 changes: 0 additions & 12 deletions packages/react/src/components/hooks/useMountedAddress.ts

This file was deleted.

2 changes: 2 additions & 0 deletions packages/react/src/constants/runtime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// eslint-disable-next-line turbo/no-undeclared-env-vars
export const __DEV__ = process.env.NODE_ENV !== "production";
Loading