Optimistic detachment
This commit is contained in:
@@ -1,4 +1,7 @@
|
|||||||
|
import React from 'react'
|
||||||
import {
|
import {
|
||||||
|
AppBskyEmbedRecord,
|
||||||
|
AppBskyEmbedRecordWithMedia,
|
||||||
AppBskyFeedDefs,
|
AppBskyFeedDefs,
|
||||||
AppBskyFeedPostgate,
|
AppBskyFeedPostgate,
|
||||||
AtUri,
|
AtUri,
|
||||||
@@ -166,6 +169,7 @@ export function useToggleQuoteDetachmentMutation() {
|
|||||||
const agent = useAgent()
|
const agent = useAgent()
|
||||||
const queryClient = useQueryClient()
|
const queryClient = useQueryClient()
|
||||||
const getPosts = useGetPosts()
|
const getPosts = useGetPosts()
|
||||||
|
const prevEmbed = React.useRef<AppBskyFeedDefs.PostView['embed']>()
|
||||||
|
|
||||||
return useMutation({
|
return useMutation({
|
||||||
mutationFn: async ({
|
mutationFn: async ({
|
||||||
@@ -177,6 +181,20 @@ export function useToggleQuoteDetachmentMutation() {
|
|||||||
quoteUri: string
|
quoteUri: string
|
||||||
action: 'detach' | 'reattach'
|
action: 'detach' | 'reattach'
|
||||||
}) => {
|
}) => {
|
||||||
|
// cache here since post shadow mutates original object
|
||||||
|
prevEmbed.current = post.embed
|
||||||
|
|
||||||
|
if (action === 'detach') {
|
||||||
|
updatePostShadow(queryClient, post.uri, {
|
||||||
|
embed: createMaybeDetachedQuoteEmbed({
|
||||||
|
post,
|
||||||
|
quote: undefined,
|
||||||
|
quoteUri,
|
||||||
|
detached: true,
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
await upsertPostgate({agent, postUri: quoteUri}, async prev => {
|
await upsertPostgate({agent, postUri: quoteUri}, async prev => {
|
||||||
if (prev) {
|
if (prev) {
|
||||||
if (action === 'detach') {
|
if (action === 'detach') {
|
||||||
@@ -202,16 +220,7 @@ export function useToggleQuoteDetachmentMutation() {
|
|||||||
})
|
})
|
||||||
},
|
},
|
||||||
async onSuccess(_data, {post, quoteUri, action}) {
|
async onSuccess(_data, {post, quoteUri, action}) {
|
||||||
if (action === 'detach') {
|
if (action === 'reattach') {
|
||||||
updatePostShadow(queryClient, post.uri, {
|
|
||||||
embed: createMaybeDetachedQuoteEmbed({
|
|
||||||
post,
|
|
||||||
quote: undefined,
|
|
||||||
quoteUri,
|
|
||||||
detached: true,
|
|
||||||
}),
|
|
||||||
})
|
|
||||||
} else if (action === 'reattach') {
|
|
||||||
try {
|
try {
|
||||||
const [quote] = await getPosts({uris: [quoteUri]})
|
const [quote] = await getPosts({uris: [quoteUri]})
|
||||||
updatePostShadow(queryClient, post.uri, {
|
updatePostShadow(queryClient, post.uri, {
|
||||||
@@ -230,6 +239,22 @@ export function useToggleQuoteDetachmentMutation() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
onError(_, {post, action}) {
|
||||||
|
if (action === 'detach' && prevEmbed.current) {
|
||||||
|
// detach failed, add the embed back
|
||||||
|
if (
|
||||||
|
AppBskyEmbedRecord.isView(prevEmbed.current) ||
|
||||||
|
AppBskyEmbedRecordWithMedia.isView(prevEmbed.current)
|
||||||
|
) {
|
||||||
|
updatePostShadow(queryClient, post.uri, {
|
||||||
|
embed: prevEmbed.current,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onSettled() {
|
||||||
|
prevEmbed.current = undefined
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user