import { useState, useCallback, useEffect } from 'react' import { Trans, useTranslation } from 'react-i18next' import { debugConsole } from '@/utils/debugging' import { postJSON, getJSON } from '@/infrastructure/fetch-json' import useAsync from '@/shared/hooks/use-async' import Notification from '@/shared/components/notification' import OLButton from '@/shared/components/ol/ol-button' import GitLogoOrange from '@/shared/svgs/git-logo-orange' import TokenTable from './token-table' import ExposeTokenModal from './modals/expose-token-modal' import { Token } from '../../../../types/api' export default function GitIntegrationWidget() { const { t } = useTranslation() const [tokens, setTokens] = useState([]) const [showExposeTokenModal, setShowExposeTokenModal] = useState(false) const [secretToken, setSecretToken] = useState(null) const { runAsync, isLoading, isError, reset } = useAsync() useEffect(() => { runAsync(getJSON('/git-bridge/personal-access-tokens')) .then((data: Token[]) => setTokens(data)) .catch(debugConsole.error) }, [runAsync]) const handleCreateToken = useCallback(() => { runAsync(postJSON('/git-bridge/personal-access-tokens')) .then((data: Token & { accessToken: string }) => { const { accessToken, ...newToken } = data setTokens(prev => [...prev, newToken]) setSecretToken(accessToken) setShowExposeTokenModal(true) }) .catch((err) => { debugConsole.error(err) setTimeout(() => reset(), 5000) }) }, [runAsync]) const handleDeleteToken = useCallback((id: string) => { setTokens(prev => prev.filter(t => t._id !== id)) }, []) const tokenCount = tokens.length return (

{t('git_integration')}

, ]} />

{t('your_git_access_tokens')}

{t('your_git_access_info')}

    {tokenCount > 0 ? ( <>
  • {t('your_git_access_info_bullet_1')}
  • {t('your_git_access_info_bullet_2')}
  • ) : ( <>
  • ]} />
  • {t('your_git_access_info_bullet_4')}
  • {t('your_git_access_info_bullet_5')}
  • )}
{isError && (
)}
{tokenCount === 0 && (
{t('generate_token')}
)} {showExposeTokenModal && secretToken && ( { setShowExposeTokenModal(false) setSecretToken(null) }} /> )}
) }