Performance improvements: structural sharing & moderation opts context (#3785)
* Fix: correctly apply structural sharing to preferences object * Move moderation opts into a context * Fix import * Remove log * Pass userdid directly * Pass moderationPrefs directly
This commit is contained in:
parent
39807a8630
commit
31cb3e5422
25 changed files with 231 additions and 120 deletions
61
src/state/preferences/moderation-opts.tsx
Normal file
61
src/state/preferences/moderation-opts.tsx
Normal file
|
@ -0,0 +1,61 @@
|
|||
import React, {createContext, useContext, useMemo} from 'react'
|
||||
import {BSKY_LABELER_DID, ModerationOpts} from '@atproto/api'
|
||||
|
||||
import {useHiddenPosts, useLabelDefinitions} from '#/state/preferences'
|
||||
import {DEFAULT_LOGGED_OUT_LABEL_PREFERENCES} from '#/state/queries/preferences/moderation'
|
||||
import {useSession} from '#/state/session'
|
||||
import {usePreferencesQuery} from '../queries/preferences'
|
||||
|
||||
export const moderationOptsContext = createContext<ModerationOpts | undefined>(
|
||||
undefined,
|
||||
)
|
||||
|
||||
// used in the moderation state devtool
|
||||
export const moderationOptsOverrideContext = createContext<
|
||||
ModerationOpts | undefined
|
||||
>(undefined)
|
||||
|
||||
export function useModerationOpts() {
|
||||
return useContext(moderationOptsContext)
|
||||
}
|
||||
|
||||
export function Provider({children}: React.PropsWithChildren<{}>) {
|
||||
const override = useContext(moderationOptsOverrideContext)
|
||||
const {currentAccount} = useSession()
|
||||
const prefs = usePreferencesQuery()
|
||||
const {labelDefs} = useLabelDefinitions()
|
||||
const hiddenPosts = useHiddenPosts() // TODO move this into pds-stored prefs
|
||||
|
||||
const userDid = currentAccount?.did
|
||||
const moderationPrefs = prefs.data?.moderationPrefs
|
||||
const value = useMemo<ModerationOpts | undefined>(() => {
|
||||
if (override) {
|
||||
return override
|
||||
}
|
||||
if (!moderationPrefs) {
|
||||
return undefined
|
||||
}
|
||||
return {
|
||||
userDid,
|
||||
prefs: {
|
||||
...moderationPrefs,
|
||||
labelers: moderationPrefs.labelers.length
|
||||
? moderationPrefs.labelers
|
||||
: [
|
||||
{
|
||||
did: BSKY_LABELER_DID,
|
||||
labels: DEFAULT_LOGGED_OUT_LABEL_PREFERENCES,
|
||||
},
|
||||
],
|
||||
hiddenPosts: hiddenPosts || [],
|
||||
},
|
||||
labelDefs,
|
||||
}
|
||||
}, [override, userDid, labelDefs, moderationPrefs, hiddenPosts])
|
||||
|
||||
return (
|
||||
<moderationOptsContext.Provider value={value}>
|
||||
{children}
|
||||
</moderationOptsContext.Provider>
|
||||
)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue