import { Button, ButtonVariety, DropdownMenu } from '@sonarsource/echoes-react';
import { addGlobalErrorMessage } from 'design-system/lib';
import React, { useCallback, useState } from 'react';
+import { useComponent } from '../../../app/components/componentContext/withComponentContext';
import { useCurrentUser } from '../../../app/components/current-user/CurrentUserContext';
import { translate } from '../../../helpers/l10n';
import { probeSonarLintServers } from '../../../helpers/sonarlint';
-import { useBranchesQuery } from '../../../queries/branch';
+import { useCurrentBranchQuery } from '../../../queries/branch';
import { useComponentForSourceViewer } from '../../../queries/component';
import { CodeSuggestion } from '../../../queries/fix-suggestions';
import { useOpenFixOrIssueInIdeMutation } from '../../../queries/sonarlint';
export function OpenFixInIde({ aiSuggestion, issue }: Readonly<Props>) {
const [ides, setIdes] = useState<Ide[]>([]);
- const { data, isLoading: isBranchLoading } = useBranchesQuery();
+ const { component } = useComponent();
+ const { data: branchLike, isLoading: isBranchLoading } = useCurrentBranchQuery(component);
const {
currentUser: { isLoggedIn },
const { data: sourceViewerFile } = useComponentForSourceViewer(
issue.component,
- data?.branchLike,
+ branchLike,
!isBranchLoading,
);
const { mutateAsync: openFixInIde, isPending } = useOpenFixOrIssueInIdeMutation();
};
await openFixInIde({
- branchLike: data?.branchLike,
+ branchLike,
ide,
fix,
issue,
ide.needsToken ? DELAY_AFTER_TOKEN_CREATION : 0,
);
},
- [aiSuggestion, issue, sourceViewerFile, data, openFixInIde],
+ [aiSuggestion, issue, sourceViewerFile, branchLike, openFixInIde],
);
const onClick = async () => {
}
};
- if (!isLoggedIn || data?.branchLike === undefined || sourceViewerFile === undefined) {
+ if (!isLoggedIn || branchLike === undefined || sourceViewerFile === undefined) {
return null;
}
import userEvent from '@testing-library/user-event';
import React, { ComponentProps } from 'react';
import BranchesServiceMock from '../../../../api/mocks/BranchesServiceMock';
+import { ComponentContext } from '../../../../app/components/componentContext/ComponentContext';
+import { mockComponent } from '../../../../helpers/mocks/component';
import { openFixOrIssueInSonarLint, probeSonarLintServers } from '../../../../helpers/sonarlint';
import { mockIssue, mockLoggedInUser } from '../../../../helpers/testMocks';
import { renderComponent } from '../../../../helpers/testReactTestingUtils';
import { CodeSuggestion, LineTypeEnum } from '../../../../queries/fix-suggestions';
+import { ComponentContextShape } from '../../../../types/component';
import { Fix, Ide } from '../../../../types/sonarlint';
import { OpenFixInIde } from '../OpenFixInIde';
await user.click(secondIde);
expect(openFixOrIssueInSonarLint).toHaveBeenCalledWith({
- branchLike: {},
+ branchLike: expect.objectContaining({ isMain: true, name: 'main' }),
calledPort: MOCK_IDES_OPEN_FIX[1].port,
fix: FIX_DATA,
issueKey: MOCK_ISSUE_KEY,
projectKey: MOCK_PROJECT_KEY,
});
+ const componentContext: ComponentContextShape = {
+ fetchComponent: jest.fn(),
+ onComponentChange: jest.fn(),
+ component: mockComponent(),
+ };
+
function Wrapper() {
const queryClient = useQueryClient();
queryClient.setQueryData(['branches', 'mycomponent', 'details'], { branchLike: {} });
- return <OpenFixInIde aiSuggestion={AI_SUGGESTION} issue={mockedIssue} {...props} />;
+
+ return (
+ <ComponentContext.Provider value={componentContext}>
+ <OpenFixInIde aiSuggestion={AI_SUGGESTION} issue={mockedIssue} {...props} />
+ </ComponentContext.Provider>
+ );
}
return renderComponent(<Wrapper />, '/?id=mycomponent', { currentUser: mockLoggedInUser() });