Skip to content

Commit fcd8257

Browse files
authored
Merge pull request #137 from ethereum/patch-layout-type
Add `Layout` type
2 parents 860908c + 1803798 commit fcd8257

File tree

3 files changed

+46
-40
lines changed

3 files changed

+46
-40
lines changed

src/lib/interfaces.ts

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
import type { Frontmatter, Lang, TranslationKey, ToCItem } from "@/lib/types"
2-
import { ReactNode } from "react"
1+
import type { ReactNode } from "react"
2+
3+
import type {
4+
Frontmatter,
5+
Lang,
6+
TranslationKey,
7+
ToCItem,
8+
Layout,
9+
} from "@/lib/types"
310

411
/**
512
* Quiz data interfaces
@@ -19,7 +26,7 @@ export interface Answer {
1926

2027
export interface RawQuestion {
2128
prompt: TranslationKey
22-
answers: Array<Answer>
29+
answers: Answer[]
2330
correctAnswerId: string
2431
}
2532

@@ -33,12 +40,12 @@ export interface QuestionBank {
3340

3441
export interface RawQuiz {
3542
title: TranslationKey
36-
questions: Array<string> // TODO: Force to be an array of questionID's
43+
questions: string[] // TODO: Force to be an array of questionID's
3744
}
3845

3946
export interface Quiz {
4047
title: string
41-
questions: Array<Question>
48+
questions: Question[]
4249
}
4350

4451
export interface RawQuizzes {
@@ -50,7 +57,7 @@ export interface DeveloperDocsLink {
5057
to: string
5158
path: string
5259
description: TranslationKey
53-
items: Array<DeveloperDocsLink>
60+
items: DeveloperDocsLink[]
5461
}
5562

5663
/**
@@ -62,7 +69,7 @@ export interface SharedFrontmatter {
6269
lang: Lang
6370
sidebarDepth?: number
6471
isOutdated?: boolean
65-
template?: string
72+
template?: Layout
6673
}
6774

6875
export interface StaticFrontmatter extends SharedFrontmatter {
@@ -72,7 +79,7 @@ export interface StaticFrontmatter extends SharedFrontmatter {
7279

7380
/**
7481
* TODO: Refactor markdown content that currently uses SummaryPointsNumbered
75-
* to use SummaryPoints (`summaryPoints: Array<string>`) instead. Then
82+
* to use SummaryPoints (`summaryPoints: string[]`) instead. Then
7683
* deprecate @/lib/util/getSummaryPoints.ts
7784
*/
7885
export interface SummaryPointsNumbered {
@@ -83,7 +90,7 @@ export interface SummaryPointsNumbered {
8390
}
8491

8592
interface SummaryPoints {
86-
summaryPoints: Array<string>
93+
summaryPoints: string[]
8794
}
8895

8996
interface ImageInfo {
@@ -93,29 +100,29 @@ interface ImageInfo {
93100

94101
export interface UpgradeFrontmatter
95102
extends SharedFrontmatter,
96-
SummaryPointsNumbered,
97-
ImageInfo {}
103+
SummaryPointsNumbered,
104+
ImageInfo { }
98105

99106
export interface RoadmapFrontmatter extends SharedFrontmatter, ImageInfo {
100-
buttons: Array<{
107+
buttons: {
101108
label: string
102109
toId?: string
103110
to?: string
104111
variant?: string
105-
}>
112+
}[]
106113
}
107114

108115
export interface UseCasesFrontmatter
109116
extends SharedFrontmatter,
110-
SummaryPointsNumbered,
111-
ImageInfo {
117+
SummaryPointsNumbered,
118+
ImageInfo {
112119
emoji: string
113120
}
114121

115122
export interface StakingFrontmatter
116123
extends SharedFrontmatter,
117-
SummaryPoints,
118-
ImageInfo {
124+
SummaryPoints,
125+
ImageInfo {
119126
emoji: string
120127
}
121128

@@ -125,7 +132,7 @@ export interface DocsFrontmatter extends SharedFrontmatter {
125132
}
126133

127134
export interface TutorialFrontmatter extends SharedFrontmatter {
128-
tags: Array<string>
135+
tags: string[]
129136
author: string
130137
source?: string
131138
sourceUrl?: string
@@ -148,7 +155,7 @@ export interface MdPageContent {
148155
slug: string
149156
content: string
150157
frontmatter: Frontmatter
151-
tocItems: Array<ToCItem>
158+
tocItems: ToCItem[]
152159
lastUpdatedDate?: string
153160
contentNotTranslated: boolean
154161
}

src/lib/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import type {
1414
} from "@/lib/interfaces"
1515
import { Options } from "mdast-util-toc"
1616

17+
import { layoutMapping } from "@/pages/[...slug]"
18+
1719
export type ChildOnlyProp = { children?: ReactNode }
1820

1921
export type NextPageWithLayout<P = {}, IP = P> = NextPage<P, IP> & {
@@ -32,6 +34,9 @@ export type Frontmatter = RoadmapFrontmatter &
3234
DocsFrontmatter &
3335
TutorialFrontmatter
3436

37+
export type LayoutMappingType = typeof layoutMapping
38+
export type Layout = keyof LayoutMappingType
39+
3540
export type Lang =
3641
| "en"
3742
| "ar"

src/pages/[...slug].tsx

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ import { serialize } from "next-mdx-remote/serialize"
1313
import readingTime from "reading-time"
1414
import remarkGfm from "remark-gfm"
1515

16-
import type { NextPageWithLayout, TocNodeType } from "@/lib/types"
16+
import type {
17+
Layout,
18+
LayoutMappingType,
19+
NextPageWithLayout,
20+
TocNodeType,
21+
} from "@/lib/types"
1722

1823
import mdComponents from "@/components/MdComponents"
1924
import PageMetadata from "@/components/PageMetadata"
@@ -49,7 +54,7 @@ interface Params extends ParsedUrlQuery {
4954
slug: string[]
5055
}
5156

52-
const layoutMapping = {
57+
export const layoutMapping = {
5358
static: StaticLayout,
5459
"use-cases": UseCasesLayout,
5560
staking: StakingLayout,
@@ -60,8 +65,6 @@ const layoutMapping = {
6065
// event: EventLayout,
6166
}
6267

63-
type LayoutMappingType = typeof layoutMapping
64-
6568
const componentsMapping = {
6669
static: staticComponents,
6770
"use-cases": useCasesComponents,
@@ -92,10 +95,7 @@ export const getStaticPaths = (({ locales }) => {
9295
}
9396
}) satisfies GetStaticPaths<Params>
9497

95-
type Props = Omit<
96-
Parameters<LayoutMappingType[keyof LayoutMappingType]>[0],
97-
"children"
98-
> &
98+
type Props = Omit<Parameters<LayoutMappingType[Layout]>[0], "children"> &
9999
SSRConfig & {
100100
mdxSource: MDXRemoteSerializeResult
101101
}
@@ -136,11 +136,9 @@ export const getStaticProps = (async (context) => {
136136
const lastDeployDate = getLastDeployDate()
137137

138138
// Get corresponding layout
139-
let layout = frontmatter.template as keyof LayoutMappingType
139+
let layout = (frontmatter.template as Layout) ?? "static"
140140

141141
if (!frontmatter.template) {
142-
layout = "static"
143-
144142
if (params.slug.includes("docs")) {
145143
layout = "docs"
146144
}
@@ -177,7 +175,10 @@ const ContentPage: NextPageWithLayout<
177175
> = ({ mdxSource, layout }) => {
178176
// TODO: Address component typing error here (flip `FC` types to prop object types)
179177
// @ts-expect-error
180-
const components: Record<string, React.ReactNode> = { ...mdComponents, ...componentsMapping[layout] }
178+
const components: Record<string, React.ReactNode> = {
179+
...mdComponents,
180+
...componentsMapping[layout],
181+
}
181182
return (
182183
<>
183184
<MDXRemote {...mdxSource} components={components} />
@@ -188,15 +189,8 @@ const ContentPage: NextPageWithLayout<
188189
// Per-Page Layouts: https://nextjs.org/docs/pages/building-your-application/routing/pages-and-layouts#with-typescript
189190
ContentPage.getLayout = (page) => {
190191
// values returned by `getStaticProps` method and passed to the page component
191-
const {
192-
slug,
193-
frontmatter,
194-
lastUpdatedDate,
195-
layout,
196-
timeToRead,
197-
tocItems,
198-
} = page.props
199-
192+
const { slug, frontmatter, lastUpdatedDate, layout, timeToRead, tocItems } =
193+
page.props
200194

201195
const layoutProps = {
202196
slug,

0 commit comments

Comments
 (0)