about summary refs log tree commit diff
path: root/src/view/shell/mobile/Menu.tsx
blob: 26cb5b9bd29ae38cab4da1dd1a6d61c6004edcc2 (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
import React from 'react'
import {
  ScrollView,
  StyleProp,
  StyleSheet,
  TouchableOpacity,
  View,
  ViewStyle,
} from 'react-native'
import {observer} from 'mobx-react-lite'
import VersionNumber from 'react-native-version-number'
import {s, colors} from '../../lib/styles'
import {useStores} from '../../../state'
import {
  HomeIcon,
  BellIcon,
  UserIcon,
  CogIcon,
  MagnifyingGlassIcon,
} from '../../lib/icons'
import {UserAvatar} from '../../com/util/UserAvatar'
import {Text} from '../../com/util/text/Text'
import {ToggleButton} from '../../com/util/forms/ToggleButton'
import {usePalette} from '../../lib/hooks/usePalette'

export const Menu = observer(
  ({visible, onClose}: {visible: boolean; onClose: () => void}) => {
    const pal = usePalette('default')
    const store = useStores()

    // events
    // =

    const onNavigate = (url: string) => {
      onClose()
      if (url === '/notifications') {
        store.nav.switchTo(1, true)
      } else {
        store.nav.switchTo(0, true)
        if (url !== '/') {
          store.nav.navigate(url)
        }
      }
    }

    // rendering
    // =

    const MenuItem = ({
      icon,
      label,
      count,
      url,
      bold,
      onPress,
    }: {
      icon: JSX.Element
      label: string
      count?: number
      url?: string
      bold?: boolean
      onPress?: () => void
    }) => (
      <TouchableOpacity
        testID={`menuItemButton-${label}`}
        style={styles.menuItem}
        onPress={onPress ? onPress : () => onNavigate(url || '/')}>
        <View style={[styles.menuItemIconWrapper]}>
          {icon}
          {count ? (
            <View style={styles.menuItemCount}>
              <Text style={styles.menuItemCountLabel}>{count}</Text>
            </View>
          ) : undefined}
        </View>
        <Text
          type="title"
          style={[
            pal.text,
            bold ? styles.menuItemLabelBold : styles.menuItemLabel,
          ]}
          numberOfLines={1}>
          {label}
        </Text>
      </TouchableOpacity>
    )

    return (
      <ScrollView testID="menuView" style={[styles.view, pal.view]}>
        <TouchableOpacity
          testID="profileCardButton"
          onPress={() => onNavigate(`/profile/${store.me.handle}`)}
          style={styles.profileCard}>
          <UserAvatar
            size={60}
            displayName={store.me.displayName}
            handle={store.me.handle}
            avatar={store.me.avatar}
          />
          <View style={s.flex1}>
            <Text
              type="title-lg"
              style={[pal.text, styles.profileCardDisplayName]}
              numberOfLines={1}>
              {store.me.displayName || store.me.handle}
            </Text>
            <Text
              style={[pal.textLight, styles.profileCardHandle]}
              numberOfLines={1}>
              @{store.me.handle}
            </Text>
          </View>
        </TouchableOpacity>
        <TouchableOpacity
          testID="searchBtn"
          style={[styles.searchBtn, pal.btn]}
          onPress={() => onNavigate('/search')}>
          <MagnifyingGlassIcon
            style={pal.text as StyleProp<ViewStyle>}
            size={25}
          />
          <Text type="title" style={[pal.text, styles.searchBtnLabel]}>
            Search
          </Text>
        </TouchableOpacity>
        <View style={[styles.section, pal.border, {paddingTop: 5}]}>
          <MenuItem
            icon={
              <HomeIcon style={pal.text as StyleProp<ViewStyle>} size="26" />
            }
            label="Home"
            url="/"
          />
          <MenuItem
            icon={
              <BellIcon style={pal.text as StyleProp<ViewStyle>} size="28" />
            }
            label="Notifications"
            url="/notifications"
            count={store.me.notificationCount}
          />
          <MenuItem
            icon={
              <UserIcon
                style={pal.text as StyleProp<ViewStyle>}
                size="30"
                strokeWidth={2}
              />
            }
            label="Profile"
            url={`/profile/${store.me.handle}`}
          />
          <MenuItem
            icon={
              <CogIcon
                style={pal.text as StyleProp<ViewStyle>}
                size="30"
                strokeWidth={2}
              />
            }
            label="Settings"
            url="/settings"
          />
        </View>
        <View style={[styles.section, pal.border]}>
          <ToggleButton
            label="Dark mode"
            isSelected={store.shell.darkMode}
            onPress={() => store.shell.setDarkMode(!store.shell.darkMode)}
          />
        </View>
        <View style={styles.footer}>
          <Text style={[pal.textLight]}>
            Build version {VersionNumber.appVersion} (
            {VersionNumber.buildVersion})
          </Text>
        </View>
        <View style={s.footerSpacer} />
      </ScrollView>
    )
  },
)

const styles = StyleSheet.create({
  view: {
    flex: 1,
  },
  section: {
    paddingHorizontal: 10,
    paddingTop: 10,
    paddingBottom: 10,
    borderBottomWidth: 1,
  },
  heading: {
    paddingVertical: 8,
    paddingHorizontal: 4,
  },

  profileCard: {
    flexDirection: 'row',
    alignItems: 'center',
    margin: 10,
    marginBottom: 6,
  },
  profileCardDisplayName: {
    marginLeft: 12,
  },
  profileCardHandle: {
    marginLeft: 12,
  },

  searchBtn: {
    flexDirection: 'row',
    borderRadius: 8,
    margin: 10,
    marginBottom: 0,
    paddingVertical: 10,
    paddingHorizontal: 12,
  },
  searchBtnLabel: {
    marginLeft: 14,
    fontWeight: 'normal',
  },

  menuItem: {
    flexDirection: 'row',
    alignItems: 'center',
    paddingVertical: 6,
    paddingLeft: 6,
    paddingRight: 10,
  },
  menuItemIconWrapper: {
    width: 36,
    height: 36,
    alignItems: 'center',
    justifyContent: 'center',
    marginRight: 12,
  },
  menuItemLabel: {
    flex: 1,
    fontWeight: 'normal',
  },
  menuItemLabelBold: {
    flex: 1,
    fontWeight: 'bold',
  },
  menuItemCount: {
    position: 'absolute',
    right: -6,
    top: -2,
    backgroundColor: colors.red3,
    paddingHorizontal: 4,
    paddingBottom: 1,
    borderRadius: 6,
  },
  menuItemCountLabel: {
    fontSize: 12,
    fontWeight: 'bold',
    color: colors.white,
  },

  footer: {
    paddingHorizontal: 14,
    paddingVertical: 18,
  },
})