Skip to content

Commit 69e8dd2

Browse files
committed
refactor: consolidate FileContributors logic
deprecate redundant CrowdinContributors and GitHubContributors components; consolidate logic to gather file contributors list into `utils/contributors.ts` and use to fetch/organize all data in getStaticProps before passing to layout/components.
1 parent f919d23 commit 69e8dd2

File tree

11 files changed

+85
-133
lines changed

11 files changed

+85
-133
lines changed

src/components/CrowdinContributors.tsx

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/components/GitHubContributors.tsx

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/layouts/Docs.tsx

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@ import { ButtonLink } from "@/components/Buttons"
2121
import CallToContribute from "@/components/CallToContribute"
2222
import Card from "@/components/Card"
2323
import Codeblock from "@/components/Codeblock"
24-
import CrowdinContributors from "@/components/CrowdinContributors"
2524
import DeveloperDocsLinks from "@/components/DeveloperDocsLinks"
2625
import DocsNav from "@/components/DocsNav"
2726
import Emoji from "@/components/Emoji"
2827
import FeedbackCard from "@/components/FeedbackCard"
29-
import GitHubContributors from "@/components/GitHubContributors"
28+
import FileContributors from "@/components/FileContributors"
3029
import GlossaryTooltip from "@/components/Glossary/GlossaryTooltip"
3130
import InfoBanner from "@/components/InfoBanner"
3231
import Link from "@/components/Link"
@@ -48,9 +47,6 @@ import YouTube from "@/components/YouTube"
4847

4948
import { getEditPath } from "@/lib/utils/editPath"
5049

