Skip to content

Commit

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

(cherry picked from commit 8dee365)

# Conflicts:
#	src/plugins/navigation/public/plugin.test.ts
#	src/plugins/navigation/public/plugin.tsx
#	src/plugins/navigation/server/ui_settings.test.ts
#	src/plugins/navigation/server/ui_settings.ts
  • Loading branch information
sebelga committed Aug 14, 2024
1 parent ff58b6a commit 6c9a3f7
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 6c9a3f7

Please sign in to comment.