"use client" import { useToast } from "@/components/hooks/use-toast" import { Button } from "@/components/ui/button" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Form, FormControl, FormField, FormItem, FormMessage } from "@/components/ui/form" import { Input } from "@/components/ui/input" import { changeSubscriptionBillingEmail } from "@/ee/features/billing/actions" import useCaptureEvent from "@/hooks/useCaptureEvent" import { useDomain } from "@/hooks/useDomain" import { isServiceError } from "@/lib/utils" import { zodResolver } from "@hookform/resolvers/zod" import { OrgRole } from "@sourcebot/db" import { Loader2 } from "lucide-react" import { useRouter } from "next/navigation" import { useState } from "react" import { useForm } from "react-hook-form" import * as z from "zod" const formSchema = z.object({ email: z.string().email("Please enter a valid email address"), }) interface ChangeBillingEmailCardProps { currentUserRole: OrgRole, billingEmail: string } export function ChangeBillingEmailCard({ currentUserRole, billingEmail }: ChangeBillingEmailCardProps) { const domain = useDomain() const [isLoading, setIsLoading] = useState(false) const { toast } = useToast() const captureEvent = useCaptureEvent(); const router = useRouter() const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { email: billingEmail, }, }) const onSubmit = async (values: z.infer) => { setIsLoading(true) const newEmail = values.email || billingEmail const result = await changeSubscriptionBillingEmail(domain, newEmail) if (!isServiceError(result)) { toast({ description: "✅ Billing email updated successfully!", }) captureEvent('wa_billing_email_updated_success', {}) router.refresh() } else { toast({ description: "❌ Failed to update billing email. Please try again.", }) captureEvent('wa_billing_email_updated_fail', { error: result.message, }) } setIsLoading(false) } return ( Billing Email The email address for your billing account
( )} />
) }