An ATproto social media client -- with an independent Appview.
1import React from 'react'
2import {msg} from '@lingui/macro'
3import {useLingui} from '@lingui/react'
4import {useQueryClient} from '@tanstack/react-query'
5
6import {sanitizeAppLanguageSetting} from '#/locale/helpers'
7import {APP_LANGUAGES} from '#/locale/languages'
8import {useLanguagePrefs, useLanguagePrefsApi} from '#/state/preferences'
9import {resetPostsFeedQueries} from '#/state/queries/post-feed'
10import {atoms as a, platform, useTheme} from '#/alf'
11import * as Select from '#/components/Select'
12import {Button} from './Button'
13
14export function AppLanguageDropdown() {
15 const t = useTheme()
16 const {_} = useLingui()
17
18 const queryClient = useQueryClient()
19 const langPrefs = useLanguagePrefs()
20 const setLangPrefs = useLanguagePrefsApi()
21 const sanitizedLang = sanitizeAppLanguageSetting(langPrefs.appLanguage)
22
23 const onChangeAppLanguage = React.useCallback(
24 (value: string) => {
25 if (!value) return
26 if (sanitizedLang !== value) {
27 setLangPrefs.setAppLanguage(sanitizeAppLanguageSetting(value))
28 }
29
30 // reset feeds to refetch content
31 resetPostsFeedQueries(queryClient)
32 },
33 [sanitizedLang, setLangPrefs, queryClient],
34 )
35
36 return (
37 <Select.Root
38 value={sanitizeAppLanguageSetting(langPrefs.appLanguage)}
39 onValueChange={onChangeAppLanguage}>
40 <Select.Trigger label={_(msg`Change app language`)}>
41 {({props}) => (
42 <Button
43 {...props}
44 label={props.accessibilityLabel}
45 size={platform({
46 web: 'tiny',
47 native: 'small',
48 })}
49 variant="ghost"
50 color="secondary"
51 style={[
52 a.pr_xs,
53 a.pl_sm,
54 platform({
55 web: [{alignSelf: 'flex-start'}, a.gap_sm],
56 native: [a.gap_xs],
57 }),
58 ]}>
59 <Select.ValueText
60 placeholder={_(msg`Select an app language`)}
61 style={[t.atoms.text_contrast_medium]}
62 />
63 <Select.Icon style={[t.atoms.text_contrast_medium]} />
64 </Button>
65 )}
66 </Select.Trigger>
67 <Select.Content
68 renderItem={({label, value}) => (
69 <Select.Item value={value} label={label}>
70 <Select.ItemIndicator />
71 <Select.ItemText>{label}</Select.ItemText>
72 </Select.Item>
73 )}
74 items={APP_LANGUAGES.map(l => ({
75 label: l.name,
76 value: l.code2,
77 }))}
78 />
79 </Select.Root>
80 )
81}