Skip to content

Commit

Permalink
[8.15] [Stateful sidenav] Don't fetch active space on unauthenticated…
Browse files Browse the repository at this point in the history
… routes (#190408) (#190487)

# Backport

This will backport the following commits from `main` to `8.15`:
- [[Stateful sidenav] Don't fetch active space on unauthenticated routes
(#190408)](#190408)

<!--- Backport version: 8.9.8 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Sébastien
Loix","email":"sebastien.loix@elastic.co"},"sourceCommit":{"committedDate":"2024-08-13T16:00:08Z","message":"[Stateful
sidenav] Don't fetch active space on unauthenticated routes
(#190408)","sha":"8dee365bda57a324d18fbd9405a965f74f1c047f","branchLabelMapping":{"^v8.16.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["bug","release_note:skip","Team:SharedUX","backport:prev-minor","Feature:Chrome","v8.16.0","v8.15.1"],"number":190408,"url":"https://github.com/elastic/kibana/pull/190408","mergeCommit":{"message":"[Stateful
sidenav] Don't fetch active space on unauthenticated routes
(#190408)","sha":"8dee365bda57a324d18fbd9405a965f74f1c047f"}},"sourceBranch":"main","suggestedTargetBranches":["8.15"],"targetPullRequestStates":[{"branch":"main","label":"v8.16.0","labelRegex":"^v8.16.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/190408","number":190408,"mergeCommit":{"message":"[Stateful
sidenav] Don't fetch active space on unauthenticated routes
(#190408)","sha":"8dee365bda57a324d18fbd9405a965f74f1c047f"}},{"branch":"8.15","label":"v8.15.1","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->
  • Loading branch information
sebelga authored Aug 14, 2024
1 parent ab29fb9 commit efc63b2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
24 changes: 24 additions & 0 deletions src/plugins/navigation/public/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,30 @@ describe('Navigation Plugin', () => {
expect(coreStart.chrome.project.changeActiveSolutionNavigation).not.toHaveBeenCalled();
});

it('should not load the active space on non authenticated pages', async () => {
const { plugin, coreStart, unifiedSearch, cloud, spaces } = setup({ featureOn });

coreStart.http.anonymousPaths.isAnonymous.mockReturnValue(true);

const activeSpace$ = of({ solution: 'es' } as Pick<Space, 'solution'>);
activeSpace$.pipe = jest.fn().mockReturnValue(activeSpace$);
activeSpace$.subscribe = jest.fn().mockReturnValue(activeSpace$);
spaces.getActiveSpace$ = jest.fn().mockReturnValue(activeSpace$);

plugin.start(coreStart, { unifiedSearch, cloud, spaces });
await new Promise((resolve) => setTimeout(resolve));

expect(activeSpace$.pipe).not.toHaveBeenCalled();
expect(activeSpace$.subscribe).not.toHaveBeenCalled();

// Test that the activeSpace$ observable is accessed when not an anonymous path
coreStart.http.anonymousPaths.isAnonymous.mockReturnValue(false);
plugin.start(coreStart, { unifiedSearch, cloud, spaces });
await new Promise((resolve) => setTimeout(resolve));

expect(activeSpace$.subscribe).toHaveBeenCalled();
});

it('should return flag to indicate that the solution navigation is disabled', async () => {
const { plugin, coreStart, unifiedSearch } = setup({ featureOn });
const isEnabled = await firstValueFrom(
Expand Down
32 changes: 24 additions & 8 deletions src/plugins/navigation/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,15 @@ import {
take,
combineLatest,
map,
switchMap,
} from 'rxjs';
import { PluginInitializerContext, CoreSetup, CoreStart, Plugin } from '@kbn/core/public';
import {
PluginInitializerContext,
CoreSetup,
CoreStart,
Plugin,
HttpStart,
} from '@kbn/core/public';
import type { UnifiedSearchPublicPluginStart } from '@kbn/unified-search-plugin/public';
import type { Space } from '@kbn/spaces-plugin/public';
import type { SolutionNavigationDefinition } from '@kbn/core-chrome-browser';
Expand Down Expand Up @@ -70,7 +77,9 @@ export class NavigationPublicPlugin
const { unifiedSearch, cloud, cloudExperiments, spaces } = depsStart;
const extensions = this.topNavMenuExtensionsRegistry.getAll();
const chrome = core.chrome as InternalChromeStart;
const activeSpace$ = spaces?.getActiveSpace$() ?? of(undefined);
const activeSpace$ = this.getIsUnauthenticated(core.http)
? of(undefined)
: spaces?.getActiveSpace$() ?? of(undefined);

/*
*
Expand Down Expand Up @@ -128,12 +137,14 @@ export class NavigationPublicPlugin
this.addSolutionNavigation(solutionNavigation);
});
},
isSolutionNavEnabled$: combineLatest([
this.isSolutionNavExperiementEnabled$,
activeSpace$,
]).pipe(
map(([isFeatureEnabled, activeSpace]) => {
return getIsProjectNav(isFeatureEnabled, activeSpace?.solution) && !isServerless;
isSolutionNavEnabled$: of(this.getIsUnauthenticated(core.http)).pipe(
switchMap((isUnauthenticated) => {
if (isUnauthenticated) return of(false);
return combineLatest([this.isSolutionNavExperiementEnabled$, activeSpace$]).pipe(
map(([isFeatureEnabled, activeSpace]) => {
return getIsProjectNav(isFeatureEnabled, activeSpace?.solution) && !isServerless;
})
);
})
),
};
Expand Down Expand Up @@ -196,6 +207,11 @@ export class NavigationPublicPlugin
chrome.project.changeActiveSolutionNavigation(solutionView!);
}
}

private getIsUnauthenticated(http: HttpStart) {
const { anonymousPaths } = http;
return anonymousPaths.isAnonymous(window.location.pathname);
}
}

function getIsProjectNav(isFeatureEnabled: boolean, solutionView?: string) {
Expand Down

0 comments on commit efc63b2

Please sign in to comment.