file-view-header.tsx 6.7 KB

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