<commit_before>import { useEffect, useState, useMemo, useRef } from "react";
import { useSelector } from "react-redux";
import * as sel from "selectors";
import { linkList, TREZOR_KEY, LN_KEY, DEX_KEY } from "./Links";
import { useHistory } from "react-router-dom";
import { FormattedMessage as T } from "react-intl";

export function useMenuLinks() {
  const location = useSelector(sel.location);
  const sidebarOnBottom = useSelector(sel.sidebarOnBottom);
  const expandSideBar = useSelector(sel.expandSideBar);
  const isTrezor = useSelector(sel.isTrezor);
  const lnEnabled = useSelector(sel.lnEnabled);
  const isSPV = useSelector(sel.isSPV);

  const newActiveVoteProposalsCount = useSelector(
    sel.newActiveVoteProposalsCount
  );
  const newPreVoteProposalsCount = useSelector(sel.newPreVoteProposalsCount);
  const newProposalsStartedVoting = useSelector(sel.newProposalsStartedVoting);

  const notifProps = useMemo(
    () => ({
      newActiveVoteProposalsCount,
      newPreVoteProposalsCount,
      newProposalsStartedVoting
    }),
    [
      newActiveVoteProposalsCount,
      newPreVoteProposalsCount,
      newProposalsStartedVoting
    ]
  );

  const prepareLinkList = () => {
    const useDexSpvExperimental = useSelector(sel.useDexSpvExperimental);
    let links = linkList;
    if (!isTrezor) {
      links = links.filter((l) => l.key !== TREZOR_KEY);
    }
    if (!lnEnabled) {
      links = links.filter((l) => l.key !== LN_KEY);
    }
    if (isTrezor) {
      links = links.filter((l) => l.key !== DEX_KEY);
    }
    if (isSPV) {
      links = links.map((l) => {
        if (l.key === DEX_KEY && !useDexSpvExperimental) {
          l.disabled = true;
          l.tooltip = (
            <T
              id="sidebar.link.disabledDexTooltip"
              m="DEX not available while using SPV. Please go to settings and disable SPV to access the DEX."
            />
          );
        }
        return l;
      });
    }

    return links;
  };

  const links = useRef(prepareLinkList());
  const menuLinks = useMemo(() => {
    return links.current.map((link) => {
      return { ...link, notifProp: notifProps[link.notifProp] };
    });
  }, [notifProps]);

  const [activeTabIndex, setActiveTabIndex] = useState(-1);
  const history = useHistory();
  const onSelectTab = (index) => {
    if (!menuLinks[index].disabled) {
      setActiveTabIndex(index);
      history.push(menuLinks[index].path);
    }
  };

  useEffect(() => {
    const tabbedPageCheck = location.pathname.indexOf("/", 1);
    const selectedTabAux =
      tabbedPageCheck > 0
        ? location.pathname.substring(0, tabbedPageCheck)<commit_msg>[sidebar] refresh menu links after toggling SPV mode (#3702)<commit_after>import { useEffect, useState, useMemo } from "react";
import { useSelector } from "react-redux";
import * as sel from "selectors";
import { linkList, TREZOR_KEY, LN_KEY, DEX_KEY } from "./Links";
import { useHistory } from "react-router-dom";
import { FormattedMessage as T } from "react-intl";
import { cloneDeep } from "fp";

export function useMenuLinks() {
  const location = useSelector(sel.location);
  const sidebarOnBottom = useSelector(sel.sidebarOnBottom);
  const expandSideBar = useSelector(sel.expandSideBar);
  const isTrezor = useSelector(sel.isTrezor);
  const lnEnabled = useSelector(sel.lnEnabled);
  const isSPV = useSelector(sel.isSPV);

  const newActiveVoteProposalsCount = useSelector(
    sel.newActiveVoteProposalsCount
  );
  const newPreVoteProposalsCount = useSelector(sel.newPreVoteProposalsCount);
  const newProposalsStartedVoting = useSelector(sel.newProposalsStartedVoting);
  const useDexSpvExperimental = useSelector(sel.useDexSpvExperimental);

  const notifProps = useMemo(
    () => ({
      newActiveVoteProposalsCount,
      newPreVoteProposalsCount,
      newProposalsStartedVoting
    }),
    [
      newActiveVoteProposalsCount,
      newPreVoteProposalsCount,
      newProposalsStartedVoting
    ]
  );

  const menuLinks = useMemo(() => {
    let links = cloneDeep(linkList);
    if (!isTrezor) {
      links = links.filter((l) => l.key !== TREZOR_KEY);
    }
    if (!lnEnabled) {
      links = links.filter((l) => l.key !== LN_KEY);
    }
    if (isTrezor) {
      links = links.filter((l) => l.key !== DEX_KEY);
    }
    if (isSPV) {
      links = links.map((l) => {
        if (l.key === DEX_KEY && !useDexSpvExperimental) {
          l.disabled = true;
          l.tooltip = (
            <T
              id="sidebar.link.disabledDexTooltip"
              m="DEX not available while using SPV. Please go to settings and disable SPV to access the DEX."
            />
          );
        }
        return l;
      });
    }
    return links.map((link) => {
      return { ...link, notifProp: notifProps[link.notifProp] };
    });
  }, [notifProps, isTrezor, lnEnabled, isSPV, useDexSpvExperimental]);

  const [activeTabIndex, setActiveTabIndex] = useState(-1);
  const history = useHistory();
  const onSelectTab = (index) => {
    if (!menuLinks[index].disabled) {
      setActiveTabIndex(index);
      history.push(menuLinks[index].path);
    }
  };

  useEffect(() => {
    const tabbedPageCheck = location.pathname.indexOf("/", 1);
    const selectedTabAux =
      tabbedPageCheck > 0
        ? location.pathname.substring(0, tabbedPageCheck)
{'fraction_non_alphanumeric': 0.12413145539906104, 'fraction_numerical': 0.0022535211267605635, 'mean_word_length': 3.1222910216718267, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3004316', 'n_tokens_mistral': 1748, 'n_tokens_neox': 1722, 'n_words': 349}