Create codemod for addressing ESLint warnings (#10032)
This commit is contained in:
@@ -1,4 +1,11 @@
|
||||
import React from 'react'
|
||||
import {
|
||||
createContext,
|
||||
useCallback,
|
||||
useContext,
|
||||
useEffect,
|
||||
useMemo,
|
||||
useState,
|
||||
} from 'react'
|
||||
|
||||
import {type AppLanguage} from '#/locale/languages'
|
||||
import * as persisted from '#/state/persisted'
|
||||
@@ -16,11 +23,11 @@ type ApiContext = {
|
||||
setAppLanguage: (code2: AppLanguage) => void
|
||||
}
|
||||
|
||||
const stateContext = React.createContext<StateContext>(
|
||||
const stateContext = createContext<StateContext>(
|
||||
persisted.defaults.languagePrefs,
|
||||
)
|
||||
stateContext.displayName = 'LanguagePrefsStateContext'
|
||||
const apiContext = React.createContext<ApiContext>({
|
||||
const apiContext = createContext<ApiContext>({
|
||||
setPrimaryLanguage: (_: string) => {},
|
||||
setPostLanguage: (_: string) => {},
|
||||
setContentLanguages: (_: string[]) => {},
|
||||
@@ -30,9 +37,9 @@ const apiContext = React.createContext<ApiContext>({
|
||||
apiContext.displayName = 'LanguagePrefsApiContext'
|
||||
|
||||
export function Provider({children}: React.PropsWithChildren<{}>) {
|
||||
const [state, setState] = React.useState(() => persisted.get('languagePrefs'))
|
||||
const [state, setState] = useState(() => persisted.get('languagePrefs'))
|
||||
|
||||
const setStateWrapped = React.useCallback(
|
||||
const setStateWrapped = useCallback(
|
||||
(fn: SetStateCb) => {
|
||||
const s = fn(persisted.get('languagePrefs'))
|
||||
setState(s)
|
||||
@@ -41,13 +48,13 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
|
||||
[setState],
|
||||
)
|
||||
|
||||
React.useEffect(() => {
|
||||
useEffect(() => {
|
||||
return persisted.onUpdate('languagePrefs', nextLanguagePrefs => {
|
||||
setState(nextLanguagePrefs)
|
||||
})
|
||||
}, [setStateWrapped])
|
||||
|
||||
const api = React.useMemo(
|
||||
const api = useMemo(
|
||||
() => ({
|
||||
setPrimaryLanguage(code2: string) {
|
||||
setStateWrapped(s => ({...s, primaryLanguage: code2}))
|
||||
@@ -102,11 +109,11 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
|
||||
}
|
||||
|
||||
export function useLanguagePrefs() {
|
||||
return React.useContext(stateContext)
|
||||
return useContext(stateContext)
|
||||
}
|
||||
|
||||
export function useLanguagePrefsApi() {
|
||||
return React.useContext(apiContext)
|
||||
return useContext(apiContext)
|
||||
}
|
||||
|
||||
export function getContentLanguages() {
|
||||
|
||||
Reference in New Issue
Block a user