All files / src/apps/concepts/components ViewConceptsHeader.tsx

8% Statements 2/25
0% Branches 0/18
0% Functions 0/10
8% Lines 2/25

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                                                    1x                                                           1x                                                                                                                                                                                                                                                          
import React, { useEffect, useState } from "react";
import { Link } from "react-router-dom";
import Header from "../../../components/Header";
import {
  DICTIONARY_CONTAINER,
  DICTIONARY_VERSION_CONTAINER,
  SOURCE_CONTAINER
} from "../constants";
import { getContainerIdFromUrl } from "../utils";
import { Button, Menu, MenuItem, TextField, Theme } from "@mui/material";
import { PREFERRED_SOURCES_VIEW_ONLY, useAnchor } from "../../../utils";
import { APISource } from "../../sources";
import { AccountTreeOutlined, FolderOpen } from "@mui/icons-material";
import { APIDictionary } from "../../dictionaries/types";
import { createStyles, makeStyles } from "@mui/styles";
 
interface Props {
  containerType: string;
  containerUrl?: string;
  gimmeAUrl: Function;
  addConceptToDictionary?: string;
  children?: React.ReactNode[];
  sources: APISource[];
  dictionaries: APIDictionary[];
}
 
const useStyles = makeStyles((theme: Theme) =>
  createStyles({
    lightColour: {
      color: "white !important"
    },
    textField: {
      padding: "0.2rem 1rem",
      cursor: "none"
    },
    input: {
      cursor: "pointer",
      borderBottom: "1px dotted black",
      paddingBottom: "0.25rem"
    },
    underline: {
      "&&&:before": {
        borderBottom: "none"
      },
      "&&:after": {
        borderBottom: "none"
      }
    },
    sourceIcon: {
      marginRight: "0.2rem",
      fill: "#8080809c",
      color:"#000000ad"
    }
  })
);
 
const ViewConceptsHeader: React.FC<Props> = ({
  containerType,
  containerUrl,
  gimmeAUrl,
  addConceptToDictionary,
  children,
  sources,
  dictionaries
}) => {
  const [showSources, setShowSources] = useState(false);
  const [preferredSources, setPreferredSources] = useState<
    { name: string; url: string }[]
  >();
  useEffect(() => {
    const defaultSources = Object.entries(
      PREFERRED_SOURCES_VIEW_ONLY
    ).map(([key, value]) => ({ name: key, url: value }));
    if (showSources) {
      const allSources = defaultSources
        .concat(sources.map(s => ({ name: s.name, url: s.url })))
        .concat(dictionaries.map(d => ({ name: d.name, url: d.url })));
      setPreferredSources(allSources);
    } else setPreferredSources(defaultSources);
  }, [showSources, sources, dictionaries]);
 
  const classes = useStyles();
  const isSourceContainer = containerType === SOURCE_CONTAINER;
  const isAddToDictionary = isSourceContainer && !!addConceptToDictionary;
  const [
    switchSourceAnchor,
    handleSwitchSourceClick,
    handleSwitchSourceClose
  ] = useAnchor();
 
  const getTitleBasedOnContainerType = () => {
    return isAddToDictionary
      ? `Import existing concept from ${getContainerIdFromUrl(containerUrl)}`
      : `Concepts in ${
          containerType === DICTIONARY_VERSION_CONTAINER ? "v" : ""
        }${getContainerIdFromUrl(containerUrl)}`;
  };
 
  const showSwitchSourceBasedOnContainerType = () => {
    return !isAddToDictionary ? null : (
      <>
        <Button
          data-testid="switch-source"
          className={classes.lightColour}
          variant="text"
          size="large"
          aria-haspopup="true"
          onClick={handleSwitchSourceClick}
        >
          Switch source (Currently {getContainerIdFromUrl(containerUrl)})
        </Button>
        <Menu
          PaperProps={{
            style: {
              marginTop: "30px",
              marginLeft: "10px"
            }
          }}
          anchorEl={switchSourceAnchor}
          keepMounted
          open={Boolean(switchSourceAnchor)}
          onClose={handleSwitchSourceClose}
        >
          <TextField
            multiline
            className={classes.textField}
            InputProps={{
              className: classes.underline
            }}
            inputProps={{
              className: classes.input
            }}
            value={
              showSources
                ? "Choose a source/dictionary"
                : "Select a different source/dictionary"
            }
            onClick={() => setShowSources(!showSources)}
          />
          {preferredSources?.map(({ name, url }) => (
            <MenuItem
              // replace because we want to keep the back button useful
              replace
              to={gimmeAUrl({}, `${url}concepts/`)}
              key={name}
              component={Link}
              onClick={handleSwitchSourceClose}
              data-testid={name}
            >
              {url?.includes("/collection") ? (
                <FolderOpen className={classes.sourceIcon} />
              ) : (
                <AccountTreeOutlined className={classes.sourceIcon} />
              )}
              {name}
            </MenuItem>
          ))}
        </Menu>
      </>
    );
  };
 
  return (
    <Header
      title={getTitleBasedOnContainerType()}
      headerComponent={showSwitchSourceBasedOnContainerType()}
      // we can only be confident about the back url when viewing a collection's concepts
      allowImplicitNavigation
      backUrl={
        containerType !== DICTIONARY_CONTAINER ? undefined : containerUrl
      }
      backText={
        containerType === SOURCE_CONTAINER ? undefined : "Back to dictionary"
      }
    >
      {children}
    </Header>
  );
};
 
export default ViewConceptsHeader;