Skip to content

Commit

Permalink
fix: mdx page dates (#914)
Browse files Browse the repository at this point in the history
Fixes issue with dates on MDX pages not reflecting the current file's
last modified date. The root cause was that we weren't passing in the
path when listing the most recent commit, resulting in reading the most
recent commit for the entire repo.

## Demo

### Before

<img width="225" alt="image"
src="https://github.com/user-attachments/assets/1bdfa937-858e-43f2-95ed-0e3e01535ff5">

### After

<img width="257" alt="image"
src="https://github.com/user-attachments/assets/9f993a00-eb2e-4d6d-948d-b605279cf442">
  • Loading branch information
codemonkey800 authored Jul 25, 2024
1 parent df26183 commit 85b02a2
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 400 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import dayjs from 'dayjs'
import { ReactNode } from 'react'

import { useI18n } from 'app/hooks/useI18n'
import { useMdxFile } from 'app/hooks/useMdxFile'

export function MdxPageTitle({ children }: { children: ReactNode }) {
export function MdxPageTitle({
children,
lastModified,
}: {
children: ReactNode
lastModified: string
}) {
const { t } = useI18n()
const { lastModified } = useMdxFile()

return (
<div className="flex flex-col gap-sds-xs mb-sds-xxl">
Expand All @@ -16,7 +19,7 @@ export function MdxPageTitle({ children }: { children: ReactNode }) {

{lastModified && (
<p className="text-sds-body-xxs leading-sds-body-xxs text-sds-gray-600">
{t('lastUpdated')}: {dayjs(lastModified).format('MMMM DD, YYYY')}
{t('lastUpdated')}: {lastModified}
</p>
)}
</div>
Expand Down
5 changes: 1 addition & 4 deletions frontend/packages/data-portal/app/hooks/useMdxFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,5 @@ import { MDXRemoteSerializeResult } from 'next-mdx-remote'
import { useTypedLoaderData } from 'remix-typedjson'

export function useMdxFile() {
return useTypedLoaderData<{
content: MDXRemoteSerializeResult
lastModified: Date | null
}>()
return useTypedLoaderData<{ content: MDXRemoteSerializeResult }>()
}
35 changes: 9 additions & 26 deletions frontend/packages/data-portal/app/utils/repo.server.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,27 @@
import rehypePrism from '@mapbox/rehype-prism'
import axios, { AxiosResponse } from 'axios'
import axios from 'axios'
import { readFileSync } from 'fs'
import { serialize } from 'next-mdx-remote/serialize'
import { Octokit } from 'octokit'
import { dirname, resolve } from 'path'
import remarkGfm from 'remark-gfm'
import sectionize from 'remark-sectionize'
import { typedjson } from 'remix-typedjson'
import { fileURLToPath } from 'url'

const octokit = new Octokit()

export interface RepoFile {
content: string
lastModified: Date | null
}

export async function getRepoFileContent(path: string): Promise<RepoFile> {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
const response = (await axios.get(
export async function getRepoFileContent(path: string): Promise<string> {
const response = await axios.get(
`https://raw.githubusercontent.com/chanzuckerberg/cryoet-data-portal/main/${path}`,
)) as AxiosResponse

const data = await octokit.rest.repos.listCommits({
owner: 'chanzuckerberg',
repo: 'cryoet-data-portal',
per_page: 1,
})

const date = data.data[0].commit.committer?.date
)

return {
content: response.data as string,
lastModified: date ? new Date(date) : null,
}
return response.data as string
}

async function serializeMdx(content: string, lastModified: Date | null) {
async function serializeMdx(content: string) {
return typedjson({
lastModified,
content: await serialize(content, {
mdxOptions: {
remarkPlugins: [sectionize, remarkGfm],
Expand All @@ -49,9 +32,9 @@ async function serializeMdx(content: string, lastModified: Date | null) {
}

export async function getRepoFileContentResponse(path: string) {
const { content, lastModified } = await getRepoFileContent(path)
const content = await getRepoFileContent(path)

return serializeMdx(content, lastModified)
return serializeMdx(content)
}

export async function getLocalFileContent(path: string) {
Expand All @@ -61,5 +44,5 @@ export async function getLocalFileContent(path: string) {
'utf-8',
)

return serializeMdx(mdxContent, null)
return serializeMdx(mdxContent)
}
1 change: 0 additions & 1 deletion frontend/packages/data-portal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
"lodash-es": "^4.17.21",
"morgan": "^1.10.0",
"next-mdx-remote": "^4.4.1",
"octokit": "^3.1.2",
"pretty-bytes": "^6.1.1",
"prop-types": "^15.8.1",
"react": "^18.2.0",
Expand Down
Loading

0 comments on commit 85b02a2

Please sign in to comment.