Skip to content

Commit 90766c4

Browse files
authored
Merge pull request #149 from Developer-DAO/fix/hydration-on-lessons-page
Squash React hydration errors on Lessons index page and Pomodoro timer
2 parents c878114 + b188739 commit 90766c4

File tree

2 files changed

+48
-21
lines changed

2 files changed

+48
-21
lines changed

components/PomodoroTimer.tsx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
PopoverArrow,
1313
useMediaQuery,
1414
} from '@chakra-ui/react'
15-
import { ReactElement, useReducer } from 'react'
15+
import { ReactElement, useEffect, useReducer, useState } from 'react'
1616
import { FaGraduationCap } from 'react-icons/fa'
1717
import { IoTimerOutline } from 'react-icons/io5'
1818
import {
@@ -137,13 +137,26 @@ export const PomodoroTimer = (props: any) => {
137137
const { isRunning, status, timeRemaining } = state
138138

139139
const { variant, ...rest } = props
140-
const [isBigScreen] = useMediaQuery('(min-width: 62em)')
141-
const maxPopoverWidth = isBigScreen ? '14rem' : '11rem'
140+
const [isBigScreenMediaQuery] = useMediaQuery('(min-width: 62em)')
141+
const [isBigScreen, setIsBigScreen] = useState(false)
142+
const [maxPopoverWidth, setMaxPopoverWidth] = useState('14rem')
143+
144+
useEffect(() => {
145+
setIsBigScreen(isBigScreenMediaQuery)
146+
}, [isBigScreenMediaQuery])
147+
148+
useEffect(() => {
149+
setMaxPopoverWidth(isBigScreen ? '14rem' : '11rem')
150+
}, [isBigScreen])
142151

143152
const timerStyle = useStyleConfig('PomodoroTimer', { variant })
144153
const iconStyle = useStyleConfig('PomodoroIcon', { variant })
145154

146-
const getButton = (data: TimerEventData, idx: number) => {
155+
const getButton = (
156+
data: TimerEventData,
157+
idx: number,
158+
isBigScreen: boolean,
159+
) => {
147160
return data.eventIcon ? (
148161
isBigScreen ? (
149162
<Button
@@ -194,7 +207,7 @@ export const PomodoroTimer = (props: any) => {
194207
{status ? <Status>{status}</Status> : <Status>&nbsp;</Status>}
195208
<TimeRemaining time={time} />
196209
<ButtonGroup variant="pomodoroControl" size="sm" gap="0" isAttached>
197-
{events.map((e, idx) => getButton(e, idx))}
210+
{events.map((e, idx) => getButton(e, idx, isBigScreen))}
198211
</ButtonGroup>
199212
</Stack>
200213
</PopoverContent>

pages/lessons/index.tsx

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
1-
import { Flex, Stack, Heading } from '@chakra-ui/react'
1+
import { Flex, Stack, Heading, Box } from '@chakra-ui/react'
22
import fs from 'fs'
33
import path from 'path'
44
import matter from 'gray-matter'
55
import { ContentBanner } from '../../components/ContentBanner'
66

7+
interface Lesson {
8+
path: string
9+
frontMatter: any
10+
slug: string
11+
}
12+
713
interface LessonProps {
8-
lessons: {
9-
path: string
10-
frontMatter: any
11-
slug: string
12-
}[]
14+
lessons: Lesson[]
1315
}
1416

15-
const Lessons: React.FC<LessonProps> = ({ lessons }) => {
16-
const result = lessons.reduce((acc: any, curr: any) => {
17-
if (!acc[curr.path]) acc[curr.path] = []
17+
interface LessonTrackMap {
18+
[key: string]: Lesson[]
19+
}
1820

21+
const Lessons: React.FC<LessonProps> = ({ lessons }: { lessons: Lesson[] }) => {
22+
const result = lessons.reduce((acc: LessonTrackMap, curr) => {
23+
if (!acc[curr.path]) {
24+
// initial an array of lessons for a given track
25+
acc[curr.path] = []
26+
}
1927
acc[curr.path].push(curr)
2028
return acc
2129
}, {})
@@ -25,16 +33,22 @@ const Lessons: React.FC<LessonProps> = ({ lessons }) => {
2533
<Flex as="main" py={5} px={[4, 10, 16]} direction="column" minH="90vh">
2634
<Stack spacing={5} direction="column">
2735
<>
28-
{Object.entries(result).map((track: any) => {
36+
{Object.entries(result).map((track, idx: number) => {
37+
const trackName = track[0].toUpperCase()
38+
const lessons = track[1]
2939
return (
30-
<>
40+
<Box key={idx}>
3141
<Heading size="lg" color="yellow.300">
32-
{track[0].toUpperCase()}
42+
{trackName}
3343
</Heading>
34-
{track[1].map((lesson: any, idx: number) => {
35-
return <ContentBanner key={idx} lesson={lesson} idx={idx} />
44+
{lessons.map((lesson: Lesson, idx: number) => {
45+
return (
46+
<Box marginTop="4" key={idx}>
47+
<ContentBanner lesson={lesson} idx={idx} />
48+
</Box>
49+
)
3650
})}
37-
</>
51+
</Box>
3852
)
3953
})}
4054
</>
@@ -48,7 +62,7 @@ export default Lessons
4862

4963
export const getStaticProps = async () => {
5064
const directories = fs.readdirSync(path.join('lessons'))
51-
const lessons: object[] = []
65+
const lessons: Lesson[] = []
5266
directories.reverse().map((filename) => {
5367
fs.readdirSync(path.join('lessons', filename)).map((file) => {
5468
const markdownWithMeta = fs.readFileSync(

0 commit comments

Comments
 (0)