about summary refs log tree commit diff
path: root/src/components/PostControls/BookmarkButton.tsx
blob: f72951520233fc6fc72503165ceecbf12e4cd8bf (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
import {memo} from 'react'
import {type Insets} from 'react-native'
import {type AppBskyFeedDefs} from '@atproto/api'
import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react'
import type React from 'react'

import {useCleanError} from '#/lib/hooks/useCleanError'
import {logger} from '#/logger'
import {type Shadow} from '#/state/cache/post-shadow'
import {useBookmarkMutation} from '#/state/queries/bookmarks/useBookmarkMutation'
import {useRequireAuth} from '#/state/session'
import {useTheme} from '#/alf'
import {Bookmark, BookmarkFilled} from '#/components/icons/Bookmark'
import {Trash_Stroke2_Corner0_Rounded as TrashIcon} from '#/components/icons/Trash'
import * as toast from '#/components/Toast'
import {PostControlButton, PostControlButtonIcon} from './PostControlButton'

export const BookmarkButton = memo(function BookmarkButton({
  post,
  big,
  logContext,
  hitSlop,
}: {
  post: Shadow<AppBskyFeedDefs.PostView>
  big?: boolean
  logContext: 'FeedItem' | 'PostThreadItem' | 'Post' | 'ImmersiveVideo'
  hitSlop?: Insets
}): React.ReactNode {
  const t = useTheme()
  const {_} = useLingui()
  const {mutateAsync: bookmark} = useBookmarkMutation()
  const cleanError = useCleanError()
  const requireAuth = useRequireAuth()

  const {viewer} = post
  const isBookmarked = !!viewer?.bookmarked

  const undoLabel = _(
    msg({
      message: `Undo`,
      context: `Button label to undo saving/removing a post from saved posts.`,
    }),
  )

  const save = async ({disableUndo}: {disableUndo?: boolean} = {}) => {
    try {
      await bookmark({
        action: 'create',
        post,
      })

      logger.metric('post:bookmark', {logContext})

      toast.show(
        <toast.Outer>
          <toast.Icon />
          <toast.Text>
            <Trans>Post saved</Trans>
          </toast.Text>
          {!disableUndo && (
            <toast.Action
              label={undoLabel}
              onPress={() => remove({disableUndo: true})}>
              {undoLabel}
            </toast.Action>
          )}
        </toast.Outer>,
        {
          type: 'success',
        },
      )
    } catch (e: any) {
      const {raw, clean} = cleanError(e)
      toast.show(clean || raw || e, {
        type: 'error',
      })
    }
  }

  const remove = async ({disableUndo}: {disableUndo?: boolean} = {}) => {
    try {
      await bookmark({
        action: 'delete',
        uri: post.uri,
      })

      logger.metric('post:unbookmark', {logContext})

      toast.show(
        <toast.Outer>
          <toast.Icon icon={TrashIcon} />
          <toast.Text>
            <Trans>Removed from saved posts</Trans>
          </toast.Text>
          {!disableUndo && (
            <toast.Action
              label={undoLabel}
              onPress={() => save({disableUndo: true})}>
              {undoLabel}
            </toast.Action>
          )}
        </toast.Outer>,
      )
    } catch (e: any) {
      const {raw, clean} = cleanError(e)
      toast.show(clean || raw || e, {
        type: 'error',
      })
    }
  }

  const onHandlePress = () =>
    requireAuth(async () => {
      if (isBookmarked) {
        await remove()
      } else {
        await save()
      }
    })

  return (
    <PostControlButton
      testID="postBookmarkBtn"
      big={big}
      label={
        isBookmarked
          ? _(msg`Remove from saved posts`)
          : _(msg`Add to saved posts`)
      }
      onPress={onHandlePress}
      hitSlop={hitSlop}>
      <PostControlButtonIcon
        fill={isBookmarked ? t.palette.primary_500 : undefined}
        icon={isBookmarked ? BookmarkFilled : Bookmark}
      />
    </PostControlButton>
  )
})