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 setLangPrefs.setPrimaryLanguage(value)
31 setLangPrefs.setContentLanguage(value)
32
33 // reset feeds to refetch content
34 resetPostsFeedQueries(queryClient)
35 },
36 [sanitizedLang, setLangPrefs, queryClient],
37 )
38
39 return (
40 <View
41 style={[
42 // We don't have hitSlop here to increase the tap region,
43 // alternative is negative margins.
44 {height: 32, marginVertical: -((32 - 14) / 2)},
45 ]}>
46 <View
47 style={[
48 a.flex_row,
49 a.gap_sm,
50 a.align_center,
51 a.flex_shrink,
52 a.h_full,
53 t.atoms.bg,
54 ]}>
55 <Text aria-hidden={true} style={t.atoms.text_contrast_medium}>
56 {APP_LANGUAGES.find(l => l.code2 === sanitizedLang)?.name}
57 </Text>
58 <ChevronDown fill={t.atoms.text.color} size="xs" style={a.flex_0} />
59 </View>
60
61 <select
62 value={sanitizedLang}
63 onChange={onChangeAppLanguage}
64 style={{
65 fontSize: a.text_sm.fontSize,
66 letterSpacing: a.text_sm.letterSpacing,
67 cursor: 'pointer',
68 position: 'absolute',
69 inset: 0,
70 opacity: 0,
71 color: t.atoms.text.color,
72 background: t.atoms.bg.backgroundColor,
73 padding: 4,
74 maxWidth: '100%',
75 }}>
76 {APP_LANGUAGES.filter(l => Boolean(l.code2)).map(l => (
77 <option key={l.code2} value={l.code2}>
78 {l.name}
79 </option>
80 ))}
81 </select>
82 </View>
83 )
84}