|
1 | 1 | 'use client'
|
2 |
| -import { memo } from 'react' |
| 2 | + |
| 3 | +import { memo, useEffect, useState } from 'react' |
3 | 4 | import MarkdownPreview from '@uiw/react-markdown-preview'
|
4 | 5 | import { useTheme } from 'next-themes'
|
| 6 | +import Markdown from 'react-markdown' |
5 | 7 |
|
6 | 8 | interface MarkdownInfo {
|
7 | 9 | content: string
|
8 | 10 | }
|
9 | 11 |
|
| 12 | +import { Skeleton } from '@/components/ui/skeleton' |
10 | 13 | const MarkdownContent = memo(({ content }: MarkdownInfo) => {
|
11 | 14 | const { theme } = useTheme()
|
12 |
| - return ( |
13 |
| - <MarkdownPreview |
14 |
| - source={content} |
15 |
| - // @ts-ignore |
16 |
| - // ignore theme variable type (it's sould only be dark or light) |
17 |
| - wrapperElement={{ 'data-color-mode': theme }} |
18 |
| - style={{ minHeight: '100%', padding: 30, background: 'none' }} |
19 |
| - /> |
20 |
| - ) |
| 15 | + |
| 16 | + // this related to error with `MarkdownPreview` |
| 17 | + // server render and client render theme can always be different, which result in error |
| 18 | + const [mounted, on_mounted] = useState(false) |
| 19 | + |
| 20 | + useEffect(() => { |
| 21 | + on_mounted(true) |
| 22 | + }, []) |
| 23 | + |
| 24 | + if (mounted) { |
| 25 | + return ( |
| 26 | + <MarkdownPreview |
| 27 | + source={content} |
| 28 | + // @ts-ignore |
| 29 | + // ignore theme variable type (it's sould only be dark or light) |
| 30 | + wrapperElement={{ 'data-color-mode': theme }} |
| 31 | + style={{ minHeight: '100%', padding: 30, background: 'none' }} |
| 32 | + /> |
| 33 | + ) |
| 34 | + } else { |
| 35 | + return ( |
| 36 | + <> |
| 37 | + {/* make sure content text is pre-rendered no matter what the style is */} |
| 38 | + <div className="hidden"> |
| 39 | + <Markdown>{content}</Markdown> |
| 40 | + </div> |
| 41 | + |
| 42 | + {/* skeleton */} |
| 43 | + <div className="flex flex-col space-y-3 p-5"> |
| 44 | + <Skeleton className="h-[125px] w-full rounded-xl" /> |
| 45 | + <div className="space-y-2"> |
| 46 | + <Skeleton className="h-4 w-[75%]" /> |
| 47 | + <Skeleton className="h-4 w-[50%]" /> |
| 48 | + </div> |
| 49 | + </div> |
| 50 | + </> |
| 51 | + ) |
| 52 | + } |
21 | 53 | })
|
22 | 54 |
|
23 | 55 | MarkdownContent.displayName = 'MarkdownContent'
|
|
0 commit comments