2021-06-25 15:13:31 +00:00
|
|
|
import { useEffect, FunctionComponent } from 'react'
|
|
|
|
import Prism from 'prismjs'
|
|
|
|
|
|
|
|
import { getExtension } from '../../utils/getFileIcon'
|
2021-12-17 13:21:25 +00:00
|
|
|
import { useStaleSWR } from '../../utils/fetchWithSWR'
|
2021-06-25 15:13:31 +00:00
|
|
|
import FourOhFour from '../FourOhFour'
|
|
|
|
import Loading from '../Loading'
|
2021-12-29 07:23:47 +00:00
|
|
|
import DownloadButtonGroup from '../DownloadBtnGtoup'
|
2021-06-25 15:13:31 +00:00
|
|
|
|
|
|
|
const CodePreview: FunctionComponent<{ file: any }> = ({ file }) => {
|
2021-12-17 13:21:25 +00:00
|
|
|
const { data, error } = useStaleSWR({ url: file['@microsoft.graph.downloadUrl'] })
|
2021-08-29 14:50:38 +00:00
|
|
|
|
2021-06-25 15:13:31 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (typeof window !== 'undefined') {
|
|
|
|
Prism.highlightAll()
|
|
|
|
}
|
|
|
|
}, [data])
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
return (
|
2021-12-17 06:35:24 +00:00
|
|
|
<div className="dark:bg-gray-900 p-3 bg-white rounded">
|
2021-06-25 15:13:31 +00:00
|
|
|
<FourOhFour errorMsg={error.message} />
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if (!data) {
|
|
|
|
return (
|
2021-12-17 06:35:24 +00:00
|
|
|
<div className="dark:bg-gray-900 p-3 bg-white rounded">
|
2021-06-25 15:13:31 +00:00
|
|
|
<Loading loadingText="Loading file content..." />
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2021-12-29 07:52:18 +00:00
|
|
|
<div>
|
2021-12-17 06:35:24 +00:00
|
|
|
<div className="markdown-body p-3 bg-gray-900 rounded">
|
2021-06-25 15:13:31 +00:00
|
|
|
<pre className={`language-${getExtension(file.name)}`}>
|
|
|
|
<code>{data}</code>
|
|
|
|
</pre>
|
|
|
|
</div>
|
2021-12-29 07:52:18 +00:00
|
|
|
<div className="border-t-gray-200 dark:border-t-gray-700 border-t p-2 sticky bottom-0 left-0 right-0 z-10 bg-white bg-opacity-80 backdrop-blur-md dark:bg-gray-900">
|
2021-12-29 07:23:47 +00:00
|
|
|
<DownloadButtonGroup downloadUrl={file['@microsoft.graph.downloadUrl']} />
|
2021-06-25 15:13:31 +00:00
|
|
|
</div>
|
2021-12-29 07:52:18 +00:00
|
|
|
</div>
|
2021-06-25 15:13:31 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default CodePreview
|