about summary refs log tree commit diff
path: root/src/components/ReportDialog/index.tsx
blob: 2401ab89f55015cbe6804b3784ae1e2f421ca23a (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
import React from 'react'
import {Pressable, View} from 'react-native'
import {type ScrollView} from 'react-native-gesture-handler'
import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react'

import {type ReportOption} from '#/lib/moderation/useReportOptions'
import {useMyLabelersQuery} from '#/state/queries/preferences'
export {useDialogControl as useReportDialogControl} from '#/components/Dialog'

import {type AppBskyLabelerDefs} from '@atproto/api'

import {atoms as a} from '#/alf'
import * as Dialog from '#/components/Dialog'
import {useDelayedLoading} from '#/components/hooks/useDelayedLoading'
import {Loader} from '#/components/Loader'
import {Text} from '#/components/Typography'
import {SelectLabelerView} from './SelectLabelerView'
import {SelectReportOptionView} from './SelectReportOptionView'
import {SubmitView} from './SubmitView'
import {type ReportDialogProps} from './types'

export function ReportDialog(props: ReportDialogProps) {
  return (
    <Dialog.Outer control={props.control}>
      <Dialog.Handle />
      <ReportDialogInner {...props} />
    </Dialog.Outer>
  )
}

function ReportDialogInner(props: ReportDialogProps) {
  const {_} = useLingui()
  const {
    isLoading: isLabelerLoading,
    data: labelers,
    error,
  } = useMyLabelersQuery({excludeNonConfigurableLabelers: true})
  const isLoading = useDelayedLoading(500, isLabelerLoading)

  const ref = React.useRef<ScrollView>(null)

  return (
    <Dialog.ScrollableInner label={_(msg`Report dialog`)} ref={ref}>
      {isLoading ? (
        <View style={[a.align_center, {height: 100}]}>
          <Loader size="xl" />
          {/* Here to capture focus for a hot sec to prevent flash */}
          <Pressable accessible={false} />
        </View>
      ) : error || !labelers ? (
        <View>
          <Text style={[a.text_md]}>
            <Trans>Something went wrong, please try again.</Trans>
          </Text>
        </View>
      ) : (
        <ReportDialogLoaded labelers={labelers} {...props} />
      )}
    </Dialog.ScrollableInner>
  )
}

function ReportDialogLoaded(
  props: ReportDialogProps & {
    labelers: AppBskyLabelerDefs.LabelerViewDetailed[]
  },
) {
  const [selectedLabeler, setSelectedLabeler] = React.useState<
    string | undefined
  >(props.labelers.length === 1 ? props.labelers[0].creator.did : undefined)
  const [selectedReportOption, setSelectedReportOption] = React.useState<
    ReportOption | undefined
  >()

  if (selectedReportOption && selectedLabeler) {
    return (
      <SubmitView
        {...props}
        selectedLabeler={selectedLabeler}
        selectedReportOption={selectedReportOption}
        goBack={() => setSelectedReportOption(undefined)}
        onSubmitComplete={() => props.control.close()}
      />
    )
  }
  if (selectedLabeler) {
    return (
      <SelectReportOptionView
        {...props}
        goBack={() => setSelectedLabeler(undefined)}
        onSelectReportOption={setSelectedReportOption}
      />
    )
  }
  return <SelectLabelerView {...props} onSelectLabeler={setSelectedLabeler} />
}