Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 25 additions & 0 deletions .changeset/fast-items-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
"thirdweb": minor
---

Adds a defaultSmsCountryCode configuration option to In-App and Ecosystem Wallets

```ts
createWallet("inApp", {
auth: {
options: [
"google",
"discord",
"telegram",
"email",
"passkey",
"phone",
"farcaster",
"line",
],
mode: "redirect",
passkeyDomain: getDomain(),
defaultSmsCountryCode: "AF",
Copy link
Member

Choose a reason for hiding this comment

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

i would just show this option, maybe with auth option "phone"

kinda confusing to see passkey/mode in here too

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
options: [
"google",
"discord",
"telegram",
"email",
"passkey",
"phone",
"farcaster",
"line",
],
mode: "redirect",
passkeyDomain: getDomain(),
defaultSmsCountryCode: "AF",
options: [
"phone",
],
defaultSmsCountryCode: "IN", // default to india country code

},
}),
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { describe, expect, it, vi } from "vitest";
import {
fireEvent,
render,
screen,
} from "../../../../../test/src/react-render.js";
import { CountrySelector } from "./CountrySelector.js";

describe("CountrySelector", () => {
it("renders with default country code", () => {
const setCountryCode = vi.fn();
render(
<CountrySelector countryCode="US +1" setCountryCode={setCountryCode} />,
);

const selectElement = screen.getByRole("combobox");
expect(selectElement).toBeTruthy();
expect(selectElement).toHaveValue("US +1");
});

it("changes country code on selection", () => {
const setCountryCode = vi.fn();
render(
<CountrySelector countryCode="US +1" setCountryCode={setCountryCode} />,
);

const selectElement = screen.getByRole("combobox");
fireEvent.change(selectElement, { target: { value: "CA +1" } });

expect(setCountryCode).toHaveBeenCalledWith("CA +1");
});

it("displays all supported countries", () => {
const setCountryCode = vi.fn();
render(
<CountrySelector countryCode="US +1" setCountryCode={setCountryCode} />,
);

const options = screen.getAllByRole("option");
expect(options.length).toBeGreaterThan(0);
expect(
options.some((option) => option.textContent?.includes("United States")),
).toBe(true);
});
});
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
"use client";
import { useQuery } from "@tanstack/react-query";
import { useRef } from "react";
import { useCustomTheme } from "../../../core/design-system/CustomThemeProvider.js";
import { radius, spacing } from "../../../core/design-system/index.js";
import { StyledOption, StyledSelect } from "../../ui/design-system/elements.js";
import {
type SupportedSmsCountry,
supportedSmsCountries,
} from "./supported-sms-countries.js";

export function getCountrySelector(countryIsoCode: SupportedSmsCountry) {
const country = supportedSmsCountries.find(
(country) => country.countryIsoCode === countryIsoCode,
);
if (!country) {
return "US +1";
}

Check warning on line 17 in packages/thirdweb/src/react/web/wallets/in-app/CountrySelector.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/wallets/in-app/CountrySelector.tsx#L16-L17

Added lines #L16 - L17 were not covered by tests
return `${country.countryIsoCode} +${country.phoneNumberCode}`;
}

export function CountrySelector({
countryCode,
Expand All @@ -14,17 +27,7 @@
}) {
const selectRef = useRef<HTMLSelectElement>(null);

const { data: supportedCountries } = useQuery({
queryKey: ["supported-sms-countries"],
queryFn: async () => {
const { supportedSmsCountries } = await import(
"./supported-sms-countries.js"
);
return supportedSmsCountries;
},
});

const supportedCountriesForSms = supportedCountries ?? [
const supportedCountriesForSms = supportedSmsCountries ?? [
{
countryIsoCode: "US",
countryName: "United States",
Expand Down Expand Up @@ -58,7 +61,7 @@
return (
<Option
key={country.countryIsoCode}
value={`${country.countryIsoCode} +${country.phoneNumberCode}`}
value={getCountrySelector(country.countryIsoCode)}
>
{country.countryName} +{country.phoneNumberCode}
</Option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import { IconButton } from "../../ui/components/buttons.js";
import { Input, InputContainer } from "../../ui/components/formElements.js";
import { Text } from "../../ui/components/text.js";
import { CountrySelector } from "./CountrySelector.js";
import { CountrySelector, getCountrySelector } from "./CountrySelector.js";
import type { SupportedSmsCountry } from "./supported-sms-countries.js";

export function InputSelectionUI(props: {
onSelect: (data: string) => void;
Expand All @@ -22,8 +23,13 @@
submitButtonText: string;
format?: "phone";
disabled?: boolean;
defaultSmsCountryCode?: SupportedSmsCountry;
}) {
const [countryCodeInfo, setCountryCodeInfo] = useState("US +1");
const [countryCodeInfo, setCountryCodeInfo] = useState(
props.defaultSmsCountryCode
? getCountrySelector(props.defaultSmsCountryCode)
: "US +1",
);

Check warning on line 32 in packages/thirdweb/src/react/web/wallets/in-app/InputSelectionUI.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/wallets/in-app/InputSelectionUI.tsx#L28-L32

Added lines #L28 - L32 were not covered by tests
const [input, setInput] = useState("");
const [error, setError] = useState<string | undefined>();
const [showError, setShowError] = useState(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export type SupportedSmsCountry =
(typeof supportedSmsCountries)[number]["countryIsoCode"];
export const supportedSmsCountries = [
{
countryIsoCode: "AD",
Expand Down Expand Up @@ -1183,4 +1185,4 @@ export const supportedSmsCountries = [
countryName: "Zimbabwe",
phoneNumberCode: "263",
},
];
] as const;
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,9 @@
disabled={props.disabled}
emptyErrorMessage={emptyErrorMessage}
submitButtonText={locale.submitEmail}
defaultSmsCountryCode={
wallet.getConfig()?.auth?.defaultSmsCountryCode

Check warning on line 452 in packages/thirdweb/src/react/web/wallets/shared/ConnectWalletSocialOptions.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/wallets/shared/ConnectWalletSocialOptions.tsx#L451-L452

Added lines #L451 - L452 were not covered by tests
}
/>
) : (
<WalletTypeRowButton
Expand Down
5 changes: 5 additions & 0 deletions packages/thirdweb/src/wallets/ecosystem/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { SupportedSmsCountry } from "../../react/web/wallets/in-app/supported-sms-countries.js";
import type {
InAppWalletAutoConnectOptions,
InAppWalletConnectionOptions,
Expand All @@ -13,6 +14,10 @@ export type EcosystemWalletCreationOptions = {
* Optional url to redirect to after authentication
*/
redirectUrl?: string;
/**
* The default country code to use for SMS authentication
*/
defaultSmsCountryCode?: SupportedSmsCountry;
};
/**
* The partnerId of the ecosystem wallet to connect to
Expand Down
5 changes: 5 additions & 0 deletions packages/thirdweb/src/wallets/in-app/core/wallet/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Chain } from "../../../../chains/types.js";
import type { ThirdwebClient } from "../../../../client/client.js";
import type { SupportedSmsCountry } from "../../../../react/web/wallets/in-app/supported-sms-countries.js";
import type { SmartWalletOptions } from "../../../smart/types.js";
import type {
AuthOption,
Expand Down Expand Up @@ -59,6 +60,10 @@ export type InAppWalletCreationOptions =
* The domain of the passkey to use for authentication
*/
passkeyDomain?: string;
/**
* The default country code to use for SMS authentication
*/
defaultSmsCountryCode?: SupportedSmsCountry;
};
/**
* Metadata to display in the Connect Modal
Expand Down
Loading