Move language preferences to new persistence + context (#1837)

This commit is contained in:
Paul Frazee 2023-11-08 09:38:28 -08:00 committed by GitHub
parent e75b2d508b
commit 5843e212c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 233 additions and 190 deletions

View file

@ -50,6 +50,12 @@ import {SelectLangBtn} from './select-language/SelectLangBtn'
import {EmojiPickerButton} from './text-input/web/EmojiPicker.web'
import {insertMentionAt} from 'lib/strings/mention-manip'
import {useRequireAltTextEnabled} from '#/state/shell'
import {
useLanguagePrefs,
useSetLanguagePrefs,
toPostLanguages,
savePostLanguageToHistory,
} from '#/state/preferences/languages'
type Props = ComposerOpts
export const ComposePost = observer(function ComposePost({
@ -63,6 +69,8 @@ export const ComposePost = observer(function ComposePost({
const {isDesktop, isMobile} = useWebMediaQueries()
const store = useStores()
const requireAltTextEnabled = useRequireAltTextEnabled()
const langPrefs = useLanguagePrefs()
const setLangPrefs = useSetLanguagePrefs()
const textInput = useRef<TextInputRef>(null)
const [isKeyboardVisible] = useIsKeyboardVisible({iosUseWillEvents: true})
const [isProcessing, setIsProcessing] = useState(false)
@ -212,7 +220,7 @@ export const ComposePost = observer(function ComposePost({
labels,
onStateChange: setProcessingState,
knownHandles: autocompleteView.knownHandles,
langs: store.preferences.postLanguages,
langs: toPostLanguages(langPrefs.postLanguage),
})
} catch (e: any) {
if (extLink) {
@ -234,7 +242,7 @@ export const ComposePost = observer(function ComposePost({
if (!replyTo) {
store.me.mainFeed.onPostCreated()
}
store.preferences.savePostLanguageToHistory()
savePostLanguageToHistory(setLangPrefs)
onPost?.()
onClose()
Toast.show(`Your ${replyTo ? 'reply' : 'post'} has been published`)

View file

@ -15,10 +15,18 @@ import {usePalette} from 'lib/hooks/usePalette'
import {useStores} from 'state/index'
import {isNative} from 'platform/detection'
import {codeToLanguageName} from '../../../../locale/helpers'
import {
useLanguagePrefs,
useSetLanguagePrefs,
toPostLanguages,
hasPostLanguage,
} from '#/state/preferences/languages'
export const SelectLangBtn = observer(function SelectLangBtn() {
const pal = usePalette('default')
const store = useStores()
const langPrefs = useLanguagePrefs()
const setLangPrefs = useSetLanguagePrefs()
const onPressMore = useCallback(async () => {
if (isNative) {
@ -29,8 +37,7 @@ export const SelectLangBtn = observer(function SelectLangBtn() {
store.shell.openModal({name: 'post-languages-settings'})
}, [store])
const postLanguagesPref = store.preferences.postLanguages
const postLanguagePref = store.preferences.postLanguage
const postLanguagesPref = toPostLanguages(langPrefs.postLanguage)
const items: DropdownItem[] = useMemo(() => {
let arr: DropdownItemButton[] = []
@ -49,13 +56,14 @@ export const SelectLangBtn = observer(function SelectLangBtn() {
arr.push({
icon:
langCodes.every(code => store.preferences.hasPostLanguage(code)) &&
langCodes.length === postLanguagesPref.length
langCodes.every(code =>
hasPostLanguage(langPrefs.postLanguage, code),
) && langCodes.length === postLanguagesPref.length
? ['fas', 'circle-dot']
: ['far', 'circle'],
label: langName,
onPress() {
store.preferences.setPostLanguage(commaSeparatedLangCodes)
setLangPrefs(v => ({...v, postLanguage: commaSeparatedLangCodes}))
},
})
}
@ -65,11 +73,11 @@ export const SelectLangBtn = observer(function SelectLangBtn() {
* Re-join here after sanitization bc postLanguageHistory is an array of
* comma-separated strings too
*/
add(postLanguagePref)
add(langPrefs.postLanguage)
}
// comma-separted strings of lang codes that have been used in the past
for (const lang of store.preferences.postLanguageHistory) {
for (const lang of langPrefs.postLanguageHistory) {
add(lang)
}
@ -82,7 +90,7 @@ export const SelectLangBtn = observer(function SelectLangBtn() {
onPress: onPressMore,
},
]
}, [store.preferences, onPressMore, postLanguagePref, postLanguagesPref])
}, [onPressMore, langPrefs, setLangPrefs, postLanguagesPref])
return (
<DropdownButton