notion-page.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. "use client";
  2. import { useTheme } from "next-themes";
  3. import { ExtendedRecordMap } from "notion-types";
  4. import { useEffect, useMemo, useState } from "react";
  5. import { NotionRenderer } from "react-notion-x";
  6. import dynamic from "next/dynamic";
  7. import { Button } from "@nextui-org/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. {
  53. ssr: false,
  54. }
  55. );
  56. const Collection = dynamic(
  57. () =>
  58. import("react-notion-x/build/third-party/collection").then(
  59. (m) => m.Collection
  60. ),
  61. {
  62. ssr: false,
  63. }
  64. );
  65. const Equation = dynamic(() =>
  66. import("react-notion-x/build/third-party/equation").then((m) => m.Equation)
  67. );
  68. const Pdf = dynamic(
  69. () => import("react-notion-x/build/third-party/pdf").then((m) => m.Pdf),
  70. {
  71. ssr: false,
  72. }
  73. );
  74. const Modal = dynamic(
  75. () => import("react-notion-x/build/third-party/modal").then((m) => m.Modal),
  76. {
  77. ssr: false,
  78. }
  79. );
  80. export const NotionPage = ({
  81. recordMap,
  82. rootPageId,
  83. title,
  84. }: {
  85. recordMap: ExtendedRecordMap;
  86. rootPageId: string;
  87. title?: string;
  88. }) => {
  89. const [isClient, setIsClient] = useState(false);
  90. const { theme } = useTheme();
  91. const [hover, setHover] = useState(false);
  92. const components = useMemo(
  93. () => ({
  94. Code,
  95. Collection,
  96. Equation,
  97. Pdf,
  98. Modal,
  99. }),
  100. []
  101. );
  102. useEffect(() => {
  103. setIsClient(true);
  104. }, []);
  105. if (!isClient) {
  106. return null;
  107. }
  108. if (!recordMap) {
  109. return null;
  110. }
  111. return (
  112. <>
  113. <Link className="self-center" href="/blog">
  114. <Button
  115. isIconOnly
  116. className="dark:border-knight dark:bg-transparent dark:border-2 bg-[#ece7e7] border-0"
  117. radius="full"
  118. variant="bordered"
  119. onMouseEnter={() => setHover(true)}
  120. onMouseLeave={() => setHover(false)}
  121. >
  122. <RxCross2 />
  123. </Button>
  124. </Link>
  125. <div
  126. className={`transition duration-700 ease-in-out transform ${hover ? "translate-y-5" : ""}`}
  127. >
  128. <h1 className="my-4 text-center text-xl font-bold md:text-2xl">
  129. {title}
  130. </h1>
  131. <div className="ml-[15px] md:mx-[296px] mb-4">
  132. <h3 className="w-fit dark:border-knight dark:bg-transparent dark:border-2 bg-[#ece7e7] border-0 py-1 px-4 rounded-full">
  133. {new Date(
  134. recordMap.block[rootPageId].value?.created_time
  135. ).toDateString()}
  136. </h3>
  137. </div>
  138. <NotionRenderer
  139. components={components}
  140. darkMode={theme === "dark"}
  141. fullPage={false}
  142. recordMap={recordMap}
  143. rootPageId={rootPageId}
  144. />
  145. </div>
  146. </>
  147. );
  148. };