-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add: Add a new react hook for getting a URL to the manual
Introduce a new hook that returns a link to the manual depending on the GSA settings and users locale.
- Loading branch information
1 parent
f2a608f
commit 362540d
Showing
2 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |