-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor profile screen to use new pager and react-query #1870
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import {useEffect, useState, useCallback, useRef} from 'react' | ||
import EventEmitter from 'eventemitter3' | ||
import {AppBskyActorDefs} from '@atproto/api' | ||
|
||
const emitter = new EventEmitter() | ||
|
||
export interface ProfileShadow { | ||
followingUri: string | undefined | ||
muted: boolean | undefined | ||
blockingUri: string | undefined | ||
} | ||
|
||
interface CacheEntry { | ||
ts: number | ||
value: ProfileShadow | ||
} | ||
|
||
type ProfileView = | ||
| AppBskyActorDefs.ProfileView | ||
| AppBskyActorDefs.ProfileViewBasic | ||
| AppBskyActorDefs.ProfileViewDetailed | ||
|
||
export function useProfileShadow<T extends ProfileView>( | ||
profile: T, | ||
ifAfterTS: number, | ||
): T { | ||
const [state, setState] = useState<CacheEntry>({ | ||
ts: Date.now(), | ||
value: fromProfile(profile), | ||
}) | ||
const firstRun = useRef(true) | ||
|
||
const onUpdate = useCallback( | ||
(value: Partial<ProfileShadow>) => { | ||
setState(s => ({ts: Date.now(), value: {...s.value, ...value}})) | ||
}, | ||
[setState], | ||
) | ||
|
||
// react to shadow updates | ||
useEffect(() => { | ||
emitter.addListener(profile.did, onUpdate) | ||
return () => { | ||
emitter.removeListener(profile.did, onUpdate) | ||
} | ||
}, [profile.did, onUpdate]) | ||
|
||
// react to profile updates | ||
useEffect(() => { | ||
// dont fire on first run to avoid needless re-renders | ||
if (!firstRun.current) { | ||
setState({ts: Date.now(), value: fromProfile(profile)}) | ||
} | ||
firstRun.current = false | ||
}, [profile]) | ||
|
||
return state.ts > ifAfterTS ? mergeShadow(profile, state.value) : profile | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably useMemo this. |
||
} | ||
|
||
export function updateProfileShadow( | ||
uri: string, | ||
value: Partial<ProfileShadow>, | ||
) { | ||
emitter.emit(uri, value) | ||
} | ||
|
||
function fromProfile(profile: ProfileView): ProfileShadow { | ||
return { | ||
followingUri: profile.viewer?.following, | ||
muted: profile.viewer?.muted, | ||
blockingUri: profile.viewer?.blocking, | ||
} | ||
} | ||
|
||
function mergeShadow<T extends ProfileView>( | ||
profile: T, | ||
shadow: ProfileShadow, | ||
): T { | ||
return { | ||
...profile, | ||
viewer: { | ||
...(profile.viewer || {}), | ||
following: shadow.followingUri, | ||
muted: shadow.muted, | ||
blocking: shadow.blockingUri, | ||
}, | ||
} | ||
} |
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 |
---|---|---|
@@ -1,13 +1,169 @@ | ||
import {useQuery} from '@tanstack/react-query' | ||
import {AtUri} from '@atproto/api' | ||
import {useQuery, useMutation} from '@tanstack/react-query' | ||
import {useSession} from '../session' | ||
import {updateProfileShadow} from '../cache/profile-shadow' | ||
|
||
import {PUBLIC_BSKY_AGENT} from '#/state/queries' | ||
export const RQKEY = (did: string) => ['profile', did] | ||
|
||
export function useProfileQuery({did}: {did: string}) { | ||
export function useProfileQuery({did}: {did: string | undefined}) { | ||
const {agent} = useSession() | ||
return useQuery({ | ||
queryKey: ['getProfile', did], | ||
queryKey: RQKEY(did), | ||
queryFn: async () => { | ||
const res = await PUBLIC_BSKY_AGENT.getProfile({actor: did}) | ||
const res = await agent.getProfile({actor: did || ''}) | ||
return res.data | ||
}, | ||
enabled: !!did, | ||
}) | ||
} | ||
|
||
export function useProfileFollowMutation() { | ||
const {agent} = useSession() | ||
return useMutation<{uri: string; cid: string}, Error, {did: string}>({ | ||
mutationFn: async ({did}) => { | ||
return await agent.follow(did) | ||
}, | ||
onMutate(variables) { | ||
// optimstically update | ||
updateProfileShadow(variables.did, { | ||
followingUri: 'pending', | ||
}) | ||
}, | ||
onSuccess(data, variables) { | ||
// finalize | ||
updateProfileShadow(variables.did, { | ||
followingUri: data.uri, | ||
}) | ||
}, | ||
onError(error, variables) { | ||
// revert the optimistic update | ||
updateProfileShadow(variables.did, { | ||
followingUri: undefined, | ||
}) | ||
}, | ||
}) | ||
} | ||
|
||
export function useProfileUnfollowMutation() { | ||
const {agent} = useSession() | ||
return useMutation<void, Error, {did: string; followUri: string}>({ | ||
mutationFn: async ({followUri}) => { | ||
return await agent.deleteFollow(followUri) | ||
}, | ||
onMutate(variables) { | ||
// optimstically update | ||
updateProfileShadow(variables.did, { | ||
followingUri: undefined, | ||
}) | ||
}, | ||
onError(error, variables) { | ||
// revert the optimistic update | ||
updateProfileShadow(variables.did, { | ||
followingUri: variables.followUri, | ||
}) | ||
}, | ||
}) | ||
} | ||
|
||
export function useProfileMuteMutation() { | ||
const {agent} = useSession() | ||
return useMutation<void, Error, {did: string}>({ | ||
mutationFn: async ({did}) => { | ||
await agent.mute(did) | ||
}, | ||
onMutate(variables) { | ||
// optimstically update | ||
updateProfileShadow(variables.did, { | ||
muted: true, | ||
}) | ||
}, | ||
onError(error, variables) { | ||
// revert the optimistic update | ||
updateProfileShadow(variables.did, { | ||
muted: false, | ||
}) | ||
}, | ||
}) | ||
} | ||
|
||
export function useProfileUnmuteMutation() { | ||
const {agent} = useSession() | ||
return useMutation<void, Error, {did: string}>({ | ||
mutationFn: async ({did}) => { | ||
await agent.unmute(did) | ||
}, | ||
onMutate(variables) { | ||
// optimstically update | ||
updateProfileShadow(variables.did, { | ||
muted: false, | ||
}) | ||
}, | ||
onError(error, variables) { | ||
// revert the optimistic update | ||
updateProfileShadow(variables.did, { | ||
muted: true, | ||
}) | ||
}, | ||
}) | ||
} | ||
|
||
export function useProfileBlockMutation() { | ||
const {agent, currentAccount} = useSession() | ||
return useMutation<{uri: string; cid: string}, Error, {did: string}>({ | ||
mutationFn: async ({did}) => { | ||
if (!currentAccount) { | ||
throw new Error('Not signed in') | ||
} | ||
return await agent.app.bsky.graph.block.create( | ||
{repo: currentAccount.did}, | ||
{subject: did, createdAt: new Date().toISOString()}, | ||
) | ||
}, | ||
onMutate(variables) { | ||
// optimstically update | ||
updateProfileShadow(variables.did, { | ||
blockingUri: 'pending', | ||
}) | ||
}, | ||
onSuccess(data, variables) { | ||
// finalize | ||
updateProfileShadow(variables.did, { | ||
blockingUri: data.uri, | ||
}) | ||
}, | ||
onError(error, variables) { | ||
// revert the optimistic update | ||
updateProfileShadow(variables.did, { | ||
blockingUri: undefined, | ||
}) | ||
}, | ||
}) | ||
} | ||
|
||
export function useProfileUnblockMutation() { | ||
const {agent, currentAccount} = useSession() | ||
return useMutation<void, Error, {did: string; blockUri: string}>({ | ||
mutationFn: async ({blockUri}) => { | ||
if (!currentAccount) { | ||
throw new Error('Not signed in') | ||
} | ||
const {rkey} = new AtUri(blockUri) | ||
await agent.app.bsky.graph.block.delete({ | ||
repo: currentAccount.did, | ||
rkey, | ||
}) | ||
}, | ||
onMutate(variables) { | ||
// optimstically update | ||
updateProfileShadow(variables.did, { | ||
blockingUri: undefined, | ||
}) | ||
}, | ||
onError(error, variables) { | ||
// revert the optimistic update | ||
updateProfileShadow(variables.did, { | ||
blockingUri: variables.blockUri, | ||
}) | ||
}, | ||
}) | ||
} |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you describe how you want this to work? Generally we shouldn't be setting state from effects. If there's a need to adjust state, the canonical way is to do this. But I think I'm not quite following the logic in general so I want to understand the intended behavior.