All files / components/Header index.tsx

42.1% Statements 8/19
59.09% Branches 13/22
37.5% Functions 3/8
47.05% Lines 8/17

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          1x                                 1x                               1x                             5x   5x                     5x                                                                             1x                       1x                                      
import React, { useState } from "react";
import * as style from "./index.module.scss";
 
import ReactGA from "react-ga4";
 
const CopyIcon = () => (
  <svg
    xmlns="http://www.w3.org/2000/svg"
    width="18"
    height="18"
    viewBox="0 0 24 24"
    fill="none"
    stroke="currentColor"
    strokeWidth="2"
    strokeLinecap="round"
    strokeLinejoin="round"
  >
    <rect x="9" y="9" width="13" height="13" rx="2" ry="2" />
    <path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1" />
  </svg>
);
 
const CheckIcon = () => (
  <svg
    xmlns="http://www.w3.org/2000/svg"
    width="18"
    height="18"
    viewBox="0 0 24 24"
    fill="none"
    stroke="currentColor"
    strokeWidth="2"
    strokeLinecap="round"
    strokeLinejoin="round"
  >
    <polyline points="20 6 9 17 4 12" />
  </svg>
);
 
const Header = ({
  img = "",
  title = "",
  subTitle = "",
  authorImage = true,
  authorName = "",
  showCopyMd = false,
}: {
  img?: string;
  title?: string;
  subTitle?: string;
  authorImage?: boolean;
  authorName?: string;
  showCopyMd?: boolean;
}) => {
  const [copied, setCopied] = useState(false);
 
  const handleCopy = (e: React.MouseEvent) => {
    e.preventDefault();
    e.stopPropagation();
    const md = (window as unknown as Record<string, string>).__RAW_MD__;
    if (!md) return;
    navigator.clipboard.writeText(md).then(() => {
      setCopied(true);
      setTimeout(() => setCopied(false), 1500);
    });
  };
 
  return (
    <div
      className="col-12 header"
      style={{ padding: 0 }}
      id="header"
      data-testid="header"
    >
      <div
        className={style.imgContainer}
        style={{
          backgroundImage: `linear-gradient( rgba(0, 0, 0, 0.2), rgba(0, 0, 0, 0.2) ), url(${img})`,
        }}
      >
        {title && (
          <h1 className={style.uTitle}>
            {title}
            {showCopyMd && (
              <button
                className={`${style.copyMd} ${copied ? style.copyMdDone : ""}`}
                onClick={handleCopy}
                title={copied ? "コピーしました" : "記事をMarkdownでコピー"}
                type="button"
                aria-label="記事をMarkdownでコピー"
              >
                {copied ? <CheckIcon /> : <CopyIcon />}
              </button>
            )}
          </h1>
        )}
        {subTitle && (
          <div className={style.uSubtitle}>
            <div className={style.mLeft}>
              {authorImage && (
                <picture>
                  <source
                    className={style.authorImage}
                    srcSet="/assets/avater.webp"
                    type="image/webp"
                    onClick={() =>
                      ReactGA.event({
                        category: "User",
                        action: "push tubone Avatar",
                      })
                    }
                  />
                  <img
                    className={style.authorImage}
                    src="/assets/avater.png"
                    alt={authorName}
                    {...{ fetchpriority: "high" }}
                    onClick={() =>
                      ReactGA.event({
                        category: "User",
                        action: "push tubone Avatar",
                      })
                    }
                  />
                </picture>
              )}
              <span className={style.authorName}>{authorName}</span>
            </div>
            <span className={style.text}>{subTitle}</span>
          </div>
        )}
      </div>
    </div>
  );
};
 
export default Header;