import { useState, useCallback, type ElementType } from 'react' import PropTypes from 'prop-types' import { Trans, useTranslation } from 'react-i18next' import Icon from '../../../shared/components/icon' import { formatTime, relativeDate } from '../../utils/format-date' import { postJSON } from '../../../infrastructure/fetch-json' import { useEditorContext } from '../../../shared/context/editor-context' import { useProjectContext } from '../../../shared/context/project-context' import importOverleafModules from '../../../../macros/import-overleaf-module.macro' import useAbortController from '../../../shared/hooks/use-abort-controller' import { LinkedFileIcon } from './file-view-icons' import { BinaryFile, hasProvider, LinkedFile } from '../types/binary-file' const tprLinkedFileInfo = importOverleafModules('tprLinkedFileInfo') as { import: { LinkedFileInfo: ElementType } path: string }[] const tprLinkedFileRefreshError = importOverleafModules( 'tprLinkedFileRefreshError' ) as { import: { LinkedFileRefreshError: ElementType } path: string }[] const MAX_URL_LENGTH = 60 const FRONT_OF_URL_LENGTH = 35 const FILLER = '...' const TAIL_OF_URL_LENGTH = MAX_URL_LENGTH - FRONT_OF_URL_LENGTH - FILLER.length function shortenedUrl(url: string) { if (!url) { return } if (url.length > MAX_URL_LENGTH) { const front = url.slice(0, FRONT_OF_URL_LENGTH) const tail = url.slice(url.length - TAIL_OF_URL_LENGTH) return front + FILLER + tail } return url } type FileViewHeaderProps = { file: BinaryFile } export default function FileViewHeader({ file }: FileViewHeaderProps) { const { _id: projectId } = useProjectContext({ _id: PropTypes.string.isRequired, }) const { permissionsLevel } = useEditorContext({ permissionsLevel: PropTypes.string, }) const { t } = useTranslation() const [refreshing, setRefreshing] = useState(false) const [refreshError, setRefreshError] = useState(null) const { signal } = useAbortController() let fileInfo if (file.linkedFileData) { if (hasProvider(file, 'url')) { fileInfo = (
) } else if (hasProvider(file, 'project_file')) { fileInfo = (
) } else if (hasProvider(file, 'project_output_file')) { fileInfo = (
) } } const refreshFile = useCallback(() => { setRefreshing(true) // Replacement of the file handled by the file tree window.expectingLinkedFileRefreshedSocketFor = file.name const body = { shouldReindexReferences: file.linkedFileData?.provider === 'mendeley' || file.linkedFileData?.provider === 'zotero' || /\.bib$/.test(file.name), } postJSON(`/project/${projectId}/linked_file/${file.id}/refresh`, { signal, body, }) .then(() => { setRefreshing(false) }) .catch(err => { setRefreshing(false) setRefreshError(err.data?.message || err.message) }) }, [file, projectId, signal]) return (
{file.linkedFileData && fileInfo} {file.linkedFileData && tprLinkedFileInfo.map(({ import: { LinkedFileInfo }, path }) => ( ))} {file.linkedFileData && permissionsLevel !== 'readOnly' && ( )}     {t('download')} {refreshError && (

{t('access_denied')}: {refreshError} {tprLinkedFileRefreshError.map( ({ import: { LinkedFileRefreshError }, path }) => ( ) )}
)}
) } type UrlProviderProps = { file: LinkedFile<'url'> } function UrlProvider({ file }: UrlProviderProps) { return (

  ] } values={{ shortenedUrl: shortenedUrl(file.linkedFileData.url), formattedDate: formatTime(file.created), relativeDate: relativeDate(file.created), }} />

) } type ProjectFilePathProviderProps = { file: LinkedFile<'project_file'> } function ProjectFilePathProvider({ file }: ProjectFilePathProviderProps) { /* eslint-disable jsx-a11y/anchor-has-content, react/jsx-key */ return (

  ] : [ , ] } values={{ sourceEntityPath: file.linkedFileData.source_entity_path.slice(1), formattedDate: formatTime(file.created), relativeDate: relativeDate(file.created), }} />

/* esline-enable jsx-a11y/anchor-has-content, react/jsx-key */ ) } type ProjectOutputFileProviderProps = { file: LinkedFile<'project_output_file'> } function ProjectOutputFileProvider({ file }: ProjectOutputFileProviderProps) { return (

  ] : [ , ] } values={{ sourceOutputFilePath: file.linkedFileData.source_output_file_path, formattedDate: formatTime(file.created), relativeDate: relativeDate(file.created), }} />

) }