mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import React from 'react'
2import {View} from 'react-native'
3import {useQueryClient} from '@tanstack/react-query'
4
5import {sanitizeAppLanguageSetting} from '#/locale/helpers'
6import {APP_LANGUAGES} from '#/locale/languages'
7import {useLanguagePrefs, useLanguagePrefsApi} from '#/state/preferences'
8import {resetPostsFeedQueries} from '#/state/queries/post-feed'
9import {atoms as a, useTheme} from '#/alf'
10import {ChevronBottom_Stroke2_Corner0_Rounded as ChevronDown} from '#/components/icons/Chevron'
11import {Text} from '#/components/Typography'
12
13export function AppLanguageDropdown() {
14 const t = useTheme()
15
16 const queryClient = useQueryClient()
17 const langPrefs = useLanguagePrefs()
18 const setLangPrefs = useLanguagePrefsApi()
19
20 const sanitizedLang = sanitizeAppLanguageSetting(langPrefs.appLanguage)
21
22 const onChangeAppLanguage = React.useCallback(
23 (ev: React.ChangeEvent<HTMLSelectElement>) => {
24 const value = ev.target.value
25
26 if (!value) return
27 if (sanitizedLang !== value) {
28 setLangPrefs.setAppLanguage(sanitizeAppLanguageSetting(value))
29 }
30
31 // reset feeds to refetch content
32 resetPostsFeedQueries(queryClient)
33 },
34 [sanitizedLang, setLangPrefs, queryClient],
35 )
36
37 return (
38 <View
39 style={[
40 // We don't have hitSlop here to increase the tap region,
41 // alternative is negative margins.
42 {height: 32, marginVertical: -((32 - 14) / 2)},
43 ]}>
44 <View
45 style={[
46 a.flex_row,
47 a.gap_sm,
48 a.align_center,
49 a.flex_shrink,
50 a.h_full,
51 t.atoms.bg,
52 ]}>
53 <Text aria-hidden={true} style={t.atoms.text_contrast_medium}>
54 {APP_LANGUAGES.find(l => l.code2 === sanitizedLang)?.name}
55 </Text>
56 <ChevronDown fill={t.atoms.text.color} size="xs" style={a.flex_0} />
57 </View>
58
59 <select
60 value={sanitizedLang}
61 onChange={onChangeAppLanguage}
62 style={{
63 fontSize: a.text_sm.fontSize,
64 letterSpacing: a.text_sm.letterSpacing,
65 cursor: 'pointer',
66 position: 'absolute',
67 inset: 0,
68 opacity: 0,
69 color: t.atoms.text.color,
70 background: t.atoms.bg.backgroundColor,
71 padding: 4,
72 maxWidth: '100%',
73 }}>
74 {APP_LANGUAGES.filter(l => Boolean(l.code2)).map(l => (
75 <option key={l.code2} value={l.code2}>
76 {l.name}
77 </option>
78 ))}
79 </select>
80 </View>
81 )
82}