mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-11-12 13:13:45 +01:00
175 lines
4.4 KiB
TypeScript
175 lines
4.4 KiB
TypeScript
import { useCallback, useEffect, useState } from 'react'
|
|
import { useTranslation } from 'react-i18next'
|
|
import getMeta from '@/utils/meta'
|
|
import { getJSON, postJSON } from '@/infrastructure/fetch-json'
|
|
import useAsync from '@/shared/hooks/use-async'
|
|
import { debugConsole } from '@/utils/debugging'
|
|
import OLButton from '@/shared/components/ol/ol-button'
|
|
import {
|
|
OLModal,
|
|
OLModalBody,
|
|
OLModalFooter,
|
|
OLModalHeader,
|
|
OLModalTitle,
|
|
} from '@/shared/components/ol/ol-modal'
|
|
import Notification from '@/shared/components/notification'
|
|
import GitLabLogo from '@/shared/svgs/gitlab-logo'
|
|
|
|
export const GitLabSyncWidget = function GitLabSyncWidget() {
|
|
const { t } = useTranslation()
|
|
const { appName } = getMeta('ol-ExposedSettings')
|
|
|
|
const {
|
|
isLoading: isCheckingConn,
|
|
isError: isErrorConnCheck,
|
|
runAsync: runAsyncConnCheck,
|
|
data: isConnected,
|
|
setData: setConnState,
|
|
} = useAsync<boolean>()
|
|
|
|
const {
|
|
isLoading: isUnlinking,
|
|
isError: isErrorUnlink,
|
|
runAsync: runAsyncUnlink,
|
|
} = useAsync<void>()
|
|
|
|
const [showUnlinkModal, setShowUnlinkModal] = useState(false)
|
|
|
|
const handleConnCheck = useCallback(() => {
|
|
runAsyncConnCheck(getJSON('/user/gitlab-sync/status')).catch(err =>
|
|
debugConsole.error(err?.data?.message || err?.message || err),
|
|
)
|
|
}, [runAsyncConnCheck])
|
|
|
|
useEffect(() => {
|
|
handleConnCheck()
|
|
}, [handleConnCheck])
|
|
|
|
const handleUnlink = useCallback(() => {
|
|
runAsyncUnlink(postJSON('/user/gitlab-sync/unlink'))
|
|
.then(() => setConnState(false))
|
|
.catch(err => debugConsole.error(err?.data?.message || err?.message || err))
|
|
.finally(() => setShowUnlinkModal(false))
|
|
}, [runAsyncUnlink])
|
|
|
|
if (isCheckingConn) {
|
|
return (
|
|
<div className="settings-widget-container">
|
|
<div>
|
|
<GitLabLogo />
|
|
</div>
|
|
|
|
<div className="description-container">
|
|
<div className="title-row">
|
|
<h4>GitLab</h4>
|
|
</div>
|
|
|
|
<p className="small">
|
|
<span>{t('loading')}…</span>
|
|
</p>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<div className="settings-widget-container">
|
|
<div>
|
|
<GitLabLogo size={40} />
|
|
</div>
|
|
|
|
<div className="description-container">
|
|
<div className="title-row">
|
|
<h4 id="gitlab-sync">GitLab</h4>
|
|
</div>
|
|
|
|
<p className="small">
|
|
{t('gitlab_sync_description', { appName })}
|
|
</p>
|
|
|
|
{isErrorConnCheck && (
|
|
<Notification
|
|
type="error"
|
|
content={t('gitlab_sync_error')}
|
|
/>
|
|
)}
|
|
|
|
{isErrorUnlink && (
|
|
<Notification
|
|
type="error"
|
|
content={t('generic_something_went_wrong')}
|
|
/>
|
|
)}
|
|
</div>
|
|
|
|
<div>
|
|
{isConnected ? (
|
|
<OLButton
|
|
variant="danger-ghost"
|
|
onClick={() => setShowUnlinkModal(true)}
|
|
disabled={isUnlinking}
|
|
>
|
|
{isUnlinking ? t('unlinking') : t('unlink')}
|
|
</OLButton>
|
|
) : isErrorConnCheck ? (
|
|
<OLButton
|
|
variant="secondary"
|
|
onClick={handleConnCheck}
|
|
>
|
|
{t('reconnect')}
|
|
</OLButton>
|
|
) : (
|
|
<OLButton
|
|
variant="secondary"
|
|
href="/user/gitlab-sync/oauth2"
|
|
>
|
|
{t('link')}
|
|
</OLButton>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
<OLModal
|
|
id="git-sync-modal"
|
|
show={showUnlinkModal}
|
|
onHide={() => setShowUnlinkModal(false)}
|
|
backdrop="static"
|
|
>
|
|
<OLModalHeader>
|
|
<OLModalTitle>
|
|
{t('unlink_provider_account_title', {
|
|
provider: 'GitLab',
|
|
})}
|
|
</OLModalTitle>
|
|
</OLModalHeader>
|
|
|
|
<OLModalBody>
|
|
<p>
|
|
{t('unlink_gitlab_warning', {
|
|
provider: 'GitLab',
|
|
})}
|
|
</p>
|
|
</OLModalBody>
|
|
|
|
<OLModalFooter>
|
|
<OLButton
|
|
variant="secondary"
|
|
onClick={() => setShowUnlinkModal(false)}
|
|
>
|
|
{t('cancel')}
|
|
</OLButton>
|
|
|
|
<OLButton
|
|
variant="danger-ghost"
|
|
onClick={handleUnlink}
|
|
disabled={isUnlinking}
|
|
>
|
|
{isUnlinking ? t('unlinking') : t('unlink')}
|
|
</OLButton>
|
|
</OLModalFooter>
|
|
</OLModal>
|
|
</>
|
|
)
|
|
}
|