-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
25 changed files
with
231 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.