import { zodResolver } from "@hookform/resolvers/zod"; import { useEffect, useState } from "react"; import { useForm } from "react-hook-form"; import { z } from "zod"; import dayjs from "@calcom/dayjs"; import { FULL_NAME_LENGTH_MAX_LIMIT } from "@calcom/lib/constants"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import { telemetryEventTypes, useTelemetry } from "@calcom/lib/telemetry"; import { trpc } from "@calcom/trpc/react"; import { Button, TimezoneSelect } from "@calcom/ui"; import { ArrowRight } from "@calcom/ui/components/icon"; import { UsernameAvailabilityField } from "@components/ui/UsernameAvailability"; interface IUserSettingsProps { nextStep: () => void; hideUsername?: boolean; } const UserSettings = (props: IUserSettingsProps) => { const { nextStep } = props; const [user] = trpc.viewer.me.useSuspenseQuery(); const { t } = useLocale(); const [selectedTimeZone, setSelectedTimeZone] = useState(dayjs.tz.guess()); const telemetry = useTelemetry(); const userSettingsSchema = z.object({ name: z .string() .min(1) .max(FULL_NAME_LENGTH_MAX_LIMIT, { message: t("max_limit_allowed_hint", { limit: FULL_NAME_LENGTH_MAX_LIMIT }), }), }); const { register, handleSubmit, formState: { errors }, } = useForm>({ defaultValues: { name: user?.name || "", }, reValidateMode: "onChange", resolver: zodResolver(userSettingsSchema), }); useEffect(() => { telemetry.event(telemetryEventTypes.onboardingStarted); }, [telemetry]); const utils = trpc.useContext(); const onSuccess = async () => { await utils.viewer.me.invalidate(); nextStep(); }; const mutation = trpc.viewer.updateProfile.useMutation({ onSuccess: onSuccess, }); const onSubmit = handleSubmit((data) => { mutation.mutate({ name: data.name, timeZone: selectedTimeZone, }); }); return (
{/* Username textfield: when not coming from signup */} {!props.hideUsername && } {/* Full name textfield */}
{errors.name && (

{errors.name.message}

)}
{/* Timezone select field */}
setSelectedTimeZone(value)} className="mt-2 w-full rounded-md text-sm" />

{t("current_time")} {dayjs().tz(selectedTimeZone).format("LT").toString().toLowerCase()}

); }; export { UserSettings };