Key post-source using URI with handle

This commit is contained in:
Eric Bailey
2025-06-10 17:38:54 -05:00
parent fc86badcc3
commit 3247c7e28f
3 changed files with 44 additions and 16 deletions
+32 -12
View File
@@ -1,5 +1,5 @@
import {createContext, useCallback, useContext, useRef, useState} from 'react'
import {type AppBskyFeedDefs} from '@atproto/api'
import {type AppBskyFeedDefs,AtUri} from '@atproto/api'
import {Logger} from '#/logger'
import {type FeedDescriptor} from '#/state/queries/post-feed'
@@ -28,22 +28,36 @@ export function Provider({children}: {children: React.ReactNode}) {
const sourcesRef = useRef<Map<string, Source>>(new Map())
const setUnstablePostSource = useCallback((uri: string, source: Source) => {
if (__DEV__) {
const urip = new AtUri(uri)
if (urip.host.startsWith('did:')) {
throw new Error(
`URI passed to setUnstablePostSource should contain a handle — use buildPostSourceUri`,
)
}
}
logger.debug('set', {uri, source})
sourcesRef.current.set(uri, source)
logger.debug('set', {
uri,
source,
})
}, [])
const consumeUnstablePostSource = useCallback((uri: string) => {
const source = sourcesRef.current.get(uri)
if (source) {
sourcesRef.current.delete(uri)
logger.debug('consume', {
uri,
source,
})
if (__DEV__) {
const urip = new AtUri(uri)
if (urip.host.startsWith('did:')) {
throw new Error(
`URI passed to consumeUnstablePostSource should contain a handle — use buildPostSourceUri`,
)
}
}
const source = sourcesRef.current.get(uri)
if (source) {
logger.debug('consume', {uri, source})
sourcesRef.current.delete(uri)
}
return source
}, [])
@@ -71,3 +85,9 @@ export function useUnstablePostSource(uri: string) {
const [source] = useState(() => consume(uri))
return source
}
export function buildPostSourceUri(uri: string, handle: string) {
const urip = new AtUri(uri)
urip.host = handle
return urip.toString()
}
+7 -2
View File
@@ -40,7 +40,10 @@ import {useLanguagePrefs} from '#/state/preferences'
import {type ThreadPost} from '#/state/queries/post-thread'
import {useSession} from '#/state/session'
import {useMergedThreadgateHiddenReplies} from '#/state/threadgate-hidden-replies'
import {useUnstablePostSource} from '#/state/unstable-post-source'
import {
buildPostSourceUri,
useUnstablePostSource,
} from '#/state/unstable-post-source'
import {PostThreadFollowBtn} from '#/view/com/post-thread/PostThreadFollowBtn'
import {ErrorMessage} from '#/view/com/util/error/ErrorMessage'
import {Link, TextLink} from '#/view/com/util/Link'
@@ -204,7 +207,9 @@ let PostThreadItemLoaded = ({
threadgateRecord?: AppBskyFeedThreadgate.Record
}): React.ReactNode => {
const {currentAccount, hasSession} = useSession()
const source = useUnstablePostSource(post.uri)
const source = useUnstablePostSource(
buildPostSourceUri(post.uri, post.author.handle),
)
const feedFeedback = useFeedFeedback(source?.feed, hasSession)
const t = useTheme()
+5 -2
View File
@@ -36,7 +36,10 @@ import {useFeedFeedbackContext} from '#/state/feed-feedback'
import {unstableCacheProfileView} from '#/state/queries/profile'
import {useSession} from '#/state/session'
import {useMergedThreadgateHiddenReplies} from '#/state/threadgate-hidden-replies'
import {useSetUnstablePostSource} from '#/state/unstable-post-source'
import {
buildPostSourceUri,
useSetUnstablePostSource,
} from '#/state/unstable-post-source'
import {FeedNameText} from '#/view/com/util/FeedInfoText'
import {Link, TextLink, TextLinkOnWebOnly} from '#/view/com/util/Link'
import {PostEmbeds, PostEmbedViewContext} from '#/view/com/util/post-embeds'
@@ -232,7 +235,7 @@ let FeedItemInner = ({
reqId,
})
unstableCacheProfileView(queryClient, post.author)
unstableSetPostSource(post.uri, {
unstableSetPostSource(buildPostSourceUri(post.uri, post.author.handle), {
feed: feedDescriptor,
post: {
post,