Advertisement
haddy315

concepts/components/viewConceptsHeader.tsx

May 7th, 2021
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. import React from "react";
  2. import { Link } from "react-router-dom";
  3. import Header from "../../../components/Header";
  4. import {
  5. DICTIONARY_CONTAINER,
  6. DICTIONARY_VERSION_CONTAINER,
  7. SOURCE_CONTAINER
  8. } from "../constants";
  9. import { getContainerIdFromUrl } from "../utils";
  10. import {
  11. Button,
  12. createStyles,
  13. makeStyles,
  14. Menu,
  15. MenuItem,
  16. Theme
  17. } from "@material-ui/core";
  18. import { PREFERRED_SOURCES_VIEW_ONLY, useAnchor } from "../../../utils";
  19. import { APISource } from "../../sources";
  20.  
  21. interface Props {
  22. containerType: string;
  23. containerUrl?: string;
  24. gimmeAUrl: Function;
  25. addConceptToDictionary?: string;
  26. children?: React.ReactNode[];
  27. sources?:APISource[]
  28. }
  29.  
  30. const useStyles = makeStyles((theme: Theme) =>
  31. createStyles({
  32. lightColour: {
  33. color: theme.palette.background.default
  34. }
  35. })
  36. );
  37.  
  38. const ViewConceptsHeader: React.FC<Props> = ({
  39. containerType,
  40. containerUrl,
  41. gimmeAUrl,
  42. addConceptToDictionary,
  43. children,
  44. sources
  45. }) => {
  46. const classes = useStyles();
  47. const isSourceContainer = containerType === SOURCE_CONTAINER;
  48. const isAddToDictionary = isSourceContainer && !!addConceptToDictionary;
  49.  
  50. const [
  51. switchSourceAnchor,
  52. handleSwitchSourceClick,
  53. handleSwitchSourceClose
  54. ] = useAnchor();
  55.  
  56. const getTitleBasedOnContainerType = () => {
  57. return isAddToDictionary
  58. ? `Import existing concept from ${getContainerIdFromUrl(containerUrl)}`
  59. : `Concepts in ${
  60. containerType === DICTIONARY_VERSION_CONTAINER ? "v" : ""
  61. }${getContainerIdFromUrl(containerUrl)}`;
  62. };
  63.  
  64. const showSwitchSourceBasedOnContainerType = () => {
  65. return !isAddToDictionary ? null : (
  66. <>
  67. <Button
  68. data-testid="switch-source"
  69. className={classes.lightColour}
  70. variant="text"
  71. size="large"
  72. aria-haspopup="true"
  73. onClick={handleSwitchSourceClick}
  74. >
  75. Switch source (Currently {getContainerIdFromUrl(containerUrl)})
  76. </Button>
  77. <Menu
  78. anchorEl={switchSourceAnchor}
  79. keepMounted
  80. open={Boolean(switchSourceAnchor)}
  81. onClose={handleSwitchSourceClose}
  82. >
  83. {sources?.map(
  84. ({name, url}) => (
  85. <MenuItem
  86. // replace because we want to keep the back button useful
  87. replace
  88. to={gimmeAUrl({}, `${url}concepts/`)}
  89. key={name}
  90. component={Link}
  91. onClick={handleSwitchSourceClose}
  92. data-testid={name}
  93. >
  94. {name}
  95. </MenuItem>
  96. )
  97. )}
  98. </Menu>
  99. </>
  100. );
  101. };
  102.  
  103. return (
  104. <Header
  105. title={getTitleBasedOnContainerType()}
  106. headerComponent={showSwitchSourceBasedOnContainerType()}
  107. // we can only be confident about the back url when viewing a collection's concepts
  108. allowImplicitNavigation
  109. backUrl={
  110. containerType !== DICTIONARY_CONTAINER ? undefined : containerUrl
  111. }
  112. backText={
  113. containerType === SOURCE_CONTAINER ? undefined : "Back to dictionary"
  114. }
  115. >
  116. {children}
  117. </Header>
  118. );
  119. };
  120.  
  121. export default ViewConceptsHeader;
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement