about summary refs log tree commit diff
path: root/src/view/com/posts/FeedShutdownMsg.tsx
blob: bc047e8311abf127c43716159892505bbdb485b1 (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
import React from 'react'
import {View} from 'react-native'
import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react'

import {PROD_DEFAULT_FEED} from '#/lib/constants'
import {logger} from '#/logger'
import {
  useAddSavedFeedsMutation,
  usePreferencesQuery,
  useRemoveFeedMutation,
  useUpdateSavedFeedsMutation,
} from '#/state/queries/preferences'
import {useSetSelectedFeed} from '#/state/shell/selected-feed'
import * as Toast from '#/view/com/util/Toast'
import {atoms as a, useTheme} from '#/alf'
import {Button, ButtonIcon, ButtonText} from '#/components/Button'
import {InlineLinkText} from '#/components/Link'
import {Loader} from '#/components/Loader'
import {Text} from '#/components/Typography'

export function FeedShutdownMsg({feedUri}: {feedUri: string}) {
  const t = useTheme()
  const {_} = useLingui()
  const setSelectedFeed = useSetSelectedFeed()
  const {data: preferences} = usePreferencesQuery()
  const {mutateAsync: addSavedFeeds, isPending: isAddSavedFeedPending} =
    useAddSavedFeedsMutation()
  const {mutateAsync: removeFeed, isPending: isRemovePending} =
    useRemoveFeedMutation()
  const {mutateAsync: updateSavedFeeds, isPending: isUpdateFeedPending} =
    useUpdateSavedFeedsMutation()

  const feedConfig = preferences?.savedFeeds?.find(
    f => f.value === feedUri && f.pinned,
  )
  const discoverFeedConfig = preferences?.savedFeeds?.find(
    f => f.value === PROD_DEFAULT_FEED('whats-hot'),
  )
  const hasFeedPinned = Boolean(feedConfig)
  const hasDiscoverPinned = Boolean(discoverFeedConfig?.pinned)

  const onRemoveFeed = React.useCallback(async () => {
    try {
      if (feedConfig) {
        await removeFeed(feedConfig)
        Toast.show(_(msg`Removed from your feeds`))
      }
    } catch (err: any) {
      Toast.show(
        _(
          msg`There was an an issue updating your feeds, please check your internet connection and try again.`,
        ),
      )
      logger.error('Failed up update feeds', {message: err})
    }
  }, [removeFeed, feedConfig, _])

  const onReplaceFeed = React.useCallback(async () => {
    try {
      if (!discoverFeedConfig) {
        await addSavedFeeds([
          {
            type: 'feed',
            value: PROD_DEFAULT_FEED('whats-hot'),
            pinned: true,
          },
        ])
      } else {
        await updateSavedFeeds([
          {
            ...discoverFeedConfig,
            pinned: true,
          },
        ])
      }
      setSelectedFeed(`feedgen|${PROD_DEFAULT_FEED('whats-hot')}`)
      if (feedConfig) {
        await removeFeed(feedConfig)
      }
      Toast.show(_(msg`The feed has been replaced with Discover.`))
    } catch (err: any) {
      Toast.show(
        _(
          msg`There was an an issue updating your feeds, please check your internet connection and try again.`,
        ),
      )
      logger.error('Failed up update feeds', {message: err})
    }
  }, [
    addSavedFeeds,
    updateSavedFeeds,
    removeFeed,
    discoverFeedConfig,
    feedConfig,
    setSelectedFeed,
    _,
  ])

  const isProcessing =
    isAddSavedFeedPending || isUpdateFeedPending || isRemovePending
  return (
    <View
      style={[
        a.py_3xl,
        a.px_2xl,
        a.gap_xl,
        t.atoms.border_contrast_low,
        a.border_t,
      ]}>
      <Text style={[a.text_5xl, a.font_bold, t.atoms.text, a.text_center]}>
        :(
      </Text>
      <Text style={[a.text_md, a.leading_snug, t.atoms.text, a.text_center]}>
        <Trans>
          This feed is no longer online. We are showing{' '}
          <InlineLinkText
            to="/profile/bsky.app/feed/whats-hot"
            style={[a.text_md]}>
            Discover
          </InlineLinkText>{' '}
          instead.
        </Trans>
      </Text>
      {hasFeedPinned ? (
        <View style={[a.flex_row, a.justify_center, a.gap_sm]}>
          <Button
            variant="outline"
            color="primary"
            size="small"
            label={_(msg`Remove feed`)}
            disabled={isProcessing}
            onPress={onRemoveFeed}>
            <ButtonText>
              <Trans>Remove feed</Trans>
            </ButtonText>
            {isRemovePending && <ButtonIcon icon={Loader} />}
          </Button>
          {!hasDiscoverPinned && (
            <Button
              variant="solid"
              color="primary"
              size="small"
              label={_(msg`Replace with Discover`)}
              disabled={isProcessing}
              onPress={onReplaceFeed}>
              <ButtonText>
                <Trans>Replace with Discover</Trans>
              </ButtonText>
              {(isAddSavedFeedPending || isUpdateFeedPending) && (
                <ButtonIcon icon={Loader} />
              )}
            </Button>
          )}
        </View>
      ) : undefined}
    </View>
  )
}