|
1 | 1 | "use client"; |
2 | 2 |
|
3 | | -import React, { useCallback, useEffect, useMemo, useState } from "react"; |
| 3 | +import React from "react"; |
4 | 4 | import { LayoutDictionaryModel } from "@/features/localization/models/layout-dictionary.model"; |
5 | 5 | import styles from "./ribbon.module.scss"; |
6 | 6 | import { BoxComponent } from "@/features/common/components/box/box.component"; |
7 | | -import { savePreferredLanguage } from "@/features/localization/services/ui-language.utils"; |
8 | 7 | import Link from "next/link"; |
9 | | -import { |
10 | | - getSanitizedThemePickerCodeValue, |
11 | | - isLightThemePreference, |
12 | | - isSystemThemePreference, |
13 | | -} from "@/features/themes/services/theme.utils"; |
14 | | -import { RibbonPickerComponent } from "@/features/common/components/bars/ribbon/ribbon-picker/ribbon-picker.component"; |
15 | | -import { LightIconComponent } from "@/features/common/components/bars/ribbon/assets/light-icon.component"; |
16 | | -import { DarkIconComponent } from "@/features/common/components/bars/ribbon/assets/dark-icon.component"; |
17 | | -import { ThemeModel } from "@/features/common/models/theme.model"; |
18 | | -import { useAppStore } from "@/features/common/services/app.store"; |
19 | | -import { SystemIconComponent } from "@/features/common/components/bars/ribbon/assets/system-icon.component"; |
20 | | -import { |
21 | | - ThemeCookieValues, |
22 | | - ThemePickerCodeValues, |
23 | | -} from "@/features/common/values/theme.values"; |
24 | | -import { savePreferredThemeInCookie } from "@/features/themes/services/theme.client.utils"; |
25 | 8 |
|
26 | 9 | interface RibbonComponentProps { |
27 | | - themeCode: ThemeCookieValues; |
28 | | - languageCode: string; |
29 | 10 | dictionary: LayoutDictionaryModel["ribbon"]; |
30 | 11 | } |
31 | 12 |
|
32 | 13 | export const RibbonComponent: React.FC<RibbonComponentProps> = ({ |
33 | | - themeCode, |
34 | | - languageCode, |
35 | 14 | dictionary, |
36 | 15 | }) => { |
37 | | - const theme$ = useAppStore((state) => state.theme$); |
38 | | - |
39 | | - const sanitizedThemePickerCodeValue = useMemo(() => { |
40 | | - return getSanitizedThemePickerCodeValue(themeCode); |
41 | | - }, [themeCode]); |
42 | | - |
43 | | - const [currentTheme, setCurrentTheme] = useState<ThemeModel>( |
44 | | - dictionary.themePicker.options.filter((element) => |
45 | | - isSystemThemePreference(themeCode) |
46 | | - ? isSystemThemePreference(element.code) |
47 | | - : element.code === sanitizedThemePickerCodeValue, |
48 | | - )[0], |
49 | | - ); |
50 | | - |
51 | | - const themeOptions = useMemo( |
52 | | - () => |
53 | | - dictionary.themePicker.options.map((option) => { |
54 | | - return { |
55 | | - code: option.code, |
56 | | - full: { |
57 | | - ...option, |
58 | | - label: option.label, |
59 | | - icon: isSystemThemePreference(option.code) ? ( |
60 | | - <SystemIconComponent /> |
61 | | - ) : isLightThemePreference(option.code) ? ( |
62 | | - <LightIconComponent /> |
63 | | - ) : ( |
64 | | - <DarkIconComponent /> |
65 | | - ), |
66 | | - }, |
67 | | - compact: { |
68 | | - ...option, |
69 | | - label: null, |
70 | | - icon: isSystemThemePreference(option.code) ? ( |
71 | | - <SystemIconComponent /> |
72 | | - ) : isLightThemePreference(option.code) ? ( |
73 | | - <LightIconComponent /> |
74 | | - ) : ( |
75 | | - <DarkIconComponent /> |
76 | | - ), |
77 | | - }, |
78 | | - }; |
79 | | - }), |
80 | | - [dictionary.themePicker.options], |
81 | | - ); |
82 | | - |
83 | | - const handleThemeSelection = useCallback( |
84 | | - async (value: ThemePickerCodeValues) => { |
85 | | - const themePreference = await savePreferredThemeInCookie( |
86 | | - value, |
87 | | - languageCode, |
88 | | - ); |
89 | | - |
90 | | - if (themePreference) { |
91 | | - setCurrentTheme(themePreference); |
92 | | - } |
93 | | - }, |
94 | | - [languageCode], |
95 | | - ); |
96 | | - |
97 | | - const handleLanguageSelection = useCallback(async (value: string) => { |
98 | | - await savePreferredLanguage(value); |
99 | | - }, []); |
100 | | - |
101 | | - useEffect(() => { |
102 | | - if (theme$) { |
103 | | - setCurrentTheme( |
104 | | - dictionary.themePicker.options.filter((element) => |
105 | | - isSystemThemePreference(themeCode) |
106 | | - ? isSystemThemePreference(element.code) |
107 | | - : element.code === sanitizedThemePickerCodeValue, |
108 | | - )[0], |
109 | | - ); |
110 | | - } |
111 | | - }, [ |
112 | | - dictionary.themePicker.options, |
113 | | - sanitizedThemePickerCodeValue, |
114 | | - theme$, |
115 | | - themeCode, |
116 | | - ]); |
117 | 16 |
|
118 | 17 | return ( |
119 | | - <> |
120 | | - <BoxComponent |
121 | | - contentAs="section" |
122 | | - containerClassName={styles.container} |
123 | | - wrapperClassName={styles.wrapper} |
124 | | - contentClassName={styles.content} |
125 | | - aria-label="Page options" |
126 | | - > |
| 18 | + |
127 | 19 | <div className={styles.cta}> |
128 | 20 | <span className={styles.cta__title}>{dictionary.cta.title}</span> |
129 | 21 | <Link |
@@ -156,30 +48,5 @@ export const RibbonComponent: React.FC<RibbonComponentProps> = ({ |
156 | 48 | </div> |
157 | 49 | </Link> |
158 | 50 | </div> |
159 | | - <div className={styles.actions}> |
160 | | - <RibbonPickerComponent<ThemePickerCodeValues> |
161 | | - label={null} |
162 | | - compactLabel={null} |
163 | | - icon={ |
164 | | - isSystemThemePreference(currentTheme.code) ? ( |
165 | | - <SystemIconComponent /> |
166 | | - ) : isLightThemePreference(currentTheme.code) ? ( |
167 | | - <LightIconComponent /> |
168 | | - ) : ( |
169 | | - <DarkIconComponent /> |
170 | | - ) |
171 | | - } |
172 | | - languageCode={languageCode} |
173 | | - selectedOptionCode={currentTheme.code} |
174 | | - handleSelection={handleThemeSelection} |
175 | | - options={themeOptions} |
176 | | - aria={{ |
177 | | - buttonLabel: dictionary.themePicker.button.ariaLabel, |
178 | | - listLabel: dictionary.themePicker.list.ariaLabel, |
179 | | - }} |
180 | | - /> |
181 | | - </div> |
182 | | - </BoxComponent> |
183 | | - </> |
184 | 51 | ); |
185 | 52 | }; |
0 commit comments