Add a little more safety

This commit is contained in:
Eric Bailey
2023-12-12 09:35:37 -06:00
parent 190ee71988
commit e9c21dc030
5 changed files with 21 additions and 10 deletions
+12
View File
@@ -0,0 +1,12 @@
import {test, expect} from '@jest/globals'
import {sanitizeAppLanguageSetting} from '#/locale/helpers'
import {AppLanguage} from '#/locale/languages'
test('sanitizeAppLanguageSetting', () => {
expect(sanitizeAppLanguageSetting('en')).toBe(AppLanguage.en)
expect(sanitizeAppLanguageSetting('hi')).toBe(AppLanguage.hi)
expect(sanitizeAppLanguageSetting('foo')).toBe(AppLanguage.en)
expect(sanitizeAppLanguageSetting('en,fr')).toBe(AppLanguage.en)
expect(sanitizeAppLanguageSetting('fr,en')).toBe(AppLanguage.en)
})
+1 -3
View File
@@ -100,9 +100,7 @@ export function getTranslatorLink(text: string, lang: string): string {
* and selects a new option, at which point we'll re-save their choice, which * and selects a new option, at which point we'll re-save their choice, which
* should then be a valid option. * should then be a valid option.
*/ */
export function sanitizeAppLanguageSetting( export function sanitizeAppLanguageSetting(appLanguage: string): AppLanguage {
appLanguage: AppLanguage | string,
): AppLanguage {
const langs = appLanguage.split(',').filter(Boolean) const langs = appLanguage.split(',').filter(Boolean)
for (const lang of langs) { for (const lang of langs) {
+3 -3
View File
@@ -10,13 +10,13 @@ export enum AppLanguage {
} }
interface AppLanguageConfig { interface AppLanguageConfig {
code2: string code2: AppLanguage
name: string name: string
} }
export const APP_LANGUAGES: AppLanguageConfig[] = [ export const APP_LANGUAGES: AppLanguageConfig[] = [
{code2: 'en', name: 'English'}, {code2: AppLanguage.en, name: 'English'},
{code2: 'hi', name: 'हिंदी'}, {code2: AppLanguage.hi, name: 'हिंदी'},
] ]
export const LANGUAGES: Language[] = [ export const LANGUAGES: Language[] = [
+4 -3
View File
@@ -1,5 +1,6 @@
import React from 'react' import React from 'react'
import * as persisted from '#/state/persisted' import * as persisted from '#/state/persisted'
import {AppLanguage} from '#/locale/languages'
type SetStateCb = ( type SetStateCb = (
s: persisted.Schema['languagePrefs'], s: persisted.Schema['languagePrefs'],
@@ -11,7 +12,7 @@ type ApiContext = {
toggleContentLanguage: (code2: string) => void toggleContentLanguage: (code2: string) => void
togglePostLanguage: (code2: string) => void togglePostLanguage: (code2: string) => void
savePostLanguageToHistory: () => void savePostLanguageToHistory: () => void
setAppLanguage: (code2: string) => void setAppLanguage: (code2: AppLanguage) => void
} }
const stateContext = React.createContext<StateContext>( const stateContext = React.createContext<StateContext>(
@@ -23,7 +24,7 @@ const apiContext = React.createContext<ApiContext>({
toggleContentLanguage: (_: string) => {}, toggleContentLanguage: (_: string) => {},
togglePostLanguage: (_: string) => {}, togglePostLanguage: (_: string) => {},
savePostLanguageToHistory: () => {}, savePostLanguageToHistory: () => {},
setAppLanguage: (_: string) => {}, setAppLanguage: (_: AppLanguage) => {},
}) })
export function Provider({children}: React.PropsWithChildren<{}>) { export function Provider({children}: React.PropsWithChildren<{}>) {
@@ -106,7 +107,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
.slice(0, 6), .slice(0, 6),
})) }))
}, },
setAppLanguage(code2: string) { setAppLanguage(code2: AppLanguage) {
setStateWrapped(s => ({...s, appLanguage: code2})) setStateWrapped(s => ({...s, appLanguage: code2}))
}, },
}), }),
+1 -1
View File
@@ -61,7 +61,7 @@ export function LanguageSettingsScreen(_props: Props) {
(value: Parameters<PickerSelectProps['onValueChange']>[0]) => { (value: Parameters<PickerSelectProps['onValueChange']>[0]) => {
if (!value) return if (!value) return
if (langPrefs.appLanguage !== value) { if (langPrefs.appLanguage !== value) {
setLangPrefs.setAppLanguage(value) setLangPrefs.setAppLanguage(sanitizeAppLanguageSetting(value))
} }
}, },
[langPrefs, setLangPrefs], [langPrefs, setLangPrefs],