Skip to content

Commit b7f7c0b

Browse files
committed
refactor: extract component logic to hook
1 parent c4a749a commit b7f7c0b

File tree

2 files changed

+38
-22
lines changed

2 files changed

+38
-22
lines changed

src/components/Nav/Menu/index.tsx

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,30 @@
1-
import { useState } from "react"
21
import { Box, type BoxProps, Flex } from "@chakra-ui/react"
32
import * as NavigationMenu from "@radix-ui/react-navigation-menu"
43

54
import { Button } from "@/components/Buttons"
65

7-
// import { trackCustomEvent } from "@/lib/utils/matomo"
86
import { SECTION_LABELS } from "@/lib/constants"
97

10-
import type { NavSectionKey, NavSections } from "../types"
8+
import type { NavSections } from "../types"
119

1210
import LvlContent from "./LvlContent"
13-
14-
import { useNavMenuColors } from "@/hooks/useNavMenuColors"
15-
import { useRtlFlip } from "@/hooks/useRtlFlip"
11+
import { useNavMenu } from "./useNavMenu"
1612

1713
type NavMenuProps = BoxProps & {
1814
sections: NavSections
1915
}
2016

2117
const Menu = ({ sections, ...props }: NavMenuProps) => {
22-
const { direction } = useRtlFlip()
23-
const menuColors = useNavMenuColors()
24-
const [activeSection, setActiveSection] = useState<NavSectionKey | null>(null)
25-
26-
const getEnglishSectionName = (
27-
activeSection: string
28-
): NavSectionKey | null => {
29-
const index = Object.values(sections).findIndex(
30-
(section) => section.label === activeSection
31-
)
32-
if (index < 0) return null
33-
return Object.keys(sections)[index] as NavSectionKey
34-
}
18+
const { direction, menuColors, activeSection, handleSectionChange } =
19+
useNavMenu(sections)
3520

3621
return (
3722
<Box {...props}>
3823
<NavigationMenu.Root
3924
dir={direction}
4025
orientation="horizontal"
4126
delayDuration={750}
42-
onValueChange={(activeSection) => {
43-
setActiveSection(getEnglishSectionName(activeSection))
44-
}}
27+
onValueChange={handleSectionChange}
4528
>
4629
<NavigationMenu.List asChild>
4730
<Flex as="ul" listStyleType="none">

src/components/Nav/Menu/useNavMenu.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { useState } from "react"
2+
3+
import type { NavSectionKey, NavSections } from "../types"
4+
5+
import { useNavMenuColors } from "@/hooks/useNavMenuColors"
6+
import { useRtlFlip } from "@/hooks/useRtlFlip"
7+
8+
export const useNavMenu = (sections: NavSections) => {
9+
const { direction } = useRtlFlip()
10+
const menuColors = useNavMenuColors()
11+
const [activeSection, setActiveSection] = useState<NavSectionKey | null>(null)
12+
13+
const getEnglishSectionName = (
14+
activeSection: string
15+
): NavSectionKey | null => {
16+
const index = Object.values(sections).findIndex(
17+
(section) => section.label === activeSection
18+
)
19+
if (index < 0) return null
20+
return Object.keys(sections)[index] as NavSectionKey
21+
}
22+
23+
const handleSectionChange = (activeSection: string) => {
24+
setActiveSection(getEnglishSectionName(activeSection))
25+
}
26+
27+
return {
28+
direction,
29+
menuColors,
30+
activeSection,
31+
handleSectionChange,
32+
}
33+
}

0 commit comments

Comments
 (0)