11"use client" ;
22
3- import React from "react" ;
3+ import React , { useCallback , useMemo , useState } from "react" ;
44import { usePathname } from "next/navigation" ;
55import { DEFAULT_LANGUAGE_CODE } from "@/features/localization/localization.config" ;
66import { LayoutDictionaryModel } from "@/features/localization/models/layout-dictionary.model" ;
@@ -10,13 +10,30 @@ import styles from "./header.module.scss";
1010import { BoxComponent } from "@/features/common/components/box/box.component" ;
1111import Link from "next/link" ;
1212import { SiteBrandComponent } from "@/features/common/components/site-brand/site-brand.component" ;
13+ import { ThemePickerComponent } from "../../theme-picker/theme-picker.component" ;
14+ import {
15+ getSanitizedThemePickerCodeValue ,
16+ isLightThemePreference ,
17+ isSystemThemePreference ,
18+ } from "@/features/themes/services/theme.utils" ;
19+ import { SystemIconComponent } from "../../bars/ribbon/assets/system-icon.component" ;
20+ import { LightIconComponent } from "../../bars/ribbon/assets/light-icon.component" ;
21+ import { DarkIconComponent } from "../../bars/ribbon/assets/dark-icon.component" ;
22+ import {
23+ ThemeCookieValues ,
24+ ThemePickerCodeValues ,
25+ } from "@/features/common/values/theme.values" ;
26+ import { ThemeModel } from "@/features/common/models/theme.model" ;
27+ import { savePreferredThemeInCookie } from "@/features/themes/services/theme.client.utils" ;
1328
1429interface HeaderComponentProps {
30+ themeCode : ThemeCookieValues ;
1531 languageCode : string ;
16- dictionary : LayoutDictionaryModel [ "header" ] ;
32+ dictionary : LayoutDictionaryModel ;
1733}
1834
1935export const HeaderComponent : React . FC < HeaderComponentProps > = ( {
36+ themeCode,
2037 languageCode,
2138 dictionary,
2239} ) => {
@@ -33,6 +50,50 @@ export const HeaderComponent: React.FC<HeaderComponentProps> = ({
3350 ? sitePaths . root
3451 : createUrlPath ( [ languageCode ] ) ;
3552
53+ const themeOptions = useMemo (
54+ ( ) =>
55+ dictionary . ribbon . themePicker . options . map ( ( option ) => {
56+ return {
57+ code : option . code ,
58+ label : option . label ,
59+ icon : isSystemThemePreference ( option . code ) ? (
60+ < SystemIconComponent />
61+ ) : isLightThemePreference ( option . code ) ? (
62+ < LightIconComponent />
63+ ) : (
64+ < DarkIconComponent />
65+ ) ,
66+ } ;
67+ } ) ,
68+ [ dictionary . ribbon . themePicker . options ]
69+ ) ;
70+
71+ const sanitizedThemePickerCodeValue = useMemo ( ( ) => {
72+ return getSanitizedThemePickerCodeValue ( themeCode ) ;
73+ } , [ themeCode ] ) ;
74+
75+ const [ currentTheme , setCurrentTheme ] = useState < ThemeModel > (
76+ dictionary . ribbon . themePicker . options . filter ( ( element ) =>
77+ isSystemThemePreference ( themeCode )
78+ ? isSystemThemePreference ( element . code )
79+ : element . code === sanitizedThemePickerCodeValue
80+ ) [ 0 ]
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+
3697 return (
3798 < BoxComponent
3899 contentAs = "nav"
@@ -42,12 +103,15 @@ export const HeaderComponent: React.FC<HeaderComponentProps> = ({
42103 aria-label = "Main navigation"
43104 >
44105 < div className = { styles . brand } >
45- < SiteBrandComponent path = { languagePathPrefix } languageCode = { languageCode } />
106+ < SiteBrandComponent
107+ path = { languagePathPrefix }
108+ languageCode = { languageCode }
109+ />
46110 </ div >
47111 < div className = { styles . navContainer } >
48112 < div className = { styles . navTabs } >
49113 < ul className = { styles . navList } >
50- { dictionary . links . map ( ( link ) => {
114+ { dictionary . header . links . map ( ( link ) => {
51115 const linkPath =
52116 languageCode === DEFAULT_LANGUAGE_CODE || link . isExternal
53117 ? link . path
@@ -73,6 +137,13 @@ export const HeaderComponent: React.FC<HeaderComponentProps> = ({
73137 </ ul >
74138 </ div >
75139 </ div >
140+ < div className = { styles . actions } >
141+ < ThemePickerComponent
142+ options = { themeOptions }
143+ handleSelection = { handleThemeSelection }
144+ selectedOptionCode = { currentTheme . code }
145+ />
146+ </ div >
76147 </ BoxComponent >
77148 ) ;
78149} ;
0 commit comments