2022-03-09 17:57:44 -05:00
|
|
|
import { memo, useEffect, useState } from "react"
|
2022-04-09 20:48:19 -04:00
|
|
|
import styles from "./preview.module.css"
|
2022-11-12 19:06:23 -05:00
|
|
|
import "@styles/markdown.css"
|
2022-11-12 20:11:05 -05:00
|
|
|
import "@styles/syntax.css"
|
2022-11-28 21:33:06 -05:00
|
|
|
import Skeleton from "@components/skeleton"
|
|
|
|
import { Spinner } from "@components/spinner"
|
2022-03-06 19:46:59 -05:00
|
|
|
|
2022-03-09 17:57:44 -05:00
|
|
|
type Props = {
|
2022-04-09 20:48:19 -04:00
|
|
|
height?: number | string
|
|
|
|
fileId?: string
|
|
|
|
content?: string
|
|
|
|
title?: string
|
2022-03-09 17:57:44 -05:00
|
|
|
}
|
|
|
|
|
2022-11-12 19:06:23 -05:00
|
|
|
const MarkdownPreview = ({
|
|
|
|
height = 500,
|
|
|
|
fileId,
|
2022-11-16 03:49:12 -05:00
|
|
|
content = "",
|
2022-11-12 19:06:23 -05:00
|
|
|
title
|
|
|
|
}: Props) => {
|
2022-11-16 03:49:12 -05:00
|
|
|
const [preview, setPreview] = useState<string>(content)
|
2022-04-09 20:48:19 -04:00
|
|
|
const [isLoading, setIsLoading] = useState<boolean>(true)
|
|
|
|
useEffect(() => {
|
|
|
|
async function fetchPost() {
|
2022-11-12 20:11:05 -05:00
|
|
|
// POST to avoid query string length limit
|
|
|
|
const method = fileId ? "GET" : "POST"
|
|
|
|
const path = fileId ? `/api/file/html/${fileId}` : "/api/file/get-html"
|
|
|
|
const body = fileId
|
|
|
|
? undefined
|
|
|
|
: JSON.stringify({
|
|
|
|
title: title || "",
|
2022-11-16 03:49:12 -05:00
|
|
|
content: content
|
2022-11-12 20:11:05 -05:00
|
|
|
})
|
2022-11-09 21:38:05 -05:00
|
|
|
|
2022-11-12 20:11:05 -05:00
|
|
|
const resp = await fetch(path, {
|
|
|
|
method: method,
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
},
|
|
|
|
body
|
|
|
|
})
|
2022-03-22 23:06:15 -04:00
|
|
|
|
2022-11-12 20:11:05 -05:00
|
|
|
if (resp.ok) {
|
|
|
|
const res = await resp.text()
|
|
|
|
setPreview(res)
|
2022-04-09 20:48:19 -04:00
|
|
|
}
|
2022-11-12 20:11:05 -05:00
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
setIsLoading(false)
|
|
|
|
}
|
|
|
|
fetchPost()
|
2022-11-16 03:49:12 -05:00
|
|
|
}, [content, fileId, title])
|
2022-11-14 02:02:31 -05:00
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{isLoading ? (
|
2022-11-28 21:33:06 -05:00
|
|
|
<Spinner />
|
2022-04-09 20:48:19 -04:00
|
|
|
) : (
|
2022-11-16 03:49:12 -05:00
|
|
|
<StaticPreview preview={preview} height={height} />
|
2022-04-09 20:48:19 -04:00
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)
|
2022-03-06 19:46:59 -05:00
|
|
|
}
|
|
|
|
|
2022-11-12 20:11:05 -05:00
|
|
|
export default memo(MarkdownPreview)
|
2022-11-12 19:06:23 -05:00
|
|
|
|
|
|
|
export const StaticPreview = ({
|
2022-11-16 03:49:12 -05:00
|
|
|
preview,
|
2022-11-12 19:06:23 -05:00
|
|
|
height = 500
|
|
|
|
}: {
|
2022-11-16 03:49:12 -05:00
|
|
|
preview: string
|
2022-11-12 19:06:23 -05:00
|
|
|
height: string | number
|
|
|
|
}) => {
|
|
|
|
return (
|
|
|
|
<article
|
|
|
|
className={styles.markdownPreview}
|
2022-11-16 03:49:12 -05:00
|
|
|
dangerouslySetInnerHTML={{ __html: preview }}
|
2022-11-12 19:06:23 -05:00
|
|
|
style={{
|
|
|
|
height
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|