From a16f5ecbffa839feb0fa144f87b4033c19a1d7a7 Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Wed, 21 May 2025 17:15:26 -0500 Subject: [PATCH] Clean up onPostSuccess --- src/state/shell/composer/index.tsx | 14 ++++++++++ src/view/com/composer/Composer.tsx | 30 ++++++++++++++------- src/view/com/post-thread/PostThreadItem.tsx | 7 +++++ src/view/screens/PostThread.tsx | 18 +++++-------- src/view/shell/Composer.ios.tsx | 1 + src/view/shell/Composer.tsx | 1 + src/view/shell/Composer.web.tsx | 1 + 7 files changed, 50 insertions(+), 22 deletions(-) diff --git a/src/state/shell/composer/index.tsx b/src/state/shell/composer/index.tsx index ad07333beb..bdbf873e5e 100644 --- a/src/state/shell/composer/index.tsx +++ b/src/state/shell/composer/index.tsx @@ -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 diff --git a/src/view/com/composer/Composer.tsx b/src/view/com/composer/Composer.tsx index 49c87dd4fd..ebc4d7ce0f 100644 --- a/src/view/com/composer/Composer.tsx +++ b/src/view/com/composer/Composer.tsx @@ -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, diff --git a/src/view/com/post-thread/PostThreadItem.tsx b/src/view/com/post-thread/PostThreadItem.tsx index 8b39072ba6..d13da1e138 100644 --- a/src/view/com/post-thread/PostThreadItem.tsx +++ b/src/view/com/post-thread/PostThreadItem.tsx @@ -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, }) } diff --git a/src/view/screens/PostThread.tsx b/src/view/screens/PostThread.tsx index 4f82af8d2a..95232e82d5 100644 --- a/src/view/screens/PostThread.tsx +++ b/src/view/screens/PostThread.tsx @@ -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 /> diff --git a/src/view/shell/Composer.ios.tsx b/src/view/shell/Composer.ios.tsx index 8b53f40416..393b8f80e6 100644 --- a/src/view/shell/Composer.ios.tsx +++ b/src/view/shell/Composer.ios.tsx @@ -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} diff --git a/src/view/shell/Composer.tsx b/src/view/shell/Composer.tsx index e40c3528b9..a17de6163d 100644 --- a/src/view/shell/Composer.tsx +++ b/src/view/shell/Composer.tsx @@ -49,6 +49,7 @@ export function Composer({winHeight}: {winHeight: number}) {