about summary refs log tree commit diff
path: root/src/view/com/composer/select-language/SelectLangBtn.tsx
blob: f487b1244ba9b0a9183a3729cf75ea04680537e4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import {useCallback, useMemo} from 'react'
import {Keyboard, StyleSheet} from 'react-native'
import {
  FontAwesomeIcon,
  FontAwesomeIconStyle,
} from '@fortawesome/react-native-fontawesome'
import {msg} from '@lingui/macro'
import {useLingui} from '@lingui/react'

import {LANG_DROPDOWN_HITSLOP} from '#/lib/constants'
import {usePalette} from '#/lib/hooks/usePalette'
import {isNative} from '#/platform/detection'
import {useModalControls} from '#/state/modals'
import {
  hasPostLanguage,
  toPostLanguages,
  useLanguagePrefs,
  useLanguagePrefsApi,
} from '#/state/preferences/languages'
import {
  DropdownButton,
  DropdownItem,
  DropdownItemButton,
} from '#/view/com/util/forms/DropdownButton'
import {Text} from '#/view/com/util/text/Text'
import {codeToLanguageName} from '../../../../locale/helpers'

export function SelectLangBtn() {
  const pal = usePalette('default')
  const {_} = useLingui()
  const {openModal} = useModalControls()
  const langPrefs = useLanguagePrefs()
  const setLangPrefs = useLanguagePrefsApi()

  const onPressMore = useCallback(async () => {
    if (isNative) {
      if (Keyboard.isVisible()) {
        Keyboard.dismiss()
      }
    }
    openModal({name: 'post-languages-settings'})
  }, [openModal])

  const postLanguagesPref = toPostLanguages(langPrefs.postLanguage)
  const items: DropdownItem[] = useMemo(() => {
    let arr: DropdownItemButton[] = []

    function add(commaSeparatedLangCodes: string) {
      const langCodes = commaSeparatedLangCodes.split(',')
      const langName = langCodes
        .map(code => codeToLanguageName(code, langPrefs.appLanguage))
        .join(' + ')

      /*
       * Filter out any duplicates
       */
      if (arr.find((item: DropdownItemButton) => item.label === langName)) {
        return
      }

      arr.push({
        icon:
          langCodes.every(code =>
            hasPostLanguage(langPrefs.postLanguage, code),
          ) && langCodes.length === postLanguagesPref.length
            ? ['fas', 'circle-dot']
            : ['far', 'circle'],
        label: langName,
        onPress() {
          setLangPrefs.setPostLanguage(commaSeparatedLangCodes)
        },
      })
    }

    if (postLanguagesPref.length) {
      /*
       * Re-join here after sanitization bc postLanguageHistory is an array of
       * comma-separated strings too
       */
      add(langPrefs.postLanguage)
    }

    // comma-separted strings of lang codes that have been used in the past
    for (const lang of langPrefs.postLanguageHistory) {
      add(lang)
    }

    return [
      {heading: true, label: _(msg`Post language`)},
      ...arr.slice(0, 6),
      {sep: true},
      {
        label: _(msg`Other...`),
        onPress: onPressMore,
      },
    ]
  }, [onPressMore, langPrefs, setLangPrefs, postLanguagesPref, _])

  return (
    <DropdownButton
      type="bare"
      testID="selectLangBtn"
      items={items}
      openUpwards
      style={styles.button}
      hitSlop={LANG_DROPDOWN_HITSLOP}
      accessibilityLabel={_(msg`Language selection`)}
      accessibilityHint="">
      {postLanguagesPref.length > 0 ? (
        <Text type="lg-bold" style={[pal.link, styles.label]} numberOfLines={1}>
          {postLanguagesPref
            .map(lang => codeToLanguageName(lang, langPrefs.appLanguage))
            .join(', ')}
        </Text>
      ) : (
        <FontAwesomeIcon
          icon="language"
          style={pal.link as FontAwesomeIconStyle}
          size={26}
        />
      )}
    </DropdownButton>
  )
}

const styles = StyleSheet.create({
  button: {
    marginHorizontal: 15,
  },
  label: {
    maxWidth: 100,
  },
})