about summary refs log tree commit diff
path: root/src/screens/ProfileList/components/Header.tsx
blob: fe4b33c753495ea149306e81c94f94444e711f3e (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
import {useMemo} from 'react'
import {View} from 'react-native'
import {AppBskyGraphDefs, RichText as RichTextAPI} from '@atproto/api'
import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react'

import {useHaptics} from '#/lib/haptics'
import {makeListLink} from '#/lib/routes/links'
import {logger} from '#/logger'
import {useListBlockMutation, useListMuteMutation} from '#/state/queries/list'
import {
  useAddSavedFeedsMutation,
  type UsePreferencesQueryResponse,
  useUpdateSavedFeedsMutation,
} from '#/state/queries/preferences'
import {useSession} from '#/state/session'
import {ProfileSubpageHeader} from '#/view/com/profile/ProfileSubpageHeader'
import {atoms as a} from '#/alf'
import {Button, ButtonIcon, ButtonText} from '#/components/Button'
import {Pin_Stroke2_Corner0_Rounded as PinIcon} from '#/components/icons/Pin'
import {Loader} from '#/components/Loader'
import {RichText} from '#/components/RichText'
import * as Toast from '#/components/Toast'
import {MoreOptionsMenu} from './MoreOptionsMenu'
import {SubscribeMenu} from './SubscribeMenu'

export function Header({
  rkey,
  list,
  preferences,
}: {
  rkey: string
  list: AppBskyGraphDefs.ListView
  preferences: UsePreferencesQueryResponse
}) {
  const {_} = useLingui()
  const {currentAccount} = useSession()
  const isCurateList = list.purpose === AppBskyGraphDefs.CURATELIST
  const isModList = list.purpose === AppBskyGraphDefs.MODLIST
  const isBlocking = !!list.viewer?.blocked
  const isMuting = !!list.viewer?.muted
  const playHaptic = useHaptics()

  const {mutateAsync: muteList, isPending: isMutePending} =
    useListMuteMutation()
  const {mutateAsync: blockList, isPending: isBlockPending} =
    useListBlockMutation()
  const {mutateAsync: addSavedFeeds, isPending: isAddSavedFeedPending} =
    useAddSavedFeedsMutation()
  const {mutateAsync: updateSavedFeeds, isPending: isUpdatingSavedFeeds} =
    useUpdateSavedFeedsMutation()

  const isPending = isAddSavedFeedPending || isUpdatingSavedFeeds

  const savedFeedConfig = preferences?.savedFeeds?.find(
    f => f.value === list.uri,
  )
  const isPinned = Boolean(savedFeedConfig?.pinned)

  const onTogglePinned = async () => {
    playHaptic()

    try {
      if (savedFeedConfig) {
        const pinned = !savedFeedConfig.pinned
        await updateSavedFeeds([
          {
            ...savedFeedConfig,
            pinned,
          },
        ])
        Toast.show(
          pinned
            ? _(msg`Pinned to your feeds`)
            : _(msg`Unpinned from your feeds`),
        )
      } else {
        await addSavedFeeds([
          {
            type: 'list',
            value: list.uri,
            pinned: true,
          },
        ])
        Toast.show(_(msg`Saved to your feeds`))
      }
    } catch (e) {
      Toast.show(_(msg`There was an issue contacting the server`), {
        type: 'error',
      })
      logger.error('Failed to toggle pinned feed', {message: e})
    }
  }

  const onUnsubscribeMute = async () => {
    try {
      await muteList({uri: list.uri, mute: false})
      Toast.show(_(msg({message: 'List unmuted', context: 'toast'})))
      logger.metric(
        'moderation:unsubscribedFromList',
        {listType: 'mute'},
        {statsig: true},
      )
    } catch {
      Toast.show(
        _(
          msg`There was an issue. Please check your internet connection and try again.`,
        ),
      )
    }
  }

  const onUnsubscribeBlock = async () => {
    try {
      await blockList({uri: list.uri, block: false})
      Toast.show(_(msg({message: 'List unblocked', context: 'toast'})))
      logger.metric(
        'moderation:unsubscribedFromList',
        {listType: 'block'},
        {statsig: true},
      )
    } catch {
      Toast.show(
        _(
          msg`There was an issue. Please check your internet connection and try again.`,
        ),
      )
    }
  }

  const descriptionRT = useMemo(
    () =>
      list.description
        ? new RichTextAPI({
            text: list.description,
            facets: list.descriptionFacets,
          })
        : undefined,
    [list],
  )

  return (
    <>
      <ProfileSubpageHeader
        href={makeListLink(list.creator.handle || list.creator.did || '', rkey)}
        title={list.name}
        avatar={list.avatar}
        isOwner={list.creator.did === currentAccount?.did}
        creator={list.creator}
        purpose={list.purpose}
        avatarType="list">
        {isCurateList ? (
          <Button
            testID={isPinned ? 'unpinBtn' : 'pinBtn'}
            color={isPinned ? 'secondary' : 'primary_subtle'}
            label={isPinned ? _(msg`Unpin`) : _(msg`Pin to home`)}
            onPress={onTogglePinned}
            disabled={isPending}
            size="small"
            style={[a.rounded_full]}>
            {!isPinned && <ButtonIcon icon={isPending ? Loader : PinIcon} />}
            <ButtonText>
              {isPinned ? <Trans>Unpin</Trans> : <Trans>Pin to home</Trans>}
            </ButtonText>
          </Button>
        ) : isModList ? (
          isBlocking ? (
            <Button
              testID="unblockBtn"
              color="secondary"
              label={_(msg`Unblock`)}
              onPress={onUnsubscribeBlock}
              size="small"
              style={[a.rounded_full]}
              disabled={isBlockPending}>
              {isBlockPending && <ButtonIcon icon={Loader} />}
              <ButtonText>
                <Trans>Unblock</Trans>
              </ButtonText>
            </Button>
          ) : isMuting ? (
            <Button
              testID="unmuteBtn"
              color="secondary"
              label={_(msg`Unmute`)}
              onPress={onUnsubscribeMute}
              size="small"
              style={[a.rounded_full]}
              disabled={isMutePending}>
              {isMutePending && <ButtonIcon icon={Loader} />}
              <ButtonText>
                <Trans>Unmute</Trans>
              </ButtonText>
            </Button>
          ) : (
            <SubscribeMenu list={list} />
          )
        ) : null}
        <MoreOptionsMenu list={list} />
      </ProfileSubpageHeader>
      {descriptionRT ? (
        <View style={[a.px_lg, a.pt_sm, a.pb_sm, a.gap_md]}>
          <RichText value={descriptionRT} style={[a.text_md, a.leading_snug]} />
        </View>
      ) : null}
    </>
  )
}