1- import React , { useState , useEffect } from "react" ;
2- import { graphql } from "gatsby" ;
3- import { Box , ResponsiveContext } from "grommet" ;
4- import { MDXProvider } from "@mdx-js/react" ;
5- import { Link } from "gatsby" ;
6- import { primaryNav , footerItems } from "../config/options" ;
7- import AppShell from "./atomic/AppShell" ;
8- import BlogHeaderCard from "./atomic/BlogHeaderCard" ;
9- import { PlainLink } from "./atomic/TattleLinks" ;
10- import { Heading } from "grommet" ;
11- import { useLocation } from "@reach/router" ;
12- import CustomCodeBlock from "./atomic/customCodeBlock" ;
13- import InlineCodeBlock from "./atomic/inlineCodeBlock" ;
14- import useBlogTags from "../hooks/useBlogTags" ;
15- import BlogSidebar from "./BlogSidebar" ;
16- import { projectSlugMaker } from "../lib/project-slug-maker" ;
17- import TagsRenderer from "./TagsRenderer" ;
18- import { getSrc , getImage } from "gatsby-plugin-image" ;
1+ import React , { useState , useEffect } from "react"
2+ import { graphql } from "gatsby"
3+ import { Box , ResponsiveContext } from "grommet"
4+ import { MDXProvider } from "@mdx-js/react"
5+ import { Link } from "gatsby"
6+ import { primaryNav , footerItems } from "../config/options"
7+ import AppShell from "./atomic/AppShell"
8+ import BlogHeaderCard from "./atomic/BlogHeaderCard"
9+ import { PlainLink } from "./atomic/TattleLinks"
10+ import { Heading } from "grommet"
11+ import { useLocation } from "@reach/router"
12+ import CustomCodeBlock from "./atomic/customCodeBlock"
13+ import InlineCodeBlock from "./atomic/inlineCodeBlock"
14+ import useBlogTags from "../hooks/useBlogTags"
15+ import BlogSidebar from "./BlogSidebar"
16+ import { projectSlugMaker } from "../lib/project-slug-maker"
17+ import TagsRenderer from "./TagsRenderer"
18+ import { getSrc , getImage } from "gatsby-plugin-image"
1919
2020const shortcodes = {
2121 Link,
2222 BlogHeaderCard,
2323 code : ( props ) => < CustomCodeBlock { ...props } /> ,
2424 inlineCode : ( props ) => < InlineCodeBlock { ...props } /> ,
25- } ;
25+ }
2626
27- export default function PageTemplate ( { data : { mdx } , pageContext : { blogNodes = [ ] } , children } ) {
28- const { tagCounts, projectTagsCounts } = useBlogTags ( ) ;
29- const { name, author, project, date, excerpt, cover } = mdx . frontmatter ;
27+ export default function PageTemplate ( {
28+ data : { mdx } ,
29+ pageContext : { blogNodes = [ ] } ,
30+ children,
31+ } ) {
32+ const { tagCounts, projectTagsCounts } = useBlogTags ( )
33+ const { name, author, project, date, excerpt, cover } = mdx . frontmatter
3034 const tags = mdx . frontmatter . tags
3135 ? mdx . frontmatter . tags . split ( "," ) . map ( ( tag ) => tag . trim ( ) )
32- : [ ] ;
36+ : [ ]
3337
34- const location = useLocation ( ) ;
35- const [ label , setLabel ] = useState ( "" ) ;
38+ const location = useLocation ( )
39+ const [ label , setLabel ] = useState ( "" )
3640
3741 useEffect ( ( ) => {
38- setLabel ( location . pathname . split ( "/" ) [ 1 ] ) ;
39- } , [ location ] ) ;
42+ setLabel ( location . pathname . split ( "/" ) [ 1 ] )
43+ } , [ location ] )
4044
41-
4245 const findRelatedPosts = ( ) => {
4346 // If no tags, return 5 most recent posts
4447 if ( ! blogNodes . length ) {
45- return [ ] ;
48+ return [ ]
4649 }
4750 if ( ! tags . length ) {
4851 return blogNodes
49- . filter ( node => node . id !== mdx . id )
50- . sort ( ( a , b ) => new Date ( b . frontmatter . date ) - new Date ( a . frontmatter . date ) )
52+ . filter ( ( node ) => node . id !== mdx . id )
53+ . sort (
54+ ( a , b ) => new Date ( b . frontmatter . date ) - new Date ( a . frontmatter . date )
55+ )
5156 . slice ( 0 , 5 )
52- . map ( post => ( {
57+ . map ( ( post ) => ( {
5358 id : post . id ,
5459 title : post . frontmatter . name ,
5560 slug : post . fields . slug ,
56- coverImage : post . frontmatter . cover ? getImage ( post . frontmatter . cover ) : null ,
61+ coverImage : post . frontmatter . cover
62+ ? getImage ( post . frontmatter . cover )
63+ : null ,
5764 excerpt : post . frontmatter . excerpt ,
5865 matchingTags : [ ] ,
59- relevanceScore : 0
60- } ) ) ;
66+ relevanceScore : 0 ,
67+ } ) )
6168 }
62-
69+
6370 const postsWithRelevance = blogNodes
64- . filter ( node => node . id !== mdx . id )
65- . map ( post => {
71+ . filter ( ( node ) => node . id !== mdx . id )
72+ . map ( ( post ) => {
6673 const postTags = post . frontmatter . tags
67- ? post . frontmatter . tags . split ( "," ) . map ( tag => tag . trim ( ) )
68- : [ ] ;
69-
70- const matchingTags = tags . filter ( tag => postTags . includes ( tag ) ) ;
71-
72- const coverImage = post . frontmatter . cover
73- ? getImage ( post . frontmatter . cover )
74- : null ;
75-
74+ ? post . frontmatter . tags . split ( "," ) . map ( ( tag ) => tag . trim ( ) )
75+ : [ ]
76+
77+ const matchingTags = tags . filter ( ( tag ) => postTags . includes ( tag ) )
78+
79+ const coverImage = post . frontmatter . cover
80+ ? getImage ( post . frontmatter . cover )
81+ : null
82+
7683 return {
7784 id : post . id ,
7885 title : post . frontmatter . name ,
7986 slug : post . fields . slug ,
8087 coverImage : coverImage ,
8188 excerpt : post . frontmatter . excerpt ,
8289 matchingTags : matchingTags ,
83- relevanceScore : matchingTags . length
84- } ;
90+ relevanceScore : matchingTags . length ,
91+ }
8592 } )
86- . filter ( post => post . relevanceScore > 0 )
87- . sort ( ( a , b ) => b . relevanceScore - a . relevanceScore ) ;
88-
89- return postsWithRelevance . slice ( 0 , 5 ) ;
90- } ;
93+ . filter ( ( post ) => post . relevanceScore > 0 )
94+ . sort ( ( a , b ) => b . relevanceScore - a . relevanceScore )
9195
92- const relatedPosts = findRelatedPosts ( ) ;
96+ return postsWithRelevance . slice ( 0 , 5 )
97+ }
98+
99+ const relatedPosts = findRelatedPosts ( )
93100
94101 return (
95102 < AppShell
@@ -112,7 +119,7 @@ export default function PageTemplate({ data: { mdx }, pageContext: { blogNodes =
112119 < Heading level = { 4 } > back to all blogs</ Heading >
113120 </ PlainLink >
114121 </ Box >
115-
122+
116123 < Box >
117124 < BlogHeaderCard
118125 name = { name }
@@ -141,40 +148,19 @@ export default function PageTemplate({ data: { mdx }, pageContext: { blogNodes =
141148 ) }
142149 </ Box >
143150 </ Box >
144- < ResponsiveContext . Consumer >
145- { size => {
146- const isLarge = size === "large" || size === "xlarge" ;
147- return (
148- < Box
149- direction = { size === "small" ? "column" : "row" }
150- gap = { isLarge ? "xlarge" : "medium" }
151- align = "start"
152-
153- >
154- { /* Main Content */ }
155- < Box
156- flex = { true }
157- width = { size === "small" ? "100%" : undefined }
158-
159- >
160- { children }
161- </ Box >
162151
163- { /* Sidebar Section */ }
164- < Box
165- width = { size === "small" ? "100%" : isLarge ? "320px" : "390px" }
166-
167- flex = { false }
168- >
169- < BlogSidebar relatedPosts = { relatedPosts } />
152+ < Box align = "start" className = "flex flex-col lg:flex-row" >
153+ { /* Main Content */ }
154+ < Box flex = { true } > { children } </ Box >
155+
156+ { /* Sidebar Section */ }
157+ < Box className = "text-center w-full lg:w-[35%] flex" >
158+ < BlogSidebar relatedPosts = { relatedPosts } />
159+ </ Box >
170160 </ Box >
171- </ Box >
172- ) ;
173- } }
174- </ ResponsiveContext . Consumer >
175161 </ MDXProvider >
176162 </ AppShell >
177- ) ;
163+ )
178164}
179165
180166export const pageQuery = graphql `
@@ -200,4 +186,4 @@ export const pageQuery = graphql`
200186 }
201187 }
202188 }
203- ` ;
189+ `
0 commit comments