All files / src/components/Relateds index.tsx

0% Statements 0/0
0% Branches 0/0
0% Functions 0/0
0% Lines 0/0

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                                                                                                                                                 
import React from "react";
import lozad from "lozad";
import RelatedCard from "@/components/RelatedCard";
 
import * as style from "./index.module.scss";
import { isBrowser } from "@/utils";
 
export type AllPost = {
  title: string;
  tags: string[];
  date: string;
  headerImage?: string;
  slug: string;
};
 
const RelatedPosts = ({
  title,
  tags,
  allPosts = [],
}: {
  title: string;
  tags: readonly (string | undefined)[];
  allPosts: AllPost[];
}) => {
  React.useEffect(() => {
    if (isBrowser()) {
      const observer = lozad(".lozad", {
        rootMargin: "10px 0px",
        threshold: 0.1,
        enableAutoReload: true,
        loaded(el) {
          el.classList.add("loaded");
        },
      });
      observer.observe();
    }
  }, []);
 
  const relatedPosts = allPosts.filter((post) => {
    if (post.title === title) return false;
    if (post.tags) {
      for (let i = 0; i < post.tags.length; i++) {
        if (tags[i] && post.tags[i] === tags[i]) return true;
      }
    }
    return false;
  });
 
  if (!relatedPosts || relatedPosts.length === 0) return null;
 
  return (
    <div className={style.relatedPosts}>
      <h2>
        <span className="icon-newspaper-o" />
        &nbsp;Related Posts
      </h2>
      {relatedPosts.map((post) => (
        <div key={post.slug}>
          <RelatedCard
            title={post.title}
            tags={post.tags}
            date={post.date}
            headerImage={post.headerImage}
            url={post.slug}
          />
        </div>
      ))}
    </div>
  );
};
 
export default RelatedPosts;