Clean up onPostSuccess

This commit is contained in:
Eric Bailey
2025-05-21 17:15:26 -05:00
parent 6dd71ebae2
commit a16f5ecbff
7 changed files with 50 additions and 22 deletions
+14
View File
@@ -2,6 +2,7 @@ import React from 'react'
import {
type AppBskyActorDefs,
type AppBskyFeedDefs,
type AppBskyUnspeccedGetPostThreadV2,
type ModerationDecision,
} from '@atproto/api'
import {msg} from '@lingui/macro'
@@ -24,9 +25,22 @@ export interface ComposerOptsPostRef {
moderation?: ModerationDecision
}
export type OnPostSuccessData =
| {
type: 'post'
posts: AppBskyUnspeccedGetPostThreadV2.ThreadItem[]
}
| {
type: 'reply'
parent: AppBskyUnspeccedGetPostThreadV2.ThreadItem
replies: AppBskyUnspeccedGetPostThreadV2.ThreadItem[]
}
| undefined
export interface ComposerOpts {
replyTo?: ComposerOptsPostRef
onPost?: (postUri: string | undefined) => void
onPostSuccess?: (data: OnPostSuccessData) => void
quote?: AppBskyFeedDefs.PostView
mention?: string // handle of user to mention
openEmojiPicker?: (pos: EmojiPickerPosition | undefined) => void
+20 -10
View File
@@ -45,7 +45,6 @@ import {type ImagePickerAsset} from 'expo-image-picker'
import {
AppBskyFeedDefs,
type AppBskyFeedGetPostThread,
type AppBskyUnspeccedGetPostThreadV2,
type BskyAgent,
type RichText,
} from '@atproto/api'
@@ -89,7 +88,7 @@ import {useProfileQuery} from '#/state/queries/profile'
import {type Gif} from '#/state/queries/tenor'
import {useAgent, useSession} from '#/state/session'
import {useComposerControls} from '#/state/shell/composer'
import {type ComposerOpts} from '#/state/shell/composer'
import {type ComposerOpts, type OnPostSuccessData} from '#/state/shell/composer'
import {CharProgress} from '#/view/com/composer/char-progress/CharProgress'
import {ComposerReplyTo} from '#/view/com/composer/ComposerReplyTo'
import {
@@ -154,6 +153,7 @@ type Props = ComposerOpts
export const ComposePost = ({
replyTo,
onPost,
onPostSuccess,
quote: initQuote,
mention: initMention,
openEmojiPicker,
@@ -391,7 +391,7 @@ export const ComposePost = ({
setIsPublishing(true)
let postUri: string | undefined
let posts: AppBskyUnspeccedGetPostThreadV2.OutputSchema['thread'] = []
let postSuccessData: OnPostSuccessData
try {
postUri = (
await apilib.post(agent, queryClient, {
@@ -403,7 +403,7 @@ export const ComposePost = ({
).uris[0]
try {
if (postUri) {
posts = await retry(
const [maybeParent, maybeReply, ...posts] = await retry(
5,
_e => true,
async () => {
@@ -424,12 +424,19 @@ export const ComposePost = ({
},
1e3,
)
if (maybeReply && maybeReply.uri === postUri) {
postSuccessData = {
type: 'reply',
parent: maybeParent,
replies: [maybeReply, ...posts],
}
} else {
postSuccessData = {
type: 'post',
posts: [maybeParent, maybeReply, ...posts].filter(Boolean),
}
}
}
await whenAppViewReady(agent, postUri, res => {
const postedThread = res?.data?.thread
return AppBskyFeedDefs.isThreadViewPost(postedThread)
})
} catch (waitErr: any) {
logger.error(waitErr, {
message: `Waiting for app view failed`,
@@ -492,12 +499,14 @@ export const ComposePost = ({
quotedThread.post.quoteCount !== initQuote.quoteCount
) {
onPost?.(postUri)
onPostSuccess?.(postSuccessData)
return true
}
return false
})
} else {
onPost?.(postUri, posts)
onPost?.(postUri)
onPostSuccess?.(postSuccessData)
}
onClose()
Toast.show(
@@ -516,6 +525,7 @@ export const ComposePost = ({
langPrefs.postLanguage,
onClose,
onPost,
onPostSuccess,
initQuote,
replyTo,
setLangPrefs,
@@ -39,6 +39,7 @@ import {FeedFeedbackProvider, useFeedFeedback} from '#/state/feed-feedback'
import {useLanguagePrefs} from '#/state/preferences'
import {type ThreadPost} from '#/state/queries/post-thread'
import {useSession} from '#/state/session'
import {type OnPostSuccessData} from '#/state/shell/composer'
import {useMergedThreadgateHiddenReplies} from '#/state/threadgate-hidden-replies'
import {useUnstablePostSource} from '#/state/unstable-post-source'
import {PostThreadFollowBtn} from '#/view/com/post-thread/PostThreadFollowBtn'
@@ -85,6 +86,7 @@ export function PostThreadItem({
hasPrecedingItem,
overrideBlur,
onPostReply,
onPostSuccess,
hideTopBorder,
threadgateRecord,
}: {
@@ -102,6 +104,7 @@ export function PostThreadItem({
hasPrecedingItem: boolean
overrideBlur: boolean
onPostReply: (postUri: string | undefined) => void
onPostSuccess?: (data: OnPostSuccessData) => void
hideTopBorder?: boolean
threadgateRecord?: AppBskyFeedThreadgate.Record
}) {
@@ -137,6 +140,7 @@ export function PostThreadItem({
hasPrecedingItem={hasPrecedingItem}
overrideBlur={overrideBlur}
onPostReply={onPostReply}
onPostSuccess={onPostSuccess}
hideTopBorder={hideTopBorder}
threadgateRecord={threadgateRecord}
/>
@@ -182,6 +186,7 @@ let PostThreadItemLoaded = ({
hasPrecedingItem,
overrideBlur,
onPostReply,
onPostSuccess,
hideTopBorder,
threadgateRecord,
}: {
@@ -200,6 +205,7 @@ let PostThreadItemLoaded = ({
hasPrecedingItem: boolean
overrideBlur: boolean
onPostReply: (postUri: string | undefined) => void
onPostSuccess?: (data: OnPostSuccessData) => void
hideTopBorder?: boolean
threadgateRecord?: AppBskyFeedThreadgate.Record
}): React.ReactNode => {
@@ -294,6 +300,7 @@ let PostThreadItemLoaded = ({
moderation,
},
onPost: onPostReply,
onPostSuccess: onPostSuccess,
})
}
+6 -12
View File
@@ -1,6 +1,5 @@
import {useCallback, useMemo, useRef, useState} from 'react'
import {useWindowDimensions, View} from 'react-native'
import {type AppBskyUnspeccedGetPostThreadV2} from '@atproto/api'
import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react'
import {useFocusEffect} from '@react-navigation/native'
@@ -26,6 +25,7 @@ import {
type Slice,
usePostThread,
} from '#/state/queries/usePostThread'
import {type OnPostSuccessData} from '#/state/shell/composer'
import {PostThreadComposePrompt} from '#/view/com/post-thread/PostThreadComposePrompt'
import {PostThreadItem} from '#/view/com/post-thread/PostThreadItem'
import {PostThreadShowHiddenReplies} from '#/view/com/post-thread/PostThreadShowHiddenReplies'
@@ -155,13 +155,9 @@ export function Inner({uri}: {uri: string | undefined}) {
},
})
const optimisticOnPostReply = (
_: any,
posts: AppBskyUnspeccedGetPostThreadV2.ThreadItem[],
) => {
if (posts.length) {
const parent = posts.at(0)
const replies = posts.slice(1)
const optimisticOnPostReply = (data: OnPostSuccessData) => {
if (data && data.type === 'reply') {
const {parent, replies} = data
if (parent && replies.length) {
insertReplies(parent, replies)
}
@@ -186,8 +182,7 @@ export function Inner({uri}: {uri: string | undefined}) {
embed: post.embed,
moderation: anchorPost.moderation,
},
// @ts-expect-error TODO
onPost: optimisticOnPostReply,
onPostSuccess: optimisticOnPostReply,
})
}
@@ -248,8 +243,7 @@ export function Inner({uri}: {uri: string | undefined}) {
overrideBlur={
shownHiddenReplyKinds.has(HiddenReplyKind.Muted) && item.depth > 0
}
// @ts-expect-error TODO
onPostReply={optimisticOnPostReply}
onPostSuccess={optimisticOnPostReply}
hideTopBorder={index === 0} // && !item.isParentLoading} // TODO
/>
</View>
+1
View File
@@ -37,6 +37,7 @@ export function Composer({}: {winHeight: number}) {
cancelRef={ref}
replyTo={state?.replyTo}
onPost={state?.onPost}
onPostSuccess={state?.onPostSuccess}
quote={state?.quote}
mention={state?.mention}
text={state?.text}
+1
View File
@@ -49,6 +49,7 @@ export function Composer({winHeight}: {winHeight: number}) {
<ComposePost
replyTo={state.replyTo}
onPost={state.onPost}
onPostSuccess={state.onPostSuccess}
quote={state.quote}
mention={state.mention}
text={state.text}
+1
View File
@@ -105,6 +105,7 @@ function Inner({state}: {state: ComposerOpts}) {
replyTo={state.replyTo}
quote={state.quote}
onPost={state.onPost}
onPostSuccess={state.onPostSuccess}
mention={state.mention}
openEmojiPicker={onOpenPicker}
text={state.text}