onedrive/components/previews/CodePreview.tsx

49 lines
1.4 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'
import { useStaleSWR } from '../../utils/fetchWithSWR'
2021-06-25 15:13:31 +00:00
import FourOhFour from '../FourOhFour'
import Loading from '../Loading'
import DownloadButtonGroup from '../DownloadBtnGtoup'
2021-06-25 15:13:31 +00:00
const CodePreview: FunctionComponent<{ file: any }> = ({ file }) => {
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 (
<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>
<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">
<DownloadButtonGroup downloadUrl={file['@microsoft.graph.downloadUrl']} />
2021-06-25 15:13:31 +00:00
</div>
</div>
2021-06-25 15:13:31 +00:00
)
}
export default CodePreview