All files / src/components AntennaMap.tsx

87.71% Statements 50/57
67.3% Branches 35/52
76.92% Functions 10/13
89.13% Lines 41/46

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270                                                          27x     27x 23x 23x 23x 23x 20x 20x 20x 20x 20x 23x 23x 23x 23x   20x 20x                             27x     27x     27x 27x 27x 27x 15x 15x 3x         3x     27x 15x 3x 3x 3x 3x 3x 3x 3x 3x     27x                                                                                 2x 1x 1x                                                                                                                                                                                                                                                                      
// File: src/components/AntennaMap.tsx
import React, { useMemo, useRef, useState, useLayoutEffect } from 'react';
import ReactDOM from 'react-dom';
import styles from '../GeoMapWidget.module.css';
import type { Antenna, RGB, ColorScaleName } from '../utils/geomap';
import { SmartboxGroups } from './SmartboxGroups';
import type { GroupByMode } from './SmartboxGroups';
import { AntennaNodes } from './AntennaNodes';
import { usePanZoom } from '../hooks/usePanZoom';
import { fmt2 } from '../utils/geomap';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faRotateLeft } from '@fortawesome/free-solid-svg-icons';
 
export function AntennaMap(props: {
  antennas: Antenna[];
  powerRange: { min: number; max: number };
  stationId: string;
  radiusMinPx: number;
  radiusMaxPx: number;
  showAntennas: boolean;
  groupBy?: GroupByMode;
  /** color scale selection + optional custom palette (overrides) */
  colorScaleName: ColorScaleName;
  customPalette?: RGB[];
  showTitleChipOnHover?: boolean;
  titleChipText?: string;
  compactGroupBy?: GroupByMode;
  onCompactGroupByChange?: (next: GroupByMode) => void;
}) {
  const mapDivRef = useRef<HTMLDivElement | null>(null);
 
  // Compute bounds once we have antennas
  const bounds = useMemo(() => {
    const pts = props.antennas
      .map((a) => ({ x: Number(a.east), y: -Number(a.north) })) // flip y
      .filter((p) => isFinite(p.x) && isFinite(p.y));
    if (!pts.length) return null;
    let minX = Infinity,
      minY = Infinity,
      maxX = -Infinity,
      maxY = -Infinity;
    for (const p of pts) {
      if (p.x < minX) minX = p.x;
      Eif (p.y < minY) minY = p.y;
      Eif (p.x > maxX) maxX = p.x;
      if (p.y > maxY) maxY = p.y;
    }
    const pad = 10; // padding so shapes never clip
    return { minX: minX - pad, minY: minY - pad, maxX: maxX + pad, maxY: maxY + pad };
  }, [props.antennas]);
 
  const {
    containerRef,
    svgRef,
    k,
    t,
    pxToVb,
    onWheel,
    onPointerDown,
    onPointerMove,
    onPointerUp,
    panning,
    reset: resetTransform
  } = usePanZoom(bounds);
 
  // attach container ref
  containerRef.current = mapDivRef.current;
 
  // Tooltip state
  const tipRef = useRef<HTMLDivElement | null>(null);
  const [hover, setHover] = useState<null | { pageX: number; pageY: number; a: Antenna }>(null);
  const [tipSize, setTipSize] = useState({ w: 0, h: 0 });
  useLayoutEffect(() => {
    const el = tipRef.current;
    if (!el) return;
    const raf = requestAnimationFrame(() => {
      if (!el.isConnected) return;
      const r = el.getBoundingClientRect();
      setTipSize({ w: r.width, h: r.height });
    });
    return () => cancelAnimationFrame(raf);
  }, [hover]);
 
  const tipPos = useMemo(() => {
    if (!hover) return null;
    const vw = window.innerWidth,
      vh = window.innerHeight,
      d = 8;
    let left = hover.pageX + d,
      top = hover.pageY + d;
    Iif (left + tipSize.w > vw) left = hover.pageX - d - tipSize.w;
    Iif (top + tipSize.h > vh) top = hover.pageY - d - tipSize.h;
    return { left, top };
  }, [hover, tipSize]);
 
  return (
    <div ref={mapDivRef} className={styles.mapWrap}>
      {!bounds && (
        <div style={{ color: '#888', padding: 8 }}>No antenna coordinates available.</div>
      )}
 
      {bounds && (
        <svg
          ref={svgRef}
          className={styles.svg}
          data-geomap-export="map"
          viewBox={`${bounds.minX} ${bounds.minY} ${bounds.maxX - bounds.minX} ${bounds.maxY - bounds.minY}`}
          preserveAspectRatio="xMidYMid meet"
          aria-label="Antenna layout map"
          onWheel={onWheel}
          onPointerDown={onPointerDown}
          onPointerMove={onPointerMove}
          onPointerUp={onPointerUp}
          style={{ cursor: panning ? 'grabbing' : 'grab' }}
        >
          {/* World transform group (pan/zoom) */}
          <g transform={`translate(${t.x} ${t.y}) scale(${k})`}>
            {/* Shapes + fills + labels under nodes */}
            <SmartboxGroups
              antennas={props.antennas}
              pxToVb={pxToVb}
              zoomK={k}
              labelZoom={2}
              groupBy={props.groupBy}
            />
 
            {/* Antennas on top (cursor: pointer on hover) */}
            {props.showAntennas && (
              <AntennaNodes
                antennas={props.antennas}
                powerRange={props.powerRange}
                radiusMinPx={props.radiusMinPx}
                radiusMaxPx={props.radiusMaxPx}
                pxToVb={pxToVb}
                k={k}
                hoveredLabel={hover?.a.label ?? null}
                onEnter={(e, a) => setHover({ pageX: e.pageX, pageY: e.pageY, a })}
                onMove={(e, a) => setHover({ pageX: e.pageX, pageY: e.pageY, a })}
                onLeave={() => setHover(null)}
                colorScaleName={props.colorScaleName}
                customPalette={props.customPalette}
              />
            )}
          </g>
        </svg>
      )}
 
      {/* Zoom badge + controls */}
      <div className={styles.mapUiStack} data-skip-screenshot>
        <div className={styles.zoomBadge} aria-live="polite">
          {Math.round(k * 100)}%
        </div>
        <button
          type="button"
          className={styles.controlButton}
          onClick={resetTransform}
          disabled={!bounds}
          title="Reset zoom"
          aria-label="Reset view to initial zoom"
        >
          <FontAwesomeIcon icon={faRotateLeft} />
          <span className={styles.buttonLabel}>Reset view</span>
        </button>
      </div>
 
      {props.showTitleChipOnHover && props.titleChipText && (
        <>
          <div className={styles.titleChipDock} data-skip-screenshot>
            <div className={styles.titleChip}>
              <span>{props.titleChipText}</span>
            </div>
          </div>
          {props.onCompactGroupByChange && (
            <div className={styles.titleChipGroupByDock} data-skip-screenshot>
              <div className={styles.titleChipGroupBy} role="group" aria-label="Group map by">
                <button
                  type="button"
                  className={`${styles.groupByChip} ${
                    props.compactGroupBy === 'smartbox' ? styles.groupByChipActive : ''
                  }`}
                  onClick={() => props.onCompactGroupByChange?.('smartbox')}
                  aria-pressed={props.compactGroupBy === 'smartbox'}
                  title="Group outlines by smartbox"
                >
                  SB
                </button>
                <button
                  type="button"
                  className={`${styles.groupByChip} ${
                    props.compactGroupBy === 'tpm' ? styles.groupByChipActive : ''
                  }`}
                  onClick={() => props.onCompactGroupByChange?.('tpm')}
                  aria-pressed={props.compactGroupBy === 'tpm'}
                  title="Group outlines by TPM"
                >
                  TPM
                </button>
              </div>
            </div>
          )}
        </>
      )}
 
      {hover &&
        tipPos &&
        ReactDOM.createPortal(
          <div
            ref={tipRef}
            className={styles.tooltip}
            style={{ left: tipPos.left, top: tipPos.top }}
          >
            <table>
              <tbody>
                <tr>
                  <th>Label</th>
                  <td>{hover.a.label}</td>
                </tr>
                <tr>
                  <th>Smartbox</th>
                  <td>{hover.a.sb}</td>
                </tr>
                <tr>
                  <th>Port</th>
                  <td>{hover.a.port}</td>
                </tr>
                <tr>
                  <th>TPM</th>
                  <td>{hover.a.tpmFull}</td>
                </tr>
                <tr>
                  <th>FEM current (mA)</th>
                  <td>{hover.a.fem_current ?? '—'}</td>
                </tr>
                <tr>
                  <th>Tripped</th>
                  <td>{String(hover.a.tripped ?? false)}</td>
                </tr>
                <tr>
                  <th>ADC X / Y</th>
                  <td>
                    {fmt2(hover.a.pwr_x)} / {fmt2(hover.a.pwr_y)}
                  </td>
                </tr>
                <tr>
                  <th>Atten X / Y (dB)</th>
                  <td>
                    {fmt2(hover.a.atten_x)} / {fmt2(hover.a.atten_y)}
                  </td>
                </tr>
                <tr>
                  <th>chX / chY</th>
                  <td>
                    {hover.a.chX ?? '—'} / {hover.a.chY ?? '—'}
                  </td>
                </tr>
                <tr>
                  <th>East / North (m)</th>
                  <td>
                    {fmt2(hover.a.east)} / {fmt2(hover.a.north)}
                  </td>
                </tr>
              </tbody>
            </table>
          </div>,
          document.body
        )}
    </div>
  );
}