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, ViewStyleProp} from '#/alf'
10import {ChevronBottom_Stroke2_Corner0_Rounded as ChevronDown} from '#/components/icons/Chevron'
11import {Text} from '#/components/Typography'
12
13export function AppLanguageDropdown({style}: ViewStyleProp) {
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 style,
44 ]}>
45 <View
46 style={[
47 a.flex_row,
48 a.gap_sm,
49 a.align_center,
50 a.flex_shrink,
51 a.h_full,
52 t.atoms.bg,
53 ]}>
54 <Text aria-hidden={true} style={t.atoms.text_contrast_medium}>
55 {APP_LANGUAGES.find(l => l.code2 === sanitizedLang)?.name}
56 </Text>
57 <ChevronDown fill={t.atoms.text.color} size="xs" style={a.flex_0} />
58 </View>
59
60 <select
61 value={sanitizedLang}
62 onChange={onChangeAppLanguage}
63 style={{
64 fontSize: a.text_sm.fontSize,
65 letterSpacing: a.text_sm.letterSpacing,
66 cursor: 'pointer',
67 position: 'absolute',
68 inset: 0,
69 opacity: 0,
70 color: t.atoms.text.color,
71 background: t.atoms.bg.backgroundColor,
72 padding: 4,
73 maxWidth: '100%',
74 }}>
75 {APP_LANGUAGES.filter(l => Boolean(l.code2)).map(l => (
76 <option key={l.code2} value={l.code2}>
77 {l.name}
78 </option>
79 ))}
80 </select>
81 </View>
82 )
83}