pages/ValuePartnership/ValuePartnershipNav.jsxjavascript
import { useState } from "react";
import PropTypes from "prop-types";
import { useNavigate, useLocation } from "react-router";
import { UIMCButton } from "@ui-marcom/react/button";
import { UIMCControlButton } from "@ui-marcom/react/control-button";
import { UIMCIcon } from "@ui-marcom/react/icon";
import { UIMCToggleButtonSwitch } from "@ui-marcom/react/toggle-button-switch";
import { useContent } from "../../services/content";
import { useConfig } from "../../services/config/config";
import { useTracking } from "../../services/tracking/useTracking";
import { navigateWithTransition } from "../../utils/viewTransition";
import { useNavHeight } from "./useNavHeight";
import "./ValuePartnershipNav.scss";

// route segment -> content.valuePartnership.navigation key (old app's page ids)
const SECTIONS = [
  { path: "globe", navKey: "globe-page", icon: "location-pin" },
  { path: "excellence", navKey: "value-excellence-page", icon: "portfolio" },
  { path: "partnerships", navKey: "value-partnerships-page", icon: "department" },
];

/**
 * Fixed nav bar shared by all 3 Value Partnership sections: section switcher
 * (labels from content) + a burger toggle on the right that reveals the
 * "Switch App" button back to the app selection (kept collapsed by default
 * per Figma). The burger holds nothing else, so with the `hideAppSwitch`
 * config param both are left out entirely. Reloading is left to the staff
 * menu (StaffControls), which works on every page.
 *
 * `children`, when passed, renders as a second button group alongside the
 * section switcher - the lifted-state slot a routed sub-page (e.g.
 * PartnershipsPage) uses to put its own page-scoped actions in this shared
 * bar, mirroring how `suppressNav`/`onSelectionChange` let a sub-page affect
 * this bar from below (see ValuePartnership.jsx).
 *
 * @param {Object} props
 * @param {*} [props.children] - React node(s) for the second button group.
 */
export const ValuePartnershipNav = ({ children }) => {
  const navigate = useNavigate();
  const { pathname, search } = useLocation();
  const { content } = useContent();
  const { trackEvent } = useTracking();
  const { hideAppSwitch } = useConfig();
  const navigation = content.valuePartnership?.navigation || {};
  // "Switch App" stays hidden behind this burger toggle because this bar's
  // section switcher already fills the layout; see Figma for the
  // collapsed/expanded burger look.
  const [menuOpen, setMenuOpen] = useState(false);

  // drawers end flush on top of this bar (166px on the 4K kiosk per Figma,
  // different on the iPad, where the page actions stack), see useNavHeight
  const navRef = useNavHeight();

  // without this, UIMCToggleButtonSwitch defaults `current` to 0 (its own
  // default) on every mount - fine when navigating by clicking (this bar
  // stays mounted across route changes, see ValuePartnership.jsx), but wrong
  // after a hard reload straight into e.g. /value-partnership/excellence,
  // which showed "Globe" as active no matter which section was actually open
  const activeIndex = Math.max(
    0,
    SECTIONS.findIndex(({ path }) => pathname.endsWith(`/${path}`))
  );

  const goToSection = (path) => {
    setMenuOpen(false);
    trackEvent?.("navigation_click", { element: path });
    navigateWithTransition(navigate, `/value-partnerships/${path}${search}`);
  };

  return (
    <nav ref={navRef} className="value-partnership-nav">
      <div className="value-partnership-nav__sections">
        <UIMCToggleButtonSwitch
          className="value-partnership-nav__switch"
          size="medium"
          current={activeIndex}>
          {SECTIONS.map(({ path, navKey }) => {
            return (
              <UIMCButton
                key={path}
                className={`value-partnership-nav__button`}
                variant="standard"
                title={navigation[navKey]?.label || path}
                size="medium"
                onClick={() => goToSection(path)}>
                <UIMCIcon slot="prefix" icon={navigation[navKey]?.icon} />
                {navigation[navKey]?.label || path}
              </UIMCButton>
            );
          })}
        </UIMCToggleButtonSwitch>
      </div>
      {children && <div className="value-partnership-nav__actions">{children}</div>}
      {!hideAppSwitch && (
        <div className="value-partnership-nav__menu">
          {menuOpen && (
            <UIMCButton
              className="value-partnership-nav__switch-app"
              variant="standard"
              title="Apps"
              onClick={() => {
                trackEvent?.("navigation_click", { element: "Apps" });
                // keep the kiosk config params (config-schema.js) - same as
                // SlideNavigation's "Apps" button in VAS/RAD
                navigateWithTransition(navigate, `/${search}`);
              }}>
              {/* <UIMCIcon slot="prefix" icon="arrow-right" /> */}
              Apps
            </UIMCButton>
          )}
          <UIMCControlButton
            className="value-partnership-nav__menu-toggle"
            variant="standard"
            size="medium"
            icon={menuOpen ? "close" : "menu"}
            title={menuOpen ? "Close" : "Menu"}
            onClick={() => setMenuOpen((open) => !open)}
          />
        </div>
      )}
    </nav>
  );
};

ValuePartnershipNav.propTypes = {
  children: PropTypes.node,
};