@@ -9,18 +9,18 @@ import rehypeRaw from "rehype-raw"
9
9
import remarkGfm from "remark-gfm"
10
10
import remarkMath from "remark-math"
11
11
12
- import { Box , Typography } from "@mui/material"
12
+ import { Box } from "@mui/material"
13
13
import { styled } from "@mui/system"
14
14
15
- import { fetchBlogDetailURL } from "@/apis/blog"
16
15
import blogSource from "@/assets/blog/main.data.json"
17
16
import LoadingPage from "@/components/LoadingPage"
18
17
import { LANGUAGE_MAP } from "@/constants"
19
18
import useCheckViewport from "@/hooks/useCheckViewport"
20
19
import useUserLanguage from "@/hooks/useUserLanguage"
21
20
import { filterBlogsByLanguage } from "@/utils"
22
21
23
- import Articles from "./articles"
22
+ import MoreBlogs from "./MoreBlogs"
23
+ import { fetchBlogContent } from "./actions"
24
24
import TOC from "./components/tableOfContents"
25
25
26
26
const BlogContainer = styled ( Box ) (
@@ -61,36 +61,37 @@ const BlogDetail = props => {
61
61
const blogsWithLang = useMemo ( ( ) => filterBlogsByLanguage ( blogSource , language ) , [ blogSource , language ] )
62
62
63
63
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 )
74
86
}
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 } ` )
87
92
}
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 } ` )
93
93
}
94
+ fetchCurrentBlog ( )
94
95
} , [ blogId , language ] )
95
96
96
97
useEffect ( ( ) => {
@@ -119,21 +120,8 @@ const BlogDetail = props => {
119
120
className = "markdown-body"
120
121
/>
121
122
</ 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 > }
137
125
</ Box >
138
126
) }
139
127
</ Box >
0 commit comments