Centralize mute word handling

This commit is contained in:
Eric Bailey
2024-12-16 17:24:09 -06:00
parent 64e47cced3
commit 3ef6134915
2 changed files with 37 additions and 35 deletions
+23 -34
View File
@@ -1,7 +1,6 @@
import React from 'react' import React from 'react'
import {View} from 'react-native' import {View} from 'react-native'
import {AtUri} from '@atproto/api' import {AtUri} from '@atproto/api'
import {hasMutedWord} from '@atproto/api/dist/moderation/mutewords'
import {msg} from '@lingui/macro' import {msg} from '@lingui/macro'
import {useLingui} from '@lingui/react' import {useLingui} from '@lingui/react'
@@ -10,7 +9,6 @@ import {feedUriToHref} from '#/lib/strings/url-helpers'
// import {Hashtag_Stroke2_Corner0_Rounded as Hashtag} from '#/components/icons/Hashtag' // import {Hashtag_Stroke2_Corner0_Rounded as Hashtag} from '#/components/icons/Hashtag'
// import {CloseQuote_Filled_Stroke2_Corner0_Rounded as Quote} from '#/components/icons/Quote' // import {CloseQuote_Filled_Stroke2_Corner0_Rounded as Quote} from '#/components/icons/Quote'
// import {UserAvatar} from '#/view/com/util/UserAvatar' // import {UserAvatar} from '#/view/com/util/UserAvatar'
import {usePreferencesQuery} from '#/state/queries/preferences'
import type {TrendingTopic} from '#/state/queries/trending/useTrendingTopics' import type {TrendingTopic} from '#/state/queries/trending/useTrendingTopics'
import {atoms as a, useTheme, ViewStyleProp} from '#/alf' import {atoms as a, useTheme, ViewStyleProp} from '#/alf'
import {Link as InternalLink, LinkProps} from '#/components/Link' import {Link as InternalLink, LinkProps} from '#/components/Link'
@@ -96,7 +94,7 @@ export function TrendingTopic({
isSmall ? [a.text_sm] : [a.text_md, {paddingBottom: 1}], isSmall ? [a.text_sm] : [a.text_md, {paddingBottom: 1}],
]} ]}
numberOfLines={1}> numberOfLines={1}>
{topic.name} {topic.displayName}
</Text> </Text>
</View> </View>
) )
@@ -152,56 +150,39 @@ export function TrendingTopicLink({
type ParsedTrendingTopic = type ParsedTrendingTopic =
| { | {
type: 'topic' | 'tag' type: 'topic' | 'tag' | 'unknown'
label: string label: string
name: string displayName: string
url: string url: string
uri: undefined uri: undefined
} }
| { | {
type: 'profile' | 'feed' type: 'profile' | 'feed'
label: string label: string
name: string displayName: string
url: string url: string
uri: AtUri uri: AtUri
} }
export function useMutedWords() { export function useTopic(raw: TrendingTopic): ParsedTrendingTopic {
const {data: preferences} = usePreferencesQuery()
return React.useMemo(() => {
return preferences?.moderationPrefs?.mutedWords || []
}, [preferences?.moderationPrefs?.mutedWords])
}
export function useTopic(raw: TrendingTopic): ParsedTrendingTopic | undefined {
const {_} = useLingui() const {_} = useLingui()
const mutedWords = useMutedWords()
return React.useMemo(() => { return React.useMemo(() => {
const {topic: name, link: uri} = raw const {topic: displayName, link: uri} = raw
if (
hasMutedWord({
mutedWords,
text: name,
})
)
return
if (!uri.startsWith('at://')) { if (!uri.startsWith('at://')) {
if (uri.startsWith('/search')) { if (uri.startsWith('/search')) {
return { return {
type: 'topic', type: 'topic',
label: _(msg`Browse posts about ${name}`), label: _(msg`Browse posts about ${displayName}`),
name, displayName,
uri: undefined, uri: undefined,
url: uri, url: uri,
} }
} else if (uri.startsWith('/hashtag')) { } else if (uri.startsWith('/hashtag')) {
return { return {
type: 'tag', type: 'tag',
label: _(msg`Browse posts tagged with ${name}`), label: _(msg`Browse posts tagged with ${displayName}`),
name: name.replace(/^#/, ''), displayName: displayName.replace(/^#/, ''),
uri: undefined, uri: undefined,
url: uri, url: uri,
} }
@@ -212,8 +193,8 @@ export function useTopic(raw: TrendingTopic): ParsedTrendingTopic | undefined {
case 'app.bsky.actor.profile': { case 'app.bsky.actor.profile': {
return { return {
type: 'profile', type: 'profile',
label: _(msg`View ${name}'s profile`), label: _(msg`View ${displayName}'s profile`),
name, displayName,
uri: urip, uri: urip,
url: makeProfileLink({did: urip.host, handle: urip.host}), url: makeProfileLink({did: urip.host, handle: urip.host}),
} }
@@ -221,13 +202,21 @@ export function useTopic(raw: TrendingTopic): ParsedTrendingTopic | undefined {
case 'app.bsky.feed.generator': { case 'app.bsky.feed.generator': {
return { return {
type: 'feed', type: 'feed',
label: _(msg`Browse the ${name} feed`), label: _(msg`Browse the ${displayName} feed`),
name, displayName,
uri: urip, uri: urip,
url: feedUriToHref(uri), url: feedUriToHref(uri),
} }
} }
} }
} }
}, [_, raw, mutedWords])
return {
type: 'unknown',
label: _(msg`Browse topic ${displayName}`),
displayName,
uri: undefined,
url: uri,
}
}, [_, raw])
} }
@@ -1,7 +1,10 @@
import React from 'react'
import {hasMutedWord} from '@atproto/api/dist/moderation/mutewords'
import {useQuery} from '@tanstack/react-query' import {useQuery} from '@tanstack/react-query'
// TEMP // TEMP
import {makeSearchLink} from '#/lib/routes/links' import {makeSearchLink} from '#/lib/routes/links'
import {usePreferencesQuery} from '#/state/queries/preferences'
export type TrendingTopic = { export type TrendingTopic = {
topic: string topic: string
@@ -15,6 +18,11 @@ export const DEFAULT_LIMIT = 12
export const trendingTopicsQueryKey = ['trending-topics'] export const trendingTopicsQueryKey = ['trending-topics']
export function useTrendingTopics() { export function useTrendingTopics() {
const {data: preferences} = usePreferencesQuery()
const mutedWords = React.useMemo(() => {
return preferences?.moderationPrefs?.mutedWords || []
}, [preferences?.moderationPrefs])
return useQuery({ return useQuery({
queryKey: trendingTopicsQueryKey, queryKey: trendingTopicsQueryKey,
async queryFn() { async queryFn() {
@@ -75,7 +83,12 @@ export function useTrendingTopics() {
}, },
] ]
return { return {
topics, topics: topics.filter(t => {
return !hasMutedWord({
mutedWords,
text: t.topic + ' ' + t.displayName + ' ' + t.description,
})
}),
recommended: [], recommended: [],
} }
}, },