about summary refs log tree commit diff
path: root/src/screens/Settings/NotificationSettings/ActivityNotificationSettings.tsx
blob: b00170f3aa6e6c302c597952e1506afec8b4c070 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import {useCallback, useMemo} from 'react'
import {type ListRenderItemInfo, Text as RNText, View} from 'react-native'
import {type ModerationOpts} from '@atproto/api'
import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react'

import {createSanitizedDisplayName} from '#/lib/moderation/create-sanitized-display-name'
import {
  type AllNavigatorParams,
  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} from '#/components/icons/BellRinging'
import {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<
  AllNavigatorParams,
  'ActivityNotificationSettings'
>
export function ActivityNotificationSettingsScreen({}: Props) {
  const t = useTheme()
  const {_} = useLingui()
  const {data: preferences, isError} = useNotificationSettingsQuery()

  const moderationOpts = useModerationOpts()

  const {
    data: subscriptions,
    isPending,
    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(async () => {
    if (isFetchingNextPage || !hasNextPage || isError) return
    try {
      await fetchNextPage()
    } catch (err) {
      logger.error('Failed to load more likes', {message: err})
    }
  }, [isFetchingNextPage, hasNextPage, isError, 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={<Trans>Activity from others</Trans>}
                subtitleText={
                  <Trans>
                    Get notified about posts and replies from accounts you
                    choose.
                  </Trans>
                }
              />
            </SettingsList.Item>
            {isError ? (
              <View style={[a.px_lg, a.pt_md]}>
                <Admonition.Admonition type="error">
                  <Trans>Failed to load notification settings.</Trans>
                </Admonition.Admonition>
              </View>
            ) : (
              <PreferenceControls
                name="subscribedPost"
                preference={preferences?.subscribedPost}
              />
            )}
          </SettingsList.Container>
        }
        data={items}
        keyExtractor={keyExtractor}
        renderItem={renderItem}
        onEndReached={onEndReached}
        onEndReachedThreshold={4}
        ListEmptyComponent={
          error ? null : (
            <View style={[a.px_xl, a.py_md]}>
              {!isPending ? (
                <Admonition.Outer type="tip">
                  <Admonition.Row>
                    <Admonition.Icon />
                    <View style={[a.flex_1, a.gap_sm]}>
                      <Admonition.Text>
                        <Trans>
                          Enable notifications for an account by visiting their
                          profile and pressing the{' '}
                          <RNText
                            style={[a.font_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={_(msg`Privacy and Security settings`)}
                            to={{screen: 'ActivityPrivacySettings'}}
                            style={[a.font_bold]}>
                            Settings &rarr; Privacy and Security
                          </InlineLinkText>
                          .
                        </Trans>
                      </Admonition.Text>
                    </View>
                  </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 {_} = useLingui()
  const t = useTheme()

  const preview = useMemo(() => {
    const actSub = profile.viewer?.activitySubscription
    if (actSub?.post && actSub?.reply) {
      return _(msg`Posts, Replies`)
    } else if (actSub?.post) {
      return _(msg`Posts`)
    } else if (actSub?.reply) {
      return _(msg`Replies`)
    }
    return _(msg`None`)
  }, [_, 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={_(
              msg`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>
  )
}