Skip to content

Commit

Permalink
NAS-130719 / 25.04 / UI Global Search passes "undefined" url path to …
Browse files Browse the repository at this point in the history
…Docs Hub (#10533)

* NAS-130719: UI Global Search passes "undefined" url path to Docs Hub

* NAS-130719: PR Update

* NAS-130719: PR update

* NAS-130719: PR Update
  • Loading branch information
AlexKarpov98 authored Aug 26, 2024
1 parent c578962 commit 974607f
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from 'rxjs';
import { DialogService } from 'app/modules/dialog/dialog.service';
import { searchDelayConst } from 'app/modules/global-search/constants/delay.const';
import { extractVersion } from 'app/modules/global-search/helpers/extract-version';
import { moveToNextFocusableElement, moveToPreviousFocusableElement } from 'app/modules/global-search/helpers/focus-helper';
import { UiSearchableElement } from 'app/modules/global-search/interfaces/ui-searchable-element.interface';
import { GlobalSearchSectionsProvider } from 'app/modules/global-search/services/global-search-sections.service';
Expand Down Expand Up @@ -151,7 +152,7 @@ export class GlobalSearchComponent implements OnInit, AfterViewInit, OnDestroy {
...searchResults,
...this.globalSearchSectionsProvider.getHelpSectionResults(
this.searchControl.value,
this.extractVersion(this.systemVersion),
extractVersion(this.systemVersion),
),
];
this.isLoading = false;
Expand All @@ -173,10 +174,6 @@ export class GlobalSearchComponent implements OnInit, AfterViewInit, OnDestroy {
});
}

private extractVersion(version: string): string {
return version.match(/(\d+\.\d+)\.\d+-/)?.[1];
}

private listenForSelectionChanges(): void {
this.searchProvider.selectionChanged$.pipe(
tap((config) => {
Expand Down
43 changes: 43 additions & 0 deletions src/app/modules/global-search/helpers/extract-version.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { extractVersion } from './extract-version';

describe('extractVersion', () => {
it('should extract the version "24.10" from "TrueNAS-SCALE-24.10.0-MASTER-20240324-065034"', () => {
const result = extractVersion('TrueNAS-SCALE-24.10.0-MASTER-20240324-065034');
expect(result).toBe('24.10');
});

it('should extract the version "24.10" from "24.10-BETA.1-INTERNAL.7"', () => {
const result = extractVersion('24.10-BETA.1-INTERNAL.7');
expect(result).toBe('24.10');
});

it('should return undefined for a string with no matching version pattern', () => {
const result = extractVersion('NoVersionHere');
expect(result).toBeUndefined();
});

it('should extract the version "1.2" from "1.2.3-alpha"', () => {
const result = extractVersion('1.2.3-alpha');
expect(result).toBe('1.2');
});

it('should extract the version "10.20" from "v10.20.30-beta-release"', () => {
const result = extractVersion('v10.20.30-beta-release');
expect(result).toBe('10.20');
});

it('should extract the version "0.1" from "v0.1.0-RC1"', () => {
const result = extractVersion('v0.1.0-RC1');
expect(result).toBe('0.1');
});

it('should return undefined for a string "Version-1-2-3"', () => {
const result = extractVersion('Version-1-2-3');
expect(result).toBeUndefined();
});

it('should extract the version "2.5" from "2.5-rc.1-build.2023"', () => {
const result = extractVersion('2.5-rc.1-build.2023');
expect(result).toBe('2.5');
});
});
3 changes: 3 additions & 0 deletions src/app/modules/global-search/helpers/extract-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function extractVersion(version: string): string | undefined {
return version.match(/(\d+\.\d+)(?:\.\d+)?/)?.[1];
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,24 @@ describe('GlobalSearchSectionsProvider', () => {
}]);
});

it('should generate help section results based on search term and missing app version', () => {
const searchTerm = 'test';
const results = spectator.service.getHelpSectionResults(searchTerm, undefined);

expect(results).toEqual([{
hierarchy: ['Search Documentation for «{value}»'],
targetHref: 'https://www.truenas.com/docs/search/?query=test',
section: GlobalSearchSection.Help,
}]);
});

it('should generate help section results based on special case search term', () => {
const searchTerm = 'help';
const appVersion = '24.10';
const results = spectator.service.getHelpSectionResults(searchTerm, appVersion);
const results = spectator.service.getHelpSectionResults(searchTerm);

expect(results).toEqual([{
hierarchy: ['Go to Documentation'],
targetHref: 'https://www.truenas.com/docs/search/',
targetHref: 'https://www.truenas.com/docs/search',
section: GlobalSearchSection.Help,
}]);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,19 @@ export class GlobalSearchSectionsProvider {
return this.searchProvider.search(searchTerm, this.globalSearchMaximumLimit);
}

getHelpSectionResults(searchTerm: string, appVersion: string): UiSearchableElement[] {
getHelpSectionResults(searchTerm: string, appVersion?: string): UiSearchableElement[] {
const documentationKeywords = new Set(['help', 'documentation', 'docs', 'guide', 'support']);
const normalizedSearchTerm = searchTerm.toLowerCase();
const isDocumentationKeyword = documentationKeywords.has(normalizedSearchTerm);
const targetHref = appVersion
? `https://www.truenas.com/docs/scale/${appVersion}/search`
: 'https://www.truenas.com/docs/search';

if (isDocumentationKeyword) {
return [
{
hierarchy: [this.translate.instant('Go to Documentation')],
targetHref: 'https://www.truenas.com/docs/search/',
targetHref,
section: GlobalSearchSection.Help,
},
];
Expand All @@ -49,7 +52,7 @@ export class GlobalSearchSectionsProvider {
return [
{
hierarchy: [this.translate.instant('Search Documentation for «{value}»', { value: searchTerm })],
targetHref: `https://www.truenas.com/docs/scale/${appVersion}/search/?query=${searchTerm}`,
targetHref: `${targetHref}/?query=${searchTerm}`,
section: GlobalSearchSection.Help,
},
];
Expand Down

0 comments on commit 974607f

Please sign in to comment.