about summary refs log tree commit diff
path: root/src/components/moderation/LabelPreference.tsx
blob: edbb12d0cb07c59cfdd390e1c5e41c3d3a4f1bed (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
import {View} from 'react-native'
import {
  type InterpretedLabelValueDefinition,
  type LabelPreference,
} from '@atproto/api'
import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react'
import type React from 'react'

import {useGlobalLabelStrings} from '#/lib/moderation/useGlobalLabelStrings'
import {useLabelBehaviorDescription} from '#/lib/moderation/useLabelBehaviorDescription'
import {getLabelStrings} from '#/lib/moderation/useLabelInfo'
import {
  usePreferencesQuery,
  usePreferencesSetContentLabelMutation,
} from '#/state/queries/preferences'
import {atoms as a, useBreakpoints, useTheme} from '#/alf'
import * as ToggleButton from '#/components/forms/ToggleButton'
import {InlineLinkText} from '#/components/Link'
import {Text} from '#/components/Typography'
import {CircleInfo_Stroke2_Corner0_Rounded as CircleInfo} from '../icons/CircleInfo'

export function Outer({children}: React.PropsWithChildren<{}>) {
  return (
    <View
      style={[
        a.flex_row,
        a.gap_sm,
        a.px_lg,
        a.py_lg,
        a.justify_between,
        a.flex_wrap,
      ]}>
      {children}
    </View>
  )
}

export function Content({
  children,
  name,
  description,
}: React.PropsWithChildren<{
  name: string
  description: string
}>) {
  const t = useTheme()
  const {gtPhone} = useBreakpoints()

  return (
    <View style={[a.gap_xs, a.flex_1]}>
      <Text emoji style={[a.font_bold, gtPhone ? a.text_sm : a.text_md]}>
        {name}
      </Text>
      <Text emoji style={[t.atoms.text_contrast_medium, a.leading_snug]}>
        {description}
      </Text>

      {children}
    </View>
  )
}

export function Buttons({
  name,
  values,
  onChange,
  ignoreLabel,
  warnLabel,
  hideLabel,
  disabled,
}: {
  name: string
  values: ToggleButton.GroupProps['values']
  onChange: ToggleButton.GroupProps['onChange']
  ignoreLabel?: string
  warnLabel?: string
  hideLabel?: string
  disabled?: boolean
}) {
  const {_} = useLingui()

  return (
    <View style={[{minHeight: 35}, a.w_full]}>
      <ToggleButton.Group
        disabled={disabled}
        label={_(
          msg`Configure content filtering setting for category: ${name}`,
        )}
        values={values}
        onChange={onChange}>
        {ignoreLabel && (
          <ToggleButton.Button name="ignore" label={ignoreLabel}>
            <ToggleButton.ButtonText>{ignoreLabel}</ToggleButton.ButtonText>
          </ToggleButton.Button>
        )}
        {warnLabel && (
          <ToggleButton.Button name="warn" label={warnLabel}>
            <ToggleButton.ButtonText>{warnLabel}</ToggleButton.ButtonText>
          </ToggleButton.Button>
        )}
        {hideLabel && (
          <ToggleButton.Button name="hide" label={hideLabel}>
            <ToggleButton.ButtonText>{hideLabel}</ToggleButton.ButtonText>
          </ToggleButton.Button>
        )}
      </ToggleButton.Group>
    </View>
  )
}

/**
 * For use on the global Moderation screen to set prefs for a "global" label,
 * not scoped to a single labeler.
 */
export function GlobalLabelPreference({
  labelDefinition,
  disabled,
}: {
  labelDefinition: InterpretedLabelValueDefinition
  disabled?: boolean
}) {
  const {_} = useLingui()

  const {identifier} = labelDefinition
  const {data: preferences} = usePreferencesQuery()
  const {mutate, variables} = usePreferencesSetContentLabelMutation()
  const savedPref = preferences?.moderationPrefs.labels[identifier]
  const pref = variables?.visibility ?? savedPref ?? 'warn'

  const allLabelStrings = useGlobalLabelStrings()
  const labelStrings =
    labelDefinition.identifier in allLabelStrings
      ? allLabelStrings[labelDefinition.identifier]
      : {
          name: labelDefinition.identifier,
          description: `Labeled "${labelDefinition.identifier}"`,
        }

  const labelOptions = {
    hide: _(msg`Hide`),
    warn: _(msg`Warn`),
    ignore: _(msg`Show`),
  }

  return (
    <Outer>
      <Content
        name={labelStrings.name}
        description={labelStrings.description}
      />
      <Buttons
        name={labelStrings.name.toLowerCase()}
        values={[pref]}
        onChange={values => {
          mutate({
            label: identifier,
            visibility: values[0] as LabelPreference,
            labelerDid: undefined,
          })
        }}
        ignoreLabel={labelOptions.ignore}
        warnLabel={labelOptions.warn}
        hideLabel={labelOptions.hide}
        disabled={disabled}
      />
    </Outer>
  )
}

/**
 * For use on individual labeler pages
 */
export function LabelerLabelPreference({
  labelDefinition,
  disabled,
  labelerDid,
}: {
  labelDefinition: InterpretedLabelValueDefinition
  disabled?: boolean
  labelerDid?: string
}) {
  const {_, i18n} = useLingui()
  const t = useTheme()
  const {gtPhone} = useBreakpoints()

  const isGlobalLabel = !labelDefinition.definedBy
  const {identifier} = labelDefinition
  const {data: preferences} = usePreferencesQuery()
  const {mutate, variables} = usePreferencesSetContentLabelMutation()
  const savedPref =
    labelerDid && !isGlobalLabel
      ? preferences?.moderationPrefs.labelers.find(l => l.did === labelerDid)
          ?.labels[identifier]
      : preferences?.moderationPrefs.labels[identifier]
  const pref =
    variables?.visibility ??
    savedPref ??
    labelDefinition.defaultSetting ??
    'warn'

  // does the 'warn' setting make sense for this label?
  const canWarn = !(
    labelDefinition.blurs === 'none' && labelDefinition.severity === 'none'
  )
  // is this label adult only?
  const adultOnly = labelDefinition.flags.includes('adult')
  // is this label disabled because it's adult only?
  const adultDisabled =
    adultOnly && !preferences?.moderationPrefs.adultContentEnabled
  // are there any reasons we cant configure this label here?
  const cantConfigure = isGlobalLabel || adultDisabled
  const showConfig = !disabled && (gtPhone || !cantConfigure)

  // adjust the pref based on whether warn is available
  let prefAdjusted = pref
  if (adultDisabled) {
    prefAdjusted = 'hide'
  } else if (!canWarn && pref === 'warn') {
    prefAdjusted = 'ignore'
  }

  // grab localized descriptions of the label and its settings
  const currentPrefLabel = useLabelBehaviorDescription(
    labelDefinition,
    prefAdjusted,
  )
  const hideLabel = useLabelBehaviorDescription(labelDefinition, 'hide')
  const warnLabel = useLabelBehaviorDescription(labelDefinition, 'warn')
  const ignoreLabel = useLabelBehaviorDescription(labelDefinition, 'ignore')
  const globalLabelStrings = useGlobalLabelStrings()
  const labelStrings = getLabelStrings(
    i18n.locale,
    globalLabelStrings,
    labelDefinition,
  )

  return (
    <Outer>
      <Content name={labelStrings.name} description={labelStrings.description}>
        {cantConfigure && (
          <View style={[a.flex_row, a.gap_xs, a.align_center, a.mt_xs]}>
            <CircleInfo size="sm" fill={t.atoms.text_contrast_high.color} />

            <Text style={[t.atoms.text_contrast_medium, a.font_bold, a.italic]}>
              {adultDisabled ? (
                <Trans>Adult content is disabled.</Trans>
              ) : isGlobalLabel ? (
                <Trans>
                  Configured in{' '}
                  <InlineLinkText
                    label={_(msg`moderation settings`)}
                    to="/moderation"
                    style={a.text_sm}>
                    moderation settings
                  </InlineLinkText>
                  .
                </Trans>
              ) : null}
            </Text>
          </View>
        )}
      </Content>

      {showConfig && (
        <>
          {cantConfigure ? (
            <View
              style={[
                {minHeight: 35},
                a.px_md,
                a.py_md,
                a.rounded_sm,
                a.border,
                t.atoms.border_contrast_low,
                a.self_start,
              ]}>
              <Text emoji style={[a.font_bold, t.atoms.text_contrast_low]}>
                {currentPrefLabel}
              </Text>
            </View>
          ) : (
            <Buttons
              name={labelStrings.name.toLowerCase()}
              values={[pref]}
              onChange={values => {
                mutate({
                  label: identifier,
                  visibility: values[0] as LabelPreference,
                  labelerDid,
                })
              }}
              ignoreLabel={ignoreLabel}
              warnLabel={canWarn ? warnLabel : undefined}
              hideLabel={hideLabel}
            />
          )}
        </>
      )}
    </Outer>
  )
}