binary-file-header.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. import React, { useState, useCallback } from 'react'
  2. import PropTypes from 'prop-types'
  3. import { Trans, useTranslation } from 'react-i18next'
  4. import Icon from '../../../shared/components/icon'
  5. import { formatTime, relativeDate } from '../../utils/format-date'
  6. import { postJSON } from '../../../infrastructure/fetch-json'
  7. import useIsMounted from '../../../shared/hooks/use-is-mounted'
  8. import { useEditorContext } from '../../../shared/context/editor-context'
  9. import importOverleafModules from '../../../../macros/import-overleaf-module.macro'
  10. const tprLinkedFileInfo = importOverleafModules('tprLinkedFileInfo')
  11. const tprLinkedFileRefreshError = importOverleafModules(
  12. 'tprLinkedFileRefreshError'
  13. )
  14. const MAX_URL_LENGTH = 60
  15. const FRONT_OF_URL_LENGTH = 35
  16. const FILLER = '...'
  17. const TAIL_OF_URL_LENGTH = MAX_URL_LENGTH - FRONT_OF_URL_LENGTH - FILLER.length
  18. function shortenedUrl(url) {
  19. if (!url) {
  20. return
  21. }
  22. if (url.length > MAX_URL_LENGTH) {
  23. const front = url.slice(0, FRONT_OF_URL_LENGTH)
  24. const tail = url.slice(url.length - TAIL_OF_URL_LENGTH)
  25. return front + FILLER + tail
  26. }
  27. return url
  28. }
  29. export default function BinaryFileHeader({ file, storeReferencesKeys }) {
  30. const { projectId } = useEditorContext({
  31. projectId: PropTypes.string.isRequired,
  32. })
  33. const { t } = useTranslation()
  34. const isMounted = useIsMounted()
  35. const [refreshing, setRefreshing] = useState(false)
  36. const [refreshError, setRefreshError] = useState(null)
  37. let fileInfo
  38. if (file.linkedFileData) {
  39. if (file.linkedFileData.provider === 'url') {
  40. fileInfo = (
  41. <div>
  42. <UrlProvider file={file} />
  43. </div>
  44. )
  45. } else if (file.linkedFileData.provider === 'project_file') {
  46. fileInfo = (
  47. <div>
  48. <ProjectFilePathProvider file={file} />
  49. </div>
  50. )
  51. } else if (file.linkedFileData.provider === 'project_output_file') {
  52. fileInfo = (
  53. <div>
  54. <ProjectOutputFileProvider file={file} />
  55. </div>
  56. )
  57. }
  58. }
  59. const refreshFile = useCallback(() => {
  60. setRefreshing(true)
  61. // Replacement of the file handled by the file tree
  62. window.expectingLinkedFileRefreshedSocketFor = file.name
  63. postJSON(`/project/${projectId}/linked_file/${file.id}/refresh`)
  64. .then(() => {
  65. if (isMounted.current) {
  66. setRefreshing(false)
  67. }
  68. })
  69. .catch(err => {
  70. if (isMounted.current) {
  71. setRefreshing(false)
  72. setRefreshError(err.message)
  73. }
  74. })
  75. .finally(() => {
  76. if (
  77. file.linkedFileData.provider === 'mendeley' ||
  78. file.linkedFileData.provider === 'zotero' ||
  79. file.name.match(/^.*\.bib$/)
  80. ) {
  81. reindexReferences()
  82. }
  83. })
  84. function reindexReferences() {
  85. const opts = {
  86. body: { shouldBroadcast: true },
  87. }
  88. postJSON(`/project/${projectId}/references/indexAll`, opts)
  89. .then(response => {
  90. // Later updated by the socket but also updated here for immediate use
  91. storeReferencesKeys(response.keys)
  92. })
  93. .catch(error => {
  94. console.log(error)
  95. })
  96. }
  97. }, [file, projectId, isMounted, storeReferencesKeys])
  98. return (
  99. <div className="binary-file-header">
  100. {file.linkedFileData && fileInfo}
  101. {file.linkedFileData &&
  102. tprLinkedFileInfo.map(({ import: { LinkedFileInfo }, path }) => (
  103. <LinkedFileInfo key={path} file={file} />
  104. ))}
  105. {file.linkedFileData && (
  106. <button
  107. className="btn btn-success"
  108. onClick={refreshFile}
  109. disabled={refreshing}
  110. >
  111. <Icon type="refresh" spin={refreshing} modifier="fw" />
  112. <span>{refreshing ? t('refreshing') + '...' : t('refresh')}</span>
  113. </button>
  114. )}
  115. &nbsp;
  116. <a
  117. href={`/project/${projectId}/file/${file.id}`}
  118. className="btn btn-info"
  119. >
  120. <Icon type="download" modifier="fw" />
  121. &nbsp;
  122. <span>{t('download')}</span>
  123. </a>
  124. {refreshError && (
  125. <div className="row">
  126. <br />
  127. <div className="alert alert-danger col-md-6 col-md-offset-3">
  128. {t('error')}: {refreshError}
  129. {tprLinkedFileRefreshError.map(
  130. ({ import: { LinkedFileRefreshError }, path }) => (
  131. <LinkedFileRefreshError key={path} file={file} />
  132. )
  133. )}
  134. </div>
  135. </div>
  136. )}
  137. </div>
  138. )
  139. }
  140. BinaryFileHeader.propTypes = {
  141. file: PropTypes.shape({
  142. id: PropTypes.string,
  143. name: PropTypes.string,
  144. linkedFileData: PropTypes.object,
  145. }).isRequired,
  146. storeReferencesKeys: PropTypes.func.isRequired,
  147. }
  148. function UrlProvider({ file }) {
  149. return (
  150. <p>
  151. <Icon
  152. type="external-link-square"
  153. modifier="rotate-180"
  154. classes={{ icon: 'linked-file-icon' }}
  155. />
  156. &nbsp;
  157. <Trans
  158. i18nKey="imported_from_external_provider_at_date"
  159. components={
  160. /* eslint-disable-next-line jsx-a11y/anchor-has-content, react/jsx-key */
  161. [<a href={file.linkedFileData.url} />]
  162. }
  163. values={{
  164. shortenedUrl: shortenedUrl(file.linkedFileData.url),
  165. formattedDate: formatTime(file.created),
  166. relativeDate: relativeDate(file.created),
  167. }}
  168. />
  169. </p>
  170. )
  171. }
  172. UrlProvider.propTypes = {
  173. file: PropTypes.shape({
  174. linkedFileData: PropTypes.object,
  175. created: PropTypes.string,
  176. }).isRequired,
  177. }
  178. function ProjectFilePathProvider({ file }) {
  179. /* eslint-disable jsx-a11y/anchor-has-content, react/jsx-key */
  180. return (
  181. <p>
  182. <Icon
  183. type="external-link-square"
  184. modifier="rotate-180"
  185. classes={{ icon: 'linked-file-icon' }}
  186. />
  187. &nbsp;
  188. <Trans
  189. i18nKey="imported_from_another_project_at_date"
  190. components={
  191. file.linkedFileData.v1_source_doc_id
  192. ? [<span />]
  193. : [
  194. <a
  195. href={`/project/${file.linkedFileData.source_project_id}`}
  196. target="_blank"
  197. />,
  198. ]
  199. }
  200. values={{
  201. sourceEntityPath: file.linkedFileData.source_entity_path.slice(1),
  202. formattedDate: formatTime(file.created),
  203. relativeDate: relativeDate(file.created),
  204. }}
  205. />
  206. </p>
  207. /* esline-enable jsx-a11y/anchor-has-content, react/jsx-key */
  208. )
  209. }
  210. ProjectFilePathProvider.propTypes = {
  211. file: PropTypes.shape({
  212. linkedFileData: PropTypes.object,
  213. created: PropTypes.string,
  214. }).isRequired,
  215. }
  216. function ProjectOutputFileProvider({ file }) {
  217. return (
  218. <p>
  219. <Icon
  220. type="external-link-square"
  221. modifier="rotate-180"
  222. classes={{ icon: 'linked-file-icon' }}
  223. />
  224. &nbsp;
  225. <Trans
  226. i18nKey="imported_from_the_output_of_another_project_at_date"
  227. components={
  228. file.linkedFileData.v1_source_doc_id
  229. ? [<span />]
  230. : [
  231. <a
  232. href={`/project/${file.linkedFileData.source_project_id}`}
  233. target="_blank"
  234. />,
  235. ]
  236. }
  237. values={{
  238. sourceOutputFilePath: file.linkedFileData.source_output_file_path,
  239. formattedDate: formatTime(file.created),
  240. relativeDate: relativeDate(file.created),
  241. }}
  242. />
  243. </p>
  244. )
  245. }
  246. ProjectOutputFileProvider.propTypes = {
  247. file: PropTypes.shape({
  248. linkedFileData: PropTypes.object,
  249. created: PropTypes.string,
  250. }).isRequired,
  251. }