1- import React , { useState , useEffect } from ' react' ;
2- import { useParams , useNavigate } from ' react-router-dom' ;
3- import moment from ' moment' ;
4- import parse from ' html-react-parser' ;
1+ import React , { useState , useEffect } from " react" ;
2+ import { useParams , useNavigate } from " react-router-dom" ;
3+ import moment from " moment" ;
4+ import parse from " html-react-parser" ;
55
6- import ArchiveRelative from '../components/archive-relative' ;
7- import RenderComponents from '../components/render-components' ;
8- import { onEntryChange } from '../sdk/entry.d' ;
9- import { getPageRes , getBlogPostRes } from '../helper/index.d' ;
10- import Skeleton from 'react-loading-skeleton' ;
6+ import ArchiveRelative from "../components/archive-relative" ;
7+ import RenderComponents from "../components/render-components" ;
8+ import { getPageRes , getBlogPostRes } from "../helper" ;
9+ import Skeleton from "react-loading-skeleton" ;
1110import { Prop , Banner , Post } from "../typescript/pages" ;
11+ import { useLivePreviewCtx } from "../context/live-preview-context-provider" ;
1212
1313export default function BlogPost ( { entry } : Prop ) {
14+ const lpTs = useLivePreviewCtx ( ) ;
1415 const { blogId } = useParams ( ) ;
1516 const history = useNavigate ( ) ;
16- const [ getEntry , setEntry ] = useState ( { banner : { } as Banner , post : { } as Post } ) ;
17+ const [ getEntry , setEntry ] = useState ( {
18+ banner : { } as Banner ,
19+ post : { } as Post ,
20+ } ) ;
1721 const [ error , setError ] = useState ( false ) ;
1822
1923 async function fetchData ( ) {
2024 try {
21- const entryUrl = blogId ? `/blog/${ blogId } ` : '/' ;
22- const banner = await getPageRes ( ' /blog' ) ;
25+ const entryUrl = blogId ? `/blog/${ blogId } ` : "/" ;
26+ const banner = await getPageRes ( " /blog" ) ;
2327 const post = await getBlogPostRes ( entryUrl ) ;
2428 ( ! banner || ! post ) && setError ( true ) ;
2529 setEntry ( { banner, post } ) ;
@@ -31,17 +35,9 @@ export default function BlogPost({ entry }: Prop) {
3135 }
3236
3337 useEffect ( ( ) => {
34- onEntryChange ( fetchData ) ;
35- } , [ ] ) ;
36- useEffect ( ( ) => {
37- error && history ( '/404' ) ;
38- } , [ error ] ) ;
39-
40- useEffect ( ( ) => {
41- if ( getEntry . post . url !== `/blog/${ blogId } ` ) {
42- fetchData ( ) ;
43- }
44- } , [ getEntry . post , blogId ] ) ;
38+ fetchData ( ) ;
39+ error && history ( "/404" ) ;
40+ } , [ blogId , lpTs , error ] ) ;
4541
4642 const { post, banner } = getEntry ;
4743 return (
@@ -61,34 +57,34 @@ export default function BlogPost({ entry }: Prop) {
6157 < div className = 'blog-container' >
6258 < article className = 'blog-detail' >
6359 { post . title ? (
64- < h2 { ...post . $ ?. title as { } } > { post . title } </ h2 >
60+ < h2 { ...( post . $ ?. title as { } ) } > { post . title } </ h2 >
6561 ) : (
6662 < h2 >
6763 < Skeleton />
6864 </ h2 >
6965 ) }
7066 { post . date ? (
71- < p { ...post . $ ?. date as { } } >
72- { moment ( post . date ) . format ( ' ddd, MMM D YYYY' ) } ,{ ' ' }
73- < strong { ...post . author [ 0 ] . $ ?. title as { } } >
67+ < p { ...( post . $ ?. date as { } ) } >
68+ { moment ( post . date ) . format ( " ddd, MMM D YYYY" ) } ,{ " " }
69+ < strong { ...( post . author [ 0 ] . $ ?. title as { } ) } >
7470 { post . author [ 0 ] . title }
7571 </ strong >
7672 </ p >
7773 ) : (
7874 < p >
79- < Skeleton width = { 300 } />
75+ < Skeleton width = { 300 } />
8076 </ p >
8177 ) }
8278 { post . body ? (
83- < div { ...post . $ ?. body as { } } > { parse ( post . body ) } </ div >
79+ < div { ...( post . $ ?. body as { } ) } > { parse ( post . body ) } </ div >
8480 ) : (
8581 < Skeleton height = { 800 } width = { 600 } />
8682 ) }
8783 </ article >
8884 < div className = 'blog-column-right' >
8985 < div className = 'related-post' >
9086 { Object . keys ( banner ) . length && banner . page_components [ 2 ] . widget ? (
91- < h2 { ...banner ?. page_components [ 2 ] . widget . $ ?. title_h2 as { } } >
87+ < h2 { ...( banner ?. page_components [ 2 ] . widget . $ ?. title_h2 as { } ) } >
9288 { banner ?. page_components [ 2 ] . widget . title_h2 }
9389 </ h2 >
9490 ) : (
0 commit comments