mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
7c97f8ab6e
* Use new JSX runtime and update Babel Node target * Update .eslintrc * Remove React imports GitOrigin-RevId: 559de0267f8f2934c56a860ea8701bb522aa861a
87 lines
2.2 KiB
JavaScript
87 lines
2.2 KiB
JavaScript
import { useState } from 'react'
|
|
import PropTypes from 'prop-types'
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
import FileViewHeader from './file-view-header'
|
|
import FileViewImage from './file-view-image'
|
|
import FileViewText from './file-view-text'
|
|
import Icon from '../../../shared/components/icon'
|
|
|
|
const imageExtensions = ['png', 'jpg', 'jpeg', 'gif']
|
|
|
|
const textExtensions = window.ExposedSettings.textExtensions
|
|
|
|
export default function FileView({ file, storeReferencesKeys }) {
|
|
const [contentLoading, setContentLoading] = useState(true)
|
|
const [hasError, setHasError] = useState(false)
|
|
|
|
const { t } = useTranslation()
|
|
|
|
const extension = file.name.split('.').pop().toLowerCase()
|
|
const isUnpreviewableFile =
|
|
!imageExtensions.includes(extension) && !textExtensions.includes(extension)
|
|
|
|
function handleLoading() {
|
|
if (contentLoading) {
|
|
setContentLoading(false)
|
|
}
|
|
}
|
|
|
|
function handleError() {
|
|
if (!hasError) {
|
|
setContentLoading(false)
|
|
setHasError(true)
|
|
}
|
|
}
|
|
|
|
const content = (
|
|
<>
|
|
<FileViewHeader file={file} storeReferencesKeys={storeReferencesKeys} />
|
|
{imageExtensions.includes(extension) && (
|
|
<FileViewImage
|
|
fileName={file.name}
|
|
fileId={file.id}
|
|
onLoad={handleLoading}
|
|
onError={handleError}
|
|
/>
|
|
)}
|
|
{textExtensions.includes(extension) && (
|
|
<FileViewText
|
|
file={file}
|
|
onLoad={handleLoading}
|
|
onError={handleError}
|
|
/>
|
|
)}
|
|
</>
|
|
)
|
|
|
|
return (
|
|
<div className="file-view full-size">
|
|
{!hasError && content}
|
|
{!isUnpreviewableFile && contentLoading && <FileViewLoadingIndicator />}
|
|
{(isUnpreviewableFile || hasError) && (
|
|
<p className="no-preview">{t('no_preview_available')}</p>
|
|
)}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
function FileViewLoadingIndicator() {
|
|
const { t } = useTranslation()
|
|
return (
|
|
<div className="loading-panel loading-panel-file-view">
|
|
<span>
|
|
<Icon type="refresh" modifier="spin" />
|
|
{t('loading')}…
|
|
</span>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
FileView.propTypes = {
|
|
file: PropTypes.shape({
|
|
id: PropTypes.string,
|
|
name: PropTypes.string,
|
|
}).isRequired,
|
|
storeReferencesKeys: PropTypes.func.isRequired,
|
|
}
|