Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264import {useCallback, useMemo} from 'react'import {type ListRenderItemInfo, Text as RNText, View} from 'react-native'import {type ModerationOpts} from '@bsky/sdk/moderation'import {Trans, useLingui} from '@lingui/react/macro'
import {createSanitizedDisplayName} from '#/lib/moderation/create-sanitized-display-name'import { type CommonNavigatorParams, type NativeStackScreenProps,} from '#/lib/routes/types'import {cleanError} from '#/lib/strings/errors'import {logger} from '#/logger'import {useProfileShadow} from '#/state/cache/profile-shadow'import {useModerationOpts} from '#/state/preferences/moderation-opts'import {useActivitySubscriptionsQuery} from '#/state/queries/activity-subscriptions'import {useNotificationSettingsQuery} from '#/state/queries/notifications/settings'import {List} from '#/view/com/util/List'import {atoms as a, useTheme} from '#/alf'import {SubscribeProfileDialog} from '#/components/activity-notifications/SubscribeProfileDialog'import * as Admonition from '#/components/Admonition'import {Button, ButtonText} from '#/components/Button'import {useDialogControl} from '#/components/Dialog'import { BellRinging_Filled_Corner0_Rounded as BellRingingFilledIcon, BellRinging_Stroke2_Corner0_Rounded as BellRingingIcon,} from '#/components/icons/BellRinging'import * as Layout from '#/components/Layout'import {InlineLinkText} from '#/components/Link'import {ListFooter} from '#/components/Lists'import {Loader} from '#/components/Loader'import * as ProfileCard from '#/components/ProfileCard'import {Text} from '#/components/Typography'import type * as bsky from '#/types/bsky'import * as SettingsList from '../components/SettingsList'import {ItemTextWithSubtitle} from './components/ItemTextWithSubtitle'import {PreferenceControls} from './components/PreferenceControls'
type Props = NativeStackScreenProps< CommonNavigatorParams, 'ActivityNotificationSettings'>
export function ActivityNotificationSettingsScreen({}: Props) { const t = useTheme() const {t: l} = useLingui() const {data: preferences, isError: isPreferencesError} = useNotificationSettingsQuery() const moderationOpts = useModerationOpts()
const { data: subscriptions, isPending, isError: isSubscriptionsError, error, isFetchingNextPage, fetchNextPage, hasNextPage, } = useActivitySubscriptionsQuery()
const items = useMemo(() => { if (!subscriptions) return [] return subscriptions.pages.flatMap(page => page.subscriptions) }, [subscriptions])
const renderItem = useCallback( ({item}: ListRenderItemInfo<bsky.profile.AnyProfileView>) => { if (!moderationOpts) return null return ( <ActivitySubscriptionCard profile={item} moderationOpts={moderationOpts} /> ) }, [moderationOpts], )
const onEndReached = useCallback(() => { if (isFetchingNextPage || !hasNextPage || isSubscriptionsError) return void fetchNextPage().catch(err => { logger.error('Failed to load more activity subscriptions', { message: err, }) }) }, [isFetchingNextPage, hasNextPage, isSubscriptionsError, fetchNextPage])
return ( <Layout.Screen> <Layout.Header.Outer> <Layout.Header.BackButton /> <Layout.Header.Content> <Layout.Header.TitleText> <Trans>Notifications</Trans> </Layout.Header.TitleText> </Layout.Header.Content> <Layout.Header.Slot /> </Layout.Header.Outer> <List ListHeaderComponent={ <SettingsList.Container> <SettingsList.Item style={[a.align_start]}> <SettingsList.ItemIcon icon={BellRingingIcon} /> <ItemTextWithSubtitle bold titleText={l`Activity from others`} subtitleText={l`Get notified about posts and replies from accounts you choose.`} /> </SettingsList.Item> {isPreferencesError ? ( <View style={[a.px_xl, a.pt_md]}> <Admonition.Admonition type="error"> <Trans>Failed to load notification settings.</Trans> </Admonition.Admonition> </View> ) : ( <View style={[a.px_xl]}> <PreferenceControls name="subscribedPost" preference={preferences?.subscribedPost} /> </View> )} </SettingsList.Container> } data={items} keyExtractor={keyExtractor} renderItem={renderItem} onEndReached={onEndReached} onEndReachedThreshold={4} ListEmptyComponent={ error ? undefined : ( <View style={[a.px_xl, a.py_md]}> {!isPending ? ( <Admonition.Outer type="tip"> <Admonition.Row> <Admonition.Icon /> <Admonition.Content> <Admonition.Text> <Trans> Enable notifications for an account by visiting their profile and pressing the{' '} <RNText style={[ a.font_semi_bold, t.atoms.text_contrast_high, ]}> bell icon </RNText>{' '} <BellRingingFilledIcon size="xs" style={t.atoms.text_contrast_high} /> . </Trans> </Admonition.Text> <Admonition.Text> <Trans> If you want to restrict who can receive notifications for your account's activity, you can change this in{' '} <InlineLinkText label={l`Privacy and Security settings`} to={{screen: 'ActivityPrivacySettings'}} style={[a.font_semi_bold]}> Settings → Privacy and Security </InlineLinkText> . </Trans> </Admonition.Text> </Admonition.Content> </Admonition.Row> </Admonition.Outer> ) : ( <View style={[a.flex_1, a.align_center, a.pt_xl]}> <Loader size="lg" /> </View> )} </View> ) } ListFooterComponent={ <ListFooter style={[items.length === 0 && a.border_transparent]} isFetchingNextPage={isFetchingNextPage} error={cleanError(error)} onRetry={fetchNextPage} hasNextPage={hasNextPage} /> } windowSize={11} /> </Layout.Screen> )}
function keyExtractor(item: bsky.profile.AnyProfileView) { return item.did}
function ActivitySubscriptionCard({ profile: profileUnshadowed, moderationOpts,}: { profile: bsky.profile.AnyProfileView moderationOpts: ModerationOpts}) { const profile = useProfileShadow(profileUnshadowed) const control = useDialogControl() const {t: l} = useLingui() const t = useTheme()
const preview = useMemo(() => { const actSub = profile.viewer?.activitySubscription if (actSub?.post && actSub?.reply) { return l`Posts, Replies` } else if (actSub?.post) { return l`Posts` } else if (actSub?.reply) { return l`Replies` } return l`None` }, [l, profile.viewer?.activitySubscription])
return ( <View style={[a.py_md, a.px_xl, a.border_t, t.atoms.border_contrast_low]}> <ProfileCard.Outer> <ProfileCard.Header> <ProfileCard.Avatar profile={profile} moderationOpts={moderationOpts} /> <View style={[a.flex_1, a.gap_2xs]}> <ProfileCard.NameAndHandle profile={profile} moderationOpts={moderationOpts} inline /> <Text style={[a.leading_snug, t.atoms.text_contrast_medium]}> {preview} </Text> </View> <Button label={l`Edit notifications from ${createSanitizedDisplayName( profile, )}`} size="small" color="primary" variant="solid" onPress={control.open}> <ButtonText> <Trans>Edit</Trans> </ButtonText> </Button> </ProfileCard.Header> </ProfileCard.Outer> <SubscribeProfileDialog control={control} profile={profile} moderationOpts={moderationOpts} includeProfile /> </View> )}