Move label defs into a context to reduce recomputes
This commit is contained in:
@@ -2,7 +2,7 @@ import React from 'react'
|
|||||||
import {View, Pressable} from 'react-native'
|
import {View, Pressable} from 'react-native'
|
||||||
import {Trans} from '@lingui/macro'
|
import {Trans} from '@lingui/macro'
|
||||||
|
|
||||||
import {useMyLabelers} from '#/state/queries/preferences'
|
import {useMyLabelersQuery} from '#/state/queries/preferences'
|
||||||
import {ReportOption} from '#/lib/moderation/useReportOptions'
|
import {ReportOption} from '#/lib/moderation/useReportOptions'
|
||||||
export {useDialogControl as useReportDialogControl} from '#/components/Dialog'
|
export {useDialogControl as useReportDialogControl} from '#/components/Dialog'
|
||||||
|
|
||||||
@@ -27,7 +27,11 @@ export function ReportDialog(props: ReportDialogProps) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function ReportDialogInner(props: ReportDialogProps) {
|
function ReportDialogInner(props: ReportDialogProps) {
|
||||||
const {isLoading: isLabelerLoading, data: labelers, error} = useMyLabelers()
|
const {
|
||||||
|
isLoading: isLabelerLoading,
|
||||||
|
data: labelers,
|
||||||
|
error,
|
||||||
|
} = useMyLabelersQuery()
|
||||||
const isLoading = useDelayedLoading(500, isLabelerLoading)
|
const isLoading = useDelayedLoading(500, isLabelerLoading)
|
||||||
const [selectedReportOption, setSelectedReportOption] = React.useState<
|
const [selectedReportOption, setSelectedReportOption] = React.useState<
|
||||||
ReportOption | undefined
|
ReportOption | undefined
|
||||||
|
|||||||
@@ -9,10 +9,10 @@ import {useLingui} from '@lingui/react'
|
|||||||
import * as bcp47Match from 'bcp-47-match'
|
import * as bcp47Match from 'bcp-47-match'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
useGlobalLabelStrings,
|
|
||||||
GlobalLabelStrings,
|
GlobalLabelStrings,
|
||||||
|
useGlobalLabelStrings,
|
||||||
} from '#/lib/moderation/useGlobalLabelStrings'
|
} from '#/lib/moderation/useGlobalLabelStrings'
|
||||||
import {useLabelDefinitions} from '#/state/queries/preferences'
|
import {useLabelDefinitions} from '#/state/preferences'
|
||||||
|
|
||||||
export interface LabelInfo {
|
export interface LabelInfo {
|
||||||
label: ComAtprotoLabelDefs.Label
|
label: ComAtprotoLabelDefs.Label
|
||||||
@@ -23,8 +23,8 @@ export interface LabelInfo {
|
|||||||
|
|
||||||
export function useLabelInfo(label: ComAtprotoLabelDefs.Label): LabelInfo {
|
export function useLabelInfo(label: ComAtprotoLabelDefs.Label): LabelInfo {
|
||||||
const {i18n} = useLingui()
|
const {i18n} = useLingui()
|
||||||
const globalLabelStrings = useGlobalLabelStrings()
|
|
||||||
const {labelDefs, labelers} = useLabelDefinitions()
|
const {labelDefs, labelers} = useLabelDefinitions()
|
||||||
|
const globalLabelStrings = useGlobalLabelStrings()
|
||||||
const def = getDefinition(labelDefs, label)
|
const def = getDefinition(labelDefs, label)
|
||||||
return {
|
return {
|
||||||
label,
|
label,
|
||||||
|
|||||||
@@ -2,9 +2,9 @@ import React from 'react'
|
|||||||
import {ModerationCause, ModerationCauseSource} from '@atproto/api'
|
import {ModerationCause, ModerationCauseSource} from '@atproto/api'
|
||||||
import {msg} from '@lingui/macro'
|
import {msg} from '@lingui/macro'
|
||||||
import {useLingui} from '@lingui/react'
|
import {useLingui} from '@lingui/react'
|
||||||
import {useGlobalLabelStrings} from './useGlobalLabelStrings'
|
|
||||||
import {useLabelDefinitions} from '#/state/queries/preferences'
|
|
||||||
import {getDefinition, getLabelStrings} from './useLabelInfo'
|
import {getDefinition, getLabelStrings} from './useLabelInfo'
|
||||||
|
import {useLabelDefinitions} from '#/state/preferences'
|
||||||
|
import {useGlobalLabelStrings} from './useGlobalLabelStrings'
|
||||||
|
|
||||||
import {Props as SVGIconProps} from '#/components/icons/common'
|
import {Props as SVGIconProps} from '#/components/icons/common'
|
||||||
import {Warning_Stroke2_Corner0_Rounded as Warning} from '#/components/icons/Warning'
|
import {Warning_Stroke2_Corner0_Rounded as Warning} from '#/components/icons/Warning'
|
||||||
@@ -24,8 +24,8 @@ export function useModerationCauseDescription(
|
|||||||
cause: ModerationCause | undefined,
|
cause: ModerationCause | undefined,
|
||||||
): ModerationCauseDescription {
|
): ModerationCauseDescription {
|
||||||
const {_, i18n} = useLingui()
|
const {_, i18n} = useLingui()
|
||||||
const globalLabelStrings = useGlobalLabelStrings()
|
|
||||||
const {labelDefs, labelers} = useLabelDefinitions()
|
const {labelDefs, labelers} = useLabelDefinitions()
|
||||||
|
const globalLabelStrings = useGlobalLabelStrings()
|
||||||
|
|
||||||
return React.useMemo(() => {
|
return React.useMemo(() => {
|
||||||
if (!cause) {
|
if (!cause) {
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ import {ScrollView} from '#/view/com/util/Views'
|
|||||||
|
|
||||||
import {
|
import {
|
||||||
UsePreferencesQueryResponse,
|
UsePreferencesQueryResponse,
|
||||||
useMyLabelers,
|
useMyLabelersQuery,
|
||||||
usePreferencesQuery,
|
usePreferencesQuery,
|
||||||
usePreferencesSetAdultContentMutation,
|
usePreferencesSetAdultContentMutation,
|
||||||
} from '#/state/queries/preferences'
|
} from '#/state/queries/preferences'
|
||||||
@@ -170,7 +170,7 @@ export function ModerationScreenInner({
|
|||||||
isLoading: isLabelersLoading,
|
isLoading: isLabelersLoading,
|
||||||
data: labelers,
|
data: labelers,
|
||||||
error: labelersError,
|
error: labelersError,
|
||||||
} = useMyLabelers()
|
} = useMyLabelersQuery()
|
||||||
|
|
||||||
useFocusEffect(
|
useFocusEffect(
|
||||||
React.useCallback(() => {
|
React.useCallback(() => {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import {Provider as AltTextRequiredProvider} from '../preferences/alt-text-requi
|
|||||||
import {Provider as HiddenPostsProvider} from '../preferences/hidden-posts'
|
import {Provider as HiddenPostsProvider} from '../preferences/hidden-posts'
|
||||||
import {Provider as ExternalEmbedsProvider} from './external-embeds-prefs'
|
import {Provider as ExternalEmbedsProvider} from './external-embeds-prefs'
|
||||||
import {Provider as InAppBrowserProvider} from './in-app-browser'
|
import {Provider as InAppBrowserProvider} from './in-app-browser'
|
||||||
|
import {Provider as LabelDefsProvider} from './label-defs'
|
||||||
|
|
||||||
export {useLanguagePrefs, useLanguagePrefsApi} from './languages'
|
export {useLanguagePrefs, useLanguagePrefsApi} from './languages'
|
||||||
export {
|
export {
|
||||||
@@ -15,6 +16,7 @@ export {
|
|||||||
useSetExternalEmbedPref,
|
useSetExternalEmbedPref,
|
||||||
} from './external-embeds-prefs'
|
} from './external-embeds-prefs'
|
||||||
export * from './hidden-posts'
|
export * from './hidden-posts'
|
||||||
|
export {useLabelDefinitions} from './label-defs'
|
||||||
|
|
||||||
export function Provider({children}: React.PropsWithChildren<{}>) {
|
export function Provider({children}: React.PropsWithChildren<{}>) {
|
||||||
return (
|
return (
|
||||||
@@ -22,7 +24,9 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
|
|||||||
<AltTextRequiredProvider>
|
<AltTextRequiredProvider>
|
||||||
<ExternalEmbedsProvider>
|
<ExternalEmbedsProvider>
|
||||||
<HiddenPostsProvider>
|
<HiddenPostsProvider>
|
||||||
<InAppBrowserProvider>{children}</InAppBrowserProvider>
|
<InAppBrowserProvider>
|
||||||
|
<LabelDefsProvider>{children}</LabelDefsProvider>
|
||||||
|
</InAppBrowserProvider>
|
||||||
</HiddenPostsProvider>
|
</HiddenPostsProvider>
|
||||||
</ExternalEmbedsProvider>
|
</ExternalEmbedsProvider>
|
||||||
</AltTextRequiredProvider>
|
</AltTextRequiredProvider>
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
import React from 'react'
|
||||||
|
import {InterpretedLabelValueDefinition, AppBskyLabelerDefs} from '@atproto/api'
|
||||||
|
import {useLabelDefinitionsQuery} from '../queries/preferences'
|
||||||
|
|
||||||
|
interface StateContext {
|
||||||
|
labelDefs: Record<string, InterpretedLabelValueDefinition[]>
|
||||||
|
labelers: AppBskyLabelerDefs.LabelerViewDetailed[]
|
||||||
|
}
|
||||||
|
|
||||||
|
const stateContext = React.createContext<StateContext>({
|
||||||
|
labelDefs: {},
|
||||||
|
labelers: [],
|
||||||
|
})
|
||||||
|
|
||||||
|
export function Provider({children}: React.PropsWithChildren<{}>) {
|
||||||
|
const {labelDefs, labelers} = useLabelDefinitionsQuery()
|
||||||
|
|
||||||
|
const state = {labelDefs, labelers}
|
||||||
|
|
||||||
|
return <stateContext.Provider value={state}>{children}</stateContext.Provider>
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useLabelDefinitions() {
|
||||||
|
return React.useContext(stateContext)
|
||||||
|
}
|
||||||
@@ -20,8 +20,7 @@ import {
|
|||||||
DEFAULT_LOGGED_OUT_PREFERENCES,
|
DEFAULT_LOGGED_OUT_PREFERENCES,
|
||||||
} from '#/state/queries/preferences/const'
|
} from '#/state/queries/preferences/const'
|
||||||
import {STALE} from '#/state/queries'
|
import {STALE} from '#/state/queries'
|
||||||
import {useLabelDefinitions} from '#/state/queries/preferences/moderation'
|
import {useHiddenPosts, useLabelDefinitions} from '#/state/preferences'
|
||||||
import {useHiddenPosts} from '#/state/preferences'
|
|
||||||
import {saveLabelers} from '#/state/session/agent-config'
|
import {saveLabelers} from '#/state/session/agent-config'
|
||||||
|
|
||||||
export * from '#/state/queries/preferences/types'
|
export * from '#/state/queries/preferences/types'
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ export const DEFAULT_LOGGED_OUT_LABEL_PREFERENCES: typeof DEFAULT_LABEL_SETTINGS
|
|||||||
Object.entries(DEFAULT_LABEL_SETTINGS).map(([key, _pref]) => [key, 'hide']),
|
Object.entries(DEFAULT_LABEL_SETTINGS).map(([key, _pref]) => [key, 'hide']),
|
||||||
)
|
)
|
||||||
|
|
||||||
export function useMyLabelers() {
|
export function useMyLabelersQuery() {
|
||||||
const prefs = usePreferencesQuery()
|
const prefs = usePreferencesQuery()
|
||||||
const dids = Array.from(
|
const dids = Array.from(
|
||||||
new Set(
|
new Set(
|
||||||
@@ -37,8 +37,8 @@ export function useMyLabelers() {
|
|||||||
}, [labelers, isLoading, error])
|
}, [labelers, isLoading, error])
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useLabelDefinitions() {
|
export function useLabelDefinitionsQuery() {
|
||||||
const labelers = useMyLabelers()
|
const labelers = useMyLabelersQuery()
|
||||||
return React.useMemo(() => {
|
return React.useMemo(() => {
|
||||||
return {
|
return {
|
||||||
labelDefs: Object.fromEntries(
|
labelDefs: Object.fromEntries(
|
||||||
|
|||||||
Reference in New Issue
Block a user