about summary refs log tree commit diff
path: root/src/components/WhoCanReply.tsx
blob: 1ffb4da39de9df1b57500a7a4d4b4e47ff9b8150 (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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import React from 'react'
import {Keyboard, StyleProp, View, ViewStyle} from 'react-native'
import {
  AppBskyFeedDefs,
  AppBskyFeedGetPostThread,
  AppBskyGraphDefs,
  AtUri,
  BskyAgent,
} from '@atproto/api'
import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react'
import {useQueryClient} from '@tanstack/react-query'

import {createThreadgate} from '#/lib/api'
import {until} from '#/lib/async/until'
import {HITSLOP_10} from '#/lib/constants'
import {makeListLink, makeProfileLink} from '#/lib/routes/links'
import {logger} from '#/logger'
import {isNative} from '#/platform/detection'
import {RQKEY_ROOT as POST_THREAD_RQKEY_ROOT} from '#/state/queries/post-thread'
import {
  ThreadgateSetting,
  threadgateViewToSettings,
} from '#/state/queries/threadgate'
import {useAgent} from '#/state/session'
import * as Toast from 'view/com/util/Toast'
import {atoms as a, useTheme} from '#/alf'
import {Button} from '#/components/Button'
import * as Dialog from '#/components/Dialog'
import {useDialogControl} from '#/components/Dialog'
import {CircleBanSign_Stroke2_Corner0_Rounded as CircleBanSign} from '#/components/icons/CircleBanSign'
import {Earth_Stroke2_Corner0_Rounded as Earth} from '#/components/icons/Globe'
import {Group3_Stroke2_Corner0_Rounded as Group} from '#/components/icons/Group'
import {Text} from '#/components/Typography'
import {TextLink} from '../view/com/util/Link'
import {ThreadgateEditorDialog} from './dialogs/ThreadgateEditor'
import {PencilLine_Stroke2_Corner0_Rounded as PencilLine} from './icons/Pencil'

interface WhoCanReplyProps {
  post: AppBskyFeedDefs.PostView
  isThreadAuthor: boolean
  style?: StyleProp<ViewStyle>
}

export function WhoCanReply({post, isThreadAuthor, style}: WhoCanReplyProps) {
  const {_} = useLingui()
  const t = useTheme()
  const infoDialogControl = useDialogControl()
  const editDialogControl = useDialogControl()
  const agent = useAgent()
  const queryClient = useQueryClient()

  const settings = React.useMemo(
    () => threadgateViewToSettings(post.threadgate),
    [post],
  )
  const isRootPost = !('reply' in post.record)

  if (!isRootPost) {
    return null
  }
  if (!settings.length && !isThreadAuthor) {
    return null
  }

  const isEverybody = settings.length === 0
  const isNobody = !!settings.find(gate => gate.type === 'nobody')
  const description = isEverybody
    ? _(msg`Everybody can reply`)
    : isNobody
    ? _(msg`Replies disabled`)
    : _(msg`Some people can reply`)

  const onPressEdit = () => {
    if (isNative && Keyboard.isVisible()) {
      Keyboard.dismiss()
    }
    if (isThreadAuthor) {
      editDialogControl.open()
    } else {
      infoDialogControl.open()
    }
  }

  const onEditConfirm = async (newSettings: ThreadgateSetting[]) => {
    if (JSON.stringify(settings) === JSON.stringify(newSettings)) {
      return
    }
    try {
      if (newSettings.length) {
        await createThreadgate(agent, post.uri, newSettings)
      } else {
        await agent.api.com.atproto.repo.deleteRecord({
          repo: agent.session!.did,
          collection: 'app.bsky.feed.threadgate',
          rkey: new AtUri(post.uri).rkey,
        })
      }
      await whenAppViewReady(agent, post.uri, res => {
        const thread = res.data.thread
        if (AppBskyFeedDefs.isThreadViewPost(thread)) {
          const fetchedSettings = threadgateViewToSettings(
            thread.post.threadgate,
          )
          return JSON.stringify(fetchedSettings) === JSON.stringify(newSettings)
        }
        return false
      })
      Toast.show(_(msg`Thread settings updated`))
      queryClient.invalidateQueries({
        queryKey: [POST_THREAD_RQKEY_ROOT],
      })
    } catch (err) {
      Toast.show(
        _(
          msg`There was an issue. Please check your internet connection and try again.`,
        ),
        'xmark',
      )
      logger.error('Failed to edit threadgate', {message: err})
    }
  }

  return (
    <>
      <Button
        label={
          isThreadAuthor ? _(msg`Edit who can reply`) : _(msg`Who can reply`)
        }
        onPress={isThreadAuthor ? onPressEdit : infoDialogControl.open}
        hitSlop={HITSLOP_10}>
        {({hovered}) => (
          <View style={[a.flex_row, a.align_center, a.gap_xs, style]}>
            <Icon
              color={t.palette.contrast_400}
              width={16}
              settings={settings}
            />
            <Text
              style={[
                a.text_sm,
                a.leading_tight,
                t.atoms.text_contrast_medium,
                hovered && a.underline,
              ]}>
              {description}
            </Text>
            {isThreadAuthor && (
              <PencilLine width={12} fill={t.palette.primary_500} />
            )}
          </View>
        )}
      </Button>
      <WhoCanReplyDialog
        control={infoDialogControl}
        post={post}
        settings={settings}
      />
      {isThreadAuthor && (
        <ThreadgateEditorDialog
          control={editDialogControl}
          threadgate={settings}
          onConfirm={onEditConfirm}
        />
      )}
    </>
  )
}

function Icon({
  color,
  width,
  settings,
}: {
  color: string
  width?: number
  settings: ThreadgateSetting[]
}) {
  const isEverybody = settings.length === 0
  const isNobody = !!settings.find(gate => gate.type === 'nobody')
  const IconComponent = isEverybody ? Earth : isNobody ? CircleBanSign : Group
  return <IconComponent fill={color} width={width} />
}

function WhoCanReplyDialog({
  control,
  post,
  settings,
}: {
  control: Dialog.DialogControlProps
  post: AppBskyFeedDefs.PostView
  settings: ThreadgateSetting[]
}) {
  return (
    <Dialog.Outer control={control}>
      <Dialog.Handle />
      <WhoCanReplyDialogInner post={post} settings={settings} />
    </Dialog.Outer>
  )
}

function WhoCanReplyDialogInner({
  post,
  settings,
}: {
  post: AppBskyFeedDefs.PostView
  settings: ThreadgateSetting[]
}) {
  const {_} = useLingui()
  return (
    <Dialog.ScrollableInner
      label={_(msg`Who can reply dialog`)}
      style={[{width: 'auto', maxWidth: 400, minWidth: 200}]}>
      <View style={[a.gap_sm]}>
        <Text style={[a.font_bold, a.text_xl]}>
          <Trans>Who can reply?</Trans>
        </Text>
        <Rules post={post} settings={settings} />
      </View>
    </Dialog.ScrollableInner>
  )
}

function Rules({
  post,
  settings,
}: {
  post: AppBskyFeedDefs.PostView
  settings: ThreadgateSetting[]
}) {
  const t = useTheme()
  return (
    <Text
      style={[
        a.text_md,
        a.leading_tight,
        a.flex_wrap,
        t.atoms.text_contrast_medium,
      ]}>
      {!settings.length ? (
        <Trans>Everybody can reply</Trans>
      ) : settings[0].type === 'nobody' ? (
        <Trans>Replies to this thread are disabled</Trans>
      ) : (
        <Trans>
          Only{' '}
          {settings.map((rule, i) => (
            <>
              <Rule
                key={`rule-${i}`}
                rule={rule}
                post={post}
                lists={post.threadgate!.lists}
              />
              <Separator key={`sep-${i}`} i={i} length={settings.length} />
            </>
          ))}{' '}
          can reply
        </Trans>
      )}
    </Text>
  )
}

function Rule({
  rule,
  post,
  lists,
}: {
  rule: ThreadgateSetting
  post: AppBskyFeedDefs.PostView
  lists: AppBskyGraphDefs.ListViewBasic[] | undefined
}) {
  const t = useTheme()
  if (rule.type === 'mention') {
    return <Trans>mentioned users</Trans>
  }
  if (rule.type === 'following') {
    return (
      <Trans>
        users followed by{' '}
        <TextLink
          type="sm"
          href={makeProfileLink(post.author)}
          text={`@${post.author.handle}`}
          style={{color: t.palette.primary_500}}
        />
      </Trans>
    )
  }
  if (rule.type === 'list') {
    const list = lists?.find(l => l.uri === rule.list)
    if (list) {
      const listUrip = new AtUri(list.uri)
      return (
        <Trans>
          <TextLink
            type="sm"
            href={makeListLink(listUrip.hostname, listUrip.rkey)}
            text={list.name}
            style={{color: t.palette.primary_500}}
          />{' '}
          members
        </Trans>
      )
    }
  }
}

function Separator({i, length}: {i: number; length: number}) {
  if (length < 2 || i === length - 1) {
    return null
  }
  if (i === length - 2) {
    return (
      <>
        {length > 2 ? ',' : ''} <Trans>and</Trans>{' '}
      </>
    )
  }
  return <>, </>
}

async function whenAppViewReady(
  agent: BskyAgent,
  uri: string,
  fn: (res: AppBskyFeedGetPostThread.Response) => boolean,
) {
  await until(
    5, // 5 tries
    1e3, // 1s delay between tries
    fn,
    () =>
      agent.app.bsky.feed.getPostThread({
        uri,
        depth: 0,
      }),
  )
}