Skip to content

Commit

Permalink
Add: Add a new react hook for getting a URL to the manual
Browse files Browse the repository at this point in the history
Introduce a new hook that returns a link to the manual depending on the
GSA settings and users locale.
  • Loading branch information
bjoernricks committed Jan 16, 2025
1 parent f2a608f commit 362540d
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/web/hooks/__tests__/useManualURL.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* SPDX-FileCopyrightText: 2025 Greenbone AG
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import {describe, test, expect} from '@gsa/testing';
import {setLocale} from 'web/store/usersettings/actions';
import {rendererWith} from 'web/utils/testing';

import useManualURL from '../useManualURL';

const gmp = {settings: {manualUrl: 'http://localhost/manual'}};

describe('useManualURL', () => {
test('should return the manual URL for the default language', () => {
const {renderHook} = rendererWith({
store: true,
gmp,
});
const {result} = renderHook(() => useManualURL());

expect(result.current).toBe('http://localhost/manual/en');
});

test('should return the manual URL for the German language', () => {
const {renderHook} = rendererWith({
store: true,
gmp,
});
const {result} = renderHook(() => useManualURL('de'));

expect(result.current).toBe('http://localhost/manual/de');
});

test('should return the manual URL for the users language', () => {
const {renderHook, store} = rendererWith({
store: true,
gmp,
});
store.dispatch(setLocale('de'));
const {result} = renderHook(() => useManualURL());

expect(result.current).toBe('http://localhost/manual/de');
});

test('should return the en manual URL for unknown language', () => {
const {renderHook} = rendererWith({
store: true,
gmp,
});
const {result} = renderHook(() => useManualURL('foo'));

expect(result.current).toBe('http://localhost/manual/en');
});

test('should return the en manual URL considering the lanuage mapping', () => {
const {renderHook, store} = rendererWith({
store: true,
gmp: {
settings: {
...gmp.settings,
manualLanguageMapping: {
fr: 'foo',
},
},
},
});
store.dispatch(setLocale('fr'));
const {result} = renderHook(() => useManualURL());

expect(result.current).toBe('http://localhost/manual/foo');
});
});
42 changes: 42 additions & 0 deletions src/web/hooks/useManualURL.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* SPDX-FileCopyrightText: 2025 Greenbone AG
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import {isDefined} from 'gmp/utils/identity';

import useGmp from './useGmp';
import useLocale from './useLocale';

const DEFAULT_LANGUAGE_MAPPING = {
de: 'de',
};

const DEFAULT_LANGUAGE_PATH = 'en';

const getLanguagePath = (locale, languageMapping) => {
if (!isDefined(locale)) {
return DEFAULT_LANGUAGE_PATH;
}

const code = locale.slice(0, 2);
const path = languageMapping[code];

return isDefined(path) ? path : DEFAULT_LANGUAGE_PATH;
};

const useManualURL = locale => {
const [userLocale] = useLocale();
const gmp = useGmp();
const {manualUrl, manualLanguageMapping = DEFAULT_LANGUAGE_MAPPING} =
gmp.settings;

let url = manualUrl;
if (!url.endsWith('/')) {
url += '/';
}
url += getLanguagePath(locale || userLocale, manualLanguageMapping);
return url;
};

export default useManualURL;

0 comments on commit 362540d

Please sign in to comment.