1
0
Fork 0
blog/src/components/atoms/Changelog.tsx

89 lines
2.2 KiB
TypeScript
Raw Normal View History

2022-04-20 22:03:12 +02:00
import React, {
2023-01-29 22:58:19 +01:00
Fragment,
2022-04-20 22:03:12 +02:00
ReactElement,
createElement,
2023-01-29 22:58:19 +01:00
useEffect,
useState
2022-04-20 22:03:12 +02:00
} from 'react'
2019-10-02 13:35:50 +02:00
import { graphql, useStaticQuery } from 'gatsby'
2023-01-29 22:58:19 +01:00
import rehypeReact from 'rehype-react'
2022-04-20 22:03:12 +02:00
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
2023-01-29 22:58:19 +01:00
import { unified } from 'unified'
import * as styles from './Changelog.module.css'
2019-10-02 13:35:50 +02:00
2019-10-16 01:45:30 +02:00
export function PureChangelog({
repo,
repos
}: {
repo: string
repos: Queries.GitHubReposQuery['github']['viewer']['repositories']['edges']
}): ReactElement | null {
2022-04-20 22:03:12 +02:00
const [changelogHtml, setChangelogHtml] = useState()
2022-11-19 22:01:53 +01:00
const repoMatch = repos
.map(({ node }) => {
2019-10-02 13:35:50 +02:00
if (node.name === repo) return node
})
2022-11-19 22:01:53 +01:00
.filter((n: any) => n)[0]
2019-10-02 13:35:50 +02:00
2022-04-20 22:03:12 +02:00
useEffect(() => {
if (!(repoMatch?.object as Queries.GitHub_Blob)?.text) return
2019-10-02 13:35:50 +02:00
2022-04-20 22:03:12 +02:00
async function init() {
const changelogHtml = await unified()
.use(remarkParse)
.use(remarkRehype)
.use(rehypeReact, { createElement, Fragment })
.processSync((repoMatch?.object as Queries.GitHub_Blob).text).result
2019-10-02 13:35:50 +02:00
2022-04-20 22:03:12 +02:00
setChangelogHtml(changelogHtml)
}
init()
}, [(repoMatch?.object as Queries.GitHub_Blob)?.text])
2019-10-02 13:35:50 +02:00
2022-04-20 22:03:12 +02:00
return repoMatch ? (
<div className={styles.content} id="changelog">
2022-04-20 22:03:12 +02:00
{changelogHtml}
<p className={styles.source}>
2022-04-20 22:03:12 +02:00
sourced from{' '}
<a href={`${repoMatch?.url}/tree/main/CHANGELOG.md`}>
<code>{`${repoMatch?.owner.login}/${repo}:CHANGELOG.md`}</code>
</a>
</p>
</div>
) : null
2019-10-02 13:35:50 +02:00
}
2019-10-12 04:25:15 +02:00
2019-11-24 14:29:25 +01:00
const queryGithub = graphql`
query GitHubRepos {
2019-11-24 14:29:25 +01:00
github {
viewer {
repositories(first: 100, privacy: PUBLIC, isFork: false) {
edges {
node {
name
url
owner {
login
}
2020-07-02 20:03:35 +02:00
object(expression: "main:CHANGELOG.md") {
2019-11-24 14:29:25 +01:00
id
... on GitHub_Blob {
text
2019-10-12 04:25:15 +02:00
}
}
}
}
}
}
}
2019-11-24 14:29:25 +01:00
}
`
2020-05-22 14:38:19 +02:00
export default function Changelog({ repo }: { repo: string }): ReactElement {
const data = useStaticQuery<Queries.GitHubReposQuery>(queryGithub)
const repos = data.github.viewer.repositories.edges
2019-10-16 01:45:30 +02:00
return <PureChangelog repo={repo} repos={repos} />
2019-10-12 04:25:15 +02:00
}