|
| 1 | +import React, { useCallback, useMemo, useState } from "react"; |
| 2 | +import { Link } from "gatsby"; |
| 3 | +import { Prism as SyntaxHighlighter } from "react-syntax-highlighter"; |
| 4 | +import { tomorrow as codeStyle } from "react-syntax-highlighter/dist/esm/styles/prism"; |
| 5 | +import analytics from "./../utils/analytics.js"; |
| 6 | +import { Composes, Content, Hook, Info, Links, LinksLi, Name } from "./styled"; |
| 7 | + |
| 8 | +const PostTemplate = ({ content, frontmatter, slug, permalink }) => { |
| 9 | + const extraLinks = frontmatter.links || []; |
| 10 | + const [codeKey, setCodeKey] = useState("code"); |
| 11 | + const isCodeSwitchAvailable = useMemo(() => Boolean(frontmatter.tsCode), []); |
| 12 | + |
| 13 | + const handleSwitchCodeClick = useCallback(() => { |
| 14 | + setCodeKey(key => (key === "code" ? "tsCode" : "code")); |
| 15 | + }, []); |
| 16 | + |
| 17 | + return ( |
| 18 | + <Hook id={frontmatter.title}> |
| 19 | + <Name> |
| 20 | + <i className="fas fa-link link-icon" /> |
| 21 | + <Link to={slug}>{frontmatter.title}</Link> |
| 22 | + </Name> |
| 23 | + |
| 24 | + {frontmatter.composes && ( |
| 25 | + <Composes> |
| 26 | + Composes:{" "} |
| 27 | + {frontmatter.composes.map((title, i) => ( |
| 28 | + <> |
| 29 | + <Link to={`/${title}`}>{title}</Link> |
| 30 | + {i < frontmatter.composes.length - 1 ? "," : ""}{" "} |
| 31 | + </> |
| 32 | + ))} |
| 33 | + </Composes> |
| 34 | + )} |
| 35 | + |
| 36 | + <Content dangerouslySetInnerHTML={{ __html: content }} /> |
| 37 | + {isCodeSwitchAvailable && ( |
| 38 | + <Content> |
| 39 | + <button |
| 40 | + className="button is-secondary has-text-weight-semibold" |
| 41 | + onClick={handleSwitchCodeClick} |
| 42 | + > |
| 43 | + View in {codeKey === "code" ? "TypeScript" : "JavaScript"} |
| 44 | + </button> |
| 45 | + </Content> |
| 46 | + )} |
| 47 | + <SyntaxHighlighter |
| 48 | + language="javascript" |
| 49 | + style={codeStyle} |
| 50 | + customStyle={{ |
| 51 | + borderRadius: "10px", |
| 52 | + padding: "1.5em", |
| 53 | + fontSize: "14px" |
| 54 | + }} |
| 55 | + > |
| 56 | + {frontmatter[codeKey]} |
| 57 | + </SyntaxHighlighter> |
| 58 | + |
| 59 | + {(permalink === true || extraLinks.length > 0) && ( |
| 60 | + <Links> |
| 61 | + <div className="links-title"> |
| 62 | + <span role="img" aria-label="books"> |
| 63 | + 📚 |
| 64 | + </span> |
| 65 | + Also check out: |
| 66 | + </div> |
| 67 | + <ul> |
| 68 | + {extraLinks.map((link, i) => ( |
| 69 | + <LinksLi key={i}> |
| 70 | + <a |
| 71 | + target={link.target || "_blank"} |
| 72 | + href={link.url} |
| 73 | + onClick={() => { |
| 74 | + analytics.track("clickExtraLink"); |
| 75 | + }} |
| 76 | + > |
| 77 | + {link.name} |
| 78 | + </a>{" "} |
| 79 | + -{" "} |
| 80 | + <span |
| 81 | + dangerouslySetInnerHTML={{ |
| 82 | + __html: link.description |
| 83 | + }} |
| 84 | + /> |
| 85 | + </LinksLi> |
| 86 | + ))} |
| 87 | + |
| 88 | + {permalink === true && ( |
| 89 | + <LinksLi key="divjoy"> |
| 90 | + <a |
| 91 | + href="https://divjoy.com?utm_source=usehooks&utm_medium=website&utm_campaign=usehooks-post-links" |
| 92 | + onClick={() => { |
| 93 | + analytics.track("clickExtraDivjoyLink"); |
| 94 | + }} |
| 95 | + > |
| 96 | + Divjoy |
| 97 | + </a>{" "} |
| 98 | + -{" "} |
| 99 | + <span>React starter kit from the creator of usehooks.com</span> |
| 100 | + </LinksLi> |
| 101 | + )} |
| 102 | + </ul> |
| 103 | + </Links> |
| 104 | + )} |
| 105 | + |
| 106 | + <Info> |
| 107 | + <div className="level-item">{frontmatter.date}</div> |
| 108 | + <div className="level-item is-hidden-mobile"> |
| 109 | + <span>•</span> |
| 110 | + </div> |
| 111 | + {frontmatter.sandbox && ( |
| 112 | + <> |
| 113 | + <div className="level-item"> |
| 114 | + <a target="_blank" href={frontmatter.sandbox}> |
| 115 | + Open in CodeSandbox |
| 116 | + </a> |
| 117 | + </div> |
| 118 | + <div className="level-item is-hidden-mobile"> |
| 119 | + <span>•</span> |
| 120 | + </div> |
| 121 | + </> |
| 122 | + )} |
| 123 | + <div className="level-item"> |
| 124 | + <a target="_blank" href={frontmatter.gist}> |
| 125 | + Suggest a change |
| 126 | + </a> |
| 127 | + </div> |
| 128 | + </Info> |
| 129 | + </Hook> |
| 130 | + ); |
| 131 | +}; |
| 132 | + |
| 133 | +PostTemplate.displayName = "PostTemplate"; |
| 134 | + |
| 135 | +export default PostTemplate; |
0 commit comments