pages/ValuePartnership/PartnershipsPage/PresentationBrowser.jsxjavascript
import { useEffect, useState } from "react";
import PropTypes from "prop-types";
import { useTracking } from "../../../services/tracking/useTracking";
import { UIMCButton } from "@ui-marcom/react/button";
import { UIMCIcon } from "@ui-marcom/react/icon";
import { UIMCToggleSwitch } from "@ui-marcom/react/toggle-switch";
import { UIMCCheckbox } from "@ui-marcom/react/checkbox";
import { QrCodeCanvas } from "./QrCodeCanvas";
import { buildBookmarksUrl } from "./qrCodeLink";
import "./PresentationBrowser.scss";

const MAX_COLLECTED = 5;
// px on the fixed-size stage - fills the free grid cell like the original app's QR
const QR_SIZE = 500;
// a visitor picks up to 5 assets in a row - only the settled selection counts as "shown"
const QR_SHOWN_TRACKING_DEBOUNCE_MS = 2000;

/**
 * Full-screen browse overlay, ported from value-slideview-filter.js (the old
 * "SlideViewFilter" - a misleading name, it has nothing to do with filtering
 * a single deck's slides). Two modes:
 * - "all": every presentation deck, grouped per content.valuePartnership's
 *   partnershipsFilterUS/partnershipsFilter link groups (hideUS picks which),
 *   normally as clickable links that open the deck; when `showQrCode` is on,
 *   as checkboxes (up to 5) that build one combined "save to conference bag"
 *   QR code.
 * - "keySlides": a thumbnail grid of content.valuePartnership.partnerships
 *   .keySlides.overview, each opening its matching mini-deck from keySlides.ppt.
 *
 * @param {Object} props
 * @param {"all"|"keySlides"} props.mode
 * @param {boolean} props.showQrCode
 * @param {function(boolean): void} props.onToggleQrCode
 * @param {boolean} props.hideUS - Accepted (per PartnershipsPage's `linkGroups` selection) but its own toggle switch is currently commented out below, so this is presently unused inside the component.
 * @param {function(boolean): void} props.onToggleHideUS - See `hideUS` - currently unused inside the component.
 * @param {Array} props.linkGroups - `content.valuePartnership.partnershipsFilterUS|Filter.groups`.
 * @param {Array} props.ppt - Full presentation-deck list (to resolve a link's ppt.id).
 * @param {Array} props.keySlidesOverview
 * @param {Array} props.keySlidesPpt
 * @param {Object} props.labels - `content.valuePartnership.labels` (conference-bag link parts).
 * @param {function(Object, number=): void} props.onSelectPpt
 * @param {function(): void} props.onClose
 * @param {function(): void} props.onSwitchMode - Jumps to the other mode ("all" <-> "keySlides").
 */
