Clean up embed replacement
This commit is contained in:
@@ -1,11 +1,16 @@
|
||||
import {AppBskyFeedPostgate, AtUri, BskyAgent} from '@atproto/api'
|
||||
import {
|
||||
AppBskyFeedDefs,
|
||||
AppBskyFeedPostgate,
|
||||
AtUri,
|
||||
BskyAgent,
|
||||
} from '@atproto/api'
|
||||
import {useMutation, useQueryClient} from '@tanstack/react-query'
|
||||
|
||||
import {networkRetry} from '#/lib/async/retry'
|
||||
import {updatePostShadow} from '#/state/cache/post-shadow'
|
||||
import {useGetPosts} from '#/state/queries/post'
|
||||
import {
|
||||
createEmbed,
|
||||
createMaybeDetachedQuoteEmbed,
|
||||
createPostgateRecord,
|
||||
mergePostgateRecords,
|
||||
POSTGATE_COLLECTION,
|
||||
@@ -109,47 +114,58 @@ export function useToggleQuoteDetachmentMutation() {
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({
|
||||
postUri,
|
||||
quotedUri,
|
||||
post,
|
||||
quoteUri,
|
||||
action,
|
||||
}: {
|
||||
postUri: string
|
||||
quotedUri: string
|
||||
post: AppBskyFeedDefs.PostView
|
||||
quoteUri: string
|
||||
action: 'detach' | 'reattach'
|
||||
}) => {
|
||||
await upsertPostgate({agent, postUri: quotedUri}, async prev => {
|
||||
await upsertPostgate({agent, postUri: quoteUri}, async prev => {
|
||||
if (prev) {
|
||||
if (action === 'detach') {
|
||||
return mergePostgateRecords(prev, {
|
||||
detachedQuotes: [postUri],
|
||||
detachedQuotes: [post.uri],
|
||||
})
|
||||
} else if (action === 'reattach') {
|
||||
return {
|
||||
...prev,
|
||||
detachedQuotes:
|
||||
prev.detachedQuotes?.filter(uri => uri !== postUri) || [],
|
||||
prev.detachedQuotes?.filter(uri => uri !== post.uri) || [],
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (action === 'detach') {
|
||||
return createPostgateRecord({
|
||||
post: quotedUri,
|
||||
detachedQuotes: [postUri],
|
||||
post: quoteUri,
|
||||
detachedQuotes: [post.uri],
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
async onSuccess(_data, {postUri, quotedUri, action}) {
|
||||
const [post, quotedPost] = await getPosts({uris: [postUri, quotedUri]})
|
||||
|
||||
updatePostShadow(queryClient, postUri, {
|
||||
embed: createEmbed({
|
||||
post,
|
||||
embeddedPost: quotedPost,
|
||||
detached: action === 'detach',
|
||||
}),
|
||||
})
|
||||
async onSuccess(_data, {post, quoteUri, action}) {
|
||||
if (action === 'detach') {
|
||||
updatePostShadow(queryClient, post.uri, {
|
||||
embed: createMaybeDetachedQuoteEmbed({
|
||||
post,
|
||||
quote: undefined,
|
||||
quoteUri,
|
||||
detached: true,
|
||||
}),
|
||||
})
|
||||
} else if (action === 'reattach') {
|
||||
const [quote] = await getPosts({uris: [quoteUri]})
|
||||
updatePostShadow(queryClient, post.uri, {
|
||||
embed: createMaybeDetachedQuoteEmbed({
|
||||
post,
|
||||
quote,
|
||||
quoteUri: undefined,
|
||||
detached: false,
|
||||
}),
|
||||
})
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -49,25 +49,56 @@ export function createEmbedViewRemovedRecord({uri}: {uri: string}) {
|
||||
}
|
||||
}
|
||||
|
||||
export function createEmbed({
|
||||
export function createMaybeDetachedQuoteEmbed({
|
||||
post,
|
||||
embeddedPost,
|
||||
quote,
|
||||
quoteUri,
|
||||
detached,
|
||||
}: {
|
||||
post: AppBskyFeedDefs.PostView
|
||||
embeddedPost: AppBskyFeedDefs.PostView
|
||||
detached: boolean
|
||||
}) {
|
||||
}:
|
||||
| {
|
||||
post: AppBskyFeedDefs.PostView
|
||||
quote: AppBskyFeedDefs.PostView
|
||||
quoteUri: undefined
|
||||
detached: false
|
||||
}
|
||||
| {
|
||||
post: AppBskyFeedDefs.PostView
|
||||
quote: undefined
|
||||
quoteUri: string
|
||||
detached: true
|
||||
}): AppBskyEmbedRecord.View | AppBskyEmbedRecordWithMedia.View | undefined {
|
||||
if (AppBskyEmbedRecord.isView(post.embed)) {
|
||||
if (detached) return createEmbedViewRemovedRecord({uri: embeddedPost.uri})
|
||||
return createEmbedRecordView({post: embeddedPost})
|
||||
if (detached) {
|
||||
return createEmbedViewRemovedRecord({uri: quoteUri})
|
||||
} else {
|
||||
return createEmbedRecordView({post: quote})
|
||||
}
|
||||
} else if (AppBskyEmbedRecordWithMedia.isView(post.embed)) {
|
||||
if (detached)
|
||||
if (detached) {
|
||||
return {
|
||||
...post.embed,
|
||||
record: createEmbedViewRemovedRecord({uri: embeddedPost.uri}),
|
||||
record: createEmbedViewRemovedRecord({uri: quoteUri}),
|
||||
}
|
||||
return createEmbedRecordWithmediaView({post, embeddedPost})
|
||||
} else {
|
||||
return createEmbedRecordWithMediaView({post, quote})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function createEmbedViewRecordFromPost(
|
||||
post: AppBskyFeedDefs.PostView,
|
||||
): AppBskyEmbedRecord.ViewRecord {
|
||||
return {
|
||||
$type: 'app.bsky.embed.record#viewRecord',
|
||||
uri: post.uri,
|
||||
cid: post.cid,
|
||||
author: post.author,
|
||||
value: post.record,
|
||||
labels: post.labels,
|
||||
replyCount: post.replyCount,
|
||||
repostCount: post.repostCount,
|
||||
likeCount: post.likeCount,
|
||||
indexedAt: post.indexedAt,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,30 +109,22 @@ export function createEmbedRecordView({
|
||||
}): AppBskyEmbedRecord.View {
|
||||
return {
|
||||
$type: 'app.bsky.embed.record#view',
|
||||
record: {
|
||||
$type: 'app.bsky.embed.record#viewRecord',
|
||||
...post,
|
||||
value: post.record,
|
||||
},
|
||||
record: createEmbedViewRecordFromPost(post),
|
||||
}
|
||||
}
|
||||
|
||||
export function createEmbedRecordWithmediaView({
|
||||
export function createEmbedRecordWithMediaView({
|
||||
post,
|
||||
embeddedPost,
|
||||
quote,
|
||||
}: {
|
||||
post: AppBskyFeedDefs.PostView
|
||||
embeddedPost: AppBskyFeedDefs.PostView
|
||||
quote: AppBskyFeedDefs.PostView
|
||||
}): AppBskyEmbedRecordWithMedia.View | undefined {
|
||||
if (!AppBskyEmbedRecordWithMedia.isView(post.embed)) return
|
||||
return {
|
||||
...(post.embed || {}),
|
||||
record: {
|
||||
record: {
|
||||
$type: 'app.bsky.embed.record#viewRecord',
|
||||
...embeddedPost,
|
||||
value: embeddedPost.record,
|
||||
},
|
||||
record: createEmbedViewRecordFromPost(quote),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -283,11 +283,11 @@ let PostDropdownBtn = ({
|
||||
|
||||
// TODO handle failure
|
||||
toggleQuoteDetachment({
|
||||
postUri,
|
||||
quotedUri: quoteEmbed.uri,
|
||||
post,
|
||||
quoteUri: quoteEmbed.uri,
|
||||
action: quoteEmbed.isDetached ? 'reattach' : 'detach',
|
||||
})
|
||||
}, [quoteEmbed, postUri, toggleQuoteDetachment])
|
||||
}, [quoteEmbed, post, toggleQuoteDetachment])
|
||||
|
||||
const canEmbed = isWeb && gtMobile && !hideInPWI
|
||||
|
||||
|
||||
Reference in New Issue
Block a user