file-view-header.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. </>
  96. )
  97. }
  98. type UrlProviderProps = {
  99. file: LinkedFile<'url'>
  100. }
  101. function UrlProvider({ file }: UrlProviderProps) {
  102. return (
  103. <p>
  104. <LinkedFileIcon />
  105. &nbsp;
  106. <Trans
  107. i18nKey="imported_from_external_provider_at_date"
  108. components={
  109. /* eslint-disable-next-line jsx-a11y/anchor-has-content, react/jsx-key */
  110. [<a href={file.linkedFileData.url} />]
  111. }
  112. values={{
  113. shortenedUrl: shortenedUrl(file.linkedFileData.url),
  114. formattedDate: formatTime(file.created),
  115. relativeDate: relativeDate(file.created),
  116. }}
  117. shouldUnescape
  118. tOptions={{ interpolation: { escapeValue: true } }}
  119. />
  120. </p>
  121. )
  122. }
  123. type ProjectFilePathProviderProps = {
  124. file: LinkedFile<'project_file'>
  125. }
  126. function ProjectFilePathProvider({ file }: ProjectFilePathProviderProps) {
  127. /* eslint-disable jsx-a11y/anchor-has-content, react/jsx-key */
  128. return (
  129. <p>
  130. <LinkedFileIcon />{' '}
  131. <Trans
  132. i18nKey="imported_from_another_project_at_date"
  133. components={
  134. file.linkedFileData.v1_source_doc_id
  135. ? [<span />]
  136. : [
  137. <a
  138. href={`/project/${file.linkedFileData.source_project_id}`}
  139. target="_blank"
  140. rel="noopener"
  141. />,
  142. ]
  143. }
  144. values={{
  145. sourceEntityPath: file.linkedFileData.source_entity_path.slice(1),
  146. formattedDate: formatTime(file.created),
  147. relativeDate: relativeDate(file.created),
  148. }}
  149. shouldUnescape
  150. tOptions={{ interpolation: { escapeValue: true } }}
  151. />
  152. </p>
  153. /* esline-enable jsx-a11y/anchor-has-content, react/jsx-key */
  154. )
  155. }
  156. type ProjectOutputFileProviderProps = {
  157. file: LinkedFile<'project_output_file'>
  158. }
  159. function ProjectOutputFileProvider({ file }: ProjectOutputFileProviderProps) {
  160. return (
  161. <p>
  162. <LinkedFileIcon />
  163. &nbsp;
  164. <Trans
  165. i18nKey="imported_from_the_output_of_another_project_at_date"
  166. components={
  167. file.linkedFileData.v1_source_doc_id
  168. ? [<span />]
  169. : [
  170. <a
  171. href={`/project/${file.linkedFileData.source_project_id}`}
  172. target="_blank"
  173. rel="noopener"
  174. />,
  175. ]
  176. }
  177. values={{
  178. sourceOutputFilePath: file.linkedFileData.source_output_file_path,
  179. formattedDate: formatTime(file.created),
  180. relativeDate: relativeDate(file.created),
  181. }}
  182. shouldUnescape
  183. tOptions={{ interpolation: { escapeValue: true } }}
  184. />
  185. </p>
  186. )
  187. }