Move require alt-text to new persistence + context (#1839)

zio/stable
Paul Frazee 2023-11-08 08:52:01 -08:00 committed by GitHub
parent 2acc88e78d
commit 3a211017d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 69 additions and 25 deletions

View File

@ -83,7 +83,6 @@ export class PreferencesModel {
prioritizeFollowedUsers: true,
lab_treeViewEnabled: false, // experimental
}
requireAltTextEnabled: boolean = false
// used to help with transitions from device-stored to server-stored preferences
legacyPreferences: LegacyPreferences | undefined
@ -111,7 +110,6 @@ export class PreferencesModel {
contentLabels: this.contentLabels,
savedFeeds: this.savedFeeds,
pinnedFeeds: this.pinnedFeeds,
requireAltTextEnabled: this.requireAltTextEnabled,
}
}
@ -180,13 +178,6 @@ export class PreferencesModel {
) {
this.pinnedFeeds = v.pinnedFeeds
}
// check if requiring alt text is enabled in preferences, then hydrate
if (
hasProp(v, 'requireAltTextEnabled') &&
typeof v.requireAltTextEnabled === 'boolean'
) {
this.requireAltTextEnabled = v.requireAltTextEnabled
}
// grab legacy values
this.legacyPreferences = getLegacyPreferences(v)
}
@ -608,10 +599,6 @@ export class PreferencesModel {
}
}
toggleRequireAltTextEnabled() {
this.requireAltTextEnabled = !this.requireAltTextEnabled
}
setPrimaryLanguage(lang: string) {
this.primaryLanguage = lang
}

View File

@ -6,7 +6,7 @@ import * as store from '#/state/persisted/store'
import BroadcastChannel from '#/state/persisted/broadcast'
export type {Schema} from '#/state/persisted/schema'
export {defaults as schema} from '#/state/persisted/schema'
export {defaults} from '#/state/persisted/schema'
const broadcast = new BroadcastChannel('BSKY_BROADCAST_CHANNEL')
const UPDATE_EVENT = 'BSKY_UPDATE'

View File

@ -0,0 +1,48 @@
import React from 'react'
import * as persisted from '#/state/persisted'
type StateContext = persisted.Schema['requireAltTextEnabled']
type SetContext = (v: persisted.Schema['requireAltTextEnabled']) => void
const stateContext = React.createContext<StateContext>(
persisted.defaults.requireAltTextEnabled,
)
const setContext = React.createContext<SetContext>(
(_: persisted.Schema['requireAltTextEnabled']) => {},
)
export function Provider({children}: React.PropsWithChildren<{}>) {
const [state, setState] = React.useState(
persisted.get('requireAltTextEnabled'),
)
const setStateWrapped = React.useCallback(
(requireAltTextEnabled: persisted.Schema['requireAltTextEnabled']) => {
setState(requireAltTextEnabled)
persisted.write('requireAltTextEnabled', requireAltTextEnabled)
},
[setState],
)
React.useEffect(() => {
return persisted.onUpdate(() => {
setState(persisted.get('requireAltTextEnabled'))
})
}, [setStateWrapped])
return (
<stateContext.Provider value={state}>
<setContext.Provider value={setStateWrapped}>
{children}
</setContext.Provider>
</stateContext.Provider>
)
}
export function useRequireAltTextEnabled() {
return React.useContext(stateContext)
}
export function useSetRequireAltTextEnabled() {
return React.useContext(setContext)
}

View File

