components/Stage/Stage.jsxjavascript
import PropTypes from "prop-types";
import { useLocation } from "react-router";
import { useConfig } from "../../services/config/config";
import { useElementSize } from "../../hooks/elementSize";
import { resolveModeForPath } from "../../services/presentationModes";
import { StageOrientationContext } from "./stageOrientation";
import "./Stage.scss";

/**
 * Whole-app scale wrapper. Renders `children` inside a fixed-size box and
 * scales that box down to fit whatever real screen it ends up on via CSS
 * `transform: scale()`.
 *
 * Replaces the previous per-page `calc(1rem / X)` + html-root-font-size
 * scaling scheme: instead of keeping dozens of components' em/rem units in
 * sync with two independent, uncoordinated breakpoint systems (the app's
 * own root font-size AND @ui-marcom/shared's internal viewport-width
 * breakpoints), every pixel authored anywhere in `children` scales together
 * as one image - which system decided a given size no longer matters once
 * it's inside this box.
 *
 * Each sales app declares its orientation via PRESENTATION_MODES'
 * `orientation` field, matched against the current route via
 * resolveModeForPath(): fixed `"landscape"`/`"portrait"`, or `"auto"` (Rad
 * Enablement), which follows the real screen's aspect ratio - measured on
 * `stage-outer`, so it re-evaluates live on resize/rotation and always
 * agrees with the `@media (orientation: ...)` mixins (styles/_mixins.scss)
 * the components use (same tie-break: a square screen counts as portrait,
 * like the CSS media feature). "/" (app selection) matches no mode and
 * defaults to landscape.
 *
 * The reference box has two independent axes, not one:
 * - WHICH physical kiosk this is (`stageDevice` config value: "4k" or
 *   "ipad") picks that device's own landscape width/height pair. A 4K panel
 *   and an iPad need separate pairs (not a shared one) because an iPad is a
 *   2x Retina display - its CSS point resolution is roughly half its
 *   physical pixel count, very different from a 4K panel's 1:1 mapping.
 * - Orientation then swaps THAT device's own pair for portrait routes - "a
 *   physically rotated panel" is only true *within* one device's pair, not
 *   across devices. Treating portrait as a single cross-device resolution
 *   was tried first and broke both ends: Rad Enablement letterboxed on the
 *   iPad (wrong aspect ratio), and would have upscaled/blurred on a
 *   rotated 4K panel (wrong magnitude) - see README.md's "Scaling (Stage)"
 *   section.
 *
 * The scale factor is never allowed above 1 in practice: the reference
 * resolution is deliberately the *largest* target device in its
 * orientation, so real screens only ever scale down. Upscaling would blur
 * the WebGL globe (InteractiveGlobe.jsx), whose canvas render resolution
 * three.js derives from layout size, not from a CSS transform applied to an
 * ancestor.
 *
 * `stage-inner` also carries `data-stage-device` so per-device CSS overrides
 * (e.g. RadEnablement.scss's iPad content-density correction, since the
 * same em-authored content necessarily renders larger on the iPad's
 * ~2x-smaller reference box) can target a device without their own JS.
 *
 * Must be mounted inside the router (see HashRouter.jsx), not in App.jsx -
 * it reads its config (stageDevice and the stageRefWidth/Height /
 * stageIpadRefWidth/Height pairs) via useConfig() and the current route via
 * useLocation(), both of which need router context and would silently fail
 * (or throw) outside a <HashRouter>.
 */
export const Stage = ({ children }) => {
  const { stageDevice, stageRefWidth, stageRefHeight, stageIpadRefWidth, stageIpadRefHeight } =
    useConfig();
  const { pathname } = useLocation();
  const [outerRef, { width, height }] = useElementSize({
    width: window.innerWidth,
    height: window.innerHeight,
  });

  const [deviceLandscapeWidth, deviceLandscapeHeight] =
    stageDevice === "ipad"
      ? [stageIpadRefWidth, stageIpadRefHeight]
      : [stageRefWidth, stageRefHeight];

  const orientation = resolveModeForPath(pathname)?.orientation;
  const isPortrait = orientation === "auto" ? height >= width : orientation === "portrait";
  const refWidth = isPortrait ? deviceLandscapeHeight : deviceLandscapeWidth;
  const refHeight = isPortrait ? deviceLandscapeWidth : deviceLandscapeHeight;

  // "contain" scaling: shrink just enough that both dimensions fit, so the
  // reference box never gets cropped - whichever dimension is relatively
  // narrower on the real screen determines the factor, the other dimension
  // letterboxes.
  const scale = Math.min(width / refWidth, height / refHeight) || 1;

  return (
    <div className="stage-outer" ref={outerRef}>
      <div
        className="stage-inner"
        data-stage-device={stageDevice}
        style={{
          "width": refWidth,
          "height": refHeight,
          // stage-relative stand-ins for `vw`/`vh` (which measure the real,
          // unscaled viewport): e.g. `calc(var(--stage-height) * 0.6)`
          "--stage-width": `${refWidth}px`,
          "--stage-height": `${refHeight}px`,
          // the staff menu's size (@tracking/staff-menu, designed for Full
          // HD): the reference box relative to 1920px, 2 on the 4K stage in
          // both orientations (the device's landscape width, so a portrait
          // route doesn't shrink it); never below 1, so text on the iPad's
          // smaller box doesn't get smaller than designed
          "--staff-menu-scale": Math.max(1, deviceLandscapeWidth / 1920),
          "transform": `scale(${scale})`,
        }}>
        <StageOrientationContext.Provider value={isPortrait ? "portrait" : "landscape"}>
          {children}
        </StageOrientationContext.Provider>
      </div>
    </div>
  );
};

Stage.propTypes = {
  children: PropTypes.node.isRequired,
};