|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useState, useEffect } from 'react'; |
| 4 | +import type { FC, ReactElement } from 'react'; |
| 5 | +import { VFile } from 'vfile'; |
| 6 | + |
| 7 | +import ChangelogModal from '@/components/Downloads/ChangelogModal'; |
| 8 | +import changelogData from '@/next-data/changelogData'; |
| 9 | +import { compileMDX } from '@/next.mdx.compiler.mjs'; |
| 10 | +import { clientMdxComponents, htmlComponents } from '@/next.mdx.use.client.mjs'; |
| 11 | +import type { NodeRelease } from '@/types'; |
| 12 | +import { |
| 13 | + getNodeJsChangelogAuthor, |
| 14 | + getNodeJsChangelogSlug, |
| 15 | +} from '@/util/getNodeJsChangelog'; |
| 16 | +import { getGitHubAvatarUrl } from '@/util/gitHubUtils'; |
| 17 | + |
| 18 | +type WithChangelogModalProps = { |
| 19 | + release: NodeRelease; |
| 20 | + modalOpen: boolean; |
| 21 | + setModalOpen: (open: boolean) => void; |
| 22 | +}; |
| 23 | + |
| 24 | +// We only need the base components for our ChangelogModal, this avoids/eliminates |
| 25 | +// the need of Next.js bundling on the client-side all our MDX components |
| 26 | +// Note that this already might increase the client-side bundle due to Shiki |
| 27 | +const clientComponents = { ...clientMdxComponents, ...htmlComponents }; |
| 28 | + |
| 29 | +const WithChangelogModal: FC<WithChangelogModalProps> = ({ |
| 30 | + release: { versionWithPrefix }, |
| 31 | + modalOpen, |
| 32 | + setModalOpen, |
| 33 | +}) => { |
| 34 | + const [ChangelogMDX, setChangelogMDX] = useState<ReactElement>(); |
| 35 | + const [changelog, setChangelog] = useState<string>(''); |
| 36 | + |
| 37 | + useEffect(() => { |
| 38 | + let isCancelled = false; |
| 39 | + |
| 40 | + const fetchChangelog = async () => { |
| 41 | + try { |
| 42 | + const data = await changelogData(versionWithPrefix); |
| 43 | + |
| 44 | + // We need to check if the component is still mounted before setting the state |
| 45 | + if (!isCancelled) { |
| 46 | + setChangelog(data); |
| 47 | + |
| 48 | + // This removes the <h2> header from the changelog content, as we already |
| 49 | + // render the changelog heading as the "ChangelogModal" subheading |
| 50 | + const changelogWithoutHeader = data.split('\n').slice(2).join('\n'); |
| 51 | + |
| 52 | + compileMDX(new VFile(changelogWithoutHeader), 'md').then( |
| 53 | + ({ MDXContent }) => { |
| 54 | + // This is a tricky one. React states does not allow you to actually store React components |
| 55 | + // hence we need to render the component within an Effect and set the state as a ReactElement |
| 56 | + // which is a function that can be eval'd by React during runtime. |
| 57 | + const renderedElement = ( |
| 58 | + <MDXContent components={clientComponents} /> |
| 59 | + ); |
| 60 | + |
| 61 | + setChangelogMDX(renderedElement); |
| 62 | + } |
| 63 | + ); |
| 64 | + } |
| 65 | + } catch (_) { |
| 66 | + throw new Error(`Failed to fetch changelog for, ${versionWithPrefix}`); |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + if (modalOpen && versionWithPrefix) { |
| 71 | + fetchChangelog(); |
| 72 | + } |
| 73 | + |
| 74 | + return () => { |
| 75 | + isCancelled = true; |
| 76 | + }; |
| 77 | + }, [modalOpen, versionWithPrefix]); |
| 78 | + |
| 79 | + const author = getNodeJsChangelogAuthor(changelog); |
| 80 | + const slug = getNodeJsChangelogSlug(changelog); |
| 81 | + |
| 82 | + const modalProps = { |
| 83 | + heading: `Node.js ${versionWithPrefix}`, |
| 84 | + avatars: [{ src: getGitHubAvatarUrl(author), alt: author }], |
| 85 | + subheading: slug, |
| 86 | + open: modalOpen && typeof ChangelogMDX !== 'undefined', |
| 87 | + onOpenChange: setModalOpen, |
| 88 | + }; |
| 89 | + |
| 90 | + return ( |
| 91 | + <ChangelogModal {...modalProps}> |
| 92 | + <main>{ChangelogMDX}</main> |
| 93 | + </ChangelogModal> |
| 94 | + ); |
| 95 | +}; |
| 96 | + |
| 97 | +export default WithChangelogModal; |
0 commit comments