*/
import * as React from 'react';
import { FormattedMessage } from 'react-intl';
-import { useNavigate, useSearchParams } from 'react-router-dom';
+import { useSearchParams } from 'react-router-dom';
import { generateToken, getTokens } from '../../api/user-tokens';
import Link from '../../components/common/Link';
import { Button } from '../../components/controls/buttons';
enum Status {
request,
tokenError,
- connectionError,
- success
+ tokenCreated,
+ tokenSent
}
interface Props {
}
export function SonarLintConnection({ currentUser }: Props) {
- const navigate = useNavigate();
const [searchParams] = useSearchParams();
const [status, setStatus] = React.useState(Status.request);
const [newToken, setNewToken] = React.useState<NewUserToken | undefined>(undefined);
const port = parseInt(searchParams.get('port') ?? '0', 10);
const ideName = searchParams.get('ideName') ?? translate('sonarlint-connection.unspecified-ide');
- // If the port is not in the expected range, redirect to home page
- React.useEffect(() => {
- if (!portIsValid(port)) {
- navigate('/');
- }
- }, [navigate, port]);
-
const { login } = currentUser;
const authorize = React.useCallback(async () => {
setNewToken(token);
+ if (!portIsValid(port)) {
+ setStatus(Status.tokenCreated);
+ return;
+ }
+
try {
await sendUserToken(port, token);
- setStatus(Status.success);
+ setStatus(Status.tokenSent);
} catch (_) {
- setStatus(Status.connectionError);
+ setStatus(Status.tokenCreated);
}
}, [port, ideName, login]);
</>
)}
- {status === Status.connectionError && newToken && (
+ {status === Status.tokenCreated && newToken && (
<>
<img
alt=""
</>
)}
- {status === Status.success && newToken && (
+ {status === Status.tokenSent && newToken && (
<>
<h1 className="big-spacer-top big-spacer-bottom">
{translate('sonarlint-connection.success.title')}
expect(handleRequiredAuthentication).toHaveBeenCalled();
});
-it('should redirect if port is not provided', () => {
+it('should let the user copy the token if the port is not valid', async () => {
+ const user = userEvent.setup();
+
renderSonarLintConnection({ port: '' });
+ await user.click(
+ await screen.findByRole('button', { name: 'sonarlint-connection.request.action' })
+ );
+
expect(
- screen.queryByRole('heading', { name: 'sonarlint-connection.title' })
- ).not.toBeInTheDocument();
+ await screen.findByText('sonarlint-connection.connection-error.description')
+ ).toBeInTheDocument();
});
function renderSonarLintConnection(overrides: { currentUser?: CurrentUser; port?: string } = {}) {