about summary refs log tree commit diff
path: root/src/view/screens/Settings.tsx
blob: d659d25d46eac7e023f53f441914ff2f0ec6b1d6 (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
import React, {useEffect} from 'react'
import {
  ActivityIndicator,
  ScrollView,
  StyleSheet,
  TouchableOpacity,
  View,
} from 'react-native'
import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome'
import {observer} from 'mobx-react-lite'
import {useStores} from '../../state'
import {ScreenParams} from '../routes'
import {s} from '../lib/styles'
import {ViewHeader} from '../com/util/ViewHeader'
import {Link} from '../com/util/Link'
import {Text} from '../com/util/text/Text'
import * as Toast from '../com/util/Toast'
import {UserAvatar} from '../com/util/UserAvatar'
import {usePalette} from '../lib/hooks/usePalette'
import {AccountData} from '../../state/models/session'

export const Settings = observer(function Settings({
  navIdx,
  visible,
}: ScreenParams) {
  const pal = usePalette('default')
  const store = useStores()
  const [isSwitching, setIsSwitching] = React.useState(false)

  useEffect(() => {
    if (!visible) {
      return
    }
    store.shell.setMinimalShellMode(false)
    store.nav.setTitle(navIdx, 'Settings')
  }, [visible, store, navIdx])

  const onPressSwitchAccount = async (acct: AccountData) => {
    setIsSwitching(true)
    if (await store.session.resumeSession(acct)) {
      setIsSwitching(false)
      Toast.show(`Signed in as ${acct.displayName || acct.handle}`)
      return
    }
    setIsSwitching(false)
    Toast.show('Sorry! We need you to enter your password.')
    store.session.clear()
  }
  const onPressAddAccount = () => {
    store.session.clear()
  }
  const onPressSignout = () => {
    store.session.logout()
  }

  return (
    <View style={[s.h100pct]} testID="settingsScreen">
      <ViewHeader title="Settings" />
      <ScrollView style={[s.mt10, s.pl10, s.pr10, s.h100pct]}>
        <View style={[s.flexRow]}>
          <Text type="xl-bold" style={pal.text}>
            Signed in as
          </Text>
          <View style={s.flex1} />
          <TouchableOpacity
            testID="signOutBtn"
            onPress={isSwitching ? undefined : onPressSignout}>
            <Text type="xl-medium" style={pal.link}>
              Sign out
            </Text>
          </TouchableOpacity>
        </View>
        {isSwitching ? (
          <View style={[pal.view, styles.profile]}>
            <ActivityIndicator />
          </View>
        ) : (
          <Link
            href={`/profile/${store.me.handle}`}
            title="Your profile"
            noFeedback>
            <View style={[pal.view, styles.profile]}>
              <UserAvatar
                size={40}
                displayName={store.me.displayName}
                handle={store.me.handle || ''}
                avatar={store.me.avatar}
              />
              <View style={[s.ml10]}>
                <Text type="xl-bold" style={pal.text}>
                  {store.me.displayName || store.me.handle}
                </Text>
                <Text style={pal.textLight}>@{store.me.handle}</Text>
              </View>
            </View>
          </Link>
        )}
        <Text type="sm-medium" style={pal.text}>
          Switch to:
        </Text>
        {store.session.switchableAccounts.map(account => (
          <TouchableOpacity
            testID={`switchToAccountBtn-${account.handle}`}
            key={account.did}
            style={[
              pal.view,
              styles.profile,
              s.mb2,
              isSwitching && styles.dimmed,
            ]}
            onPress={
              isSwitching ? undefined : () => onPressSwitchAccount(account)
            }>
            <UserAvatar
              size={40}
              displayName={account.displayName}
              handle={account.handle || ''}
              avatar={account.aviUrl}
            />
            <View style={[s.ml10]}>
              <Text type="xl-bold" style={pal.text}>
                {account.displayName || account.handle}
              </Text>
              <Text style={pal.textLight}>@{account.handle}</Text>
            </View>
          </TouchableOpacity>
        ))}
        <TouchableOpacity
          testID="switchToNewAccountBtn"
          style={[
            pal.view,
            styles.profile,
            styles.alignCenter,
            s.mb2,
            isSwitching && styles.dimmed,
          ]}
          onPress={isSwitching ? undefined : onPressAddAccount}>
          <FontAwesomeIcon icon="plus" />
          <View style={[s.ml5]}>
            <Text type="md-medium" style={pal.text}>
              Add account
            </Text>
          </View>
        </TouchableOpacity>
        <View style={styles.spacer} />
        <Text type="sm-medium" style={[s.mb5]}>
          Developer tools
        </Text>
        <Link
          style={[pal.view, s.p10, s.mb2]}
          href="/sys/log"
          title="System log">
          <Text style={pal.link}>System log</Text>
        </Link>
        <Link
          style={[pal.view, s.p10, s.mb2]}
          href="/sys/debug"
          title="Debug tools">
          <Text style={pal.link}>Storybook</Text>
        </Link>
        <View style={s.footerSpacer} />
      </ScrollView>
    </View>
  )
})

const styles = StyleSheet.create({
  dimmed: {
    opacity: 0.5,
  },
  spacer: {
    height: 50,
  },
  alignCenter: {
    alignItems: 'center',
  },
  title: {
    fontSize: 32,
    fontWeight: 'bold',
    marginTop: 20,
    marginBottom: 14,
  },
  profile: {
    flexDirection: 'row',
    marginVertical: 6,
    borderRadius: 4,
    paddingVertical: 10,
    paddingHorizontal: 10,
  },
  avi: {
    width: 40,
    height: 40,
    borderRadius: 30,
    marginRight: 8,
  },
})