-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecorator-utils.ts
107 lines (94 loc) · 3.38 KB
/
decorator-utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { DecoratorParams } from '@navikt/nav-dekoratoren-moduler';
import { Language } from 'translations';
import { ContentProps, ContentType } from 'types/content-props/_content-common';
import { LanguageProps } from 'types/language';
import { Audience, getAudience } from 'types/component-props/_mixins';
import { stripXpPathPrefix } from './urls';
import { getContentLanguages } from './languages';
import { hasWhiteHeader } from './appearance';
const defaultLanguage: DecoratorParams['language'] = 'nb';
const xpLangToDecoratorLang: {
[key in Language]: DecoratorParams['language'];
} = {
en: 'en',
no: 'nb',
nn: 'nn',
pl: 'pl',
se: 'se',
uk: 'uk',
ru: 'ru',
};
const getDecoratorLangFromXpLang = (xpLang: Language) =>
xpLangToDecoratorLang[xpLang] || defaultLanguage;
const getDecoratorLanguagesParam = (
languages: LanguageProps[],
currentLang: Language,
currentPath: string
): DecoratorParams['availableLanguages'] =>
languages.length > 0
? languages
.map((lang) => ({
handleInApp: true,
locale: getDecoratorLangFromXpLang(lang.language),
url: stripXpPathPrefix(lang._path),
}))
.concat([
{
handleInApp: true,
locale: getDecoratorLangFromXpLang(currentLang),
url: stripXpPathPrefix(currentPath),
},
])
: [];
const pathToRoleContext: { [key: string]: DecoratorParams['context'] } = {
person: 'privatperson',
bedrift: 'arbeidsgiver',
samarbeidspartner: 'samarbeidspartner',
};
const audienceToRoleContext: {
[key in Audience]: DecoratorParams['context'];
} = {
[Audience.PERSON]: 'privatperson',
[Audience.EMPLOYER]: 'arbeidsgiver',
[Audience.PROVIDER]: 'samarbeidspartner',
};
const errorParams = (content: ContentProps): DecoratorParams => ({
feedback: false,
breadcrumbs: content?.breadcrumbs || [],
});
const defaultParams = {
feedback: false,
language: 'nb',
maskHotjar: false,
};
export const getDecoratorParams = (content: ContentProps): DecoratorParams => {
if (!content || content.type === ContentType.Error) {
return errorParams(content);
}
const { _path, breadcrumbs, language, data, editorView } = content;
const audience = data?.audience ? getAudience(data.audience) : undefined;
const rolePathSegment = _path.split('/')[2];
const context = audience ? audienceToRoleContext[audience] : pathToRoleContext[rolePathSegment];
const decoratorLanguage = getDecoratorLangFromXpLang(language);
const feedbackEnabled = data?.feedbackToggle;
const chatbotDisabled =
data?.chatbotToggle === false || editorView === 'edit' || editorView === 'inline';
return {
...defaultParams,
...(context && { context }),
...(decoratorLanguage && { language: decoratorLanguage }),
breadcrumbs:
breadcrumbs?.map((crumb) => ({
handleInApp: true,
...crumb,
})) || [],
availableLanguages: getDecoratorLanguagesParam(
getContentLanguages(content),
language,
_path
),
...(feedbackEnabled && { feedback: true }),
chatbot: !chatbotDisabled,
utilsBackground: hasWhiteHeader(content) ? 'white' : 'gray',
};
};