Skip to content

Commit

Permalink
[Lens] Fix infinite loop when loading rejected data view (#113375)
Browse files Browse the repository at this point in the history
* [Lens] Fix infinite loop when loading rejected index pattern

* Update x-pack/plugins/lens/public/app_plugin/app.test.tsx
  • Loading branch information
mbondyra authored Sep 29, 2021
1 parent c8223bf commit 23e55b3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
13 changes: 12 additions & 1 deletion x-pack/plugins/lens/public/app_plugin/app.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 19 additions & 4 deletions x-pack/plugins/lens/public/app_plugin/lens_top_nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ export const LensTopNavMenu = ({
);

const [indexPatterns, setIndexPatterns] = useState<IndexPattern[]>([]);
const [rejectedIndexPatterns, setRejectedIndexPatterns] = useState<string[]>([]);

const {
isSaveable,
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 23e55b3

Please sign in to comment.