onedrive/components/previews/CodePreview.tsx

49 lines
1.2 KiB
TypeScript
Raw Normal View History

2021-06-25 15:13:31 +00:00
import { useEffect, FunctionComponent } from 'react'
import Prism from 'prismjs'
import { getExtension } from '../../utils/getFileIcon'
2021-08-29 14:50:38 +00:00
import { useStaleSWR } from '../../utils/tools'
2021-06-25 15:13:31 +00:00
import FourOhFour from '../FourOhFour'
import Loading from '../Loading'
import DownloadBtn from '../DownloadBtn'
const CodePreview: FunctionComponent<{ file: any }> = ({ file }) => {
2021-08-29 14:50:38 +00:00
const { data, error } = useStaleSWR(file['@microsoft.graph.downloadUrl'])
2021-06-25 15:13:31 +00:00
useEffect(() => {
if (typeof window !== 'undefined') {
Prism.highlightAll()
}
}, [data])
if (error) {
return (
2021-09-04 14:15:09 +00:00
<div className="dark:bg-gray-900 p-3 bg-white rounded shadow">
2021-06-25 15:13:31 +00:00
<FourOhFour errorMsg={error.message} />
</div>
)
}
if (!data) {
return (
2021-09-04 14:15:09 +00:00
<div className="dark:bg-gray-900 p-3 bg-white rounded shadow">
2021-06-25 15:13:31 +00:00
<Loading loadingText="Loading file content..." />
</div>
)
}
return (
<>
2021-09-04 14:15:09 +00:00
<div className="markdown-body p-3 bg-gray-900 rounded shadow">
2021-06-25 15:13:31 +00:00
<pre className={`language-${getExtension(file.name)}`}>
<code>{data}</code>
</pre>
</div>
<div className="mt-4">
<DownloadBtn downloadUrl={file['@microsoft.graph.downloadUrl']} />
</div>
</>
)
}
export default CodePreview