Server-side thread mutes (#4518)
* update atproto/api * move thread mutes to server side * rm log * move muted threads provider to inside did key * use map instead of object
This commit is contained in:
parent
35e54e24a0
commit
5f5d845053
13 changed files with 223 additions and 220 deletions
|
|
@ -8,6 +8,7 @@ import {logEvent, LogEvents, toClout} from '#/lib/statsig/statsig'
|
|||
import {updatePostShadow} from '#/state/cache/post-shadow'
|
||||
import {Shadow} from '#/state/cache/types'
|
||||
import {useAgent, useSession} from '#/state/session'
|
||||
import {useIsThreadMuted, useSetThreadMute} from '../cache/thread-mutes'
|
||||
import {findProfileQueryData} from './profile'
|
||||
|
||||
const RQKEY_ROOT = 'post'
|
||||
|
|
@ -291,3 +292,72 @@ export function usePostDeleteMutation() {
|
|||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function useThreadMuteMutationQueue(
|
||||
post: Shadow<AppBskyFeedDefs.PostView>,
|
||||
rootUri: string,
|
||||
) {
|
||||
const threadMuteMutation = useThreadMuteMutation()
|
||||
const threadUnmuteMutation = useThreadUnmuteMutation()
|
||||
const isThreadMuted = useIsThreadMuted(rootUri, post.viewer?.threadMuted)
|
||||
const setThreadMute = useSetThreadMute()
|
||||
|
||||
const queueToggle = useToggleMutationQueue<boolean>({
|
||||
initialState: isThreadMuted,
|
||||
runMutation: async (_prev, shouldLike) => {
|
||||
if (shouldLike) {
|
||||
await threadMuteMutation.mutateAsync({
|
||||
uri: rootUri,
|
||||
})
|
||||
return true
|
||||
} else {
|
||||
await threadUnmuteMutation.mutateAsync({
|
||||
uri: rootUri,
|
||||
})
|
||||
return false
|
||||
}
|
||||
},
|
||||
onSuccess(finalIsMuted) {
|
||||
// finalize
|
||||
setThreadMute(rootUri, finalIsMuted)
|
||||
},
|
||||
})
|
||||
|
||||
const queueMuteThread = useCallback(() => {
|
||||
// optimistically update
|
||||
setThreadMute(rootUri, true)
|
||||
return queueToggle(true)
|
||||
}, [setThreadMute, rootUri, queueToggle])
|
||||
|
||||
const queueUnmuteThread = useCallback(() => {
|
||||
// optimistically update
|
||||
setThreadMute(rootUri, false)
|
||||
return queueToggle(false)
|
||||
}, [rootUri, setThreadMute, queueToggle])
|
||||
|
||||
return [isThreadMuted, queueMuteThread, queueUnmuteThread] as const
|
||||
}
|
||||
|
||||
function useThreadMuteMutation() {
|
||||
const agent = useAgent()
|
||||
return useMutation<
|
||||
{},
|
||||
Error,
|
||||
{uri: string} // the root post's uri
|
||||
>({
|
||||
mutationFn: ({uri}) => {
|
||||
logEvent('post:mute', {})
|
||||
return agent.api.app.bsky.graph.muteThread({root: uri})
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
function useThreadUnmuteMutation() {
|
||||
const agent = useAgent()
|
||||
return useMutation<{}, Error, {uri: string}>({
|
||||
mutationFn: ({uri}) => {
|
||||
logEvent('post:unmute', {})
|
||||
return agent.api.app.bsky.graph.unmuteThread({root: uri})
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue