notion-page.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. "use client";
  2. import { useTheme } from "next-themes";
  3. import { ExtendedRecordMap } from "notion-types";
  4. import { useMemo, useState } from "react";
  5. import { NotionRenderer } from "react-notion-x";
  6. import dynamic from "next/dynamic";
  7. import { Button } from "@heroui/button";
  8. import { RxCross2 } from "react-icons/rx";
  9. import Link from "next/link";
  10. const prismComponents = [
  11. "prism-markup-templating",
  12. "prism-markup",
  13. "prism-bash",
  14. "prism-c",
  15. "prism-cpp",
  16. "prism-csharp",
  17. "prism-docker",
  18. "prism-java",
  19. "prism-js-templates",
  20. "prism-coffeescript",
  21. "prism-diff",
  22. "prism-git",
  23. "prism-go",
  24. "prism-graphql",
  25. "prism-handlebars",
  26. "prism-less",
  27. "prism-makefile",
  28. "prism-markdown",
  29. "prism-objectivec",
  30. "prism-ocaml",
  31. "prism-python",
  32. "prism-reason",
  33. "prism-rust",
  34. "prism-sass",
  35. "prism-scss",
  36. "prism-solidity",
  37. "prism-sql",
  38. "prism-stylus",
  39. "prism-swift",
  40. "prism-wasm",
  41. "prism-yaml",
  42. ];
  43. const Code = dynamic(
  44. () =>
  45. import("react-notion-x/build/third-party/code").then(async (m) => {
  46. const importPromises = prismComponents.map(
  47. (component) => import(`prismjs/components/${component}.js`),
  48. );
  49. await Promise.allSettled(importPromises);
  50. return m.Code;
  51. }),
  52. { ssr: false },
  53. );
  54. const Collection = dynamic(
  55. () =>
  56. import("react-notion-x/build/third-party/collection").then(
  57. (m) => m.Collection,
  58. ),
  59. { ssr: false },
  60. );
  61. const Equation = dynamic(() =>
  62. import("react-notion-x/build/third-party/equation").then((m) => m.Equation),
  63. );
  64. const Pdf = dynamic(
  65. () => import("react-notion-x/build/third-party/pdf").then((m) => m.Pdf),
  66. { ssr: false },
  67. );
  68. const Modal = dynamic(
  69. () => import("react-notion-x/build/third-party/modal").then((m) => m.Modal),
  70. { ssr: false },
  71. );
  72. export const NotionPage = ({
  73. recordMap,
  74. rootPageId,
  75. title,
  76. }: {
  77. recordMap: ExtendedRecordMap;
  78. rootPageId: string;
  79. title?: string;
  80. }) => {
  81. const { theme } = useTheme();
  82. const [hover, setHover] = useState(false);
  83. const components = useMemo(
  84. () => ({
  85. Code,
  86. Collection,
  87. Equation,
  88. Pdf,
  89. Modal,
  90. }),
  91. [],
  92. );
  93. if (!recordMap) {
  94. return null;
  95. }
  96. return (
  97. <>
  98. <Link className="self-center" href="/blog" prefetch={true}>
  99. <Button
  100. isIconOnly
  101. className="dark:border-knight dark:bg-transparent dark:border-2 bg-[#ece7e7] border-0"
  102. radius="full"
  103. variant="bordered"
  104. onMouseEnter={() => setHover(true)}
  105. onMouseLeave={() => setHover(false)}
  106. >
  107. <RxCross2 />
  108. </Button>
  109. </Link>
  110. <div
  111. className={`transition duration-700 ease-in-out transform ${hover ? "translate-y-5" : ""}`}
  112. >
  113. <h1 className="my-4 text-center text-xl font-bold md:text-2xl">
  114. {title}
  115. </h1>
  116. <div className="ml-[15px] md:mx-[296px] mb-4">
  117. <h3 className="w-fit dark:border-knight dark:bg-transparent dark:border-2 bg-[#ece7e7] border-0 py-1 px-4 rounded-full">
  118. {new Date(
  119. recordMap.block[rootPageId].value?.created_time,
  120. ).toDateString()}
  121. </h3>
  122. </div>
  123. <NotionRenderer
  124. components={components}
  125. darkMode={theme === "dark"}
  126. fullPage={false}
  127. recordMap={recordMap}
  128. rootPageId={rootPageId}
  129. />
  130. </div>
  131. </>
  132. );
  133. };