file-view-header.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 { 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 { useSnapshotContext } from '@/features/ide-react/context/snapshot-context'
  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 { fileTreeFromHistory } = useSnapshotContext()
  49. const { t } = useTranslation()
  50. const [refreshError, setRefreshError] = useState<Nullable<string>>(null)
  51. let fileInfo
  52. if (file.linkedFileData) {
  53. if (hasProvider(file, 'url')) {
  54. fileInfo = <UrlProvider file={file} />
  55. } else if (hasProvider(file, 'project_file')) {
  56. fileInfo = <ProjectFilePathProvider file={file} />
  57. } else if (hasProvider(file, 'project_output_file')) {
  58. fileInfo = <ProjectOutputFileProvider file={file} />
  59. }
  60. }
  61. return (
  62. <>
  63. {file.linkedFileData && fileInfo}
  64. {file.linkedFileData &&
  65. tprFileViewInfo.map(({ import: { TPRFileViewInfo }, path }) => (
  66. <TPRFileViewInfo key={path} file={file} />
  67. ))}
  68. <div className="file-view-buttons">
  69. {file.linkedFileData && !fileTreeReadOnly && (
  70. <FileViewRefreshButton
  71. file={file}
  72. setRefreshError={setRefreshError}
  73. />
  74. )}
  75. <OLButton
  76. variant="secondary"
  77. download={file.name}
  78. href={
  79. fileTreeFromHistory
  80. ? `/project/${projectId}/blob/${file.hash}`
  81. : `/project/${projectId}/file/${file.id}`
  82. }
  83. >
  84. <BootstrapVersionSwitcher
  85. bs3={<Icon type="download" fw />}
  86. bs5={<MaterialIcon type="download" className="align-middle" />}
  87. />{' '}
  88. <span>{t('download')}</span>
  89. </OLButton>
  90. </div>
  91. {file.linkedFileData &&
  92. tprFileViewNotOriginalImporter.map(
  93. ({ import: { TPRFileViewNotOriginalImporter }, path }) => (
  94. <TPRFileViewNotOriginalImporter key={path} file={file} />
  95. )
  96. )[0]}
  97. {refreshError && (
  98. <FileViewRefreshError file={file} refreshError={refreshError} />
  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. }