feat: display country code from IP (#19788)

* feat: display country code from IP

* chore: remove

* fix: unit test
This commit is contained in:
Udit Takkar authored and GitHub committed 2025-03-06 19:16:41 +05:30
1 parent 1a96149ce3
commit d59fdf21fd
11 files changed
+44 -41

No files matched your search

@@ -8,9 +8,18 @@ import { FormProvider, useForm } from "react-hook-form";
import { expect } from "vitest";
import { vi } from "vitest";
import PhoneInput from "@calcom/features/components/phone-input/PhoneInput";
import { getBookingFieldsWithSystemFields } from "../../../lib/getBookingFields";
import { BookingFields } from "./BookingFields";
// Mock PhoneInput to avoid calling the lazy import
vi.mock("@calcom/features/components/phone-input", () => {
return {
default: PhoneInput,
};
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type FormMethods = UseFormReturn<any>;
@@ -0,0 +1,151 @@
"use client";
import { isSupportedCountry } from "libphonenumber-js";
import { useState, useEffect } from "react";
import PhoneInput from "react-phone-input-2";
import "react-phone-input-2/lib/style.css";
import { useIsPlatform } from "@calcom/atoms/hooks/useIsPlatform";
import { trpc } from "@calcom/trpc/react";
import classNames from "@calcom/ui/classNames";
export type PhoneInputProps = {
value?: string;
id?: string;
placeholder?: string;
required?: boolean;
className?: string;
name?: string;
disabled?: boolean;
onChange: (value: string) => void;
defaultCountry?: string;
};
function BasePhoneInput({
name,
className = "",
onChange,
value,
defaultCountry = "us",
...rest
}: PhoneInputProps) {
const isPlatform = useIsPlatform();
if (!isPlatform) {
return (
<BasePhoneInputWeb name={name} className={className} onChange={onChange} value={value} {...rest} />
);
}
return (
<PhoneInput
{...rest}
value={value ? value.trim().replace(/^\+?/, "+") : undefined}
enableSearch
disableSearchIcon
country={defaultCountry}
inputProps={{
name: name,
required: rest.required,
placeholder: rest.placeholder,
}}
onChange={(value) => {
onChange(`+${value}`);
}}
containerClass={classNames(
"hover:border-emphasis dark:focus:border-emphasis border-default !bg-default rounded-md border focus-within:outline-none focus-within:ring-2 focus-within:ring-brand-default disabled:cursor-not-allowed",
className
)}
inputClass="text-sm focus:ring-0 !bg-default text-default placeholder:text-muted"
buttonClass="text-emphasis !bg-default hover:!bg-emphasis"
searchClass="!text-default !bg-default hover:!bg-emphasis"
dropdownClass="!text-default !bg-default"
inputStyle={{ width: "inherit", border: 0 }}
searchStyle={{
display: "flex",
flexDirection: "row",
alignItems: "center",
padding: "6px 12px",
gap: "8px",
width: "296px",
height: "28px",
marginLeft: "-4px",
}}
dropdownStyle={{ width: "max-content" }}
/>
);
}
function BasePhoneInputWeb({
name,
className = "",
onChange,
value,
...rest
}: Omit<PhoneInputProps, "defaultCountry">) {
const defaultCountry = useDefaultCountry();
return (
<PhoneInput
{...rest}
value={value ? value.trim().replace(/^\+?/, "+") : undefined}
country={value ? undefined : defaultCountry}
enableSearch
disableSearchIcon
inputProps={{
name: name,
required: rest.required,
placeholder: rest.placeholder,
}}
onChange={(value) => {
onChange(`+${value}`);
}}
containerClass={classNames(
"hover:border-emphasis dark:focus:border-emphasis border-default !bg-default rounded-md border focus-within:outline-none focus-within:ring-2 focus-within:ring-brand-default disabled:cursor-not-allowed",
className
)}
inputClass="text-sm focus:ring-0 !bg-default text-default placeholder:text-muted"
buttonClass="text-emphasis !bg-default hover:!bg-emphasis"
searchClass="!text-default !bg-default hover:!bg-emphasis"
dropdownClass="!text-default !bg-default"
inputStyle={{ width: "inherit", border: 0 }}
searchStyle={{
display: "flex",
flexDirection: "row",
alignItems: "center",
padding: "6px 12px",
gap: "8px",
width: "296px",
height: "28px",
marginLeft: "-4px",
}}
dropdownStyle={{ width: "max-content" }}
/>
);
}
const useDefaultCountry = () => {
const [defaultCountry, setDefaultCountry] = useState("us");
const query = trpc.viewer.public.countryCode.useQuery(undefined, {
refetchOnWindowFocus: false,
refetchOnReconnect: false,
retry: false,
});
useEffect(
function refactorMeWithoutEffect() {
const data = query.data;
if (!data?.countryCode) {
return;
}
isSupportedCountry(data?.countryCode)
? setDefaultCountry(data.countryCode.toLowerCase())
: setDefaultCountry(navigator.language.split("-")[1]?.toLowerCase() || "us");
},
[query.data]
);
return defaultCountry;
};
export default BasePhoneInput;
@@ -0,0 +1,8 @@
import dynamic from "next/dynamic";
/** These are like 40kb that not every user needs */
const PhoneInputLazy = dynamic(
() => import("./PhoneInput")
) as unknown as typeof import("./PhoneInput").default;
export default PhoneInputLazy;
@@ -5,6 +5,7 @@ import { useState } from "react";
import { Controller, useForm } from "react-hook-form";
import { z } from "zod";
import PhoneInput from "@calcom/features/components/phone-input";
import { SENDER_ID, SENDER_NAME } from "@calcom/lib/constants";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import { WorkflowActions } from "@calcom/prisma/enums";
@@ -21,7 +22,6 @@ import {
Icon,
Input,
Label,
PhoneInput,
Select,
Tooltip,
} from "@calcom/ui";
@@ -6,6 +6,7 @@ import type { UseFormReturn } from "react-hook-form";
import { Controller } from "react-hook-form";
import "react-phone-number-input/style.css";
import PhoneInput from "@calcom/features/components/phone-input";
import { SENDER_ID, SENDER_NAME } from "@calcom/lib/constants";
import { useHasActiveTeamPlan } from "@calcom/lib/hooks/useHasPaidPlan";
import { useLocale } from "@calcom/lib/hooks/useLocale";
@@ -33,7 +34,6 @@ import {
Icon,
Input,
Label,
PhoneInput,
Select,
showToast,
TextArea,
@@ -9,6 +9,7 @@ import type { UseFormGetValues, UseFormSetValue, Control, FormState } from "reac
import type { EventLocationType } from "@calcom/app-store/locations";
import { getEventLocationType, MeetLocationType } from "@calcom/app-store/locations";
import { useIsPlatform } from "@calcom/atoms/hooks/useIsPlatform";
import PhoneInput from "@calcom/features/components/phone-input";
import type {
LocationFormValues,
EventTypeSetupProps,
@@ -22,7 +23,7 @@ import type {
import LocationSelect from "@calcom/features/form/components/LocationSelect";
import { WEBAPP_URL } from "@calcom/lib/constants";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import { Icon, Input, PhoneInput, Button, showToast } from "@calcom/ui";
import { Icon, Input, Button, showToast } from "@calcom/ui";
import classNames from "@calcom/ui/classNames";
export type TEventTypeLocation = Pick<EventTypeSetupProps["eventType"], "locations">;
@@ -4,6 +4,7 @@ import React, { useState } from "react";
import { useFormContext, Controller } from "react-hook-form";
import { z } from "zod";
import PhoneInput from "@calcom/features/components/phone-input";
import { getTemplateFieldsSchema } from "@calcom/features/ee/cal-ai-phone/getTemplateFieldsSchema";
import { TEMPLATES_FIELDS } from "@calcom/features/ee/cal-ai-phone/template-fields-map";
import type { TemplateType } from "@calcom/features/ee/cal-ai-phone/zod-utils";
@@ -12,17 +13,7 @@ import type { EventTypeSetup, FormValues } from "@calcom/features/eventtypes/lib
import { ComponentForField } from "@calcom/features/form-builder/FormBuilderField";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import { trpc } from "@calcom/trpc/react";
import {
Button,
Label,
EmptyScreen,
SettingsToggle,
Divider,
TextField,
PhoneInput,
showToast,
Icon,
} from "@calcom/ui";
import { Button, Label, EmptyScreen, SettingsToggle, Divider, TextField, showToast, Icon } from "@calcom/ui";
import classNames from "@calcom/ui/classNames";
type AIEventControllerProps = {
@@ -6,6 +6,7 @@ import type {
TextLikeComponentProps,
} from "@calcom/app-store/routing-forms/components/react-awesome-query-builder/widgets";
import Widgets from "@calcom/app-store/routing-forms/components/react-awesome-query-builder/widgets";
import PhoneInput from "@calcom/features/components/phone-input";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import {
AddressInput,
@@ -17,7 +18,6 @@ import {
InfoBadge,
InputField,
Label,
PhoneInput,
RadioField,
Tooltip,
} from "@calcom/ui";