1
- import { Flex , Stack , Heading } from '@chakra-ui/react'
1
+ import { Flex , Stack , Heading , Box } from '@chakra-ui/react'
2
2
import fs from 'fs'
3
3
import path from 'path'
4
4
import matter from 'gray-matter'
5
5
import { ContentBanner } from '../../components/ContentBanner'
6
6
7
+ interface Lesson {
8
+ path : string
9
+ frontMatter : any
10
+ slug : string
11
+ }
12
+
7
13
interface LessonProps {
8
- lessons : {
9
- path : string
10
- frontMatter : any
11
- slug : string
12
- } [ ]
14
+ lessons : Lesson [ ]
13
15
}
14
16
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
+ }
18
20
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
+ }
19
27
acc [ curr . path ] . push ( curr )
20
28
return acc
21
29
} , { } )
@@ -25,16 +33,22 @@ const Lessons: React.FC<LessonProps> = ({ lessons }) => {
25
33
< Flex as = "main" py = { 5 } px = { [ 4 , 10 , 16 ] } direction = "column" minH = "90vh" >
26
34
< Stack spacing = { 5 } direction = "column" >
27
35
< >
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 ]
29
39
return (
30
- < >
40
+ < Box key = { idx } >
31
41
< Heading size = "lg" color = "yellow.300" >
32
- { track [ 0 ] . toUpperCase ( ) }
42
+ { trackName }
33
43
</ 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
+ )
36
50
} ) }
37
- </ >
51
+ </ Box >
38
52
)
39
53
} ) }
40
54
</ >
@@ -48,7 +62,7 @@ export default Lessons
48
62
49
63
export const getStaticProps = async ( ) => {
50
64
const directories = fs . readdirSync ( path . join ( 'lessons' ) )
51
- const lessons : object [ ] = [ ]
65
+ const lessons : Lesson [ ] = [ ]
52
66
directories . reverse ( ) . map ( ( filename ) => {
53
67
fs . readdirSync ( path . join ( 'lessons' , filename ) ) . map ( ( file ) => {
54
68
const markdownWithMeta = fs . readFileSync (
0 commit comments