@@ -9,18 +9,18 @@ import rehypeRaw from "rehype-raw"
99import remarkGfm from "remark-gfm"
1010import remarkMath from "remark-math"
1111
12- import { Box , Typography } from "@mui/material"
12+ import { Box } from "@mui/material"
1313import { styled } from "@mui/system"
1414
15- import { fetchBlogDetailURL } from "@/apis/blog"
1615import blogSource from "@/assets/blog/main.data.json"
1716import LoadingPage from "@/components/LoadingPage"
1817import { LANGUAGE_MAP } from "@/constants"
1918import useCheckViewport from "@/hooks/useCheckViewport"
2019import useUserLanguage from "@/hooks/useUserLanguage"
2120import { filterBlogsByLanguage } from "@/utils"
2221
23- import Articles from "./articles"
22+ import MoreBlogs from "./MoreBlogs"
23+ import { fetchBlogContent } from "./actions"
2424import TOC from "./components/tableOfContents"
2525
2626const BlogContainer = styled ( Box ) (
@@ -61,36 +61,37 @@ const BlogDetail = props => {
6161 const blogsWithLang = useMemo ( ( ) => filterBlogsByLanguage ( blogSource , language ) , [ blogSource , language ] )
6262
6363 useEffect ( ( ) => {
64- const regex = / ( [ ^ _ ] * ?) _ l a n g _ [ ^ _ ] + / g
65- const blogIdMatch = blogId ?. match ( regex )
66-
67- const blogItemWithLang = blogSource . find ( item => item . id === `${ blogId } _lang_${ language } ` )
68-
69- if ( ( ! blogIdMatch && language === "en" ) || ( blogIdMatch && language !== "en" ) || ( ! blogIdMatch && language !== "en" && ! blogItemWithLang ) ) {
70- let anchors = [ ...document . querySelectorAll ( "a" ) ]
71- anchors . map ( anchor => {
72- if ( anchor . href . includes ( "/Content/" ) ) {
73- anchor . setAttribute ( "target" , "" )
64+ async function fetchCurrentBlog ( ) {
65+ const regex = / ( [ ^ _ ] * ?) _ l a n g _ [ ^ _ ] + / g
66+ const blogIdMatch = blogId ?. match ( regex )
67+
68+ const blogItemWithLang = blogSource . find ( item => item . id === `${ blogId } _lang_${ language } ` )
69+
70+ if ( ( ! blogIdMatch && language === "en" ) || ( blogIdMatch && language !== "en" ) || ( ! blogIdMatch && language !== "en" && ! blogItemWithLang ) ) {
71+ let anchors = [ ...document . querySelectorAll ( "a" ) ]
72+ anchors . map ( anchor => {
73+ if ( anchor . href . includes ( "/Content/" ) ) {
74+ anchor . setAttribute ( "target" , "" )
75+ }
76+ return anchor
77+ } )
78+ try {
79+ setLoading ( true )
80+ const text = await fetchBlogContent ( blogId )
81+ setBlogContent ( text )
82+ } catch ( _error ) {
83+ router . push ( "/404" )
84+ } finally {
85+ setLoading ( false )
7486 }
75- return anchor
76- } )
77- try {
78- setLoading ( true )
79- fetch ( fetchBlogDetailURL ( blogId ) )
80- . then ( response => response . text ( ) )
81- . then ( text => {
82- setLoading ( false )
83- setBlogContent ( text )
84- } )
85- } catch ( _error ) {
86- router . push ( "/404" )
87+ } else if ( blogIdMatch && language === "en" ) {
88+ const nextBlogId = blogId . replace ( regex , "$1" )
89+ router . push ( `/blog/${ nextBlogId } ` )
90+ } else if ( blogItemWithLang ) {
91+ router . push ( `/blog/${ blogId } _lang_${ language } ` )
8792 }
88- } else if ( blogIdMatch && language === "en" ) {
89- const nextBlogId = blogId . replace ( regex , "$1" )
90- router . push ( `/blog/${ nextBlogId } ` )
91- } else if ( blogItemWithLang ) {
92- router . push ( `/blog/${ blogId } _lang_${ language } ` )
9393 }
94+ fetchCurrentBlog ( )
9495 } , [ blogId , language ] )
9596
9697 useEffect ( ( ) => {
@@ -119,21 +120,8 @@ const BlogDetail = props => {
119120 className = "markdown-body"
120121 />
121122 </ BlogContainer >
122- { isPortrait ? (
123- < Box sx = { { paddingBottom : "10rem" } } >
124- < Typography
125- variant = "h1"
126- sx = { {
127- textAlign : "center" ,
128- mt : [ "3rem" , "5rem" ] ,
129- mb : [ "2rem" , "3rem" ] ,
130- } }
131- >
132- { LANGUAGE_MAP [ language ] . more_articles }
133- </ Typography >
134- < Articles blogs = { moreBlog } />
135- </ Box >
136- ) : null }
123+
124+ { ! ! isPortrait && < MoreBlogs sx = { { pb : "10rem" } } blogs = { moreBlog } title = { LANGUAGE_MAP [ language ] . more_articles } > </ MoreBlogs > }
137125 </ Box >
138126 ) }
139127 </ Box >
0 commit comments