Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

10.0 KB
Raw Blame
import { useEffect, useState } from 'react';
import { createPortal } from 'react-dom';
import PropTypes from 'prop-types';
import {
  Joyride,
  STATUS,
  ACTIONS,
  EVENTS,
  LIFECYCLE,
} from 'react-joyride';
import util from '../../util/util';
import { JOYRIDE_INCREMENT } from '../../util/constants';

const { events } = util;
const placeholderElements = [];
let key = 0;
let joyrideProps;

/**
 *
 * @param {*} props
 */
export default function JoyrideWrapper ({
  tourSteps, currentTourStep, map, proj, tourComplete, resetProductPicker,
}) {
  if (!map) return null;
  const currentStepObj = tourSteps[currentTourStep - 1];
  const { stepLink } = currentStepObj;
  const projParam = stepLink.split('&').filter((param) => param.includes('p='));
  const stepProj = projParam.length ? projParam[0].substr(2) : 'geographic';
  const projMatches = stepProj === proj;
  const styles = {
    buttonClose: {
      height: 14,
      padding: 15,
      width: 14,
    },
    buttonPrimary: {
      color: 'rgb(255, 255, 255)',
    },
    overlay: {
      transition: 'none',
    },
    tooltip: {
      padding: 15,
    },
    tooltipContent: {
      padding: '20px 10px',
    },
    tooltipFooter: {
      marginTop: 15,
    },
  };

  // Joyride config properties.  Some can also be configured per-step.
  // See https://docs.react-joyride.com/step#common-props-inheritance for more info.
  const {
    continuous,
    disableOverlayClose,
    spotlightClicks,
    steps,
    hideCloseButton,
    eventTriggersIncrement,
  } = (currentStepObj || {}).joyride || {};

  const hasTargetCoordinates = !!(steps || []).find((s) => s?.targetCoordinates);

  const [, setElementPositionKey] = useState(key);
  const [stepIndex, setStepIndex] = useState();
  const [run, setRun] = useState(false);
  const [isInitializing, setIsInitializing] = useState(true);
  const [hasJoyrideOverlay, setHasJoyrideOverlay] = useState(false);
  const [overlayStarted, setOverlayStarted] = useState(false);

  const incrementKey = () => {
    key += 1;
    setElementPositionKey(key);
  };

  function updatePlaceholderLocations() {
    (steps || []).forEach((step) => {
      const { target, targetCoordinates } = step || {};
      if (target && targetCoordinates) {
        const placeholderEl = document.querySelector(target);
        if (placeholderEl) {
          setPlaceholderLocation(placeholderEl, targetCoordinates);
        }
      }
    });
  }

  const currentJoyrideStep = steps && steps[stepIndex];

  if (currentJoyrideStep) {
    const { hideNextButton } = currentJoyrideStep;
    if (hideNextButton) styles.buttonNext = { display: 'none' };
    if (hideCloseButton) styles.buttonClose = { display: 'none' };
  }

  // Allow triggering step increment via event
  useEffect(() => {
    const incrementStep = () => {
      if (run && eventTriggersIncrement) setStepIndex(stepIndex + 1);
    };
    events.on(JOYRIDE_INCREMENT, incrementStep);
    return () => {
      events.off(JOYRIDE_INCREMENT, incrementStep);
    };
  }, [run, eventTriggersIncrement, stepIndex]);

  // For the tutorial tour, we need to reset the product picker to initial state
  useEffect(() => {
    if (eventTriggersIncrement) {
      resetProductPicker();
    }
  }, [currentTourStep]);

  /**
   * Set a placeholder DOM element's position based on map coords
   * @param {*} el
   * @param {*} targetCoordinates
   */
  function setPlaceholderLocation (el, targetCoordinates) {
    const element = { ...el };
    const { topLeft, bottomRight } = targetCoordinates;
    let [x1, y1] = map.getPixelFromCoordinate(topLeft) || [0, 0];
    let [x2, y2] = map.getPixelFromCoordinate(bottomRight) || [0, 0];
    x1 = x1.toFixed(0);
    y1 = y1.toFixed(0);
    x2 = x2.toFixed(0);
    y2 = y2.toFixed(0);
    element.style.top = `${y1}px`;
    element.style.left = `${x1}px`;
    element.style.height = `${y2 - y1}px`;
    element.style.width = `${x2 - x1}px`;
  }

  /**
   * Add placeholder DOM elements based on map coordinates so
   * Joyride can place a beacon on them
   */
  function addPlaceholderElements() {
    (steps || []).forEach((step) => {
      const { target, targetCoordinates } = step || {};
      const existingEl = document.querySelector(target);
      if (map && target && targetCoordinates && !existingEl) {
        const placeholderEl = document.createElement('span');
        placeholderEl.id = target.substr(1, target.length);
        placeholderEl.style.position = 'absolute';
        placeholderEl.style.zIndex = '0';
        placeholderEl.style.pointerEvents = 'none';
        setPlaceholderLocation(placeholderEl, targetCoordinates);
        document.querySelector('body').appendChild(placeholderEl);
        placeholderElements.push(placeholderEl);
      }
    });
  }

  /**
   * When the map is moved, zoomed, or the browser is resized,
   * any placeholder DOM elements being used as Joyride targets
   * need to have their positiions updated.
   */
  function updateTargetsOnResize() {
    const { status, action } = joyrideProps || {};
    if (
      status === STATUS.FINISHED ||
      action === ACTIONS.RESET ||
      !(steps && steps.length)
    ) {
      return;
    }
    if (!hasTargetCoordinates) return;
    updatePlaceholderLocations();
    setIsInitializing(false);
    // Force a re-render so that Joyride updates the beacon location,
    // otherwise it doesn't know the DOM element position was updated
    incrementKey();
  }

  const joyrideStateCallback = (data) => {
    joyrideProps = data;
    const {
      action, index, type, status,
    } = data;

    if (data.lifecycle === LIFECYCLE.TOOLTIP) setOverlayStarted(true);

    if ([EVENTS.STEP_AFTER, EVENTS.TARGET_NOT_FOUND].includes(type)) {
      const newIndex = index + (action === ACTIONS.PREV ? -1 : 1);
      setStepIndex(newIndex);
      if (
        hasTargetCoordinates &&
        newIndex >= 0 &&
        newIndex < steps.length &&
        steps[newIndex].targetCoordinates
      ) {
        // Let Joyride update its internal state first, then reposition placeholders.
        requestAnimationFrame(() => updateTargetsOnResize());
      }
    }
    if ([STATUS.FINISHED, STATUS.SKIPPED].includes(status)) {
      setStepIndex(0);
      setRun(false);
      setOverlayStarted(false);
    }
  };

  /**
   * Forcing a re-render on a target resize (by calling incrementKey())
   * was causing the beacon to be skipped due to this line in the
   * Joyride Step component: https://github.com/gilbarbara/react-joyride/blob/2a40561e698f71b3a5c10e018eb7b95f5a797555/src/components/Step.js#L109
   *
   * If any tour step has a Joyride step beyond the first which inicludes
   * targetCoordinates, it cannot be run as continuous.
   */
  function checkContinuous () {
    let isContinuous = continuous;
    (steps || []).forEach((step, index) => {
      if (index > 0 && step.targetCoordinates) isContinuous = false;
    });
    return isContinuous;
  }

  // Handle effects related to changing the tour step
  useEffect(() => {
    addPlaceholderElements();
    updatePlaceholderLocations();
    setIsInitializing(false);
    setOverlayStarted(false);
    if (steps && steps.length) {
      setStepIndex(0);
      setRun(true);
    } else {
      setStepIndex(undefined);
      setRun(false);
    }
  }, [currentTourStep]);

  // Force re-render on projection change to reset Joyride
  useEffect(incrementKey, [proj]);

  // Register/de-register evnt listeners for map changes
  useEffect(() => {
    if (!hasTargetCoordinates) return undefined;
    map.getView().on('change', updateTargetsOnResize);
    return () => map.getView().un('change', updateTargetsOnResize);
  }, [map, hasTargetCoordinates]);

  // Joyride's overlay can briefly unmount between steps; keep a fallback overlay
  // visible during those gaps to prevent a distracting flash.
  useEffect(() => {
    if (!run || !projMatches || isInitializing) {
      setHasJoyrideOverlay(false);
      setOverlayStarted(false);
      return undefined;
    }

    const getOverlayPresent = () => !!document.querySelector('.react-joyride__overlay');
    const updateOverlayState = () => setHasJoyrideOverlay(getOverlayPresent());

    updateOverlayState();

    const observer = new MutationObserver(updateOverlayState);
    observer.observe(document.body, {
      subtree: true,
      childList: true,
      attributes: true,
      attributeFilter: ['class', 'style'],
    });

    return () => observer.disconnect();
  }, [run, projMatches, isInitializing]);

  // When tour is complete, remove all placeholder elements
  useEffect(() => {
    if (tourComplete) {
      placeholderElements.forEach((element) => element.remove());
    }
  });

  return !projMatches || isInitializing
    ? null
    : (
      <>
        {run && overlayStarted && !hasJoyrideOverlay && typeof document !== 'undefined' && (
          createPortal(
            <div className="react-joyride__overlay-fallback" />,
            document.body,
          )
        )}
        <Joyride
          run={run}
          stepIndex={stepIndex}
          steps={steps || []}
          continuous={checkContinuous()}
          onEvent={joyrideStateCallback}
          styles={styles}
          floatingOptions={{
            disableAnimation: true,
          }}
          options={{
            blockTargetInteraction: !spotlightClicks,
            hideOverlay: !overlayStarted,
            overlayClickAction: disableOverlayClose ? false : 'close',
            arrowColor: '#eee',
            backgroundColor: '#eee',
            beaconSize: 50,
            overlayColor: 'rgba(0, 0, 0, 0.5)',
            primaryColor: '#d54e21',
            spotlightShadow: '0 0 25px rgba(0, 0, 0, 0.75)',
            lineHeight: '16px',
            textColor: '#333',
            width: undefined,
            zIndex: 2000,
            skipScroll: true,
            buttons: ['back', 'close', 'primary'],
          }}
        />
      </>
    );
}

JoyrideWrapper.propTypes = {
  currentTourStep: PropTypes.number,
  map: PropTypes.oneOfType([PropTypes.object, PropTypes.oneOf(['null'])]),
  proj: PropTypes.string,
  tourComplete: PropTypes.bool,
  tourSteps: PropTypes.oneOfType([PropTypes.array, PropTypes.oneOf(['null'])]),
  resetProductPicker: PropTypes.func,
};