Memoize the memoizers

This commit is contained in:
Eric Bailey
2024-03-14 18:24:04 -05:00
parent 7f178ffb88
commit 6a7f904457
2 changed files with 120 additions and 107 deletions
@@ -1,3 +1,4 @@
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'
@@ -25,6 +26,8 @@ export function useModerationCauseDescription(
const {_, i18n} = useLingui() const {_, i18n} = useLingui()
const globalLabelStrings = useGlobalLabelStrings() const globalLabelStrings = useGlobalLabelStrings()
const {labelDefs, labelers} = useLabelDefinitions() const {labelDefs, labelers} = useLabelDefinitions()
return React.useMemo(() => {
if (!cause) { if (!cause) {
return { return {
icon: Warning, icon: Warning,
@@ -117,7 +120,9 @@ export function useModerationCauseDescription(
description: strings.description, description: strings.description,
source: source:
labeler?.creator.displayName || labeler?.creator.displayName ||
(labeler?.creator.handle ? '@' + labeler?.creator.handle : undefined) || (labeler?.creator.handle
? '@' + labeler?.creator.handle
: undefined) ||
cause.label.src, cause.label.src,
sourceType: cause.source.type, sourceType: cause.source.type,
} }
@@ -128,4 +133,5 @@ export function useModerationCauseDescription(
name: '', name: '',
description: ``, description: ``,
} }
}, [labelDefs, labelers, globalLabelStrings, cause, _, i18n.locale])
} }
+9 -2
View File
@@ -1,3 +1,4 @@
import React from 'react'
import { import {
DEFAULT_LABEL_SETTINGS, DEFAULT_LABEL_SETTINGS,
BskyAgent, BskyAgent,
@@ -25,15 +26,20 @@ export function useMyLabelers() {
), ),
) )
const labelers = useLabelersDetailedInfoQuery({dids}) const labelers = useLabelersDetailedInfoQuery({dids})
const isLoading = prefs.isLoading || labelers.isLoading
const error = prefs.error || labelers.error
return React.useMemo(() => {
return { return {
isLoading: prefs.isLoading || labelers.isLoading, isLoading,
error: prefs.error || labelers.error, error,
data: labelers.data, data: labelers.data,
} }
}, [labelers, isLoading, error])
} }
export function useLabelDefinitions() { export function useLabelDefinitions() {
const labelers = useMyLabelers() const labelers = useMyLabelers()
return React.useMemo(() => {
return { return {
labelDefs: Object.fromEntries( labelDefs: Object.fromEntries(
(labelers.data || []).map(labeler => [ (labelers.data || []).map(labeler => [
@@ -43,4 +49,5 @@ export function useLabelDefinitions() {
), ),
labelers: labelers.data || [], labelers: labelers.data || [],
} }
}, [labelers])
} }