export const PresentationBrowser = ({
  mode,
  showQrCode,
  onToggleQrCode,
  hideUS,
  onToggleHideUS,
  linkGroups,
  ppt,
  keySlidesOverview,
  keySlidesPpt,
  labels,
  onSelectPpt,
  onClose,
  onSwitchMode,
}) => {
  const [collected, setCollected] = useState([]);
  const { trackEvent } = useTracking();

  // tracked here, not inside the setState updater - StrictMode runs updaters twice
  const toggleCollected = (item) => {
    const wasCollected = collected.some((i) => i.id === item.id);
    if (!wasCollected && collected.length >= MAX_COLLECTED) return;
    trackEvent?.("qr_collect_toggle", {
      element: item.label,
      payload: {
        pptId: item.id,
        collected: !wasCollected,
        count: collected.length + (wasCollected ? -1 : 1),
      },
    });
    setCollected((prev) => {
      if (prev.some((i) => i.id === item.id)) return prev.filter((i) => i.id !== item.id);
      if (prev.length >= MAX_COLLECTED) return prev;
      return [...prev, item];
    });
  };

  // snapshot of what the visible QR code contains, once the selection has
  // settled - so quick successive picks don't each produce a "shown" event
  useEffect(() => {
    if (!showQrCode || collected.length === 0) return undefined;
    const timer = setTimeout(
      () =>
        trackEvent?.("qr_code_shown", {
          element: "collect",
          payload: {
            items: collected.map(({ id, label }) => ({ pptId: id, label })),
            count: collected.length,
          },
        }),
      QR_SHOWN_TRACKING_DEBOUNCE_MS
    );
    return () => clearTimeout(timer);
  }, [showQrCode, collected, trackEvent]);

  return (
    <div
      className={`presentation-browser${mode === "all" ? " presentation-browser--all-presentations" : ""}`}>
      <h1 className="presentation-browser__headline">
        {mode === "all" ? "All presentations" : "Key slides"}
      </h1>

      {mode === "all" ? (
        <div className={`presentation-browser__card`}>
          <div className="presentation-browser__groups">
            {linkGroups
              .filter((group) => group.options.length > 0)
              .map((group) => (
                <div key={group.label} className="presentation-browser__group">
                  {/* QR mode keeps the exact link-list markup (chevron
                      included, just hidden) and overlays a checkbox mark on
                      it, so toggling never shifts the layout. */}
                  <ul className="presentation-browser__link-list">
                    {group.options.map((option) => {
                      const isCollected = collected.some((i) => i.id === option.id);
                      const isDisabled = !isCollected && collected.length >= MAX_COLLECTED;
                      return (
                        <li key={option.id}>
                          <a
                            href="#value-partnerships/partnerships"
                            className={isDisabled ? "is-disabled" : undefined}
                            role={showQrCode ? "checkbox" : undefined}
                            aria-checked={showQrCode ? isCollected : undefined}
                            aria-disabled={showQrCode ? isDisabled : undefined}
                            onClick={(e) => {
                              e.preventDefault();
                              if (showQrCode) {
                                toggleCollected(option);
                                return;
                              }
                              const found = ppt.find((p) => p.id === option.id);
                              if (found) onSelectPpt(found);
                            }}>
                            <UIMCIcon
                              icon="arrow-right-medium"
                              size="small"
                              className={showQrCode ? "is-hidden" : undefined}
                            />
                            {showQrCode && (
                              <span className="presentation-browser__mark">
                                <UIMCCheckbox checked={isCollected} />
                              </span>
                            )}
                            {option.label}
                          </a>
                        </li>
                      );
                    })}
                  </ul>
                </div>
              ))}
            {showQrCode && collected.length > 0 && (
              <div className="presentation-browser__qr-slot">
                <div className="presentation-browser__qr">
                  <QrCodeCanvas url={buildBookmarksUrl(labels, collected)} size={QR_SIZE} />
                  <h5>
                    Scan your assets
                    <br />
                    to read them later
                  </h5>
                </div>
              </div>
            )}
          </div>
        </div>
      ) : (
        <div className="presentation-browser__key-slides">
          {keySlidesOverview.map((slide) => (
            <div
              key={slide.id}
              className="presentation-browser__key-slide"
              onClick={() => {
                const found = keySlidesPpt.find((k) => k.id === slide.id);
                if (found) onSelectPpt(found, 1);
              }}>
              <img src={slide.src} alt="" loading="lazy" />
              <p>{slide.title}</p>
            </div>
          ))}
        </div>
      )}

      <UIMCButton
        className="presentation-browser__switch-mode"
        variant="standard"
        title={mode === "all" ? "Show key slides" : "Show all presentations"}
        onClick={onSwitchMode}>
        <UIMCIcon slot="prefix" icon="arrow-right-medium" />
        {mode === "all" ? "Show key slides" : "Show all presentations"}
      </UIMCButton>

      <UIMCButton
        className="presentation-browser__close"
        variant="primary-content"
        title={mode === "all" ? "Close all presentations" : "Close key slides"}
        onClick={onClose}>
        <UIMCIcon slot="prefix" icon="close" />
        {mode === "all" ? "Close all presentations" : "Close key slides"}
      </UIMCButton>
      {mode === "all" && (
        <div className="presentation-browser__toggles">
          <UIMCToggleSwitch
            className="presentation-browser__toggle"
            isChecked={showQrCode}
            onChange={({ checked }) => {
              if (!checked) setCollected([]);
              onToggleQrCode(checked);
            }}
          />
          <span>QR Codes for saving highlights</span>

          <UIMCToggleSwitch
            className="presentation-browser__toggle"
            isChecked={hideUS}
            onChange={({ checked }) => onToggleHideUS(checked)}
          />
          <span>Hide US content</span>
        </div>
      )}
    </div>
  );
};

PresentationBrowser.propTypes = {
  mode: PropTypes.oneOf(["all", "keySlides"]).isRequired,
  showQrCode: PropTypes.bool.isRequired,
  onToggleQrCode: PropTypes.func.isRequired,
  hideUS: PropTypes.bool.isRequired,
  onToggleHideUS: PropTypes.func.isRequired,
  linkGroups: PropTypes.array.isRequired,
  ppt: PropTypes.array.isRequired,
  keySlidesOverview: PropTypes.array.isRequired,
  keySlidesPpt: PropTypes.array.isRequired,
  labels: PropTypes.object.isRequired,
  onSelectPpt: PropTypes.func.isRequired,
  onClose: PropTypes.func.isRequired,
  onSwitchMode: PropTypes.func.isRequired,
};