HelpMenu.tsx 4.22 KB
Newer Older
Andrey Azov's avatar
Andrey Azov committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/**
 * See the NOTICE file distributed with this work for additional information
 * regarding copyright ownership.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import React, { useEffect, useState, useRef } from 'react';
import classNames from 'classnames';
import { useDispatch } from 'react-redux';
import { push } from 'connected-react-router';
import { Link } from 'react-router-dom';

Andrey Azov's avatar
Andrey Azov committed
23
import Chevron from 'src/shared/components/chevron/Chevron';
Andrey Azov's avatar
Andrey Azov committed
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

import {
  Menu as MenuType,
  MenuItem
} from 'src/shared/types/help-and-docs/menu';

import styles from './HelpMenu.scss';

export type Props = {
  menu: MenuType;
  currentUrl: string;
};

const HelpMenu = (props: Props) => {
  const [submenuItems, setSubmenuItems] = useState<MenuItem[] | null>(null);
  const clickedMenuRef = useRef<number | null>(null);

  const toggleMegaMenu = (items: MenuItem[], menuIndex: number) => {
    let nextValue = null;
    if (
      clickedMenuRef.current === null ||
      clickedMenuRef.current !== menuIndex
    ) {
      // clicking on a menu item for the first time
      clickedMenuRef.current = menuIndex;
      nextValue = items;
    } else {
      // this means a repeated click on the same menu iteem
      clickedMenuRef.current = null;
    }

    setSubmenuItems(nextValue);
  };

  const closeMegaMenu = () => {
    setSubmenuItems(null);
    clickedMenuRef.current = null;
  };

  const topLevelItems = props.menu.items.map((item, index) => {
    const className = classNames(styles.topMenuItem);
    const commonProps = {
      key: index,
      className
    };
    return item.type === 'collection' ? (
      <span {...commonProps} onClick={() => toggleMegaMenu(item.items, index)}>
        {item.name}
      </span>
    ) : (
      <Link {...commonProps} to={item.url} onClick={closeMegaMenu}>
        {item.name}
      </Link>
    );
  });

  return (
    <div className={styles.helpMenu}>
      <div className={styles.menuBar}>{topLevelItems}</div>
      {submenuItems && (
        <>
          <div className={styles.expandedMenuPanel}>
            <Submenu items={submenuItems} onLinkClick={closeMegaMenu} />
          </div>
          <div
            className={styles.backdrop}
            onMouseEnter={closeMegaMenu}
            onClick={closeMegaMenu}
          />
        </>
      )}
    </div>
  );
};

type SubmenuProps = {
  items: MenuItem[];
  onLinkClick: () => void;
};
const Submenu = (props: SubmenuProps) => {
  const [childItems, setChildItems] = useState<MenuItem[] | null>(null);
  const dispatch = useDispatch();

  useEffect(() => {
    setChildItems(null);
  }, [props.items]);

  const onLinkClick = (url: string) => {
    // hopefully, the url is an internal one;
    // might need extra logic if we can have external urls in the menu
    props.onLinkClick();
    dispatch(push(url));
  };

  const renderedMenuItems = props.items.map((item, index) => {
    const className = classNames(styles.submenuItem);
    const props: Record<string, unknown> = {};
    if (item.type === 'collection') {
      props.onMouseOver = () => setChildItems(item.items);
    } else {
      props.onMouseOver = () => setChildItems(null);
      props.onClick = () => onLinkClick(item.url);
    }
    return (
      <li key={index} {...props} className={className}>
        {item.type === 'collection' ? (
          <>
            {item.name}
Andrey Azov's avatar
Andrey Azov committed
132
            <Chevron direction="right" classNames={{ svg: styles.chevron }} />
Andrey Azov's avatar
Andrey Azov committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
          </>
        ) : (
          item.name
        )}
      </li>
    );
  });

  const renderedSubmenu = (
    <ul className={styles.submenu}>{renderedMenuItems}</ul>
  );

  return childItems ? (
    <>
      {renderedSubmenu}
      <Submenu items={childItems} onLinkClick={props.onLinkClick} />
    </>
  ) : (
    renderedSubmenu
  );
};

export default HelpMenu;