2023-10-23 23:11:59 +02:00
|
|
|
"use client"
|
2023-07-30 18:50:14 +02:00
|
|
|
|
2023-10-23 23:11:59 +02:00
|
|
|
import { usePathname } from "next/navigation"
|
|
|
|
import { useCallback, useEffect, useState, useRef } from "react"
|
|
|
|
import classNames from "clsx"
|
2021-04-18 18:02:55 +02:00
|
|
|
|
2023-10-23 23:11:59 +02:00
|
|
|
import type { Locale as LocaleType, CookiesStore } from "@/utils/constants"
|
|
|
|
import { LOCALES } from "@/utils/constants"
|
2021-12-04 15:52:51 +01:00
|
|
|
|
2023-10-23 23:11:59 +02:00
|
|
|
import { Arrow } from "./Arrow"
|
|
|
|
import { LocaleFlag } from "./LocaleFlag"
|
2023-07-31 19:06:46 +02:00
|
|
|
|
|
|
|
export interface LocalesProps {
|
|
|
|
currentLocale: string
|
|
|
|
cookiesStore: CookiesStore
|
|
|
|
}
|
|
|
|
|
|
|
|
export const Locales = (props: LocalesProps): JSX.Element => {
|
|
|
|
const { currentLocale, cookiesStore } = props
|
2023-08-01 17:07:19 +02:00
|
|
|
const pathname = usePathname()
|
2021-04-18 18:02:55 +02:00
|
|
|
|
|
|
|
const [hiddenMenu, setHiddenMenu] = useState(true)
|
2022-05-03 10:05:11 +02:00
|
|
|
const languageClickRef = useRef<HTMLDivElement | null>(null)
|
2021-04-18 18:02:55 +02:00
|
|
|
|
2021-06-15 20:35:52 +02:00
|
|
|
const handleHiddenMenu = useCallback(() => {
|
2022-09-04 20:40:58 +02:00
|
|
|
setHiddenMenu((oldHiddenMenu) => {
|
|
|
|
return !oldHiddenMenu
|
|
|
|
})
|
2022-05-03 10:05:11 +02:00
|
|
|
}, [])
|
2021-06-15 20:35:52 +02:00
|
|
|
|
2021-04-18 18:02:55 +02:00
|
|
|
useEffect(() => {
|
2022-05-03 10:05:11 +02:00
|
|
|
const handleClickEvent = (event: MouseEvent): void => {
|
|
|
|
if (languageClickRef.current == null || event.target == null) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (!languageClickRef.current.contains(event.target as Node)) {
|
|
|
|
setHiddenMenu(true)
|
|
|
|
}
|
2021-04-18 18:02:55 +02:00
|
|
|
}
|
|
|
|
|
2023-10-23 23:11:59 +02:00
|
|
|
window.document.addEventListener("click", handleClickEvent)
|
2022-05-03 10:05:11 +02:00
|
|
|
|
2021-04-18 18:02:55 +02:00
|
|
|
return () => {
|
2023-10-23 23:11:59 +02:00
|
|
|
return window.removeEventListener("click", handleClickEvent)
|
2021-04-18 18:02:55 +02:00
|
|
|
}
|
2022-05-03 10:05:11 +02:00
|
|
|
}, [])
|
2021-04-18 18:02:55 +02:00
|
|
|
|
2023-08-01 14:11:46 +02:00
|
|
|
const handleLocale = async (locale: LocaleType): Promise<void> => {
|
2023-10-23 23:11:59 +02:00
|
|
|
const { setLocale } = await import("@/i18n/i18n.server")
|
2023-07-31 19:06:46 +02:00
|
|
|
setLocale(locale)
|
2021-04-18 18:02:55 +02:00
|
|
|
}
|
|
|
|
|
2023-10-23 23:11:59 +02:00
|
|
|
if (pathname.startsWith("/blog")) {
|
2023-08-01 17:07:19 +02:00
|
|
|
return <></>
|
|
|
|
}
|
|
|
|
|
2021-04-18 18:02:55 +02:00
|
|
|
return (
|
2023-10-23 23:11:59 +02:00
|
|
|
<div className="flex cursor-pointer flex-col items-center justify-center">
|
2021-08-13 15:48:29 +02:00
|
|
|
<div
|
2022-05-03 10:05:11 +02:00
|
|
|
ref={languageClickRef}
|
2023-10-23 23:11:59 +02:00
|
|
|
data-cy="locale-click"
|
|
|
|
className="mr-5 flex items-center"
|
2021-08-13 15:48:29 +02:00
|
|
|
onClick={handleHiddenMenu}
|
|
|
|
>
|
2023-07-31 19:06:46 +02:00
|
|
|
<LocaleFlag
|
|
|
|
locale={currentLocale}
|
|
|
|
cookiesStore={cookiesStore?.toString()}
|
|
|
|
/>
|
2021-05-08 19:52:04 +02:00
|
|
|
<Arrow />
|
2021-04-18 18:02:55 +02:00
|
|
|
</div>
|
2021-08-23 19:41:39 +02:00
|
|
|
|
|
|
|
<ul
|
2023-10-23 23:11:59 +02:00
|
|
|
data-cy="locales-list"
|
2021-08-23 19:41:39 +02:00
|
|
|
className={classNames(
|
2023-10-23 23:11:59 +02:00
|
|
|
"absolute top-14 z-10 mr-4 mt-3 flex w-32 list-none flex-col items-center justify-center rounded-lg bg-white p-0 shadow-lightFlag dark:bg-black dark:shadow-darkFlag",
|
|
|
|
{ hidden: hiddenMenu },
|
2021-08-23 19:41:39 +02:00
|
|
|
)}
|
|
|
|
>
|
2023-08-01 14:11:46 +02:00
|
|
|
{LOCALES.filter((locale) => {
|
2023-07-31 19:06:46 +02:00
|
|
|
return locale !== currentLocale
|
|
|
|
}).map((locale) => {
|
2021-08-23 19:41:39 +02:00
|
|
|
return (
|
|
|
|
<li
|
2023-07-31 19:06:46 +02:00
|
|
|
key={locale}
|
2024-01-28 03:21:11 +01:00
|
|
|
className="flex h-12 w-full items-center justify-center hover:bg-[#4f545c]/20"
|
2022-09-04 20:40:58 +02:00
|
|
|
onClick={async () => {
|
2023-07-31 19:06:46 +02:00
|
|
|
return await handleLocale(locale)
|
2022-09-04 20:40:58 +02:00
|
|
|
}}
|
2021-08-23 19:41:39 +02:00
|
|
|
>
|
2023-07-31 19:06:46 +02:00
|
|
|
<LocaleFlag
|
|
|
|
locale={locale}
|
|
|
|
cookiesStore={cookiesStore?.toString()}
|
|
|
|
/>
|
2021-08-23 19:41:39 +02:00
|
|
|
</li>
|
|
|
|
)
|
|
|
|
})}
|
|
|
|
</ul>
|
2021-05-08 19:52:04 +02:00
|
|
|
</div>
|
2021-04-18 18:02:55 +02:00
|
|
|
)
|
|
|
|
}
|