51-
// Utils
52-
import { DEFAULT_LOCALE } from "@/lib/constants"
53-
5450
const Page = (props: ChildOnlyProp & Pick<FlexProps, "dir">) => (
5551
<Flex
5652
direction="column"
@@ -202,9 +198,8 @@ type DocsLayoutProps = Pick<
202198
| "slug"
203199
| "tocItems"
204200
| "lastUpdatedDate"
205-
| "crowdinContributors"
201+
| "contributors"
206202
| "contentNotTranslated"
207-
| "gitContributors"
208203
> &
209204
Required<Pick<MdPageContent, "lastUpdatedDate">> &
210205
ChildOnlyProp & {
@@ -217,20 +212,13 @@ export const DocsLayout = ({
217212
slug,
218213
tocItems,
219214
lastUpdatedDate,
220-
crowdinContributors,
215+
contributors,
221216
contentNotTranslated,
222-
gitContributors,
223217
}: DocsLayoutProps) => {
224218
const isPageIncomplete = !!frontmatter.incomplete
225219
const { asPath: relativePath } = useRouter()
226220
const absoluteEditPath = getEditPath(relativePath)
227221

228-
const gitHubLastEdit = gitContributors[0]?.date
229-
const intlLastEdit = gitHubLastEdit || lastUpdatedDate
230-
231-
const useGitHubContributors =
232-
frontmatter.lang === DEFAULT_LOCALE || crowdinContributors.length === 0
233-
234222
return (
235223
<Page>
236224
<SideNavMobile path={relativePath} />
@@ -243,18 +231,10 @@ export const DocsLayout = ({
243231
<SideNav path={relativePath} />
244232
<Content>
245233
<H1 id="top">{frontmatter.title}</H1>
246-
{useGitHubContributors ? (
247-
<GitHubContributors
248-
lastUpdatedDate={lastUpdatedDate}
249-
contributors={gitContributors}
250-
/>
251-
) : (
252-
<CrowdinContributors
253-
relativePath={relativePath}
254-
lastUpdatedDate={intlLastEdit}
255-
contributors={crowdinContributors}
256-
/>
257-
)}
234+
<FileContributors
235+
contributors={contributors}
236+
lastEdit={lastUpdatedDate}
237+
/>
258238
<TableOfContents
259239
slug={slug}
260240
editPath={absoluteEditPath}

src/layouts/Tutorial.tsx

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,17 @@ import {
1212
useToken,
1313
} from "@chakra-ui/react"
1414

15-
import type { ChildOnlyProp, TranslationKey } from "@/lib/types"
15+
import type { ChildOnlyProp } from "@/lib/types"
1616
import type { MdPageContent, TutorialFrontmatter } from "@/lib/interfaces"
1717

1818
import { ButtonLink } from "@/components/Buttons"
1919
import CallToContribute from "@/components/CallToContribute"
2020
import Card from "@/components/Card"
2121
import Codeblock from "@/components/Codeblock"
22-
import CrowdinContributors from "@/components/CrowdinContributors"
2322
import Emoji from "@/components/Emoji"
2423
import EnvWarningBanner from "@/components/EnvWarningBanner"
2524
import FeedbackCard from "@/components/FeedbackCard"
26-
import GitHubContributors from "@/components/GitHubContributors"
25+
import FileContributors from "@/components/FileContributors"
2726
import GlossaryTooltip from "@/components/Glossary/GlossaryTooltip"
2827
import InfoBanner from "@/components/InfoBanner"
2928
import MainArticle from "@/components/MainArticle"
@@ -41,8 +40,6 @@ import YouTube from "@/components/YouTube"
4140

4241
import { getEditPath } from "@/lib/utils/editPath"
4342

44-
import { DEFAULT_LOCALE } from "@/lib/constants"
45-
4643
type ContentContainerProps = Pick<BoxProps, "children" | "dir">
4744

4845
const ContentContainer = (props: ContentContainerProps) => {
@@ -157,13 +154,7 @@ export const tutorialsComponents = {
157154
YouTube,
158155
}
159156
type TutorialLayoutProps = ChildOnlyProp &
160-
Pick<
161-
MdPageContent,
162-
| "tocItems"
163-
| "crowdinContributors"
164-
| "contentNotTranslated"
165-
| "gitContributors"
166-
> &
157+
Pick<MdPageContent, "tocItems" | "contributors" | "contentNotTranslated"> &
167158
Required<Pick<MdPageContent, "lastUpdatedDate">> & {
168159
frontmatter: TutorialFrontmatter
169160
timeToRead: number
@@ -175,19 +166,13 @@ export const TutorialLayout = ({
175166
tocItems,
176167
timeToRead,
177168
lastUpdatedDate,
178-
crowdinContributors,
169+
contributors,
179170
contentNotTranslated,
180-
gitContributors,
181171
}: TutorialLayoutProps) => {
182172
const { asPath: relativePath } = useRouter()
183173
const absoluteEditPath = getEditPath(relativePath)
184174

185175
const borderColor = useToken("colors", "border")
186-
const gitHubLastEdit = gitContributors[0]?.date
187-
const intlLastEdit = gitHubLastEdit || lastUpdatedDate
188-
189-
const useGitHubContributors =
190-
frontmatter.lang === DEFAULT_LOCALE || crowdinContributors.length === 0
191176

192177
return (
193178
<>
@@ -209,18 +194,10 @@ export const TutorialLayout = ({
209194
pt={8}
210195
/>
211196
{children}
212-
{useGitHubContributors ? (
213-
<GitHubContributors
214-
contributors={gitContributors}
215-
lastUpdatedDate={lastUpdatedDate}
216-
/>
217-
) : (
218-
<CrowdinContributors
219-
relativePath={relativePath}
220-
lastUpdatedDate={intlLastEdit}
221-
contributors={crowdinContributors}
222-
/>
223-
)}
197+
<FileContributors
198+
contributors={contributors}
199+
lastEdit={lastUpdatedDate}
200+
/>
224201
<FeedbackCard />
225202
</ContentContainer>
226203
{tocItems && (

src/lib/api/fetchGitHistory.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ async function fetchWithRateLimit(
4949
}
5050

5151
// Fetch commit history and save it to a JSON file
52-
export const fetchAndCacheGitContributors = async (mdDir: string) => {
52+
export const fetchAndCacheGitContributors = async (
53+
mdDir: string
54+
): Promise<FileContributor[]> => {
5355
const filepath = join("/", mdDir, "index.md")
5456

5557
// Load cache

src/lib/interfaces.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ export interface MdPageContent {
107107
tocItems: ToCItem[]
108108
lastUpdatedDate?: string
109109
contentNotTranslated: boolean
110-
crowdinContributors: CrowdinContributor[]
111-
gitContributors: FileContributor[]
110+
contributors: FileContributor[]
112111
}
113112

114113
// Local environment framework

src/lib/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ export type FileContributor = {
374374
login: string
375375
avatar_url: string
376376
html_url: string
377-
date: string
377+
date?: string
378378
}
379379

380380
/**

src/lib/utils/contributors.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import type { FileContributor, Lang, Layout } from "@/lib/types"
2+
3+
import { DEFAULT_LOCALE } from "@/lib/constants"
4+
5+
import {
6+
convertToFileContributorFromCrowdin,
7+
getCrowdinContributors,
8+
} from "./crowdin"
9+
import { getLastModifiedDate } from "./gh"
10+
11+
import { fetchAndCacheGitContributors } from "@/lib/api/fetchGitHistory"
12+
13+
export const getFileContributorInfo = async (
14+
mdDir: string,
15+
mdPath: string,
16+
slug: string,
17+
locale: string,
18+
fileLang: string,
19+
layout: Layout
20+
) => {
21+
const gitContributors = await fetchAndCacheGitContributors(mdDir)
22+
23+
const latestCommitDate = getLastModifiedDate(slug, locale!)
24+
const gitHubLastEdit = gitContributors[0]?.date
25+
const lastUpdatedDate = gitHubLastEdit || latestCommitDate
26+
27+
const crowdinContributors = ["docs", "tutorial"].includes(layout)
28+
? convertToFileContributorFromCrowdin(
29+
getCrowdinContributors(mdPath, locale as Lang)
30+
)
31+
: []
32+
33+
const useGitHubContributors: boolean =
34+
fileLang === DEFAULT_LOCALE || crowdinContributors.length === 0
35+
36+
const contributors: FileContributor[] = useGitHubContributors
37+
? gitContributors
38+
: crowdinContributors
39+
40+
return { contributors, lastUpdatedDate }
41+
}

src/lib/utils/crowdin.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { CrowdinContributor, Lang } from "@/lib/types"
1+
import type { CrowdinContributor, FileContributor, Lang } from "@/lib/types"
22

33
import translators from "@/data/crowdin/combined-translators.json"
44
import fileIds from "@/data/crowdin/file-ids.json"
@@ -22,3 +22,12 @@ export const getCrowdinContributors = (
2222
const data = translators.filter(({ lang }) => lang === locale)[0]?.data ?? []
2323
return data.filter(({ fileId }) => +fileId === _fileId)[0]?.contributors ?? []
2424
}
25+
26+
export const convertToFileContributorFromCrowdin = (
27+
contributors: CrowdinContributor[]
28+
): FileContributor[] =>
29+
contributors.map(({ username, avatarUrl }) => ({
30+
login: username,
31+
avatar_url: avatarUrl,
32+
html_url: `https://crowdin.com/profile/${username}`,
33+
}))

src/lib/utils/md.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,10 +332,7 @@ export const getContentBySlug = (slug: string) => {
332332
const fileContents = fs.readFileSync(fullPath, "utf8")
333333
const { data, content } = matter(fileContents)
334334
const frontmatter = data as Frontmatter
335-
const items: Omit<
336-
MdPageContent,
337-
"tocItems" | "crowdinContributors" | "gitContributors"
338-
> = {
335+
const items: Omit<MdPageContent, "tocItems" | "contributors"> = {
339336
slug,
340337
content,
341338
frontmatter,

0 commit comments

Comments
 (0)