All files / components/Utils Plaintext.jsx

100% Statements 17/17
100% Branches 5/5
100% Functions 1/1
100% Lines 17/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 241x   1x   1x 588x 1x 1x 587x 588x 588x 588x 588x 588x 588x 588x 588x 588x         588x  
import { Fragment } from "react";
 
// based in part on this SO answer: https://codereview.stackexchange.com/a/211511
 
export default function Plaintext({ text }) {
  if (text == null) {
    return <pre data-testid="plaintext-empty"></pre>;
  }
  const textToRender =
    typeof text === "string" ? text : JSON.stringify(text, null, 2);
  const [firstLine, ...rest] = textToRender.split("\n");
  return (
    <pre data-testid="plaintext">
      <span>{firstLine}</span>
      {rest.map((line, i) => (
        <Fragment key={i}>
          <br />
          <span>{line}</span>
        </Fragment>
      ))}
    </pre>
  );
}