All files / src/components/Sidebar/TagCloud index.tsx

100% Statements 12/12
100% Branches 2/2
100% Functions 5/5
100% Lines 12/12

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            6x 19x   19x 2337x 7847x 3002x   4845x         19x 23066x   19x 19x                 380x              
import React from "react";
import { Link, withPrefix } from "gatsby";
 
import Tag from "@/components/Tag";
import { AllPost } from "../entity";
 
const TagCloud = ({ allPosts }: { allPosts: AllPost[] }) => {
  const mapping: { [key: string]: number } = {};
 
  allPosts.forEach(({ node }) => {
    node.frontmatter.tags.forEach((name) => {
      if (mapping[name]) {
        mapping[name] += 1;
      } else {
        mapping[name] = 1;
      }
    });
  });
 
  const tags = Array.from(Object.keys(mapping)).sort(
    (b, a) => mapping[a] - mapping[b]
  );
  const limitTags = tags.slice(0, 20);
  return (
    <div className="d-none d-lg-block information my-2">
      <Link to={withPrefix("tags")} title="Tags">
        <p>
          <span className="icon-tags" />
          &nbsp;{limitTags.length} / {tags.length} Tags
        </p>
      </Link>
      {limitTags.map((item) => (
        <Tag name={item} key={item} count={mapping[item]} />
      ))}
    </div>
  );
};
 
export default TagCloud;