66 lines
1.7 KiB
TypeScript
66 lines
1.7 KiB
TypeScript
import { useRouter } from 'next/router'
|
|
import { useTranslation } from 'next-i18next'
|
|
|
|
import FourOhFour from '../FourOhFour'
|
|
import Loading from '../Loading'
|
|
import { DownloadButton } from '../DownloadBtnGtoup'
|
|
import useFileContent from '../../utils/fetchOnMount'
|
|
import { DownloadBtnContainer, PreviewContainer } from './Containers'
|
|
|
|
const parseDotUrl = (content: string): string | undefined => {
|
|
return content
|
|
.split('\n')
|
|
.find(line => line.startsWith('URL='))
|
|
?.split('=')[1]
|
|
}
|
|
|
|
const TextPreview = ({ file }) => {
|
|
const { asPath } = useRouter()
|
|
const { t } = useTranslation()
|
|
|
|
const { response: content, error, validating } = useFileContent(`/api/raw/?path=${asPath}`, asPath)
|
|
if (error) {
|
|
return (
|
|
<PreviewContainer>
|
|
<FourOhFour errorMsg={error} />
|
|
</PreviewContainer>
|
|
)
|
|
}
|
|
|
|
if (validating) {
|
|
return (
|
|
<PreviewContainer>
|
|
<Loading loadingText={t('Loading file content...')} />
|
|
</PreviewContainer>
|
|
)
|
|
}
|
|
|
|
if (!content) {
|
|
return (
|
|
<PreviewContainer>
|
|
<FourOhFour errorMsg={t('File is empty.')} />
|
|
</PreviewContainer>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<div>
|
|
<PreviewContainer>
|
|
<pre className="overflow-x-scroll p-0 text-sm md:p-3">{content}</pre>
|
|
</PreviewContainer>
|
|
<DownloadBtnContainer>
|
|
<div className="flex justify-center">
|
|
<DownloadButton
|
|
onClickCallback={() => window.open(parseDotUrl(content) ?? '')}
|
|
btnColor="blue"
|
|
btnText={t('Open URL')}
|
|
btnIcon="external-link-alt"
|
|
btnTitle={t('Open URL{{url}}', { url: ' ' + parseDotUrl(content) ?? '' })}
|
|
/>
|
|
</div>
|
|
</DownloadBtnContainer>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default TextPreview
|