about summary refs log tree commit diff
path: root/src/view/com/auth/create/Step1.tsx
blob: 7e3ea062dc3644df3e6a0a4914300237587de64e (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
import React from 'react'
import {StyleSheet, TouchableWithoutFeedback, View} from 'react-native'
import {observer} from 'mobx-react-lite'
import debounce from 'lodash.debounce'
import {Text} from 'view/com/util/text/Text'
import {StepHeader} from './StepHeader'
import {CreateAccountModel} from 'state/models/ui/create-account'
import {useTheme} from 'lib/ThemeContext'
import {usePalette} from 'lib/hooks/usePalette'
import {s} from 'lib/styles'
import {HelpTip} from '../util/HelpTip'
import {TextInput} from '../util/TextInput'
import {Button} from 'view/com/util/forms/Button'
import {ErrorMessage} from 'view/com/util/error/ErrorMessage'
import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react'

import {LOCAL_DEV_SERVICE, STAGING_SERVICE, PROD_SERVICE} from 'state/index'
import {LOGIN_INCLUDE_DEV_SERVERS} from 'lib/build-flags'

/** STEP 1: Your hosting provider
 * @field Bluesky (default)
 * @field Other (staging, local dev, your own PDS, etc.)
 */
export const Step1 = observer(function Step1Impl({
  model,
}: {
  model: CreateAccountModel
}) {
  const pal = usePalette('default')
  const [isDefaultSelected, setIsDefaultSelected] = React.useState(true)
  const {_} = useLingui()

  const onPressDefault = React.useCallback(() => {
    setIsDefaultSelected(true)
    model.setServiceUrl(PROD_SERVICE)
    model.fetchServiceDescription()
  }, [setIsDefaultSelected, model])

  const onPressOther = React.useCallback(() => {
    setIsDefaultSelected(false)
    model.setServiceUrl('https://')
    model.setServiceDescription(undefined)
  }, [setIsDefaultSelected, model])

  const fetchServiceDescription = React.useMemo(
    () => debounce(() => model.fetchServiceDescription(), 1e3), // debouce for 1 second (1e3 = 1000ms)
    [model],
  )

  const onChangeServiceUrl = React.useCallback(
    (v: string) => {
      model.setServiceUrl(v)
      fetchServiceDescription()
    },
    [model, fetchServiceDescription],
  )

  const onDebugChangeServiceUrl = React.useCallback(
    (v: string) => {
      model.setServiceUrl(v)
      model.fetchServiceDescription()
    },
    [model],
  )

  return (
    <View>
      <StepHeader step="1" title={_(msg`Your hosting provider`)} />
      <Text style={[pal.text, s.mb10]}>
        <Trans>This is the service that keeps you online.</Trans>
      </Text>
      <Option
        testID="blueskyServerBtn"
        isSelected={isDefaultSelected}
        label="Bluesky"
        help="&nbsp;(default)"
        onPress={onPressDefault}
      />
      <Option
        testID="otherServerBtn"
        isSelected={!isDefaultSelected}
        label="Other"
        onPress={onPressOther}>
        <View style={styles.otherForm}>
          <Text nativeID="addressProvider" style={[pal.text, s.mb5]}>
            <Trans>Enter the address of your provider:</Trans>
          </Text>
          <TextInput
            testID="customServerInput"
            icon="globe"
            placeholder={_(msg`Hosting provider address`)}
            value={model.serviceUrl}
            editable
            onChange={onChangeServiceUrl}
            accessibilityHint="Input hosting provider address"
            accessibilityLabel={_(msg`Hosting provider address`)}
            accessibilityLabelledBy="addressProvider"
          />
          {LOGIN_INCLUDE_DEV_SERVERS && (
            <View style={[s.flexRow, s.mt10]}>
              <Button
                testID="stagingServerBtn"
                type="default"
                style={s.mr5}
                label={_(msg`Staging`)}
                onPress={() => onDebugChangeServiceUrl(STAGING_SERVICE)}
              />
              <Button
                testID="localDevServerBtn"
                type="default"
                label={_(msg`Dev Server`)}
                onPress={() => onDebugChangeServiceUrl(LOCAL_DEV_SERVICE)}
              />
            </View>
          )}
        </View>
      </Option>
      {model.error ? (
        <ErrorMessage message={model.error} style={styles.error} />
      ) : (
        <HelpTip text={_(msg`You can change hosting providers at any time.`)} />
      )}
    </View>
  )
})

function Option({
  children,
  isSelected,
  label,
  help,
  onPress,
  testID,
}: React.PropsWithChildren<{
  isSelected: boolean
  label: string
  help?: string
  onPress: () => void
  testID?: string
}>) {
  const theme = useTheme()
  const pal = usePalette('default')
  const circleFillStyle = React.useMemo(
    () => ({
      backgroundColor: theme.palette.primary.background,
    }),
    [theme],
  )

  return (
    <View style={[styles.option, pal.border]}>
      <TouchableWithoutFeedback
        onPress={onPress}
        testID={testID}
        accessibilityRole="button"
        accessibilityLabel={label}
        accessibilityHint={`Sets hosting provider to ${label}`}>
        <View style={styles.optionHeading}>
          <View style={[styles.circle, pal.border]}>
            {isSelected ? (
              <View style={[circleFillStyle, styles.circleFill]} />
            ) : undefined}
          </View>
          <Text type="xl" style={pal.text}>
            {label}
            {help ? (
              <Text type="xl" style={pal.textLight}>
                {help}
              </Text>
            ) : undefined}
          </Text>
        </View>
      </TouchableWithoutFeedback>
      {isSelected && children}
    </View>
  )
}

const styles = StyleSheet.create({
  error: {
    borderRadius: 6,
  },

  option: {
    borderWidth: 1,
    borderRadius: 6,
    marginBottom: 10,
  },
  optionHeading: {
    flexDirection: 'row',
    alignItems: 'center',
    padding: 10,
  },
  circle: {
    width: 26,
    height: 26,
    borderRadius: 15,
    padding: 4,
    borderWidth: 1,
    marginRight: 10,
  },
  circleFill: {
    width: 16,
    height: 16,
    borderRadius: 10,
  },

  otherForm: {
    paddingBottom: 10,
    paddingHorizontal: 12,
  },
})