onedrive/components/previews/TextPreview.tsx

62 lines
1.5 KiB
TypeScript
Raw Normal View History

2022-02-14 11:33:19 +00:00
import { useRouter } from 'next/router'
import { useTranslation } from 'next-i18next'
2021-06-25 14:15:00 +00:00
import FourOhFour from '../FourOhFour'
import Loading from '../Loading'
import DownloadButtonGroup from '../DownloadBtnGtoup'
2022-02-14 11:33:19 +00:00
import useFileContent from '../../utils/fetchOnMount'
import { DownloadBtnContainer, PreviewContainer } from './Containers'
2021-06-25 14:15:00 +00:00
const TextPreview = ({ file }) => {
2022-02-14 11:33:19 +00:00
const { asPath } = useRouter()
const { t } = useTranslation()
2022-02-14 11:33:19 +00:00
const { response: content, error, validating } = useFileContent(`/api/raw/?path=${asPath}`, asPath)
2021-06-25 14:15:00 +00:00
if (error) {
return (
<PreviewContainer>
<FourOhFour errorMsg={error} />
</PreviewContainer>
2021-06-25 14:15:00 +00:00
)
}
if (validating) {
2021-06-25 14:15:00 +00:00
return (
<>
<PreviewContainer>
<Loading loadingText={t('Loading file content...')} />
</PreviewContainer>
<DownloadBtnContainer>
<DownloadButtonGroup />
</DownloadBtnContainer>
</>
)
}
if (!content) {
return (
<>
<PreviewContainer>
<FourOhFour errorMsg={t('File is empty.')} />
</PreviewContainer>
<DownloadBtnContainer>
<DownloadButtonGroup />
</DownloadBtnContainer>
</>
2021-06-25 14:15:00 +00:00
)
}
return (
<div>
<PreviewContainer>
<pre className="overflow-x-scroll p-0 text-sm md:p-3">{content}</pre>
</PreviewContainer>
<DownloadBtnContainer>
2022-02-14 11:33:19 +00:00
<DownloadButtonGroup />
</DownloadBtnContainer>
</div>
2021-06-25 14:15:00 +00:00
)
}
export default TextPreview