file-view-header.tsx 6.5 KB

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