TrackPanelBarIcon.tsx 1.34 KB
Newer Older
Imran Salam's avatar
Imran Salam committed
1
import React, { FunctionComponent, memo, useState, useCallback } from 'react';
2 3 4 5 6
import { TrackPanelBarItem } from './trackPanelBarConfig';

import styles from './TrackPanelBarIcon.scss';

type TrackPanelBarIconProps = {
Imran Salam's avatar
Imran Salam committed
7
  closeTrackPanelModal: () => void;
8
  iconConfig: TrackPanelBarItem;
Imran Salam's avatar
Imran Salam committed
9 10
  openTrackPanelModal: (trackPanelModalView: string) => void;
  trackPanelModalView: string;
11 12 13
};

const TrackPanelBarIcon: FunctionComponent<TrackPanelBarIconProps> = memo(
Imran Salam's avatar
Imran Salam committed
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
  (props: TrackPanelBarIconProps) => {
    const [toggleState, setToggleState] = useState(false);

    const toggleModalView = useCallback(() => {
      const newToggleState: boolean = !toggleState;

      if (newToggleState === true) {
        props.openTrackPanelModal(props.iconConfig.name);
      } else {
        props.closeTrackPanelModal();
      }

      setToggleState(newToggleState);
    }, [props.iconConfig.name, toggleState]);

    const getViewIcon = () => {
      const { iconConfig, trackPanelModalView } = props;

      return iconConfig.name === trackPanelModalView
        ? iconConfig.icon.selected
        : iconConfig.icon.default;
    };

    return (
      <dt className={styles.barIcon}>
        <button onClick={toggleModalView}>
          <img src={getViewIcon()} alt={props.iconConfig.description} />
        </button>
      </dt>
    );
  }
45 46 47
);

export default TrackPanelBarIcon;