diff --git a/src/components/TrendingTopics.tsx b/src/components/TrendingTopics.tsx index 26eaade8f..7d8779b12 100644 --- a/src/components/TrendingTopics.tsx +++ b/src/components/TrendingTopics.tsx @@ -173,7 +173,15 @@ export function useTopic(raw: TrendingTopic): ParsedTrendingTopic { return React.useMemo(() => { const {topic: displayName, link} = raw - if (link.startsWith('/search')) { + if (link.startsWith('/topic/')) { + return { + type: 'topic', + label: _(msg`Browse posts about ${displayName}`), + displayName, + uri: undefined, + url: link, + } + } else if (link.startsWith('/search')) { return { type: 'topic', label: _(msg`Browse posts about ${displayName}`), diff --git a/src/screens/Topic.tsx b/src/screens/Topic.tsx index d23b7895d..43f1abb64 100644 --- a/src/screens/Topic.tsx +++ b/src/screens/Topic.tsx @@ -5,6 +5,7 @@ import {msg} from '@lingui/macro' import {useLingui} from '@lingui/react' import {useFocusEffect} from '@react-navigation/native' import {type NativeStackScreenProps} from '@react-navigation/native-stack' +import {type QueryClient, useQuery} from '@tanstack/react-query' import {HITSLOP_10} from '#/lib/constants' import {useInitialNumToRender} from '#/lib/hooks/useInitialNumToRender' @@ -14,6 +15,7 @@ import {shareUrl} from '#/lib/sharing' import {cleanError} from '#/lib/strings/errors' import {enforceLen} from '#/lib/strings/helpers' import {useSearchPostsQuery} from '#/state/queries/search-posts' +import {useAgent} from '#/state/session' import {useSetMinimalShellMode} from '#/state/shell' import {Pager} from '#/view/com/pager/Pager' import {TabBar} from '#/view/com/pager/TabBar' @@ -25,6 +27,8 @@ import {ArrowOutOfBoxModified_Stroke2_Corner2_Rounded as Share} from '#/componen import * as Layout from '#/components/Layout' import {ListFooter, ListMaybePlaceholder} from '#/components/Lists' +const TOPICS_API = 'https://api.blacksky.community' + const renderItem = ({item}: ListRenderItemInfo) => { return } @@ -36,18 +40,25 @@ const keyExtractor = (item: AppBskyFeedDefs.PostView, index: number) => { export default function TopicScreen({ route, }: NativeStackScreenProps) { - const {topic} = route.params + const {topic: topicParam} = route.params const {_} = useLingui() + // topic param is either a numeric ID (from trending) or a search term (legacy) + const isTopicId = /^\d+$/.test(topicParam) + + const [topicName, setTopicName] = React.useState( + isTopicId ? '' : decodeURIComponent(topicParam), + ) + const headerTitle = React.useMemo(() => { - return enforceLen(decodeURIComponent(topic), 24, true, 'middle') - }, [topic]) + return topicName ? enforceLen(topicName, 24, true, 'middle') : _(msg`Topic`) + }, [topicName, _]) const onShare = React.useCallback(() => { const url = new URL('https://blacksky.community') - url.pathname = `/topic/${topic}` + url.pathname = `/topic/${topicParam}` shareUrl(url.toString()) - }, [topic]) + }, [topicParam]) const [activeTab, setActiveTab] = React.useState(0) const setMinimalShellMode = useSetMinimalShellMode() @@ -67,25 +78,54 @@ export default function TopicScreen({ ) const sections = React.useMemo(() => { + if (isTopicId) { + // Curated topic feed from our API + return [ + { + title: _(msg`Top`), + component: ( + + ), + }, + { + title: _(msg`Latest`), + component: ( + + ), + }, + ] + } return [ { title: _(msg`Top`), component: ( - + ), }, { title: _(msg`Latest`), component: ( ), }, ] - }, [_, topic, activeTab]) + }, [_, topicParam, topicName, isTopicId, activeTab]) return ( @@ -124,6 +164,109 @@ export default function TopicScreen({ ) } +function CuratedTopicTab({ + topicId, + active, + onTopicName, +}: { + topicId: string + active: boolean + onTopicName?: (name: string) => void +}) { + const {_} = useLingui() + const initialNumToRender = useInitialNumToRender() + const [isPTR, setIsPTR] = React.useState(false) + const trackPostView = usePostViewTracking('Topic') + const agent = useAgent() + + const {data, isLoading, isFetched, isError, error, refetch} = useQuery({ + enabled: active, + staleTime: 3 * 60 * 1000, + queryKey: ['topic-feed', topicId], + queryFn: async () => { + // Fetch curated post URIs from our API + const res = await fetch( + `${TOPICS_API}/xrpc/app.bsky.unspecced.getTopicFeed?topicId=${topicId}&limit=30`, + ) + if (!res.ok) throw new Error(`getTopicFeed failed: ${res.status}`) + const json = (await res.json()) as { + posts: string[] + topic: {name: string} + } + + if (json.topic?.name && onTopicName) { + onTopicName(json.topic.name) + } + + if (!json.posts?.length) return [] + + // Hydrate post URIs through the appview (max 25 per call) + const uris = json.posts.slice(0, 25) + const hydrated = await agent.getPosts({uris}) + return hydrated.data.posts + }, + }) + + const posts = data || [] + + const onRefresh = React.useCallback(async () => { + setIsPTR(true) + await refetch() + setIsPTR(false) + }, [refetch]) + + return ( + <> + {posts.length < 1 ? ( + + ) : ( + + } + initialNumToRender={initialNumToRender} + windowSize={11} + /> + )} + + ) +} + +export function* findAllPostsInTopicQueryData( + queryClient: QueryClient, + uri: string, +): Generator { + const queryDatas = queryClient.getQueriesData({ + queryKey: ['topic-feed'], + }) + for (const [_queryKey, queryData] of queryDatas) { + if (!queryData) continue + for (const post of queryData) { + if (post.uri === uri) { + yield post + } + } + } +} + function TopicScreenTab({ topic, sort, diff --git a/src/state/cache/post-shadow.ts b/src/state/cache/post-shadow.ts index 0afa272c5..07b9bb6ed 100644 --- a/src/state/cache/post-shadow.ts +++ b/src/state/cache/post-shadow.ts @@ -15,6 +15,7 @@ import {findAllPostsInQueryData as findAllPostsInFeedQueryData} from '#/state/qu import {findAllPostsInQueryData as findAllPostsInQuoteQueryData} from '#/state/queries/post-quotes' import {findAllPostsInQueryData as findAllPostsInSearchQueryData} from '#/state/queries/search-posts' import {findAllPostsInQueryData as findAllPostsInThreadV2QueryData} from '#/state/queries/usePostThread/queryCache' +import {findAllPostsInTopicQueryData} from '#/screens/Topic' import {castAsShadow, type Shadow} from './types' export type {Shadow} from './types' @@ -194,4 +195,7 @@ function* findPostsInCache( for (let post of findAllPostsInBookmarksQueryData(queryClient, uri)) { yield post } + for (let post of findAllPostsInTopicQueryData(queryClient, uri)) { + yield post + } } diff --git a/src/state/queries/trending/useGetTrendsQuery.ts b/src/state/queries/trending/useGetTrendsQuery.ts index 3c53bd336..363ef37e7 100644 --- a/src/state/queries/trending/useGetTrendsQuery.ts +++ b/src/state/queries/trending/useGetTrendsQuery.ts @@ -14,7 +14,7 @@ export const DEFAULT_LIMIT = 5 export const createGetTrendsQueryKey = () => ['trends'] -const PUBLIC_API = 'https://public.api.bsky.app' +const PUBLIC_API = 'https://api.blacksky.community' export function useGetTrendsQuery() { const {data: preferences} = usePreferencesQuery() diff --git a/src/state/queries/trending/useTrendingTopics.ts b/src/state/queries/trending/useTrendingTopics.ts index bb8aff259..2d4d0bb3d 100644 --- a/src/state/queries/trending/useTrendingTopics.ts +++ b/src/state/queries/trending/useTrendingTopics.ts @@ -16,7 +16,7 @@ export const DEFAULT_LIMIT = 14 export const trendingTopicsQueryKey = ['trending-topics'] -const PUBLIC_API = 'https://public.api.bsky.app' +const PUBLIC_API = 'https://api.blacksky.community' export function useTrendingTopics() { const {data: preferences} = usePreferencesQuery()