Update APIs

This commit is contained in:
Eric Bailey
2025-05-25 13:05:32 -05:00
parent da4afb78d0
commit ef3baf8368
6 changed files with 42 additions and 73 deletions
+7 -12
View File
@@ -11,17 +11,13 @@ import {
HiddenReplyKind,
type UsePostThreadProps,
} from '#/state/queries/usePostThread/types'
import {
getThreadgateRecord,
mapSortOptionsToSortID,
} from '#/state/queries/usePostThread/utils'
import {getThreadgateRecord} from '#/state/queries/usePostThread/utils'
import {useAgent, useSession} from '#/state/session'
import {useMergeThreadgateHiddenReplies} from '#/state/threadgate-hidden-replies'
export * from '#/state/queries/usePostThread/types'
export function usePostThread({
uri,
enabled: isEnabled,
params,
state,
@@ -32,9 +28,8 @@ export function usePostThread({
const moderationOpts = useModerationOpts()
const mergeThreadgateHiddenReplies = useMergeThreadgateHiddenReplies()
const enabled = isEnabled !== false && !!uri && !!moderationOpts
const enabled = isEnabled !== false && !!params.anchor && !!moderationOpts
const queryKey = createPostThreadQueryKey({
uri,
params,
})
@@ -44,16 +39,17 @@ export function usePostThread({
gcTime: 0,
async queryFn() {
const {data} = await agent.app.bsky.unspecced.getPostThreadV2({
anchor: uri!,
anchor: params.anchor!,
branchingFactor: params.view === 'linear' ? 1 : 3, // 100 TODO
below: 3,
sorting: mapSortOptionsToSortID(params.sort),
sort: params.sort,
prioritizeFollowedUsers: params.prioritizeFollowedUsers,
})
return data
},
placeholderData() {
if (!uri) return
const placeholder = getThreadPlaceholder(qc, uri)
if (!params.anchor) return
const placeholder = getThreadPlaceholder(qc, params.anchor)
if (placeholder) {
return {thread: [placeholder]}
}
@@ -96,7 +92,6 @@ export function usePostThread({
return {
...query,
data: {
anchorIndex: items.findIndex(i => Boolean(i.ui?.isAnchor)),
items,
threadgate: query.data?.threadgate,
},
+11 -7
View File
@@ -1,7 +1,6 @@
import {
APP_BSKY_UNSPECCED,
AtUri,
AppBskyUnspeccedGetPostThreadV2,
AtUri,
type ModerationDecision,
type ModerationOpts,
} from '@atproto/api'
@@ -30,7 +29,11 @@ export function flatten(
if (item.type === 'threadPost') {
// TODO should not insert if not found post etc
if (item.ui.isAnchor && hasSession && !item.value.post.viewer?.replyDisabled) {
if (
item.ui.isAnchor &&
hasSession &&
!item.value.post.viewer?.replyDisabled
) {
flattened.splice(i + 1, 0, {
type: 'replyComposer',
key: 'replyComposer',
@@ -39,18 +42,19 @@ export function flatten(
const prev = unhydratedReplyIntervals[unhydratedReplyIntervals.length - 1]
if (item.annotations.has(APP_BSKY_UNSPECCED.GetPostThreadV2HasMoreReplies)) {
if (item.value.moreReplies > 0) {
unhydratedReplyIntervals.push({
item,
replyCount: item.value.post.replyCount || 0,
replyCount: item.value.moreReplies,
})
}
/*
* If direct child of previous item with `hasMoreReplies`, subtract
* If direct child of previous item with `hasMoreReplies`, subtract
*/
if (prev && item.depth === prev.item.depth + 1) {
prev.replyCount = Math.max(0, prev.replyCount - 1)
// TODO test if we need this
// prev.replyCount = Math.max(0, prev.replyCount - 1)
}
if (prev && item.depth <= prev.item.depth) {
+7 -15
View File
@@ -1,28 +1,26 @@
import {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
type APP_BSKY_UNSPECCED,
type AtUri,
type AppBskyFeedDefs,
type AppBskyFeedPost,
type AppBskyUnspeccedGetPostThreadV2,
type BskyThreadViewPreference,
type AtUri,
type ModerationDecision,
} from '@atproto/api'
export const postThreadQueryKeyRoot = 'getPostThreadV2' as const
export const createPostThreadQueryKey = (
props: Pick<UsePostThreadProps, 'uri' | 'params'>,
props: Pick<UsePostThreadProps, 'params'>,
) => [postThreadQueryKeyRoot, props] as const
export type PostThreadParams = {
export type PostThreadParams = Pick<
AppBskyUnspeccedGetPostThreadV2.QueryParams,
'sort' | 'prioritizeFollowedUsers'
> & {
anchor?: string
view: 'tree' | 'linear'
sort: 'top' | 'oldest' | 'newest' | string
prioritizeFollows: BskyThreadViewPreference['prioritizeFollowedUsers']
}
export type UsePostThreadProps = {
uri?: string
enabled?: boolean
params: PostThreadParams
state: {
@@ -47,12 +45,6 @@ export type Slice =
}
}
moderation: ModerationDecision
/**
* Reference via {@link APP_BSKY_UNSPECCED}
*/
annotations: Set<
AppBskyUnspeccedGetPostThreadV2.ThreadItemPost['annotations'][number]
>
ui: {
isAnchor: boolean
showParentReplyLine: boolean
-15
View File
@@ -1,5 +1,4 @@
import {
APP_BSKY_UNSPECCED,
type AppBskyFeedDefs,
AppBskyFeedPost,
AppBskyFeedThreadgate,
@@ -7,22 +6,8 @@ import {
AtUri,
} from '@atproto/api'
import {type PostThreadParams} from '#/state/queries/usePostThread/types'
import * as bsky from '#/types/bsky'
export function mapSortOptionsToSortID(sort: PostThreadParams['sort']) {
switch (sort) {
case 'top':
return APP_BSKY_UNSPECCED.GetPostThreadV2Top
case 'oldest':
return APP_BSKY_UNSPECCED.GetPostThreadV2Oldest
case 'newest':
return APP_BSKY_UNSPECCED.GetPostThreadV2Newest
default:
return APP_BSKY_UNSPECCED.GetPostThreadV2Top
}
}
export function getThreadgateRecord(
view: AppBskyUnspeccedGetPostThreadV2.OutputSchema['threadgate'],
) {
+5 -2
View File
@@ -90,7 +90,6 @@ export function threadPost({
record: AppBskyFeedPost.Record
},
},
annotations: new Set(value.annotations),
moderation: moderatePost(value.post, moderationOpts),
ui: {
isAnchor: depth === 0,
@@ -114,7 +113,11 @@ export function postViewToThreadPlaceholder(
value: {
$type: 'app.bsky.unspecced.getPostThreadV2#threadItemPost',
post,
annotations: [],
hiddenByThreadgate: false,
opThread: false,
moreParents: false,
moreReplies: 0,
mutedByViewer: false,
},
}
}