merge main
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
import {AppBskyGraphGetKnownFollowers} from '@atproto/api'
|
||||
import {InfiniteData, QueryKey, useInfiniteQuery} from '@tanstack/react-query'
|
||||
|
||||
import {useAgent} from '#/state/session'
|
||||
|
||||
const PAGE_SIZE = 50
|
||||
type RQPageParam = string | undefined
|
||||
|
||||
const RQKEY_ROOT = 'profile-known-followers'
|
||||
export const RQKEY = (did: string) => [RQKEY_ROOT, did]
|
||||
|
||||
export function useProfileKnownFollowersQuery(did: string | undefined) {
|
||||
const agent = useAgent()
|
||||
return useInfiniteQuery<
|
||||
AppBskyGraphGetKnownFollowers.OutputSchema,
|
||||
Error,
|
||||
InfiniteData<AppBskyGraphGetKnownFollowers.OutputSchema>,
|
||||
QueryKey,
|
||||
RQPageParam
|
||||
>({
|
||||
queryKey: RQKEY(did || ''),
|
||||
async queryFn({pageParam}: {pageParam: RQPageParam}) {
|
||||
const res = await agent.app.bsky.graph.getKnownFollowers({
|
||||
actor: did!,
|
||||
limit: PAGE_SIZE,
|
||||
cursor: pageParam,
|
||||
})
|
||||
return res.data
|
||||
},
|
||||
initialPageParam: undefined,
|
||||
getNextPageParam: lastPage => lastPage.cursor,
|
||||
enabled: !!did,
|
||||
})
|
||||
}
|
||||
@@ -41,6 +41,8 @@ export interface ThreadCtx {
|
||||
hasMore?: boolean
|
||||
isParentLoading?: boolean
|
||||
isChildLoading?: boolean
|
||||
isSelfThread?: boolean
|
||||
hasMoreSelfThread?: boolean
|
||||
}
|
||||
|
||||
export type ThreadPost = {
|
||||
@@ -88,9 +90,11 @@ export function usePostThreadQuery(uri: string | undefined) {
|
||||
gcTime: 0,
|
||||
queryKey: RQKEY(uri || ''),
|
||||
async queryFn() {
|
||||
const res = await agent.getPostThread({uri: uri!})
|
||||
const res = await agent.getPostThread({uri: uri!, depth: 10})
|
||||
if (res.success) {
|
||||
return responseToThreadNodes(res.data.thread)
|
||||
const thread = responseToThreadNodes(res.data.thread)
|
||||
annotateSelfThread(thread)
|
||||
return thread
|
||||
}
|
||||
return {type: 'unknown', uri: uri!}
|
||||
},
|
||||
@@ -234,6 +238,8 @@ function responseToThreadNodes(
|
||||
isHighlightedPost: depth === 0,
|
||||
hasMore:
|
||||
direction === 'down' && !node.replies?.length && !!node.replyCount,
|
||||
isSelfThread: false, // populated `annotateSelfThread`
|
||||
hasMoreSelfThread: false, // populated in `annotateSelfThread`
|
||||
},
|
||||
}
|
||||
} else if (AppBskyFeedDefs.isBlockedPost(node)) {
|
||||
@@ -245,6 +251,48 @@ function responseToThreadNodes(
|
||||
}
|
||||
}
|
||||
|
||||
function annotateSelfThread(thread: ThreadNode) {
|
||||
if (thread.type !== 'post') {
|
||||
return
|
||||
}
|
||||
const selfThreadNodes: ThreadPost[] = [thread]
|
||||
|
||||
let parent: ThreadNode | undefined = thread.parent
|
||||
while (parent) {
|
||||
if (
|
||||
parent.type !== 'post' ||
|
||||
parent.post.author.did !== thread.post.author.did
|
||||
) {
|
||||
// not a self-thread
|
||||
return
|
||||
}
|
||||
selfThreadNodes.unshift(parent)
|
||||
parent = parent.parent
|
||||
}
|
||||
|
||||
let node = thread
|
||||
for (let i = 0; i < 10; i++) {
|
||||
const reply = node.replies?.find(
|
||||
r => r.type === 'post' && r.post.author.did === thread.post.author.did,
|
||||
)
|
||||
if (reply?.type !== 'post') {
|
||||
break
|
||||
}
|
||||
selfThreadNodes.push(reply)
|
||||
node = reply
|
||||
}
|
||||
|
||||
if (selfThreadNodes.length > 1) {
|
||||
for (const selfThreadNode of selfThreadNodes) {
|
||||
selfThreadNode.ctx.isSelfThread = true
|
||||
}
|
||||
const last = selfThreadNodes[selfThreadNodes.length - 1]
|
||||
if (last && last.post.replyCount && !last.replies?.length) {
|
||||
last.ctx.hasMoreSelfThread = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function findPostInQueryData(
|
||||
queryClient: QueryClient,
|
||||
uri: string,
|
||||
|
||||
@@ -3,7 +3,7 @@ import {InfiniteData, QueryKey, useInfiniteQuery} from '@tanstack/react-query'
|
||||
|
||||
import {useAgent} from '#/state/session'
|
||||
|
||||
const PAGE_SIZE = 30
|
||||
const PAGE_SIZE = 50
|
||||
type RQPageParam = string | undefined
|
||||
|
||||
// TODO refactor invalidate on mutate?
|
||||
@@ -30,6 +30,9 @@ export function useProfileFeedgensQuery(
|
||||
limit: PAGE_SIZE,
|
||||
cursor: pageParam,
|
||||
})
|
||||
res.data.feeds.sort((a, b) => {
|
||||
return (b.likeCount || 0) - (a.likeCount || 0)
|
||||
})
|
||||
return res.data
|
||||
},
|
||||
initialPageParam: undefined,
|
||||
|
||||
Reference in New Issue
Block a user