import React, { useEffect } from 'react' import { useTranslation } from 'react-i18next' import useAsync from '@/shared/hooks/use-async' import { debugConsole } from '@/utils/debugging' import { postJSON, getJSON, } from '@/infrastructure/fetch-json' import getMeta from '@/utils/meta' import { OLModal, OLModalBody, OLModalFooter, OLModalHeader, OLModalTitle, } from '@/shared/components/ol/ol-modal' import OLButton from '@/shared/components/ol/ol-button' import OLSpinner from '@/shared/components/ol/ol-spinner' import Notification from '@/shared/components/notification' type GitSyncRepo = { name: string fullName: string defaultBranchName: string } type GitSyncReposResponse = { repos?: GitSyncRepo[] } function ImportFromGitLabModalContent({ handleHide }: { handleHide: () => void }) { const { t } = useTranslation() const { appName } = getMeta('ol-ExposedSettings') const { isLoading, isSuccess, isError, data, runAsync, } = useAsync() useEffect(() => { runAsync(getJSON('/user/gitlab-sync/repos')) .catch(err => debugConsole.error(err?.data?.message || err?.message || err)) }, [runAsync]) const reposExist = data?.repos != null const repos = reposExist ? data.repos : [] const { isLoading: isImporting, isSuccess: isImported, isError: isErrorImport, runAsync: runAsyncImport, } = useAsync<{ projectId: string }>() useEffect(() => { if (isImported) { handleHide() } }, [handleHide, isImported]) let gitlabUrl = getMeta('ol-ExposedSettings').gitlabUrl || '' gitlabUrl = gitlabUrl.endsWith('/') ? gitlabUrl.slice(0, -1) : gitlabUrl const showLinkToGitLab = !isImporting && isSuccess && !reposExist const showRepos = !isImporting && isSuccess && reposExist const handleImport = (repo: GitSyncRepo) => { runAsyncImport( postJSON('/project/new/gitlab-sync', { body: repo }) ) .then(data => { window.location.href = `/project/${data.projectId}` }) .catch(err => debugConsole.error(err?.data?.message || err?.message || err)) } return ( <> {t('import_from_gitlab')} {isLoading && ( {t('loading_gitlab_repositories')} )} {isImporting && ( {t('importing')} )} {isError && (
)} {showLinkToGitLab && (

{t('link_to_gitlab_description', { appName })}

{t('link_to_gitlab')}
)} {showRepos && (
{repos.length === 0 ? (

{t('you_dont_have_any_repositories')}

) : ( <>

{t('select_gitlab_repository', { appName })}

{repos.map(repo => ( ))}
{repo.name} handleImport(repo)} > {t('import_to_sharelatex', { appName: 'Overleaf' })}
)}
)} {isErrorImport && (
)}
{!isImporting && ( {t('cant_see_what_youre_looking_for_question')} )} {t('cancel')} ) } export default function ImportFromGitLabModal({ onHide }: { onHide: () => void }) { return ( ) }