import { useState, useEffect } from "react"; import { useNavigate, useLocation } from "react-router-dom"; import { useTranslation } from "react-i18next"; import routes, { hasCrossButton, hasNavigation, isNotEntrypoint, } from "@/routes"; import BackButton from "./BackButton"; import iconUrl from "./icon.png"; import menuUrl from "./menu.png"; import styles from "./styles.module.css"; type HeaderProps = { openMenu?: () => void; showBack?: boolean; showCross?: boolean; classCross?: CSSModuleClasses | string; clickCross?: () => void; }; function Header({ openMenu, showBack, showCross = true, classCross, clickCross = () => { undefined; }, ...props }: HeaderProps & React.HTMLAttributes): JSX.Element { const { t } = useTranslation(); const navigate = useNavigate(); const location = useLocation(); const [initialPath, setInitialPath] = useState(null); const [isNavigated, setIsNavigated] = useState(false); const showBackButton = isNotEntrypoint(location.pathname); const showMenuButton = hasNavigation(location.pathname); const showCrossButton = hasCrossButton(location.pathname); useEffect(() => { if (!initialPath) { setInitialPath(location.pathname); } if (initialPath && location.pathname !== initialPath && !isNavigated) { setIsNavigated(true); } }, [location.pathname, initialPath, isNavigated]); const goBack = () => { if ( location.pathname.includes("/questionnaire") || location.pathname.includes("/about-us") || location.pathname.includes("/payment/stripe") ) { return navigate(-1); } if (initialPath && isNotEntrypoint(initialPath) && !isNavigated) { return navigate(routes.client.root()); } return navigate(-1); }; return (
{showBackButton || showBack ? ( ) : null}
logo {t("app_name")}
{showCrossButton && showCross ? ( Cross ) : null} {showMenuButton ? (
menu
) : null}
); } export default Header;