2022-02-14 11:33:19 +00:00
|
|
|
import { useRouter } from 'next/router'
|
2022-02-06 12:34:03 +00:00
|
|
|
import { useTranslation } from 'next-i18next'
|
|
|
|
|
2021-06-25 14:15:00 +00:00
|
|
|
import FourOhFour from '../FourOhFour'
|
|
|
|
import Loading from '../Loading'
|
2021-12-29 07:23:47 +00:00
|
|
|
import DownloadButtonGroup from '../DownloadBtnGtoup'
|
2022-02-14 11:33:19 +00:00
|
|
|
import useFileContent from '../../utils/fetchOnMount'
|
2022-01-06 08:34:16 +00:00
|
|
|
import { DownloadBtnContainer, PreviewContainer } from './Containers'
|
2021-06-25 14:15:00 +00:00
|
|
|
|
2022-01-06 08:34:16 +00:00
|
|
|
const TextPreview = ({ file }) => {
|
2022-02-14 11:33:19 +00:00
|
|
|
const { asPath } = useRouter()
|
2022-02-06 12:34:03 +00:00
|
|
|
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 (
|
2022-01-06 08:34:16 +00:00
|
|
|
<PreviewContainer>
|
|
|
|
<FourOhFour errorMsg={error} />
|
|
|
|
</PreviewContainer>
|
2021-06-25 14:15:00 +00:00
|
|
|
)
|
|
|
|
}
|
2022-01-06 08:34:16 +00:00
|
|
|
|
|
|
|
if (validating) {
|
2021-06-25 14:15:00 +00:00
|
|
|
return (
|
2022-02-24 06:22:48 +00:00
|
|
|
<>
|
|
|
|
<PreviewContainer>
|
|
|
|
<Loading loadingText={t('Loading file content...')} />
|
|
|
|
</PreviewContainer>
|
|
|
|
<DownloadBtnContainer>
|
|
|
|
<DownloadButtonGroup />
|
|
|
|
</DownloadBtnContainer>
|
|
|
|
</>
|
2022-01-06 08:34:16 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!content) {
|
|
|
|
return (
|
2022-02-22 06:09:57 +00:00
|
|
|
<>
|
|
|
|
<PreviewContainer>
|
|
|
|
<FourOhFour errorMsg={t('File is empty.')} />
|
|
|
|
</PreviewContainer>
|
|
|
|
<DownloadBtnContainer>
|
|
|
|
<DownloadButtonGroup />
|
|
|
|
</DownloadBtnContainer>
|
|
|
|
</>
|
2021-06-25 14:15:00 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2021-12-29 07:52:18 +00:00
|
|
|
<div>
|
2022-01-06 08:34:16 +00:00
|
|
|
<PreviewContainer>
|
2022-02-04 08:05:28 +00:00
|
|
|
<pre className="overflow-x-scroll p-0 text-sm md:p-3">{content}</pre>
|
2022-01-06 08:34:16 +00:00
|
|
|
</PreviewContainer>
|
|
|
|
<DownloadBtnContainer>
|
2022-02-14 11:33:19 +00:00
|
|
|
<DownloadButtonGroup />
|
2022-01-06 08:34:16 +00:00
|
|
|
</DownloadBtnContainer>
|
2021-12-29 07:52:18 +00:00
|
|
|
</div>
|
2021-06-25 14:15:00 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default TextPreview
|