1- import React from 'react' ;
1+ import React , { useRef , useEffect } from 'react' ;
22import { graphql } from 'gatsby' ;
33import { LearnPageData } from '../types' ;
44import Layout from '../components/layout' ;
@@ -11,99 +11,95 @@ import Page404 from './404';
1111type Props = {
1212 data : LearnPageData ;
1313 location : Location ;
14- }
14+ } ;
1515
16- export default class LearnPage extends React . Component < Props > {
17- prevOffset : number = - 1 ;
18-
19- componentDidMount ( ) {
20- this . magicHeroNumber ( ) ;
21- }
16+ export default ( { data, location } : Props ) => {
17+ const prevOffset = useRef ( - 1 ) ;
2218
23- /**
24- * When on the "Learn" page, we need to update the background gradient
25- * that runs the side menu's title color change from white to black
26- * as it becomes sticky and overlays the hero banner.
27- */
28- magicHeroNumber = ( ) => {
29- if ( typeof window === 'undefined' ) { return ; } // Guard for SSR.
19+ const magicHeroNumber = ( ) => {
20+ if ( typeof window === 'undefined' ) {
21+ return ;
22+ } // Guard for SSR.
3023 const doc = window . document ;
3124 const offset = Math . min ( doc . scrollingElement ! . scrollTop - 62 , 210 ) ;
32- if ( Math . abs ( this . prevOffset - offset ) > 5 ) {
33- this . prevOffset = offset ;
25+ if ( Math . abs ( prevOffset . current - offset ) > 5 ) {
26+ prevOffset . current = offset ;
3427 doc . body . setAttribute ( 'style' , `--magic-hero-number: ${ 356 - offset } px` ) ;
3528 }
36- window . requestAnimationFrame ( this . magicHeroNumber ) ;
37- }
29+ window . requestAnimationFrame ( magicHeroNumber ) ;
30+ } ;
3831
39- render ( ) {
40- const { data, location } = this . props ;
41- const currentPage = location . pathname . split ( '/' ) . pop ( ) ;
42- const { activePage, previousPage, nextPage, navigationSections } = findActive ( data . sections . group , currentPage ) ;
43- if ( ! activePage ) {
44- // Rendering 404 page as a component here
45- // The reason is to show the 404 component but maintaining the url (instead of redirecting to 404)
46- return < Page404 />
47- }
32+ useEffect ( magicHeroNumber ) ;
4833
49- return (
50- < Layout
51- title = { ` ${ activePage . frontmatter . title } by ${ activePage . frontmatter . author } ` }
52- description = { activePage . frontmatter . description }
53- >
54- < Hero title = { activePage . frontmatter . title } />
55- < Navigation activePage = { activePage } sections = { navigationSections } />
56- < Article page = { activePage } previous = { previousPage } next = { nextPage } />
57- </ Layout >
58- ) ;
34+ const currentPage = location . pathname . split ( '/' ) . pop ( ) ;
35+ const { activePage , previousPage , nextPage , navigationSections } = findActive (
36+ data . sections . group ,
37+ currentPage
38+ ) ;
39+
40+ if ( ! activePage ) {
41+ // Rendering 404 page as a component here
42+ // The reason is to show the 404 component but maintaining the url (instead of redirecting to 404)
43+ return < Page404 /> ;
5944 }
60- }
6145
62- export const query = graphql `{
63- sections: allMarkdownRemark(
64- sort: {
65- fields: [fileAbsolutePath]
66- order: ASC
67- }
68- ) {
69- group(field: frontmatter___section) {
70- fieldValue
71- edges {
72- node {
73- id
74- fileAbsolutePath
75- html,
76- parent {
77- ... on File {
78- relativePath
46+ return (
47+ < Layout
48+ title = { `${ activePage . frontmatter . title } by ${
49+ activePage . frontmatter . author
50+ } `}
51+ description = { activePage . frontmatter . description } >
52+ < Hero title = { activePage . frontmatter . title } />
53+ < Navigation sections = { navigationSections } />
54+ < Article page = { activePage } previous = { previousPage } next = { nextPage } />
55+ </ Layout >
56+ ) ;
57+ } ;
58+
59+ export const query = graphql `
60+ {
61+ sections: allMarkdownRemark(
62+ sort: { fields: [fileAbsolutePath], order: ASC }
63+ ) {
64+ group(field: frontmatter___section) {
65+ fieldValue
66+ edges {
67+ node {
68+ id
69+ fileAbsolutePath
70+ html
71+ parent {
72+ ... on File {
73+ relativePath
74+ }
75+ }
76+ frontmatter {
77+ title
78+ description
79+ author
80+ }
81+ fields {
82+ slug
7983 }
8084 }
81- frontmatter {
82- title
83- description
84- author
85- }
86- fields {
87- slug
88- }
89- }
90- next {
91- frontmatter {
92- title
93- }
94- fields {
95- slug
96- }
97- }
98- previous {
99- frontmatter {
100- title
85+ next {
86+ frontmatter {
87+ title
88+ }
89+ fields {
90+ slug
91+ }
10192 }
102- fields {
103- slug
93+ previous {
94+ frontmatter {
95+ title
96+ }
97+ fields {
98+ slug
99+ }
104100 }
105101 }
106102 }
107103 }
108104 }
109- } `;
105+ ` ;
0 commit comments