"use client"; import React, { useState } from "react"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; import { MessageCircle, CheckCircle2 } from "lucide-react"; import { useUserContext } from "@app/hooks/useUserContext"; import { useEnvContext } from "@app/hooks/useEnvContext"; import { createApiClient, formatAxiosError } from "@app/lib/api"; import { cn } from "@app/lib/cn"; import { useToast } from "@app/hooks/useToast"; import { Popover, PopoverContent, PopoverTrigger } from "@app/components/ui/popover"; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@app/components/ui/tooltip"; import { Button } from "@app/components/ui/button"; import { Input } from "@app/components/ui/input"; import { Textarea } from "@app/components/ui/textarea"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@app/components/ui/form"; import { useTranslations } from "next-intl"; type SupportFormValues = { subject: string; body: string; }; type SidebarSupportButtonProps = { isCollapsed: boolean; }; export function SidebarSupportButton({ isCollapsed }: SidebarSupportButtonProps) { const [isOpen, setIsOpen] = useState(false); const [isSubmitting, setIsSubmitting] = useState(false); const [isSuccess, setIsSuccess] = useState(false); const { user } = useUserContext(); const { env } = useEnvContext(); const api = createApiClient({ env }); const { toast } = useToast(); const t = useTranslations(); const form = useForm({ resolver: zodResolver(z.object({ subject: z .string() .min(1, t("supportSubjectRequired")) .max(255, t("supportSubjectMaxLength")), body: z.string().min(1, t("supportMessageRequired")) })), defaultValues: { subject: "", body: "" } }); const onSubmit = async (data: SupportFormValues) => { if (!user?.email) { toast({ variant: "destructive", title: t("supportNotAvailableTitle"), description: t("supportNotAvailableDescription") }); return; } setIsSubmitting(true); try { await api.post("/send-support-request", { subject: data.subject, body: data.body }); setIsSuccess(true); toast({ title: t("supportRequestSentTitle"), description: t("supportRequestSentDescription") }); form.reset(); } catch (error) { toast({ variant: "destructive", title: t("supportRequestFailedTitle"), description: formatAxiosError( error, t("supportRequestFailedDescription") ) }); } finally { setIsSubmitting(false); } }; if (!user?.email) { // Show message that support is not available return ( {isCollapsed ? (

{t("support", { defaultValue: "Support" })}

) : ( )}

{t("supportNotAvailableDescription")}

); } return ( { setIsOpen(open); if (!open) { setIsSuccess(false); } }} > {isCollapsed ? (

{t("messageSupport")}

) : ( )} {isSuccess ? (

{t("supportMessageSent")}

{t("supportWillContact")}

) : (
{t("supportReplyTo")} ( {t("supportSubject")} )} /> ( {t("supportMessage")}