-
Notifications
You must be signed in to change notification settings - Fork 353
Expand file tree
/
Copy pathribbon.component.tsx
More file actions
185 lines (174 loc) · 5.84 KB
/
ribbon.component.tsx
File metadata and controls
185 lines (174 loc) · 5.84 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
"use client";
import React, { useCallback, useEffect, useMemo, useState } from "react";
import { LayoutDictionaryModel } from "@/features/localization/models/layout-dictionary.model";
import styles from "./ribbon.module.scss";
import { BoxComponent } from "@/features/common/components/box/box.component";
import { savePreferredLanguage } from "@/features/localization/services/ui-language.utils";
import Link from "next/link";
import {
getSanitizedThemePickerCodeValue,
isLightThemePreference,
isSystemThemePreference,
} from "@/features/themes/services/theme.utils";
import { RibbonPickerComponent } from "@/features/common/components/bars/ribbon/ribbon-picker/ribbon-picker.component";
import { LightIconComponent } from "@/features/common/components/bars/ribbon/assets/light-icon.component";
import { DarkIconComponent } from "@/features/common/components/bars/ribbon/assets/dark-icon.component";
import { ThemeModel } from "@/features/common/models/theme.model";
import { useAppStore } from "@/features/common/services/app.store";
import { SystemIconComponent } from "@/features/common/components/bars/ribbon/assets/system-icon.component";
import {
ThemeCookieValues,
ThemePickerCodeValues,
} from "@/features/common/values/theme.values";
import { savePreferredThemeInCookie } from "@/features/themes/services/theme.client.utils";
interface RibbonComponentProps {
themeCode: ThemeCookieValues;
languageCode: string;
dictionary: LayoutDictionaryModel["ribbon"];
}
export const RibbonComponent: React.FC<RibbonComponentProps> = ({
themeCode,
languageCode,
dictionary,
}) => {
const theme$ = useAppStore((state) => state.theme$);
const sanitizedThemePickerCodeValue = useMemo(() => {
return getSanitizedThemePickerCodeValue(themeCode);
}, [themeCode]);
const [currentTheme, setCurrentTheme] = useState<ThemeModel>(
dictionary.themePicker.options.filter((element) =>
isSystemThemePreference(themeCode)
? isSystemThemePreference(element.code)
: element.code === sanitizedThemePickerCodeValue,
)[0],
);
const themeOptions = useMemo(
() =>
dictionary.themePicker.options.map((option) => {
return {
code: option.code,
full: {
...option,
label: option.label,
icon: isSystemThemePreference(option.code) ? (
<SystemIconComponent />
) : isLightThemePreference(option.code) ? (
<LightIconComponent />
) : (
<DarkIconComponent />
),
},
compact: {
...option,
label: null,
icon: isSystemThemePreference(option.code) ? (
<SystemIconComponent />
) : isLightThemePreference(option.code) ? (
<LightIconComponent />
) : (
<DarkIconComponent />
),
},
};
}),
[dictionary.themePicker.options],
);
const handleThemeSelection = useCallback(
async (value: ThemePickerCodeValues) => {
const themePreference = await savePreferredThemeInCookie(
value,
languageCode,
);
if (themePreference) {
setCurrentTheme(themePreference);
}
},
[languageCode],
);
const handleLanguageSelection = useCallback(async (value: string) => {
await savePreferredLanguage(value);
}, []);
useEffect(() => {
if (theme$) {
setCurrentTheme(
dictionary.themePicker.options.filter((element) =>
isSystemThemePreference(themeCode)
? isSystemThemePreference(element.code)
: element.code === sanitizedThemePickerCodeValue,
)[0],
);
}
}, [
dictionary.themePicker.options,
sanitizedThemePickerCodeValue,
theme$,
themeCode,
]);
return (
<>
<BoxComponent
contentAs="section"
containerClassName={styles.container}
wrapperClassName={styles.wrapper}
contentClassName={styles.content}
aria-label="Page options"
>
<div className={styles.cta}>
<span className={styles.cta__title}>{dictionary.cta.title}</span>
<Link
target="_blank"
rel="noopener noreferrer"
href={dictionary.cta.link.url}
className={styles.cta__description}
>
{dictionary.cta.description}
<div className={styles.cta__arrow}>
<svg
aria-hidden={true}
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 16 16"
fill="none"
>
<path
d="M3.10449 12.9991L12.8917 3.21191"
stroke="#FBFBFB"
strokeWidth="1.5"
strokeLinecap="round"
/>
<path
d="M3 3H13V13"
stroke="#FBFBFB"
strokeWidth="1.5"
strokeLinecap="round"
/>
</svg>
</div>
</Link>
</div>
<div className={styles.actions}>
<RibbonPickerComponent<ThemePickerCodeValues>
label={null}
compactLabel={null}
icon={
isSystemThemePreference(currentTheme.code) ? (
<SystemIconComponent />
) : isLightThemePreference(currentTheme.code) ? (
<LightIconComponent />
) : (
<DarkIconComponent />
)
}
languageCode={languageCode}
selectedOptionCode={currentTheme.code}
handleSelection={handleThemeSelection}
options={themeOptions}
aria={{
buttonLabel: dictionary.themePicker.button.ariaLabel,
listLabel: dictionary.themePicker.list.ariaLabel,
}}
/>
</div>
</BoxComponent>
</>
);
};