2021-06-23 05:37:08 -04:00
|
|
|
import { useState, useCallback } from 'react'
|
2021-04-28 07:41:20 -04:00
|
|
|
import PropTypes from 'prop-types'
|
2021-05-21 07:32:51 -04:00
|
|
|
import { Trans, useTranslation } from 'react-i18next'
|
|
|
|
|
2021-04-28 07:41:20 -04:00
|
|
|
import Icon from '../../../shared/components/icon'
|
|
|
|
import { formatTime, relativeDate } from '../../utils/format-date'
|
|
|
|
import { postJSON } from '../../../infrastructure/fetch-json'
|
2021-09-29 05:05:12 -04:00
|
|
|
import { useEditorContext } from '../../../shared/context/editor-context'
|
2021-06-25 04:13:17 -04:00
|
|
|
import { useProjectContext } from '../../../shared/context/project-context'
|
2021-04-28 07:41:20 -04:00
|
|
|
|
2021-05-21 07:32:51 -04:00
|
|
|
import importOverleafModules from '../../../../macros/import-overleaf-module.macro'
|
2021-06-23 04:09:23 -04:00
|
|
|
import useAbortController from '../../../shared/hooks/use-abort-controller'
|
2022-01-19 06:56:57 -05:00
|
|
|
import { LinkedFileIcon } from './file-view-icons'
|
2021-04-28 07:41:20 -04:00
|
|
|
const tprLinkedFileInfo = importOverleafModules('tprLinkedFileInfo')
|
|
|
|
const tprLinkedFileRefreshError = importOverleafModules(
|
|
|
|
'tprLinkedFileRefreshError'
|
|
|
|
)
|
|
|
|
|
|
|
|
const MAX_URL_LENGTH = 60
|
|
|
|
const FRONT_OF_URL_LENGTH = 35
|
|
|
|
const FILLER = '...'
|
|
|
|
const TAIL_OF_URL_LENGTH = MAX_URL_LENGTH - FRONT_OF_URL_LENGTH - FILLER.length
|
|
|
|
|
|
|
|
function shortenedUrl(url) {
|
|
|
|
if (!url) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (url.length > MAX_URL_LENGTH) {
|
|
|
|
const front = url.slice(0, FRONT_OF_URL_LENGTH)
|
|
|
|
const tail = url.slice(url.length - TAIL_OF_URL_LENGTH)
|
|
|
|
return front + FILLER + tail
|
|
|
|
}
|
|
|
|
return url
|
|
|
|
}
|
|
|
|
|
2021-06-10 07:26:04 -04:00
|
|
|
export default function FileViewHeader({ file, storeReferencesKeys }) {
|
2021-06-25 04:13:17 -04:00
|
|
|
const { _id: projectId } = useProjectContext({
|
|
|
|
_id: PropTypes.string.isRequired,
|
2021-05-21 07:32:51 -04:00
|
|
|
})
|
2021-09-29 05:05:12 -04:00
|
|
|
const { permissionsLevel } = useEditorContext({
|
2021-10-08 05:25:13 -04:00
|
|
|
permissionsLevel: PropTypes.string,
|
2021-09-29 05:05:12 -04:00
|
|
|
})
|
2021-04-28 07:41:20 -04:00
|
|
|
const { t } = useTranslation()
|
|
|
|
|
2021-05-21 07:32:51 -04:00
|
|
|
const [refreshing, setRefreshing] = useState(false)
|
|
|
|
const [refreshError, setRefreshError] = useState(null)
|
|
|
|
|
2021-06-23 04:09:23 -04:00
|
|
|
const { signal } = useAbortController()
|
|
|
|
|
2021-04-28 07:41:20 -04:00
|
|
|
let fileInfo
|
|
|
|
if (file.linkedFileData) {
|
|
|
|
if (file.linkedFileData.provider === 'url') {
|
|
|
|
fileInfo = (
|
|
|
|
<div>
|
|
|
|
<UrlProvider file={file} />
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
} else if (file.linkedFileData.provider === 'project_file') {
|
|
|
|
fileInfo = (
|
|
|
|
<div>
|
|
|
|
<ProjectFilePathProvider file={file} />
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
} else if (file.linkedFileData.provider === 'project_output_file') {
|
|
|
|
fileInfo = (
|
|
|
|
<div>
|
|
|
|
<ProjectOutputFileProvider file={file} />
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const refreshFile = useCallback(() => {
|
|
|
|
setRefreshing(true)
|
|
|
|
// Replacement of the file handled by the file tree
|
|
|
|
window.expectingLinkedFileRefreshedSocketFor = file.name
|
2021-06-23 04:09:23 -04:00
|
|
|
postJSON(`/project/${projectId}/linked_file/${file.id}/refresh`, { signal })
|
2021-04-28 07:41:20 -04:00
|
|
|
.then(() => {
|
2021-06-23 04:09:23 -04:00
|
|
|
setRefreshing(false)
|
2021-04-28 07:41:20 -04:00
|
|
|
})
|
|
|
|
.catch(err => {
|
2021-06-23 04:09:23 -04:00
|
|
|
setRefreshing(false)
|
2021-07-30 04:57:48 -04:00
|
|
|
setRefreshError(err.data?.message || err.message)
|
2021-04-28 07:41:20 -04:00
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
if (
|
|
|
|
file.linkedFileData.provider === 'mendeley' ||
|
|
|
|
file.linkedFileData.provider === 'zotero' ||
|
|
|
|
file.name.match(/^.*\.bib$/)
|
|
|
|
) {
|
|
|
|
reindexReferences()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
function reindexReferences() {
|
|
|
|
const opts = {
|
|
|
|
body: { shouldBroadcast: true },
|
|
|
|
}
|
|
|
|
|
2021-05-21 07:32:51 -04:00
|
|
|
postJSON(`/project/${projectId}/references/indexAll`, opts)
|
2021-04-28 07:41:20 -04:00
|
|
|
.then(response => {
|
|
|
|
// Later updated by the socket but also updated here for immediate use
|
|
|
|
storeReferencesKeys(response.keys)
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.log(error)
|
|
|
|
})
|
|
|
|
}
|
2021-06-23 04:09:23 -04:00
|
|
|
}, [file, projectId, signal, storeReferencesKeys])
|
2021-04-28 07:41:20 -04:00
|
|
|
|
|
|
|
return (
|
2021-06-10 07:26:04 -04:00
|
|
|
<div>
|
2021-04-28 07:41:20 -04:00
|
|
|
{file.linkedFileData && fileInfo}
|
|
|
|
{file.linkedFileData &&
|
|
|
|
tprLinkedFileInfo.map(({ import: { LinkedFileInfo }, path }) => (
|
|
|
|
<LinkedFileInfo key={path} file={file} />
|
|
|
|
))}
|
2021-09-29 05:05:12 -04:00
|
|
|
{file.linkedFileData && permissionsLevel !== 'readOnly' && (
|
2021-04-28 07:41:20 -04:00
|
|
|
<button
|
|
|
|
className="btn btn-success"
|
|
|
|
onClick={refreshFile}
|
|
|
|
disabled={refreshing}
|
|
|
|
>
|
2022-01-19 06:56:57 -05:00
|
|
|
<Icon type="refresh" spin={refreshing} fw />
|
2021-04-28 07:41:20 -04:00
|
|
|
<span>{refreshing ? t('refreshing') + '...' : t('refresh')}</span>
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<a
|
2021-05-21 07:32:51 -04:00
|
|
|
href={`/project/${projectId}/file/${file.id}`}
|
2021-04-28 07:41:20 -04:00
|
|
|
className="btn btn-info"
|
|
|
|
>
|
2022-01-19 06:56:57 -05:00
|
|
|
<Icon type="download" fw />
|
2021-05-07 09:20:55 -04:00
|
|
|
|
|
|
|
<span>{t('download')}</span>
|
2021-04-28 07:41:20 -04:00
|
|
|
</a>
|
|
|
|
{refreshError && (
|
|
|
|
<div className="row">
|
|
|
|
<br />
|
|
|
|
<div className="alert alert-danger col-md-6 col-md-offset-3">
|
2021-05-07 09:20:55 -04:00
|
|
|
{t('error')}: {refreshError}
|
2021-04-28 07:41:20 -04:00
|
|
|
{tprLinkedFileRefreshError.map(
|
|
|
|
({ import: { LinkedFileRefreshError }, path }) => (
|
|
|
|
<LinkedFileRefreshError key={path} file={file} />
|
|
|
|
)
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-06-10 07:26:04 -04:00
|
|
|
FileViewHeader.propTypes = {
|
2021-04-28 07:41:20 -04:00
|
|
|
file: PropTypes.shape({
|
|
|
|
id: PropTypes.string,
|
|
|
|
name: PropTypes.string,
|
|
|
|
linkedFileData: PropTypes.object,
|
|
|
|
}).isRequired,
|
|
|
|
storeReferencesKeys: PropTypes.func.isRequired,
|
|
|
|
}
|
|
|
|
|
|
|
|
function UrlProvider({ file }) {
|
|
|
|
return (
|
|
|
|
<p>
|
2022-01-19 06:56:57 -05:00
|
|
|
<LinkedFileIcon />
|
2021-04-28 07:41:20 -04:00
|
|
|
|
|
|
|
<Trans
|
|
|
|
i18nKey="imported_from_external_provider_at_date"
|
|
|
|
components={
|
|
|
|
/* eslint-disable-next-line jsx-a11y/anchor-has-content, react/jsx-key */
|
|
|
|
[<a href={file.linkedFileData.url} />]
|
|
|
|
}
|
|
|
|
values={{
|
|
|
|
shortenedUrl: shortenedUrl(file.linkedFileData.url),
|
|
|
|
formattedDate: formatTime(file.created),
|
|
|
|
relativeDate: relativeDate(file.created),
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</p>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
UrlProvider.propTypes = {
|
|
|
|
file: PropTypes.shape({
|
|
|
|
linkedFileData: PropTypes.object,
|
|
|
|
created: PropTypes.string,
|
|
|
|
}).isRequired,
|
|
|
|
}
|
|
|
|
|
|
|
|
function ProjectFilePathProvider({ file }) {
|
|
|
|
/* eslint-disable jsx-a11y/anchor-has-content, react/jsx-key */
|
|
|
|
return (
|
|
|
|
<p>
|
2022-01-19 06:56:57 -05:00
|
|
|
<LinkedFileIcon />
|
2021-04-28 07:41:20 -04:00
|
|
|
|
|
|
|
<Trans
|
|
|
|
i18nKey="imported_from_another_project_at_date"
|
|
|
|
components={
|
|
|
|
file.linkedFileData.v1_source_doc_id
|
|
|
|
? [<span />]
|
|
|
|
: [
|
|
|
|
<a
|
|
|
|
href={`/project/${file.linkedFileData.source_project_id}`}
|
|
|
|
target="_blank"
|
2021-09-14 06:43:57 -04:00
|
|
|
rel="noopener"
|
2021-04-28 07:41:20 -04:00
|
|
|
/>,
|
|
|
|
]
|
|
|
|
}
|
|
|
|
values={{
|
|
|
|
sourceEntityPath: file.linkedFileData.source_entity_path.slice(1),
|
|
|
|
formattedDate: formatTime(file.created),
|
|
|
|
relativeDate: relativeDate(file.created),
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</p>
|
|
|
|
/* esline-enable jsx-a11y/anchor-has-content, react/jsx-key */
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
ProjectFilePathProvider.propTypes = {
|
|
|
|
file: PropTypes.shape({
|
|
|
|
linkedFileData: PropTypes.object,
|
|
|
|
created: PropTypes.string,
|
|
|
|
}).isRequired,
|
|
|
|
}
|
|
|
|
|
|
|
|
function ProjectOutputFileProvider({ file }) {
|
|
|
|
return (
|
|
|
|
<p>
|
2022-01-19 06:56:57 -05:00
|
|
|
<LinkedFileIcon />
|
2021-04-28 07:41:20 -04:00
|
|
|
|
|
|
|
<Trans
|
|
|
|
i18nKey="imported_from_the_output_of_another_project_at_date"
|
|
|
|
components={
|
|
|
|
file.linkedFileData.v1_source_doc_id
|
|
|
|
? [<span />]
|
|
|
|
: [
|
|
|
|
<a
|
|
|
|
href={`/project/${file.linkedFileData.source_project_id}`}
|
|
|
|
target="_blank"
|
2021-09-14 06:43:57 -04:00
|
|
|
rel="noopener"
|
2021-04-28 07:41:20 -04:00
|
|
|
/>,
|
|
|
|
]
|
|
|
|
}
|
|
|
|
values={{
|
|
|
|
sourceOutputFilePath: file.linkedFileData.source_output_file_path,
|
|
|
|
formattedDate: formatTime(file.created),
|
|
|
|
relativeDate: relativeDate(file.created),
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</p>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
ProjectOutputFileProvider.propTypes = {
|
|
|
|
file: PropTypes.shape({
|
|
|
|
linkedFileData: PropTypes.object,
|
|
|
|
created: PropTypes.string,
|
|
|
|
}).isRequired,
|
|
|
|
}
|