file-view-header.tsx 7.3 KB

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