Remove dead code for optimistic updates (#2615)

zio/stable
dan 2024-01-25 20:19:25 +00:00 committed by GitHub
parent e4622e876e
commit 7f7b7492d6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 8 additions and 138 deletions

View File

@ -149,7 +149,6 @@ export function useProfileFollowMutationQueue(
if (shouldFollow) { if (shouldFollow) {
const {uri} = await followMutation.mutateAsync({ const {uri} = await followMutation.mutateAsync({
did, did,
skipOptimistic: true,
}) })
return uri return uri
} else { } else {
@ -157,7 +156,6 @@ export function useProfileFollowMutationQueue(
await unfollowMutation.mutateAsync({ await unfollowMutation.mutateAsync({
did, did,
followUri: prevFollowingUri, followUri: prevFollowingUri,
skipOptimistic: true,
}) })
} }
return undefined return undefined
@ -191,68 +189,22 @@ export function useProfileFollowMutationQueue(
} }
function useProfileFollowMutation() { function useProfileFollowMutation() {
return useMutation< return useMutation<{uri: string; cid: string}, Error, {did: string}>({
{uri: string; cid: string},
Error,
{did: string; skipOptimistic?: boolean}
>({
mutationFn: async ({did}) => { mutationFn: async ({did}) => {
return await getAgent().follow(did) return await getAgent().follow(did)
}, },
onMutate(variables) {
if (!variables.skipOptimistic) {
// optimistically update
updateProfileShadow(variables.did, {
followingUri: 'pending',
})
}
},
onSuccess(data, variables) { onSuccess(data, variables) {
if (!variables.skipOptimistic) { track('Profile:Follow', {username: variables.did})
// finalize
updateProfileShadow(variables.did, {
followingUri: data.uri,
})
track('Profile:Follow', {username: variables.did})
}
},
onError(error, variables) {
if (!variables.skipOptimistic) {
// revert the optimistic update
updateProfileShadow(variables.did, {
followingUri: undefined,
})
}
}, },
}) })
} }
function useProfileUnfollowMutation() { function useProfileUnfollowMutation() {
return useMutation< return useMutation<void, Error, {did: string; followUri: string}>({
void,
Error,
{did: string; followUri: string; skipOptimistic?: boolean}
>({
mutationFn: async ({followUri}) => { mutationFn: async ({followUri}) => {
track('Profile:Unfollow', {username: followUri}) track('Profile:Unfollow', {username: followUri})
return await getAgent().deleteFollow(followUri) return await getAgent().deleteFollow(followUri)
}, },
onMutate(variables) {
if (!variables.skipOptimistic) {
// optimistically update
updateProfileShadow(variables.did, {
followingUri: undefined,
})
}
},
onError(error, variables) {
if (!variables.skipOptimistic) {
// revert the optimistic update
updateProfileShadow(variables.did, {
followingUri: variables.followUri,
})
}
},
}) })
} }
@ -270,13 +222,11 @@ export function useProfileMuteMutationQueue(
if (shouldMute) { if (shouldMute) {
await muteMutation.mutateAsync({ await muteMutation.mutateAsync({
did, did,
skipOptimistic: true,
}) })
return true return true
} else { } else {
await unmuteMutation.mutateAsync({ await unmuteMutation.mutateAsync({
did, did,
skipOptimistic: true,
}) })
return false return false
} }
@ -308,57 +258,25 @@ export function useProfileMuteMutationQueue(
function useProfileMuteMutation() { function useProfileMuteMutation() {
const queryClient = useQueryClient() const queryClient = useQueryClient()
return useMutation<void, Error, {did: string; skipOptimistic?: boolean}>({ return useMutation<void, Error, {did: string}>({
mutationFn: async ({did}) => { mutationFn: async ({did}) => {
await getAgent().mute(did) await getAgent().mute(did)
}, },
onMutate(variables) {
if (!variables.skipOptimistic) {
// optimistically update
updateProfileShadow(variables.did, {
muted: true,
})
}
},
onSuccess() { onSuccess() {
queryClient.invalidateQueries({queryKey: RQKEY_MY_MUTED()}) queryClient.invalidateQueries({queryKey: RQKEY_MY_MUTED()})
}, },
onError(error, variables) {
if (!variables.skipOptimistic) {
// revert the optimistic update
updateProfileShadow(variables.did, {
muted: false,
})
}
},
}) })
} }
function useProfileUnmuteMutation() { function useProfileUnmuteMutation() {
const queryClient = useQueryClient() const queryClient = useQueryClient()
return useMutation<void, Error, {did: string; skipOptimistic?: boolean}>({ return useMutation<void, Error, {did: string}>({
mutationFn: async ({did}) => { mutationFn: async ({did}) => {
await getAgent().unmute(did) await getAgent().unmute(did)
}, },
onMutate(variables) {
if (!variables.skipOptimistic) {
// optimistically update
updateProfileShadow(variables.did, {
muted: false,
})
}
},
onSuccess() { onSuccess() {
queryClient.invalidateQueries({queryKey: RQKEY_MY_MUTED()}) queryClient.invalidateQueries({queryKey: RQKEY_MY_MUTED()})
}, },
onError(error, variables) {
if (!variables.skipOptimistic) {
// revert the optimistic update
updateProfileShadow(variables.did, {
muted: true,
})
}
},
}) })
} }
@ -376,7 +294,6 @@ export function useProfileBlockMutationQueue(
if (shouldFollow) { if (shouldFollow) {
const {uri} = await blockMutation.mutateAsync({ const {uri} = await blockMutation.mutateAsync({
did, did,
skipOptimistic: true,
}) })
return uri return uri
} else { } else {
@ -384,7 +301,6 @@ export function useProfileBlockMutationQueue(
await unblockMutation.mutateAsync({ await unblockMutation.mutateAsync({
did, did,
blockUri: prevBlockUri, blockUri: prevBlockUri,
skipOptimistic: true,
}) })
} }
return undefined return undefined
@ -420,11 +336,7 @@ export function useProfileBlockMutationQueue(
function useProfileBlockMutation() { function useProfileBlockMutation() {
const {currentAccount} = useSession() const {currentAccount} = useSession()
const queryClient = useQueryClient() const queryClient = useQueryClient()
return useMutation< return useMutation<{uri: string; cid: string}, Error, {did: string}>({
{uri: string; cid: string},
Error,
{did: string; skipOptimistic?: boolean}
>({
mutationFn: async ({did}) => { mutationFn: async ({did}) => {
if (!currentAccount) { if (!currentAccount) {
throw new Error('Not signed in') throw new Error('Not signed in')
@ -434,41 +346,15 @@ function useProfileBlockMutation() {
{subject: did, createdAt: new Date().toISOString()}, {subject: did, createdAt: new Date().toISOString()},
) )
}, },
onMutate(variables) { onSuccess() {
if (!variables.skipOptimistic) {
// optimistically update
updateProfileShadow(variables.did, {
blockingUri: 'pending',
})
}
},
onSuccess(data, variables) {
if (!variables.skipOptimistic) {
// finalize
updateProfileShadow(variables.did, {
blockingUri: data.uri,
})
}
queryClient.invalidateQueries({queryKey: RQKEY_MY_BLOCKED()}) queryClient.invalidateQueries({queryKey: RQKEY_MY_BLOCKED()})
}, },
onError(error, variables) {
if (!variables.skipOptimistic) {
// revert the optimistic update
updateProfileShadow(variables.did, {
blockingUri: undefined,
})
}
},
}) })
} }
function useProfileUnblockMutation() { function useProfileUnblockMutation() {
const {currentAccount} = useSession() const {currentAccount} = useSession()
return useMutation< return useMutation<void, Error, {did: string; blockUri: string}>({
void,
Error,
{did: string; blockUri: string; skipOptimistic?: boolean}
>({
mutationFn: async ({blockUri}) => { mutationFn: async ({blockUri}) => {
if (!currentAccount) { if (!currentAccount) {
throw new Error('Not signed in') throw new Error('Not signed in')
@ -479,22 +365,6 @@ function useProfileUnblockMutation() {
rkey, rkey,
}) })
}, },
onMutate(variables) {
if (!variables.skipOptimistic) {
// optimistically update
updateProfileShadow(variables.did, {
blockingUri: undefined,
})
}
},
onError(error, variables) {
if (!variables.skipOptimistic) {
// revert the optimistic update
updateProfileShadow(variables.did, {
blockingUri: variables.blockUri,
})
}
},
}) })
} }