From 5671495bb44d3be088c881920280b210bf827945 Mon Sep 17 00:00:00 2001 From: Yuliang Weng <59968766+yld-weng@users.noreply.github.com> Date: Tue, 8 Nov 2022 12:22:28 +0000 Subject: [PATCH] fix: issuesreported in sonar cloud --- .../flowChart/flowChart.jsx | 4 +- .../components/datasetSummary.jsx | 2 +- gatsby-node.js | 4 +- src/components/blog/menuTag.jsx | 12 +- src/components/blog/menuTagSlide.jsx | 16 ++- src/components/blog/postPagination.jsx | 2 +- src/components/home/eventNotice.jsx | 6 +- src/components/home/latestPost.jsx | 2 +- src/components/home/searchHome.jsx | 6 +- src/components/shared/header.jsx | 17 +-- src/components/shared/scrollTop.jsx | 30 ++--- src/components/shared/search.jsx | 50 ++++---- src/components/visualisation/utils.js | 108 ++++++++++-------- src/components/visualisation/visDownload.jsx | 6 +- src/components/visualisation/visEmbed.jsx | 12 +- src/components/visualisation/visLayout.jsx | 10 +- src/components/visualisation/visUtils.jsx | 12 +- src/pages/events.jsx | 6 +- src/pages/index.jsx | 8 +- src/templates/blog/blogCategoryTemplate.jsx | 38 +++--- src/templates/blog/blogPostTemplate.jsx | 16 +-- src/templates/blog/blogPostTemplateCustom.jsx | 48 ++++---- src/templates/blog/blogTagTemplate.jsx | 38 +++--- src/templates/blog/blogTemplate.jsx | 12 +- src/templates/docs/docsTemplate.jsx | 8 +- src/templates/docs/docsTemplateCustom.jsx | 48 ++++---- .../visualisation/visCategoryTemplate.jsx | 81 +++++++------ .../visualisation/visItemTemplate.jsx | 6 +- .../visualisation/visTagTemplate.jsx | 13 ++- src/templates/visualisation/visTemplate.jsx | 13 ++- src/utils/hooks/backgroundMovement.jsx | 6 +- src/utils/hooks/loadMoreVisualisation.jsx | 37 +++--- src/utils/hooks/trackScrollPosition.jsx | 4 +- src/utils/hooks/trackTableOfContent.jsx | 59 +++++----- 34 files changed, 393 insertions(+), 347 deletions(-) diff --git a/content/visualisation/2021-08-04-Which-Statistical-Test-To-Use-For-Two-Variables/flowChart/flowChart.jsx b/content/visualisation/2021-08-04-Which-Statistical-Test-To-Use-For-Two-Variables/flowChart/flowChart.jsx index 4a27e6fa9..0b117a0e2 100644 --- a/content/visualisation/2021-08-04-Which-Statistical-Test-To-Use-For-Two-Variables/flowChart/flowChart.jsx +++ b/content/visualisation/2021-08-04-Which-Statistical-Test-To-Use-For-Two-Variables/flowChart/flowChart.jsx @@ -69,7 +69,7 @@ const connectionLineStyle = { stroke: "#fff" }; * Return flowchart * @returns *************************/ -const flowChart = () => { +const FlowChart = () => { const [displayChart, setDisplayChart] = useState(false); const [showAll, toggleShowAll] = useState(false); const [centreOnClick, toggleCentre] = useState(true); @@ -629,4 +629,4 @@ const flowChart = () => { ); }; -export default flowChart; +export default FlowChart; diff --git a/content/visualisation/2022-01-31-ORDA-datasets-FAIRness-assessment/components/datasetSummary.jsx b/content/visualisation/2022-01-31-ORDA-datasets-FAIRness-assessment/components/datasetSummary.jsx index cc31d4711..ed1e78ba7 100644 --- a/content/visualisation/2022-01-31-ORDA-datasets-FAIRness-assessment/components/datasetSummary.jsx +++ b/content/visualisation/2022-01-31-ORDA-datasets-FAIRness-assessment/components/datasetSummary.jsx @@ -182,7 +182,7 @@ const DatasetSummary = ({ currentDataset }) => { }; if (loading) { -
Loading...
; + return
Loading...
; } return ( diff --git a/gatsby-node.js b/gatsby-node.js index 383a8f08f..91462c589 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -332,7 +332,7 @@ exports.createPages = async ({ graphql, actions, reporter }) => { }); const POSTS_PER_PAGE = 12; - var numPages = posts.length; + let numPages = posts.length; const categories = []; const tags = []; @@ -341,7 +341,7 @@ exports.createPages = async ({ graphql, actions, reporter }) => { // Call `createPage` for each result/post posts.forEach(({ node }, index, arr) => { - var excluded = false; + let excluded = false; // For each post, add their tags/categories to arrays node.frontmatter.category && diff --git a/src/components/blog/menuTag.jsx b/src/components/blog/menuTag.jsx index 42712109d..b30e5c1d6 100644 --- a/src/components/blog/menuTag.jsx +++ b/src/components/blog/menuTag.jsx @@ -6,10 +6,10 @@ import { FaTags, FaAngleDown } from "react-icons/fa"; import { FiSearch } from "react-icons/fi"; import { useLocation } from "@gatsbyjs/reach-router"; -const menuTag = ({ pageContext, handleTagMenu, tagMenu }) => { +const MenuTag = ({ pageContext, handleTagMenu, tagMenu }) => { const [filterTag, setfilterTag] = useState(pageContext.allTags); - var { href } = useLocation(); + let { href } = useLocation(); if (!href) href = ""; function searchTag(e) { @@ -86,8 +86,10 @@ const menuTag = ({ pageContext, handleTagMenu, tagMenu }) => { ); }; -export default menuTag; +export default MenuTag; -menuTag.propTypes = { - pageContext: PropTypes.any +MenuTag.propTypes = { + pageContext: PropTypes.any, + handleTagMenu: PropTypes.func, + tagMenu: PropTypes.bool }; diff --git a/src/components/blog/menuTagSlide.jsx b/src/components/blog/menuTagSlide.jsx index 3c2678bc1..7facba460 100644 --- a/src/components/blog/menuTagSlide.jsx +++ b/src/components/blog/menuTagSlide.jsx @@ -7,10 +7,10 @@ import { FiSearch } from "react-icons/fi"; import { IoMdClose } from "react-icons/io"; import { useLocation } from "@gatsbyjs/reach-router"; -const menuTagSlide = ({ pageContext, handleTagMenu, tagMenu }) => { +const MenuTagSlide = ({ pageContext, handleTagMenu, tagMenu }) => { const BUTTON_STYLES = `bg-brand-blue text-brand-black hover:bg-brand-black hover:text-white`; const [filterTag, setfilterTag] = useState(pageContext.allTags); - var { href } = useLocation(); + let { href } = useLocation(); if (!href) href = ""; function handleChange(e) { @@ -61,7 +61,9 @@ const menuTagSlide = ({ pageContext, handleTagMenu, tagMenu }) => { title="Close the menu" aria-label="Close the slide tag menu" > -
+
@@ -117,8 +119,10 @@ const menuTagSlide = ({ pageContext, handleTagMenu, tagMenu }) => { ); }; -export default menuTagSlide; +export default MenuTagSlide; -menuTagSlide.propTypes = { - pageContext: PropTypes.any +MenuTagSlide.propTypes = { + pageContext: PropTypes.any, + handleTagMenu: PropTypes.func, + tagMenu: PropTypes.bool }; diff --git a/src/components/blog/postPagination.jsx b/src/components/blog/postPagination.jsx index aeae1d193..85d44bf40 100644 --- a/src/components/blog/postPagination.jsx +++ b/src/components/blog/postPagination.jsx @@ -22,7 +22,7 @@ class PostPagination extends React.Component { const displayShareButtons = this.props.displayShareButtons ?? true; const { githubLink, shareLink, shareMessage } = shareLinks; - var mdxType = mdx.frontmatter.type || "blog"; + let mdxType = mdx.frontmatter.type || "blog"; let prevTitle = prev ? shortenText(prev.node.frontmatter.title, 6) : ""; let nextTitle = next ? shortenText(next.node.frontmatter.title, 6) : ""; diff --git a/src/components/home/eventNotice.jsx b/src/components/home/eventNotice.jsx index 557d9cf63..412921fa0 100644 --- a/src/components/home/eventNotice.jsx +++ b/src/components/home/eventNotice.jsx @@ -6,7 +6,7 @@ import Fade from "react-reveal/Fade"; import { shortenText, calculateUserLocalTime } from "../../utils/shared"; import NoThumb from "../../images/no_thumbnail.png"; -const eventNotice = ({ eventBrite }) => { +const EventNotice = ({ eventBrite }) => { const UPDATE_TIME_MS = 30000; const [currentDate, setDate] = useState( moment().format("ddd DD MMMM YYYY, hh:mm A") @@ -114,8 +114,8 @@ const eventNotice = ({ eventBrite }) => { ); }; -export default eventNotice; +export default EventNotice; -eventNotice.propTypes = { +EventNotice.propTypes = { eventBrite: PropTypes.any }; diff --git a/src/components/home/latestPost.jsx b/src/components/home/latestPost.jsx index f2c28a261..389851237 100644 --- a/src/components/home/latestPost.jsx +++ b/src/components/home/latestPost.jsx @@ -9,7 +9,7 @@ import { getImageSource, shortenText } from "../../utils/shared"; import { GatsbyImage } from "gatsby-plugin-image"; const latestPost = ({ post }) => { - var count = 0; + let count = 0; return ( <> diff --git a/src/components/home/searchHome.jsx b/src/components/home/searchHome.jsx index 604212a83..24201382e 100644 --- a/src/components/home/searchHome.jsx +++ b/src/components/home/searchHome.jsx @@ -206,12 +206,12 @@ class SearchHome extends Component { } getSearchResults(query) { - var index = window.__FLEXSEARCH__.en.index; - var store = window.__FLEXSEARCH__.en.store; + let index = window.__FLEXSEARCH__.en.index; + let store = window.__FLEXSEARCH__.en.store; if (!query || !index) { return []; } else { - var results = []; + let results = []; // search the indexed fields Object.keys(index).forEach((idx) => { results.push(...index[idx].values.search(query)); // more search options at https://github.com/nextapps-de/flexsearch#index.search diff --git a/src/components/shared/header.jsx b/src/components/shared/header.jsx index 04896eb7b..014501128 100644 --- a/src/components/shared/header.jsx +++ b/src/components/shared/header.jsx @@ -13,14 +13,15 @@ const Header = () => { const [isScroll, toggleScrolled] = useState(false); const [isOpen, toggleOpen] = useState(false); const location = useLocation(); - var currentPagePath = location.pathname; + let currentPagePath = location.pathname; // close mobile menu on route change - if (typeof window !== "undefined" && window.screen.width <= 1200) { - useEffect(() => { + useEffect(() => { + if (typeof window !== "undefined" && window.screen.width <= 1200) { toggleExpansion(false); - }, [location]); - } + } + toggleExpansion(false); + }, [location]); // monitor page scroll useEffect(() => { @@ -99,7 +100,7 @@ const Header = () => { */ const NavLink = (props) => { function navColourClass(mobile = false) { - var className = "transition duration-300 relative "; // don't remove space + let className = "transition duration-300 relative "; // don't remove space if (mobile == "true") { if (isScroll) { @@ -176,7 +177,7 @@ const Header = () => { }; function mobileMenuIconClass() { - var className = + let className = "px-3 py-2 flex items-center border rounded outline-none transition duration-300 group-hover:border-gray-700 group-hover:text-gray-700"; if (isExpanded || isScroll || currentPagePath === "/") { @@ -189,7 +190,7 @@ const Header = () => { function copyRssLink() { toggleOpen(true); - var copyText = document.querySelector("#rssLink"); + let copyText = document.querySelector("#rssLink"); copyText.select(); copyText.setSelectionRange(0, 9999); document.execCommand("copy"); diff --git a/src/components/shared/scrollTop.jsx b/src/components/shared/scrollTop.jsx index 38255a38c..749c26c35 100644 --- a/src/components/shared/scrollTop.jsx +++ b/src/components/shared/scrollTop.jsx @@ -1,22 +1,24 @@ -import React from 'react' -import PropTypes from "prop-types" -import { IoIosArrowUp } from "react-icons/io" -import { trackScrollPosition } from "../../utils/hooks/trackScrollPosition" +import React from "react"; +import PropTypes from "prop-types"; +import { IoIosArrowUp } from "react-icons/io"; +import { useTrackScrollPosition } from "../../utils/hooks/trackScrollPosition"; const scrollTop = () => { - - trackScrollPosition("scrollTop-btn"); + useTrackScrollPosition("scrollTop-btn"); return ( -
window.scrollTo({ top: 0, behavior: 'smooth' })}> - +
window.scrollTo({ top: 0, behavior: "smooth" })} + > +
- ) -} + ); +}; -export default scrollTop +export default scrollTop; scrollTop.propTypes = { - pageContext: PropTypes.any, - -} \ No newline at end of file + pageContext: PropTypes.any +}; diff --git a/src/components/shared/search.jsx b/src/components/shared/search.jsx index cbb0de140..954167432 100644 --- a/src/components/shared/search.jsx +++ b/src/components/shared/search.jsx @@ -20,8 +20,8 @@ class Search extends Component { componentDidMount = () => { setTimeout(() => { if (this.props.location.state && this.props.location.state.searchWord) { - var queryHome = this.props.location.state.searchWord; - var input = document.getElementById("pageSearch"); + let queryHome = this.props.location.state.searchWord; + let input = document.getElementById("pageSearch"); this.setState({ query: queryHome }); input.setAttribute("value", queryHome); this.searchFunction(queryHome); @@ -31,34 +31,30 @@ class Search extends Component { render() { const ResultList = () => { - // query length changed from 2 to 0 - if (this.state.results.length > 0 && this.state.query.length > 0) { - const data = useStaticQuery(graphql` - query postList { - allMdx( - sort: { fields: [frontmatter___date], order: DESC } - filter: { frontmatter: { published: { ne: false } } } - ) { - edges { - node { - id - frontmatter { - date(formatString: "ddd, DD MMM YYYY") - thumbnail { - childImageSharp { - gatsbyImageData( - placeholder: BLURRED - layout: FULL_WIDTH - ) - } + const data = useStaticQuery(graphql` + query postList { + allMdx( + sort: { fields: [frontmatter___date], order: DESC } + filter: { frontmatter: { published: { ne: false } } } + ) { + edges { + node { + id + frontmatter { + date(formatString: "ddd, DD MMM YYYY") + thumbnail { + childImageSharp { + gatsbyImageData(placeholder: BLURRED, layout: FULL_WIDTH) } } } } } } - `); - + } + `); + // query length changed from 2 to 0 + if (this.state.results.length > 0 && this.state.query.length > 0) { return (
@@ -252,13 +248,13 @@ class Search extends Component { //index - a flexsearch index instance (variables set in gatsby-config) //store - object that stores the indexed gatsby nodes where the id of each node corresponds to the id the filter, according with flexsearch.js best practices (https://github.com/nextapps-de/flexsearch#best-practices)). - var index = window.__FLEXSEARCH__.en.index; - var store = window.__FLEXSEARCH__.en.store; + let index = window.__FLEXSEARCH__.en.index; + let store = window.__FLEXSEARCH__.en.store; if (!query || !index) { return []; } else { - var results = []; + let results = []; // search the indexed fields Object.keys(index).forEach((idx) => { results.push( diff --git a/src/components/visualisation/utils.js b/src/components/visualisation/utils.js index f20a2a2e0..e2178b1b8 100644 --- a/src/components/visualisation/utils.js +++ b/src/components/visualisation/utils.js @@ -1,100 +1,116 @@ -import * as htmlToImage from 'html-to-image'; -import kebabCase from 'lodash.kebabcase'; -import { checkURL } from '../../utils/shared'; +import * as htmlToImage from "html-to-image"; +import kebabCase from "lodash.kebabcase"; +import { checkURL } from "../../utils/shared"; /** * Solution for nested function within a mocked function * https://github.com/facebook/jest/issues/936 - * - * Require() make an object reference to the module, and when + * + * Require() make an object reference to the module, and when * you mock a function A() this will overwrite the reference object. - * - * When you call A() and it has a nested function B() in the same module, + * + * When you call A() and it has a nested function B() in the same module, * the actual function will be called rather the one in the reference, * Even if B() is also mocked. - * + * * To solve this issue, use current file */ -import * as thisFile from "./utils" +import * as thisFile from "./utils"; /** * Handle image download for current visualisation - * @param {object} targetVis the target visualisation as a HTML object + * @param {object} targetVis the target visualisation as a HTML object * @param {object} mdx current mdx object * @param {string} pngImagePath path of the saved png image (if any) * @param {string} svgImagePath path of the saved svg image (if any) * @param {string} type which type of image to download * @returns {void} */ -export const handleImageDownload = (targetVis, mdx, pngImagePath = "", svgImagePath = "", type = "png") => { +export const handleImageDownload = ( + targetVis, + mdx, + pngImagePath = "", + svgImagePath = "", + type = "png" +) => { if (type == "png") { return thisFile.downloadAsPng(targetVis, mdx, pngImagePath); } return thisFile.downloadAsSvg(targetVis, mdx, svgImagePath); -} +}; /** * Download svg image for current visualisation - * @param {object} visElement the target visualisation as a HTML object + * @param {object} visElement the target visualisation as a HTML object * @param {object} mdxObject current mdx object * @param {string} svgPath path of the saved svg image (if any) * @returns {void} */ - -export const downloadAsSvg = (visElement, mdxObject, svgPath = '') => { +export const downloadAsSvg = (visElement, mdxObject, svgPath = "") => { if (svgPath) { - const link = checkURL(svgPath) ? svgPath : `${mdxObject.folderLink}${svgPath}`; - window.open(link, '_blank', 'noopener,noreferrer'); + const link = checkURL(svgPath) + ? svgPath + : `${mdxObject.folderLink}${svgPath}`; + window.open(link, "_blank", "noopener,noreferrer"); return; } - htmlToImage.toSvg(visElement) - .then((dataURL) => { - var image = new Image(); - image.src = dataURL; - return createLinkForImage(`${kebabCase(mdxObject.frontmatter.title)}.svg`, dataURL); - }) - .catch((error) => { - window.alert('Oops, something went wrong!', error.toString()); - }); -} - + htmlToImage + .toSvg(visElement) + .then((dataURL) => { + let image = new Image(); + image.src = dataURL; + return createLinkForImage( + `${kebabCase(mdxObject.frontmatter.title)}.svg`, + dataURL + ); + }) + .catch((error) => { + window.alert("Oops, something went wrong!", error.toString()); + }); +}; /** * Download png image for current visualisation - * @param {object} visElement the target visualisation as a HTML object + * @param {object} visElement the target visualisation as a HTML object * @param {object} mdxObject current mdx object * @param {string} pngPath path of the saved png image (if any) * @returns {void} */ -export const downloadAsPng = (visElement, mdxObject, pngPath = '') => { +export const downloadAsPng = (visElement, mdxObject, pngPath = "") => { if (pngPath) { - const link = checkURL(pngPath) ? pngPath : `${mdxObject.folderLink}${pngPath}`; - window.open(link, '_blank', 'noopener,noreferrer'); + const link = checkURL(pngPath) + ? pngPath + : `${mdxObject.folderLink}${pngPath}`; + window.open(link, "_blank", "noopener,noreferrer"); return; } - htmlToImage.toPng(visElement, { - quality: 1, - pixelRatio: 4, - }) - .then((dataURL) => { - return createLinkForImage(`${kebabCase(mdxObject.frontmatter.title)}.png`, dataURL); - }) - .catch((error) => { - window.alert('Oops, something went wrong!', error.toString()); - }); -} + htmlToImage + .toPng(visElement, { + quality: 1, + pixelRatio: 4 + }) + .then((dataURL) => { + return createLinkForImage( + `${kebabCase(mdxObject.frontmatter.title)}.png`, + dataURL + ); + }) + .catch((error) => { + window.alert("Oops, something went wrong!", error.toString()); + }); +}; /** * Create link for image url and perform the click action - * @param {string} fileName name for the download file + * @param {string} fileName name for the download file * @param {string} imageURL url of the image to download */ export const createLinkForImage = (fileName, imageURL) => { - let link = document.createElement('a'); + let link = document.createElement("a"); link.download = fileName; link.href = imageURL; link.click(); -} \ No newline at end of file +}; diff --git a/src/components/visualisation/visDownload.jsx b/src/components/visualisation/visDownload.jsx index e21ff0b6e..62c49bf48 100644 --- a/src/components/visualisation/visDownload.jsx +++ b/src/components/visualisation/visDownload.jsx @@ -15,7 +15,7 @@ import { MdCancel, MdError } from "react-icons/md"; * 4a. YES => capture html as image and download (Recommend Chrome as svg generated in Firefox doesn't work in Chrome) * 4b. No => Hide/disable download button */ -const visDownload = ({ mdx }) => { +const VisDownload = ({ mdx }) => { const [menu, setMenu] = useState(false); const [targetVis, setTargetVis] = useState(""); const pngImagePath = @@ -129,8 +129,8 @@ const visDownload = ({ mdx }) => { ); }; -visDownload.propTypes = { +VisDownload.propTypes = { mdx: PropTypes.any }; -export default visDownload; +export default VisDownload; diff --git a/src/components/visualisation/visEmbed.jsx b/src/components/visualisation/visEmbed.jsx index 4501cfbd9..186053370 100644 --- a/src/components/visualisation/visEmbed.jsx +++ b/src/components/visualisation/visEmbed.jsx @@ -18,7 +18,7 @@ import { Prism as SyntaxHighlighter } from "react-syntax-highlighter"; * IF url is not specified, use localPath to get one of the saved images * - localPath: name of the image property in frontmatter to use: "svgExternalImagePath", "svgImagePath", "pngImagePath", "pngExternalImagePath" */ -const visEmbed = (props) => { +const VisEmbed = (props) => { const [embedMenu, setEmbedMenu] = useState(false); const [embedCode, setEmbedCode] = useState(""); @@ -78,7 +78,7 @@ const visEmbed = (props) => { let copyText = element.innerText || element.textContent; // create an input element for copying to clipboard - var input = document.body.appendChild(document.createElement("input")); + let input = document.body.appendChild(document.createElement("input")); input.value = copyText; input.select(); document.execCommand("copy"); @@ -163,11 +163,13 @@ const visEmbed = (props) => { ); }; -visEmbed.propTypes = { - props: PropTypes.any +VisEmbed.propTypes = { + props: PropTypes.any, + children: PropTypes.any, + mdx: PropTypes.object }; -export default visEmbed; +export default VisEmbed; /** * Get embed code template based on type and URL. diff --git a/src/components/visualisation/visLayout.jsx b/src/components/visualisation/visLayout.jsx index 52a954091..14ad9ecb9 100644 --- a/src/components/visualisation/visLayout.jsx +++ b/src/components/visualisation/visLayout.jsx @@ -17,7 +17,7 @@ import VisUtils from "./visUtils"; * @param {Object} pageContext Other page related information * @returns */ -const visLayout = ({ currentMDXs, nextPageRef, title, pageContext }) => { +const VisLayout = ({ currentMDXs, nextPageRef, title, pageContext }) => { const [tagMenu, setTagMenu] = useState(false); // category & tag will pass a title @@ -182,9 +182,11 @@ const visLayout = ({ currentMDXs, nextPageRef, title, pageContext }) => { ); }; -export default visLayout; +export default VisLayout; -visLayout.propTypes = { +VisLayout.propTypes = { currentMDXs: PropTypes.any, - nextPageRef: PropTypes.any + nextPageRef: PropTypes.any, + title: PropTypes.string, + pageContext: PropTypes.object }; diff --git a/src/components/visualisation/visUtils.jsx b/src/components/visualisation/visUtils.jsx index 4f609ac4c..9bb1fdab6 100644 --- a/src/components/visualisation/visUtils.jsx +++ b/src/components/visualisation/visUtils.jsx @@ -1,6 +1,6 @@ import React, { useState, useEffect } from "react"; import PropTypes from "prop-types"; -import { trackScrollPosition } from "../../utils/hooks/trackScrollPosition"; +import { useTrackScrollPosition } from "../../utils/hooks/trackScrollPosition"; import { VisTagMenuBtn, VisFooterBtn } from "../style/visStyle"; import { FaTags } from "react-icons/fa"; import { MdClose } from "react-icons/md"; @@ -23,8 +23,8 @@ const VisUtils = React.memo(({ tagMenu, handleTagMenu, tags }) => { const footerIconStyle = "text-2xl group-hover:scale-110 transition duration-500"; - trackScrollPosition("vis-tag-button"); - trackScrollPosition("vis-footer-button"); + useTrackScrollPosition("vis-tag-button"); + useTrackScrollPosition("vis-footer-button"); function searchTag() { let searchWord = searchValue.toLowerCase(); @@ -58,7 +58,11 @@ const VisUtils = React.memo(({ tagMenu, handleTagMenu, tags }) => { onClick={() => setFooter(!footer)} tabindex="101" id="vis-footer-button" - className={`group ${footer ? "bg-brand-blue text-brand-black" : "bg-brand-black text-white"}`} + className={`group ${ + footer + ? "bg-brand-blue text-brand-black" + : "bg-brand-black text-white" + }`} title="Footer" > {!footer && } diff --git a/src/pages/events.jsx b/src/pages/events.jsx index 3fe3c7cad..07dd6218d 100644 --- a/src/pages/events.jsx +++ b/src/pages/events.jsx @@ -8,7 +8,7 @@ import PastEventsBlog from "../components/events/pastEventsBlog"; import moment from "moment"; import { calculateUserLocalTime } from "../utils/shared"; -const events = ({ data: { eventBrite, pastEvent, pastEventBlog } }) => { +const Events = ({ data: { eventBrite, pastEvent, pastEventBlog } }) => { const UPDATE_TIME_MS = 30000; const [currentDate, setDate] = useState( moment().format("ddd DD MMMM YYYY, hh:mm A") @@ -72,9 +72,9 @@ const events = ({ data: { eventBrite, pastEvent, pastEventBlog } }) => { ); }; -export default events; +export default Events; -events.propTypes = { +Events.propTypes = { data: PropTypes.any }; diff --git a/src/pages/index.jsx b/src/pages/index.jsx index 14d40b9fc..acc3059f8 100644 --- a/src/pages/index.jsx +++ b/src/pages/index.jsx @@ -15,11 +15,11 @@ import Covid from "../images/home/COVIDDeathPropMSOA.png"; import Bg from "../images/home/lineBackground.png"; import { ArrowButton, AnimateButton } from "../components/style/styleComponent"; import { randomInteger } from "../utils/shared"; -import { backgroundMovement } from "../utils/hooks/backgroundMovement"; +import { useBackgroundMovement } from "../utils/hooks/backgroundMovement"; const IndexPage = ({ data: { featuredItem, latestPost, eventBrite } }) => { let datePrev = moment(); - var words = [ + let words = [ "Colour", "What are you looking for?", "blog post", @@ -33,7 +33,7 @@ const IndexPage = ({ data: { featuredItem, latestPost, eventBrite } }) => { ]; useEffect(() => { - var input = document.getElementById("homeSearch"); + let input = document.getElementById("homeSearch"); const interval = setInterval(() => { let dateNow = moment(); let dateDiff = dateNow - datePrev; @@ -48,7 +48,7 @@ const IndexPage = ({ data: { featuredItem, latestPost, eventBrite } }) => { }; }, [words]); - backgroundMovement("dynamicBg"); + useBackgroundMovement("dynamicBg"); return (
diff --git a/src/templates/blog/blogCategoryTemplate.jsx b/src/templates/blog/blogCategoryTemplate.jsx index f2c8e3808..553a98570 100644 --- a/src/templates/blog/blogCategoryTemplate.jsx +++ b/src/templates/blog/blogCategoryTemplate.jsx @@ -1,21 +1,21 @@ -import React, { useState } from "react" -import kebabCase from "lodash.kebabcase" -import { graphql } from "gatsby" -import MenuCategory from "../../components/blog/menuCategory" -import BlogLayout from "../../components/blog/blogLayout" -import PropTypes from "prop-types" -import SEO from "../../components/shared/seo" -import Bg from "../../images/blog/colorful-world.jpg" +import React, { useState } from "react"; +import kebabCase from "lodash.kebabcase"; +import { graphql } from "gatsby"; +import MenuCategory from "../../components/blog/menuCategory"; +import BlogLayout from "../../components/blog/blogLayout"; +import PropTypes from "prop-types"; +import SEO from "../../components/shared/seo"; +import Bg from "../../images/blog/colorful-world.jpg"; -const blogCategoryTemplate = ({ data: { allMdx }, pageContext }) => { - const [tagMenu, toggleTagMenu] = useState(false) +const BlogCategoryTemplate = ({ data: { allMdx }, pageContext }) => { + const [tagMenu, toggleTagMenu] = useState(false); function handleTagMenu() { - toggleTagMenu(!tagMenu) + toggleTagMenu(!tagMenu); if (screen.width <= 1280 && tagMenu === false) { - var element = document.querySelector("#tagMenu") - element.scrollIntoView() + let element = document.querySelector("#tagMenu"); + element.scrollIntoView(); } } @@ -58,15 +58,15 @@ const blogCategoryTemplate = ({ data: { allMdx }, pageContext }) => { tagMenu={tagMenu} /> - ) -} + ); +}; -export default blogCategoryTemplate +export default BlogCategoryTemplate; -blogCategoryTemplate.propTypes = { +BlogCategoryTemplate.propTypes = { pageContext: PropTypes.any, data: PropTypes.any -} +}; export const query = graphql` query blogCategory($category: String, $skip: Int!, $limit: Int!) { @@ -91,4 +91,4 @@ export const query = graphql` } } } -` +`; diff --git a/src/templates/blog/blogPostTemplate.jsx b/src/templates/blog/blogPostTemplate.jsx index aa0df873e..7034f4245 100644 --- a/src/templates/blog/blogPostTemplate.jsx +++ b/src/templates/blog/blogPostTemplate.jsx @@ -25,7 +25,7 @@ import Fade from "react-reveal/Fade"; import Pulse from "react-reveal/Pulse"; import trianglify from "trianglify"; -const blogPostTemplate = ({ data: { mdx }, pageContext }) => { +const BlogPostTemplate = ({ data: { mdx }, pageContext }) => { const { title, date, author, category, tag, disableTOC } = mdx.frontmatter; const { prev, next } = pageContext; @@ -46,7 +46,7 @@ const blogPostTemplate = ({ data: { mdx }, pageContext }) => { } // enable/disable table of content - var tableOfContent; + let tableOfContent; if (disableTOC === true) { tableOfContent = null; } else { @@ -89,9 +89,9 @@ const blogPostTemplate = ({ data: { mdx }, pageContext }) => { // produce trianglify image useEffect(() => { - var element = document.getElementById("headElement"); - var dimensions = element.getClientRects()[0]; - var pattern = trianglify({ + let element = document.getElementById("headElement"); + let dimensions = element.getClientRects()[0]; + let pattern = trianglify({ width: dimensions.width, height: dimensions.height, cellSize: 60 + Math.ceil(Math.random() * 100), @@ -100,7 +100,7 @@ const blogPostTemplate = ({ data: { mdx }, pageContext }) => { seed: Math.random().toString(5) }).toCanvas(); - var img = pattern.toDataURL("image/png"); + let img = pattern.toDataURL("image/png"); element.style["background-image"] = "linear-gradient(0deg, rgba(0,0,0,0.05) 70%, rgba(0,0,0,0.60) 100%), url(" + img + @@ -267,9 +267,9 @@ const blogPostTemplate = ({ data: { mdx }, pageContext }) => { ); }; -export default blogPostTemplate; +export default BlogPostTemplate; -blogPostTemplate.propTypes = { +BlogPostTemplate.propTypes = { data: PropTypes.any, pageContext: PropTypes.any }; diff --git a/src/templates/blog/blogPostTemplateCustom.jsx b/src/templates/blog/blogPostTemplateCustom.jsx index fa3f8f6bf..15b7a3885 100644 --- a/src/templates/blog/blogPostTemplateCustom.jsx +++ b/src/templates/blog/blogPostTemplateCustom.jsx @@ -1,29 +1,29 @@ -import React from "react" -import PropTypes from "prop-types" -import SEO from "../../components/shared/seo" -import { Helmet } from "react-helmet" -import { graphql, withPrefix } from "gatsby" -import PostPagination from "../../components/blog/postPagination" +import React from "react"; +import PropTypes from "prop-types"; +import SEO from "../../components/shared/seo"; +import { Helmet } from "react-helmet"; +import { graphql, withPrefix } from "gatsby"; +import PostPagination from "../../components/blog/postPagination"; -import CommonMdxProvider from "../../components/shared/commonMdxProvider" -import "katex/dist/katex.min.css" +import CommonMdxProvider from "../../components/shared/commonMdxProvider"; +import "katex/dist/katex.min.css"; -import Comment from "../../components/blog/comment" -import { useScript } from "../../utils/hooks/useScript" -import { getShareLinks } from "../../utils/shared" +import Comment from "../../components/blog/comment"; +import { useScript } from "../../utils/hooks/useScript"; +import { getShareLinks } from "../../utils/shared"; -const blogPostTemplateCustom = ({ data: { mdx }, pageContext }) => { - const { prev, next } = pageContext - const shareLinks = getShareLinks(mdx) - const d3 = mdx.frontmatter.d3 || null +const BlogPostTemplateCustom = ({ data: { mdx }, pageContext }) => { + const { prev, next } = pageContext; + const shareLinks = getShareLinks(mdx); + const d3 = mdx.frontmatter.d3 || null; // include d3 scripts - useScript("https://unpkg.com/topojson@3", "", false) + useScript("https://unpkg.com/topojson@3", "", false); { d3 && d3.map((d) => { - useScript(withPrefix(`d3/${d}`), "", false) - }) + useScript(withPrefix(`d3/${d}`), "", false); + }); } return ( @@ -68,15 +68,15 @@ const blogPostTemplateCustom = ({ data: { mdx }, pageContext }) => { /> - ) -} + ); +}; -export default blogPostTemplateCustom +export default BlogPostTemplateCustom; -blogPostTemplateCustom.propTypes = { +BlogPostTemplateCustom.propTypes = { data: PropTypes.any, pageContext: PropTypes.any -} +}; export const query = graphql` query BlogPostQuery_custom($id: String) { @@ -95,4 +95,4 @@ export const query = graphql` } } } -` +`; diff --git a/src/templates/blog/blogTagTemplate.jsx b/src/templates/blog/blogTagTemplate.jsx index dc675a6c0..3e3dda317 100644 --- a/src/templates/blog/blogTagTemplate.jsx +++ b/src/templates/blog/blogTagTemplate.jsx @@ -1,21 +1,21 @@ -import React, { useState } from "react" -import { graphql } from "gatsby" -import MenuCategory from "../../components/blog/menuCategory" -import PropTypes from "prop-types" -import BlogLayout from "../../components/blog/blogLayout" -import SEO from "../../components/shared/seo" -import kebabCase from "lodash.kebabcase" -import Bg from "../../images/blog/colorful-world.jpg" +import React, { useState } from "react"; +import { graphql } from "gatsby"; +import MenuCategory from "../../components/blog/menuCategory"; +import PropTypes from "prop-types"; +import BlogLayout from "../../components/blog/blogLayout"; +import SEO from "../../components/shared/seo"; +import kebabCase from "lodash.kebabcase"; +import Bg from "../../images/blog/colorful-world.jpg"; -const blogTagTemplate = ({ data: { allMdx }, pageContext }) => { - const [tagMenu, toggleTagMenu] = useState(false) +const BlogTagTemplate = ({ data: { allMdx }, pageContext }) => { + const [tagMenu, toggleTagMenu] = useState(false); function handleTagMenu() { - toggleTagMenu(!tagMenu) + toggleTagMenu(!tagMenu); if (screen.width <= 1280 && tagMenu === false) { - var element = document.querySelector("#tagMenu") - element.scrollIntoView() + let element = document.querySelector("#tagMenu"); + element.scrollIntoView(); } } @@ -58,15 +58,15 @@ const blogTagTemplate = ({ data: { allMdx }, pageContext }) => { tagMenu={tagMenu} /> - ) -} + ); +}; -export default blogTagTemplate +export default BlogTagTemplate; -blogTagTemplate.propTypes = { +BlogTagTemplate.propTypes = { pageContext: PropTypes.any, data: PropTypes.any -} +}; export const query = graphql` query blogTag($tag: String, $skip: Int!, $limit: Int!) { @@ -89,4 +89,4 @@ export const query = graphql` } } } -` +`; diff --git a/src/templates/blog/blogTemplate.jsx b/src/templates/blog/blogTemplate.jsx index 9e48ef04a..19daed39f 100644 --- a/src/templates/blog/blogTemplate.jsx +++ b/src/templates/blog/blogTemplate.jsx @@ -7,21 +7,21 @@ import SEO from "../../components/shared/seo"; import Flip from "react-reveal/Flip"; import { GreyButton } from "../../components/style/styleComponent"; import Bg from "../../images/blog/colorful-world.jpg"; -import { backgroundMovement } from "../../utils/hooks/backgroundMovement"; +import { useBackgroundMovement } from "../../utils/hooks/backgroundMovement"; -const blogTemplate = ({ data: { allMdx }, pageContext }) => { +const BlogTemplate = ({ data: { allMdx }, pageContext }) => { const [tagMenu, toggleTagMenu] = useState(false); function handleTagMenu() { toggleTagMenu(!tagMenu); if (screen.width <= 1280 && tagMenu === false) { - var element = document.querySelector("#tagMenu"); + let element = document.querySelector("#tagMenu"); element.scrollIntoView(); } } - backgroundMovement("blogBackground"); + useBackgroundMovement("blogBackground"); return ( <> @@ -98,9 +98,9 @@ const blogTemplate = ({ data: { allMdx }, pageContext }) => { ); }; -export default blogTemplate; +export default BlogTemplate; -blogTemplate.propTypes = { +BlogTemplate.propTypes = { pageContext: PropTypes.any, data: PropTypes.any }; diff --git a/src/templates/docs/docsTemplate.jsx b/src/templates/docs/docsTemplate.jsx index 9b040b394..454f76cb7 100644 --- a/src/templates/docs/docsTemplate.jsx +++ b/src/templates/docs/docsTemplate.jsx @@ -21,7 +21,7 @@ import { trackTableOfContent } from "../../utils/hooks/trackTableOfContent"; import Fade from "react-reveal/Fade"; -const docsTemplate = ({ data: { mdx }, pageContext }) => { +const DocsTemplate = ({ data: { mdx }, pageContext }) => { const { title, date, author, disableTOC } = mdx.frontmatter; const { prev, next } = pageContext; @@ -39,7 +39,7 @@ const docsTemplate = ({ data: { mdx }, pageContext }) => { } // enable/disable table of content - var tableOfContent; + let tableOfContent; if (disableTOC === true) { tableOfContent = null; @@ -204,9 +204,9 @@ const docsTemplate = ({ data: { mdx }, pageContext }) => { ); }; -export default docsTemplate; +export default DocsTemplate; -docsTemplate.propTypes = { +DocsTemplate.propTypes = { data: PropTypes.any, pageContext: PropTypes.any }; diff --git a/src/templates/docs/docsTemplateCustom.jsx b/src/templates/docs/docsTemplateCustom.jsx index 8cc2e8c35..b44bcd8b8 100644 --- a/src/templates/docs/docsTemplateCustom.jsx +++ b/src/templates/docs/docsTemplateCustom.jsx @@ -1,27 +1,27 @@ -import React from "react" -import PropTypes from "prop-types" -import SEO from "../../components/shared/seo" -import { Helmet } from "react-helmet" -import { graphql, withPrefix } from "gatsby" -import PostPagination from "../../components/blog/postPagination" -import "katex/dist/katex.min.css" -import Comment from "../../components/blog/comment" -import { useScript } from "../../utils/hooks/useScript" -import { getShareLinks } from "../../utils/shared" -import CommonMdxProvider from "../../components/shared/commonMdxProvider" +import React from "react"; +import PropTypes from "prop-types"; +import SEO from "../../components/shared/seo"; +import { Helmet } from "react-helmet"; +import { graphql, withPrefix } from "gatsby"; +import PostPagination from "../../components/blog/postPagination"; +import "katex/dist/katex.min.css"; +import Comment from "../../components/blog/comment"; +import { useScript } from "../../utils/hooks/useScript"; +import { getShareLinks } from "../../utils/shared"; +import CommonMdxProvider from "../../components/shared/commonMdxProvider"; -const docsTemplateCustom = ({ data: { mdx }, pageContext }) => { - const { prev, next } = pageContext - const shareLinks = getShareLinks(mdx) - const d3 = mdx.frontmatter.d3 ? mdx.frontmatter.d3 : null +const DocsTemplateCustom = ({ data: { mdx }, pageContext }) => { + const { prev, next } = pageContext; + const shareLinks = getShareLinks(mdx); + const d3 = mdx.frontmatter.d3 ? mdx.frontmatter.d3 : null; // include d3 scripts - useScript("https://unpkg.com/topojson@3", "", false) + useScript("https://unpkg.com/topojson@3", "", false); { d3 && d3.map((d) => { - useScript(withPrefix(`d3/${d}`), "", false) - }) + useScript(withPrefix(`d3/${d}`), "", false); + }); } return ( @@ -67,15 +67,15 @@ const docsTemplateCustom = ({ data: { mdx }, pageContext }) => { /> - ) -} + ); +}; -export default docsTemplateCustom +export default DocsTemplateCustom; -docsTemplateCustom.propTypes = { +DocsTemplateCustom.propTypes = { data: PropTypes.any, pageContext: PropTypes.any -} +}; export const docsCustomQuery = graphql` query docsQuery_custom($id: String) { @@ -94,4 +94,4 @@ export const docsCustomQuery = graphql` } } } -` +`; diff --git a/src/templates/visualisation/visCategoryTemplate.jsx b/src/templates/visualisation/visCategoryTemplate.jsx index 9a99d4325..2eca1eb5a 100644 --- a/src/templates/visualisation/visCategoryTemplate.jsx +++ b/src/templates/visualisation/visCategoryTemplate.jsx @@ -1,50 +1,55 @@ -import React, { useRef } from "react" -import { graphql } from "gatsby" -import PropTypes from "prop-types" -import { loadMoreVisualisation } from "../../utils/hooks/loadMoreVisualisation" -import VisLayout from "../../components/visualisation/visLayout" +import React, { useRef } from "react"; +import { graphql } from "gatsby"; +import PropTypes from "prop-types"; +import { useLoadMoreVisualisation } from "../../utils/hooks/loadMoreVisualisation"; +import VisLayout from "../../components/visualisation/visLayout"; -const visCategoryTemplate = ({ data: {allMdx}, pageContext }) => { +const VisCategoryTemplate = ({ data: { allMdx }, pageContext }) => { const nextPageRef = useRef(); - const currentMDXs = loadMoreVisualisation(allMdx.edges, nextPageRef); + const currentMDXs = useLoadMoreVisualisation(allMdx.edges, nextPageRef); - return( - - ) -} + return ( + + ); +}; -export default visCategoryTemplate +export default VisCategoryTemplate; -visCategoryTemplate.propTypes = { +VisCategoryTemplate.propTypes = { data: PropTypes.any, -} - + pageContext: PropTypes.any +}; export const query = graphql` - query visCategory($category: String) { - allMdx( - sort: { fields: [frontmatter___date], order: DESC } - filter: { - frontmatter: { - category: { in: [$category] }, - published: {ne: false }, + query visCategory($category: String) { + allMdx( + sort: { fields: [frontmatter___date], order: DESC } + filter: { + frontmatter: { + category: { in: [$category] } + published: { ne: false } type: { eq: "visualisation" } - } + } } - ) { - edges { - node { - id - frontmatter { - ...MdxFrontmatter + ) { + edges { + node { + id + frontmatter { + ...MdxFrontmatter rowSpan columnSpan - } - fields { - ...MdxFields - } - } - } - } - } -` \ No newline at end of file + } + fields { + ...MdxFields + } + } + } + } + } +`; diff --git a/src/templates/visualisation/visItemTemplate.jsx b/src/templates/visualisation/visItemTemplate.jsx index d743103e1..941bccfd7 100644 --- a/src/templates/visualisation/visItemTemplate.jsx +++ b/src/templates/visualisation/visItemTemplate.jsx @@ -20,7 +20,7 @@ import VisDetail from "../../components/visualisation/visDetail"; import { VisDiv, VisBackBtn, EmbedCode } from "../../components/style/visStyle"; import { AiOutlineRollback, AiOutlineHome } from "react-icons/ai"; -const visItemTemplate = ({ data: { mdx }, pageContext }) => { +const VisItemTemplate = ({ data: { mdx }, pageContext }) => { const components = { VisPagination, VisDetail, VisDiv, EmbedCode }; const shareLinks = getShareLinks(mdx); @@ -112,9 +112,9 @@ const visItemTemplate = ({ data: { mdx }, pageContext }) => { ); }; -export default visItemTemplate; +export default VisItemTemplate; -visItemTemplate.propTypes = { +VisItemTemplate.propTypes = { data: PropTypes.any, pageContext: PropTypes.any }; diff --git a/src/templates/visualisation/visTagTemplate.jsx b/src/templates/visualisation/visTagTemplate.jsx index 74c718c3f..cb5b194ff 100644 --- a/src/templates/visualisation/visTagTemplate.jsx +++ b/src/templates/visualisation/visTagTemplate.jsx @@ -1,12 +1,12 @@ import React, { useRef } from "react"; import { graphql } from "gatsby"; import PropTypes from "prop-types"; -import { loadMoreVisualisation } from "../../utils/hooks/loadMoreVisualisation"; +import { useLoadMoreVisualisation } from "../../utils/hooks/loadMoreVisualisation"; import VisLayout from "../../components/visualisation/visLayout"; -const visTagTemplate = ({ data: { allMdx }, pageContext }) => { +const VisTagTemplate = ({ data: { allMdx }, pageContext }) => { const nextPageRef = useRef(); - const currentMDXs = loadMoreVisualisation(allMdx.edges, nextPageRef); + const currentMDXs = useLoadMoreVisualisation(allMdx.edges, nextPageRef); return ( { ); }; -export default visTagTemplate; +export default VisTagTemplate; -visTagTemplate.propTypes = { - data: PropTypes.any +VisTagTemplate.propTypes = { + data: PropTypes.any, + pageContext: PropTypes.any }; export const query = graphql` diff --git a/src/templates/visualisation/visTemplate.jsx b/src/templates/visualisation/visTemplate.jsx index 02609fd28..0894cd324 100644 --- a/src/templates/visualisation/visTemplate.jsx +++ b/src/templates/visualisation/visTemplate.jsx @@ -1,12 +1,12 @@ import React, { useRef } from "react"; import { graphql } from "gatsby"; import PropTypes from "prop-types"; -import { loadMoreVisualisation } from "../../utils/hooks/loadMoreVisualisation"; +import { useLoadMoreVisualisation } from "../../utils/hooks/loadMoreVisualisation"; import VisLayout from "../../components/visualisation/visLayout"; -const visTemplate = ({ data: { allMdx }, pageContext }) => { +const VisTemplate = ({ data: { allMdx }, pageContext }) => { const nextPageRef = useRef(); - const currentMDXs = loadMoreVisualisation(allMdx.edges, nextPageRef); + const currentMDXs = useLoadMoreVisualisation(allMdx.edges, nextPageRef); return ( { ); }; -export default visTemplate; +export default VisTemplate; -visTemplate.propTypes = { - data: PropTypes.any +VisTemplate.propTypes = { + data: PropTypes.any, + pageContext: PropTypes.any }; export const query = graphql` diff --git a/src/utils/hooks/backgroundMovement.jsx b/src/utils/hooks/backgroundMovement.jsx index 13239b007..17c580318 100644 --- a/src/utils/hooks/backgroundMovement.jsx +++ b/src/utils/hooks/backgroundMovement.jsx @@ -6,7 +6,7 @@ import { useState, useEffect, useCallback } from "react"; * @parent boolean: whether to move background on mouse hovering parent element of 'elementId' * Make sure to add the style overflow: hidden to parent/element, and add the style transform: scale(1.1) to the element */ -export function backgroundMovement( +export function useBackgroundMovement( elementId, parent = true, xFactor = 0.06, @@ -50,12 +50,12 @@ export function backgroundMovement( }px, ${-yFactor * yMove + translateValues.y}px) scale(1.1)`; }; - var background = document.querySelector(`#${elementId}`); + let background = document.querySelector(`#${elementId}`); if (!background) { return; } background.style.transition = "none"; - var hoverElement = background; + let hoverElement = background; if (parent === true) { hoverElement = background.parentElement; diff --git a/src/utils/hooks/loadMoreVisualisation.jsx b/src/utils/hooks/loadMoreVisualisation.jsx index 12011c389..6cf5241bc 100644 --- a/src/utils/hooks/loadMoreVisualisation.jsx +++ b/src/utils/hooks/loadMoreVisualisation.jsx @@ -1,17 +1,24 @@ -import { useState, useEffect } from 'react'; +import { useState, useEffect } from "react"; /** * Hook for load more visualisations * @param {object} allMdx All visualisation items - * @param {*} nextPageRef React useRef - referencing load-more div. + * @param {*} nextPageRef React useRef - referencing load-more div. * @param {string} referenceId Element id for load-more div. - * @param {int} pageLength + * @param {int} pageLength * @returns current list of visualisation */ -export function loadMoreVisualisation(allMdx, nextPageRef, referenceId, pageLength) { +export function useLoadMoreVisualisation( + allMdx, + nextPageRef, + referenceId, + pageLength +) { const PAGE_LENGTH = pageLength ?? 10; const refId = referenceId ?? "visualisation-invite"; - const [currentMDXs, setCurrentMDXs] = useState([...allMdx.slice(0, PAGE_LENGTH)]); + const [currentMDXs, setCurrentMDXs] = useState([ + ...allMdx.slice(0, PAGE_LENGTH) + ]); const [loadNextPage, setLoadNextPage] = useState(false); const [hasNextPage, setHasNextPage] = useState(allMdx.length > PAGE_LENGTH); @@ -21,7 +28,7 @@ export function loadMoreVisualisation(allMdx, nextPageRef, referenceId, pageLeng const options = { root: null, rootMargin: "300px 0px 0px 0px" - } + }; const refObserver = new IntersectionObserver((entries) => { if (entries[0].isIntersecting) { setLoadNextPage(true); @@ -33,7 +40,6 @@ export function loadMoreVisualisation(allMdx, nextPageRef, referenceId, pageLeng } }, []); - // Monitor remaining visualisations on update of visualisation objects useEffect(() => { const hasNext = allMdx.length > currentMDXs.length; @@ -41,11 +47,10 @@ export function loadMoreVisualisation(allMdx, nextPageRef, referenceId, pageLeng if (!hasNext) { const addMoreVisBox = document.querySelector(`#${refId}`); - addMoreVisBox.style.visibility = 'visible'; + addMoreVisBox.style.visibility = "visible"; addMoreVisBox.parentElement.appendChild(addMoreVisBox); } - },[currentMDXs]) - + }, [currentMDXs]); // Load more visualisations objects useEffect(() => { @@ -56,12 +61,14 @@ export function loadMoreVisualisation(allMdx, nextPageRef, referenceId, pageLeng // Get next page content const moreMDX = allMdx.length > currentMDXs.length; - const nextPageMDX = moreMDX ? allMdx.slice(currentMDXs.length, currentMDXs.length + PAGE_LENGTH) : []; - + const nextPageMDX = moreMDX + ? allMdx.slice(currentMDXs.length, currentMDXs.length + PAGE_LENGTH) + : []; + // Merge into current content setCurrentMDXs([...currentMDXs, ...nextPageMDX]); setLoadNextPage(false); - }, [loadNextPage, hasNextPage]) - + }, [loadNextPage, hasNextPage]); + return currentMDXs; -} \ No newline at end of file +} diff --git a/src/utils/hooks/trackScrollPosition.jsx b/src/utils/hooks/trackScrollPosition.jsx index c33cc2d04..c8a815e13 100644 --- a/src/utils/hooks/trackScrollPosition.jsx +++ b/src/utils/hooks/trackScrollPosition.jsx @@ -5,12 +5,12 @@ import { useEffect } from "react"; * @param {*} elementId affected element on scroll * @param {*} pageOffset position to make the element visible */ -export function trackScrollPosition(elementId, pageOffset = 300) { +export function useTrackScrollPosition(elementId, pageOffset = 300) { let element; useEffect(() => { function scrollAction() { - var el = document.getElementById(elementId); + let el = document.getElementById(elementId); el.style.opacity = "0"; el.style.visibility = "invisible"; diff --git a/src/utils/hooks/trackTableOfContent.jsx b/src/utils/hooks/trackTableOfContent.jsx index ecd847a97..637c85400 100644 --- a/src/utils/hooks/trackTableOfContent.jsx +++ b/src/utils/hooks/trackTableOfContent.jsx @@ -1,11 +1,11 @@ /* eslint-disable no-unused-vars */ -import { useEffect } from "react" +import { useEffect } from "react"; /** - * - * @param {insert selector for your table of contents elements} tocSelector - * @param {item are selected in the form of `itemSelector [id]`} itemSelector - * + * + * @param {insert selector for your table of contents elements} tocSelector + * @param {item are selected in the form of `itemSelector [id]`} itemSelector + * * structure of toc should be : *
  • * some heading @@ -17,44 +17,47 @@ import { useEffect } from "react" *
  • */ export function trackTableOfContent(tocSelector, itemSelector) { - - function handleClassList(element, tagName, type="add") { - if(tagName.substring(0,1) !== "H") return; - var el = element.parentElement; - var rm = (type === "remove") - - rm ? el.classList.remove('active') : el.classList.add('active'); - - if(tagName !== "H1") { - handleClassList(el.parentElement, `H${tagName.substring(1,) - 1}`, rm ? "remove" : "add"); + function handleClassList(element, tagName, type = "add") { + if (tagName.substring(0, 1) !== "H") return; + let el = element.parentElement; + let rm = type === "remove"; + + rm ? el.classList.remove("active") : el.classList.add("active"); + + if (tagName !== "H1") { + handleClassList( + el.parentElement, + `H${tagName.substring(1) - 1}`, + rm ? "remove" : "add" + ); } } useEffect(() => { function handleTOC() { - const observer = new IntersectionObserver(entries => { - entries.forEach(entry => { - var id = entry.target.getAttribute('id'); - var element = document.querySelector(`${tocSelector}[href="#${id}"]`) - if(element === null) return; + const observer = new IntersectionObserver((entries) => { + entries.forEach((entry) => { + let id = entry.target.getAttribute("id"); + let element = document.querySelector(`${tocSelector}[href="#${id}"]`); + if (element === null) return; if (entry.intersectionRatio > 0) { - handleClassList(element, entry.target.tagName) + handleClassList(element, entry.target.tagName); } else { - handleClassList(element, entry.target.tagName, "remove") + handleClassList(element, entry.target.tagName, "remove"); } }); }); - + // Track all items document.querySelectorAll(`${itemSelector} [id]`).forEach((item) => { observer.observe(item); }); } - document.addEventListener('DOMContentLoaded', handleTOC); + document.addEventListener("DOMContentLoaded", handleTOC); return () => { - document.removeEventListener('DOMContentLoaded', handleTOC); - } - }, []) -} \ No newline at end of file + document.removeEventListener("DOMContentLoaded", handleTOC); + }; + }, []); +}