forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import React from 'react'
2import {type AppBskyUnspeccedDefs, hasMutedWord} from '@atproto/api'
3import {useQuery} from '@tanstack/react-query'
4
5import {STALE} from '#/state/queries'
6import {usePreferencesQuery} from '#/state/queries/preferences'
7import {useAgent} from '#/state/session'
8
9export type TrendingTopic = AppBskyUnspeccedDefs.TrendingTopic
10
11type Response = {
12 topics: TrendingTopic[]
13 suggested: TrendingTopic[]
14}
15
16export const DEFAULT_LIMIT = 14
17
18export const trendingTopicsQueryKey = ['trending-topics']
19
20export function useTrendingTopics() {
21 const agent = useAgent()
22 const {data: preferences} = usePreferencesQuery()
23 const mutedWords = React.useMemo(() => {
24 return preferences?.moderationPrefs?.mutedWords || []
25 }, [preferences?.moderationPrefs])
26
27 return useQuery<Response>({
28 refetchOnWindowFocus: true,
29 staleTime: STALE.MINUTES.THREE,
30 queryKey: trendingTopicsQueryKey,
31 async queryFn() {
32 const {data} = await agent.api.app.bsky.unspecced.getTrendingTopics({
33 limit: DEFAULT_LIMIT,
34 })
35 return {
36 topics: data.topics ?? [],
37 suggested: data.suggested ?? [],
38 }
39 },
40 select: React.useCallback(
41 (data: Response) => {
42 return {
43 topics: data.topics.filter(t => {
44 return !hasMutedWord({
45 mutedWords,
46 text: t.topic + ' ' + t.displayName + ' ' + t.description,
47 })
48 }),
49 suggested: data.suggested.filter(t => {
50 return !hasMutedWord({
51 mutedWords,
52 text: t.topic + ' ' + t.displayName + ' ' + t.description,
53 })
54 }),
55 }
56 },
57 [mutedWords],
58 ),
59 })
60}