From 1f1887730a210c57c1e5a0eb0f47c42c42cf1b4b Mon Sep 17 00:00:00 2001 From: Hailey Date: Mon, 19 Aug 2024 13:30:25 -0700 Subject: [PATCH] update quotes count after quoting --- src/state/queries/post-quotes.ts | 25 ++++++++++++++++++++- src/view/com/post-thread/PostThreadItem.tsx | 21 ++++++++++++++++- src/view/com/util/post-ctrls/PostCtrls.tsx | 10 ++++++--- 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/src/state/queries/post-quotes.ts b/src/state/queries/post-quotes.ts index 817bba26b2..5d7a4fec1f 100644 --- a/src/state/queries/post-quotes.ts +++ b/src/state/queries/post-quotes.ts @@ -1,4 +1,9 @@ -import {AppBskyActorDefs, AppBskyFeedGetQuotes} from '@atproto/api' +import { + AppBskyActorDefs, + AppBskyFeedGetPostThread, + AppBskyFeedGetQuotes, + BskyAgent, +} from '@atproto/api' import { InfiniteData, QueryClient, @@ -7,6 +12,7 @@ import { } from '@tanstack/react-query' import {useAgent} from '#/state/session' +import {until} from 'lib/async/until' import {getEmbeddedPost} from './util' const PAGE_SIZE = 30 @@ -66,3 +72,20 @@ export function* findAllProfilesInQueryData( } } } + +export async function whenAppViewReady( + agent: BskyAgent, + uri: string, + fn: (res: AppBskyFeedGetPostThread.Response) => boolean, +) { + await until( + 3, // 5 tries + 1e3, // 1s delay between tries + fn, + () => + agent.app.bsky.feed.getPostThread({ + uri, + depth: 0, + }), + ) +} diff --git a/src/view/com/post-thread/PostThreadItem.tsx b/src/view/com/post-thread/PostThreadItem.tsx index 3b3f54aec8..5e09305575 100644 --- a/src/view/com/post-thread/PostThreadItem.tsx +++ b/src/view/com/post-thread/PostThreadItem.tsx @@ -14,6 +14,7 @@ import {useLingui} from '@lingui/react' import {POST_TOMBSTONE, Shadow, usePostShadow} from '#/state/cache/post-shadow' import {useLanguagePrefs} from '#/state/preferences' import {useOpenLink} from '#/state/preferences/in-app-browser' +import {whenAppViewReady as whenAppViewReadyQuote} from '#/state/queries/post-quotes' import {ThreadPost} from '#/state/queries/post-thread' import {useComposerControls} from '#/state/shell/composer' import {MAX_POST_LINES} from 'lib/constants' @@ -26,7 +27,7 @@ import {countLines} from 'lib/strings/helpers' import {niceDate} from 'lib/strings/time' import {s} from 'lib/styles' import {isWeb} from 'platform/detection' -import {useSession} from 'state/session' +import {useAgent, useSession} from 'state/session' import {PostThreadFollowBtn} from 'view/com/post-thread/PostThreadFollowBtn' import {atoms as a} from '#/alf' import {RichText} from '#/components/RichText' @@ -174,6 +175,7 @@ let PostThreadItemLoaded = ({ }): React.ReactNode => { const pal = usePalette('default') const {_} = useLingui() + const agent = useAgent() const langPrefs = useLanguagePrefs() const {openComposer} = useComposerControls() const [limitLines, setLimitLines] = React.useState( @@ -232,6 +234,21 @@ let PostThreadItemLoaded = ({ }) }, [openComposer, post, record, onPostReply, moderation]) + const onPostQuote = React.useCallback(() => { + // We need to wait until the quote count is updated before we can show the new total. We'll wait for the appview + // here. Worst case scenario - someone deletes a quote right as you make yours - this will fail after 3 retries + whenAppViewReadyQuote(agent, post.uri, res => { + const thread = res.data.thread + if (AppBskyFeedDefs.isThreadViewPost(thread)) { + if (thread.post.quoteCount !== post.quoteCount) { + onPostReply(undefined) + return true + } + } + return false + }) + }, [agent, post.uri, post.quoteCount, onPostReply]) + const onPressShowMore = React.useCallback(() => { setLimitLines(false) }, [setLimitLines]) @@ -418,6 +435,7 @@ let PostThreadItemLoaded = ({ record={record} richText={richText} onPressReply={onPressReply} + onPostQuote={onPostQuote} logContext="PostThreadItem" /> @@ -569,6 +587,7 @@ let PostThreadItemLoaded = ({ record={record} richText={richText} onPressReply={onPressReply} + onPostQuote={onPostQuote} logContext="PostThreadItem" /> diff --git a/src/view/com/util/post-ctrls/PostCtrls.tsx b/src/view/com/util/post-ctrls/PostCtrls.tsx index 478b8f0f89..9ada7c6465 100644 --- a/src/view/com/util/post-ctrls/PostCtrls.tsx +++ b/src/view/com/util/post-ctrls/PostCtrls.tsx @@ -58,6 +58,7 @@ let PostCtrls = ({ feedContext, style, onPressReply, + onPostQuote, logContext, }: { big?: boolean @@ -67,6 +68,7 @@ let PostCtrls = ({ feedContext?: string | undefined style?: StyleProp onPressReply: () => void + onPostQuote?: () => void logContext: 'FeedItem' | 'PostThreadItem' | 'Post' }): React.ReactNode => { const t = useTheme() @@ -169,16 +171,18 @@ let PostCtrls = ({ author: post.author, indexedAt: post.indexedAt, }, + onPost: onPostQuote, }) }, [ - openComposer, + sendInteraction, post.uri, post.cid, post.author, post.indexedAt, - record.text, - sendInteraction, feedContext, + openComposer, + record.text, + onPostQuote, ]) const onShare = useCallback(() => {