"use client"; import { usePathname, useRouter } from "next/navigation"; import { useState } from "react"; import { useFormContext } from "react-hook-form"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import type { RoutingFormWithResponseCount } from "@calcom/routing-forms/types/types"; import { Button } from "@calcom/ui/components/button"; import { DropdownMenuSeparator } from "@calcom/ui/components/dropdown"; import { ToggleGroup } from "@calcom/ui/components/form"; import { Icon } from "@calcom/ui/components/icon"; import { Tooltip } from "@calcom/ui/components/tooltip"; import { FormAction, FormActionsDropdown } from "../FormActions"; import { FormSettingsSlideover } from "./FormSettingsSlideover"; // Toggle group doesnt support HREF navigation, so we need to use this hook to handle navigation const useRoutingFormNavigation = ( form: RoutingFormWithResponseCount, appUrl: string, setShowInfoLostDialog: (value: boolean) => void ) => { const pathname = usePathname(); const router = useRouter(); const formContext = useFormContext(); // Get the current page based on the pathname since we use a custom routing system const getCurrentPage = () => { const path = pathname || ""; if (path.includes("/form-edit/")) return "form-edit"; if (path.includes("/route-builder/")) return "route-builder"; if (path.includes("/incomplete-booking/")) return "incomplete-booking"; return "form-edit"; // default to form-edit if no match }; const handleNavigation = (value: string) => { if (!value) return; const baseUrl = `${appUrl}/${value}/${form.id}`; if (value === "route-builder" && formContext.formState.isDirty) { setShowInfoLostDialog(true); } else { router.push(baseUrl); } }; return { getCurrentPage, handleNavigation, }; }; const Actions = ({ form, isSaving, appUrl, setIsTestPreviewOpen, isTestPreviewOpen, isMobile = false, permissions, }: { form: RoutingFormWithResponseCount; setIsTestPreviewOpen: (value: boolean) => void; isSaving: boolean; appUrl: string; isTestPreviewOpen: boolean; isMobile?: boolean; permissions: { canCreate: boolean; canRead: boolean; canEdit: boolean; canDelete: boolean; }; }) => { const { t } = useLocale(); const formContext = useFormContext(); const [isSettingsDialogOpen, setIsSettingsDialogOpen] = useState(false); return ( <>
); }; export function Header({ routingForm, isSaving, appUrl, setShowInfoLostDialog, setIsTestPreviewOpen, isTestPreviewOpen, permissions, }: { routingForm: RoutingFormWithResponseCount; isSaving: boolean; appUrl: string; setShowInfoLostDialog: (value: boolean) => void; setIsTestPreviewOpen: (value: boolean) => void; isTestPreviewOpen: boolean; permissions: { canCreate: boolean; canRead: boolean; canEdit: boolean; canDelete: boolean; }; }) { const { t } = useLocale(); const [isEditing, setIsEditing] = useState(false); const [title, setTitle] = useState(routingForm.name); const form = useFormContext(); const { getCurrentPage, handleNavigation } = useRoutingFormNavigation( routingForm, appUrl, setShowInfoLostDialog ); const handleTitleChange = (e: React.ChangeEvent) => { setTitle(e.target.value); }; const handleTitleSubmit = () => { setIsEditing(false); }; const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === "Enter") { form.setValue("name", title); handleTitleSubmit(); } else if (e.key === "Escape") { form.setValue("name", routingForm.name); setIsEditing(false); } }; const watchedName = form.watch("name"); return (
{/* Left - Back button and title */}
)}
{/* Mobile/Tablet layout - Second row with toggle group and actions on the same row */}
{/* Navigation Tabs - Left aligned */}
, dataTestId: "toggle-group-item-form-edit", }, { value: "route-builder", label: t("routing"), iconLeft: , }, ]} />
{/* Actions - Right aligned */}
{" "}
{/* Desktop layout - Toggle group in center column */}
, }, { value: "route-builder", label: t("routing"), iconLeft: , }, ]} />
{/* Desktop layout - Actions in right column */}
); }