@ -3,6 +3,7 @@ import {Provider as DrawerOpenProvider} from './drawer-open'
import {Provider as DrawerSwipableProvider} from './drawer-swipe-disabled'
import {Provider as MinimalModeProvider} from './minimal-mode'
import {Provider as ColorModeProvider} from './color-mode'
import {Provider as AltTextRequiredProvider} from './alt-text-required'
export {useIsDrawerOpen, useSetDrawerOpen} from './drawer-open'
export {
@ -11,13 +12,19 @@ export {
} from './drawer-swipe-disabled'
export {useMinimalShellMode, useSetMinimalShellMode} from './minimal-mode'
export {useColorMode, useSetColorMode} from './color-mode'
export {
useRequireAltTextEnabled,
useSetRequireAltTextEnabled,
} from './alt-text-required'
export function Provider({children}: React.PropsWithChildren<{}>) {
return (
<DrawerOpenProvider>
<DrawerSwipableProvider>
<MinimalModeProvider>
<ColorModeProvider>{children}</ColorModeProvider>
<ColorModeProvider>
<AltTextRequiredProvider>{children}</AltTextRequiredProvider>
</ColorModeProvider>
</MinimalModeProvider>
</DrawerSwipableProvider>
</DrawerOpenProvider>

View File

@ -49,6 +49,7 @@ import {LabelsBtn} from './labels/LabelsBtn'
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'
type Props = ComposerOpts
export const ComposePost = observer(function ComposePost({
@ -61,6 +62,7 @@ export const ComposePost = observer(function ComposePost({
const pal = usePalette('default')
const {isDesktop, isMobile} = useWebMediaQueries()
const store = useStores()
const requireAltTextEnabled = useRequireAltTextEnabled()
const textInput = useRef<TextInputRef>(null)
const [isKeyboardVisible] = useIsKeyboardVisible({iosUseWillEvents: true})
const [isProcessing, setIsProcessing] = useState(false)
@ -187,7 +189,7 @@ export const ComposePost = observer(function ComposePost({
if (isProcessing || graphemeLength > MAX_GRAPHEME_LENGTH) {
return
}
if (store.preferences.requireAltTextEnabled && gallery.needsAltText) {
if (requireAltTextEnabled && gallery.needsAltText) {
return
}
@ -241,12 +243,8 @@ export const ComposePost = observer(function ComposePost({
const canPost = useMemo(
() =>
graphemeLength <= MAX_GRAPHEME_LENGTH &&
(!store.preferences.requireAltTextEnabled || !gallery.needsAltText),
[
graphemeLength,
store.preferences.requireAltTextEnabled,
gallery.needsAltText,
],
(!requireAltTextEnabled || !gallery.needsAltText),
[graphemeLength, requireAltTextEnabled, gallery.needsAltText],
)
const selectTextInputPlaceholder = replyTo ? 'Write your reply' : `What's up?`
@ -314,7 +312,7 @@ export const ComposePost = observer(function ComposePost({
</>
)}
</View>
{store.preferences.requireAltTextEnabled && gallery.needsAltText && (
{requireAltTextEnabled && gallery.needsAltText && (
<View style={[styles.reminderLine, pal.viewLight]}>
<View style={styles.errorIcon}>
<FontAwesomeIcon

View File

@ -50,6 +50,8 @@ import {
useSetMinimalShellMode,
useColorMode,
useSetColorMode,
useRequireAltTextEnabled,
useSetRequireAltTextEnabled,
} from '#/state/shell'
// TEMPORARY (APP-700)
@ -66,6 +68,8 @@ export const SettingsScreen = withAuthRequired(
const pal = usePalette('default')
const store = useStores()
const setMinimalShellMode = useSetMinimalShellMode()
const requireAltTextEnabled = useRequireAltTextEnabled()
const setRequireAltTextEnabled = useSetRequireAltTextEnabled()
const navigation = useNavigation<NavigationProp>()
const {isMobile} = useWebMediaQueries()
const {screen, track} = useAnalytics()
@ -372,8 +376,8 @@ export const SettingsScreen = withAuthRequired(
type="default-light"
label="Require alt text before posting"
labelType="lg"
isSelected={store.preferences.requireAltTextEnabled}
onPress={store.preferences.toggleRequireAltTextEnabled}
isSelected={requireAltTextEnabled}
onPress={() => setRequireAltTextEnabled(!requireAltTextEnabled)}
/>
</View>