Skip to content

Commit 2137963

Browse files
committed
chore: clean up unused variables
1 parent c4e3639 commit 2137963

File tree

2 files changed

+28
-34
lines changed

2 files changed

+28
-34
lines changed

src/components/LanguagePicker/index.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ const LanguagePicker = ({
3636
firstItemRef,
3737
filterValue,
3838
setFilterValue,
39-
browserLocales,
4039
filteredNames,
4140
} = useLanguagePicker(handleClose)
4241

src/components/LanguagePicker/useLanguagePicker.tsx

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCallback, useEffect, useRef, useState } from "react"
1+
import { useEffect, useRef, useState } from "react"
22
import { useRouter } from "next/router"
33
import { useTranslation } from "next-i18next"
44
import { useDisclosure } from "@chakra-ui/react"
@@ -27,10 +27,32 @@ export const useLanguagePicker = (handleClose?: () => void) => {
2727
const [filterValue, setFilterValue] = useState("")
2828

2929
const [filteredNames, setFilteredNames] = useState<LocaleDisplayInfo[]>([])
30-
const [browserLocales, setBrowserLocales] = useState<Lang[]>([])
3130

32-
const localeToDisplayInfo = useCallback(
33-
(localeOption: Lang): LocaleDisplayInfo => {
31+
// perform all the filtering and mapping when the filter value change
32+
useEffect(() => {
33+
// Get the preferred languages for the users browser
34+
const navLangs = typeof navigator !== "undefined" ? navigator.languages : []
35+
36+
// For each browser preference, reduce to the most specific match found in `locales` array
37+
const allBrowserLocales: Lang[] = navLangs
38+
.map(
39+
(navLang) =>
40+
locales?.reduce((acc, cur) => {
41+
if (cur.toLowerCase() === navLang.toLowerCase()) return cur
42+
if (
43+
navLang.toLowerCase().startsWith(cur.toLowerCase()) &&
44+
acc !== navLang
45+
)
46+
return cur
47+
return acc
48+
}, "") as Lang
49+
)
50+
.filter((i) => !!i) // Remove those without matches
51+
52+
// Remove duplicate matches
53+
const browserLocales = Array.from(new Set(allBrowserLocales))
54+
55+
const localeToDisplayInfo = (localeOption: Lang): LocaleDisplayInfo => {
3456
const i18nItem: I18nLocale = languages[localeOption]
3557
const englishName = i18nItem.name
3658

@@ -90,33 +112,7 @@ export const useLanguagePicker = (handleClose?: () => void) => {
90112
wordsApproved,
91113
isBrowserDefault,
92114
}
93-
},
94-
[browserLocales, locale, t]
95-
)
96-
97-
// perform all the filtering and mapping when the filter value change
98-
useEffect(() => {
99-
// Get the preferred languages for the users browser
100-
const navLangs = typeof navigator !== "undefined" ? navigator.languages : []
101-
102-
// For each browser preference, reduce to the most specific match found in `locales` array
103-
const allBrowserLocales: Lang[] = navLangs
104-
.map(
105-
(navLang) =>
106-
locales?.reduce((acc, cur) => {
107-
if (cur.toLowerCase() === navLang.toLowerCase()) return cur
108-
if (
109-
navLang.toLowerCase().startsWith(cur.toLowerCase()) &&
110-
acc !== navLang
111-
)
112-
return cur
113-
return acc
114-
}, "") as Lang
115-
)
116-
.filter((i) => !!i) // Remove those without matches
117-
118-
// Remove duplicate matches
119-
setBrowserLocales(Array.from(new Set(allBrowserLocales)))
115+
}
120116

121117
const displayNames: LocaleDisplayInfo[] =
122118
(locales as Lang[])?.map(localeToDisplayInfo).sort((a, b) => {
@@ -136,7 +132,7 @@ export const useLanguagePicker = (handleClose?: () => void) => {
136132
.includes(filterValue.toLowerCase())
137133
)
138134
)
139-
}, [browserLocales, filterValue, localeToDisplayInfo, locales])
135+
}, [filterValue, locale, locales, t])
140136

141137
const { isOpen, ...menu } = useDisclosure()
142138

@@ -178,7 +174,6 @@ export const useLanguagePicker = (handleClose?: () => void) => {
178174
firstItemRef,
179175
filterValue,
180176
setFilterValue,
181-
browserLocales,
182177
filteredNames,
183178
}
184179
}

0 commit comments

Comments
 (0)