[Lens] Fix infinite loop when loading rejected data view (#113375)

* [Lens] Fix infinite loop when loading rejected index pattern

* Update x-pack/plugins/lens/public/app_plugin/app.test.tsx
This commit is contained in:
Marta Bondyra 2021-09-29 17:05:45 +02:00 committed by GitHub
parent c8223bf4f6
commit 23e55b3270
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 5 deletions

View file

@ -336,7 +336,18 @@ describe('Lens App', () => {
{}
);
});
it('handles rejected index pattern', async () => {
const customServices = makeDefaultServices(sessionIdSubject);
customServices.data.indexPatterns.get = jest
.fn()
.mockImplementation((id) => Promise.reject({ reason: 'Could not locate that data view' }));
const customProps = makeDefaultProps();
const { services } = await mountWith({ props: customProps, services: customServices });
expect(services.navigation.ui.TopNavMenu).toHaveBeenCalledWith(
expect.objectContaining({ indexPatterns: [] }),
{}
);
});
describe('save buttons', () => {
interface SaveProps {
newCopyOnSave: boolean;

View file

@ -169,6 +169,7 @@ export const LensTopNavMenu = ({
);
const [indexPatterns, setIndexPatterns] = useState<IndexPattern[]>([]);
const [rejectedIndexPatterns, setRejectedIndexPatterns] = useState<string[]>([]);
const {
isSaveable,
@ -200,17 +201,31 @@ export const LensTopNavMenu = ({
datasourceStates,
});
const hasIndexPatternsChanged =
indexPatterns.length !== indexPatternIds.length ||
indexPatternIds.some((id) => !indexPatterns.find((indexPattern) => indexPattern.id === id));
indexPatterns.length + rejectedIndexPatterns.length !== indexPatternIds.length ||
indexPatternIds.some(
(id) =>
![...indexPatterns.map((ip) => ip.id), ...rejectedIndexPatterns].find(
(loadedId) => loadedId === id
)
);
// Update the cached index patterns if the user made a change to any of them
if (hasIndexPatternsChanged) {
getIndexPatternsObjects(indexPatternIds, data.indexPatterns).then(
({ indexPatterns: indexPatternObjects }) => {
({ indexPatterns: indexPatternObjects, rejectedIds }) => {
setIndexPatterns(indexPatternObjects);
setRejectedIndexPatterns(rejectedIds);
}
);
}
}, [datasourceStates, activeDatasourceId, data.indexPatterns, datasourceMap, indexPatterns]);
}, [
datasourceStates,
activeDatasourceId,
rejectedIndexPatterns,
datasourceMap,
indexPatterns,
data.indexPatterns,
]);
const { TopNavMenu } = navigation.ui;
const { from, to } = data.query.timefilter.timefilter.getTime();