file-view-header.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import { useState, type ElementType } from 'react'
  2. import { Trans, useTranslation } from 'react-i18next'
  3. import { formatTime, relativeDate } from '../../utils/format-date'
  4. import { useFileTreeData } from '@/shared/context/file-tree-data-context'
  5. import { useProjectContext } from '@/shared/context/project-context'
  6. import { Nullable } from '../../../../../types/utils'
  7. import importOverleafModules from '../../../../macros/import-overleaf-module.macro'
  8. import { LinkedFileIcon } from './file-view-icons'
  9. import { BinaryFile, hasProvider, LinkedFile } from '../types/binary-file'
  10. import FileViewRefreshButton from './file-view-refresh-button'
  11. import FileViewRefreshError from './file-view-refresh-error'
  12. import MaterialIcon from '@/shared/components/material-icon'
  13. import OLButton from '@/shared/components/ol/ol-button'
  14. const tprFileViewInfo = importOverleafModules('tprFileViewInfo') as {
  15. import: { TPRFileViewInfo: ElementType }
  16. path: string
  17. }[]
  18. const tprFileViewNotOriginalImporter = importOverleafModules(
  19. 'tprFileViewNotOriginalImporter'
  20. ) as {
  21. import: { TPRFileViewNotOriginalImporter: 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. }
  42. export default function FileViewHeader({ file }: FileViewHeaderProps) {
  43. const { projectId } = useProjectContext()
  44. const { fileTreeReadOnly } = useFileTreeData()
  45. const { t } = useTranslation()
  46. const [refreshError, setRefreshError] = useState<Nullable<string>>(null)
  47. let fileInfo
  48. if (file.linkedFileData) {
  49. if (hasProvider(file, 'url')) {
  50. fileInfo = <UrlProvider file={file} />
  51. } else if (hasProvider(file, 'project_file')) {
  52. fileInfo = <ProjectFilePathProvider file={file} />
  53. } else if (hasProvider(file, 'project_output_file')) {
  54. fileInfo = <ProjectOutputFileProvider file={file} />
  55. }
  56. }
  57. return (
  58. <>
  59. {file.linkedFileData && fileInfo}
  60. {file.linkedFileData &&
  61. tprFileViewInfo.map(({ import: { TPRFileViewInfo }, path }) => (
  62. <TPRFileViewInfo key={path} file={file} />
  63. ))}
  64. <div className="file-view-buttons">
  65. {file.linkedFileData && !fileTreeReadOnly && (
  66. <FileViewRefreshButton
  67. file={file}
  68. setRefreshError={setRefreshError}
  69. />
  70. )}
  71. <OLButton
  72. variant="secondary"
  73. download={file.name}
  74. href={`/project/${projectId}/blob/${file.hash}`}
  75. >
  76. <MaterialIcon type="download" className="align-middle" />{' '}
  77. <span>{t('download')}</span>
  78. </OLButton>
  79. </div>
  80. {file.linkedFileData &&
  81. tprFileViewNotOriginalImporter.map(
  82. ({ import: { TPRFileViewNotOriginalImporter }, path }) => (
  83. <TPRFileViewNotOriginalImporter key={path} file={file} />
  84. )
  85. )[0]}
  86. {refreshError && (
  87. <FileViewRefreshError file={file} refreshError={refreshError} />
  88. )}
  89. {/* Workaround for Safari issue: https://github.com/overleaf/internal/issues/21363
  90. * The editor behind a file view receives key events and updates the file even if Codemirror view is not focused.
  91. * Changing the focus to a hidden textarea prevents this
  92. */}
  93. <textarea
  94. // eslint-disable-next-line jsx-a11y/no-autofocus
  95. autoFocus
  96. aria-label="Invisible element to manage focus and prevent unintended behavior"
  97. tabIndex={-1}
  98. style={{ position: 'absolute', left: '-9999px' }}
  99. />
  100. </>
  101. )
  102. }
  103. type UrlProviderProps = {
  104. file: LinkedFile<'url'>
  105. }
  106. function UrlProvider({ file }: UrlProviderProps) {
  107. return (
  108. <p>
  109. <LinkedFileIcon />
  110. &nbsp;
  111. <Trans
  112. i18nKey="imported_from_external_provider_at_date"
  113. components={
  114. /* eslint-disable-next-line jsx-a11y/anchor-has-content, react/jsx-key */
  115. [<a href={file.linkedFileData.url} />]
  116. }
  117. values={{
  118. shortenedUrl: shortenedUrl(file.linkedFileData.url),
  119. formattedDate: formatTime(file.created),
  120. relativeDate: relativeDate(file.created),
  121. }}
  122. shouldUnescape
  123. tOptions={{ interpolation: { escapeValue: true } }}
  124. />
  125. </p>
  126. )
  127. }
  128. type ProjectFilePathProviderProps = {
  129. file: LinkedFile<'project_file'>
  130. }
  131. function ProjectFilePathProvider({ file }: ProjectFilePathProviderProps) {
  132. /* eslint-disable jsx-a11y/anchor-has-content, react/jsx-key */
  133. return (
  134. <p>
  135. <LinkedFileIcon />{' '}
  136. <Trans
  137. i18nKey="imported_from_another_project_at_date"
  138. components={
  139. file.linkedFileData.v1_source_doc_id
  140. ? [<span />]
  141. : [
  142. <a
  143. href={`/project/${file.linkedFileData.source_project_id}`}
  144. target="_blank"
  145. rel="noopener"
  146. />,
  147. ]
  148. }
  149. values={{
  150. sourceEntityPath: file.linkedFileData.source_entity_path.slice(1),
  151. formattedDate: formatTime(file.created),
  152. relativeDate: relativeDate(file.created),
  153. }}
  154. shouldUnescape
  155. tOptions={{ interpolation: { escapeValue: true } }}
  156. />
  157. </p>
  158. /* esline-enable jsx-a11y/anchor-has-content, react/jsx-key */
  159. )
  160. }
  161. type ProjectOutputFileProviderProps = {
  162. file: LinkedFile<'project_output_file'>
  163. }
  164. function ProjectOutputFileProvider({ file }: ProjectOutputFileProviderProps) {
  165. return (
  166. <p>
  167. <LinkedFileIcon />
  168. &nbsp;
  169. <Trans
  170. i18nKey="imported_from_the_output_of_another_project_at_date"
  171. components={
  172. file.linkedFileData.v1_source_doc_id
  173. ? [<span />]
  174. : [
  175. <a
  176. href={`/project/${file.linkedFileData.source_project_id}`}
  177. target="_blank"
  178. rel="noopener"
  179. />,
  180. ]
  181. }
  182. values={{
  183. sourceOutputFilePath: file.linkedFileData.source_output_file_path,
  184. formattedDate: formatTime(file.created),
  185. relativeDate: relativeDate(file.created),
  186. }}
  187. shouldUnescape
  188. tOptions={{ interpolation: { escapeValue: true } }}
  189. />
  190. </p>
  191. )
  192. }