/
Profile.tsx
149 lines (134 loc) · 4.52 KB
/
Profile.tsx
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
import React, { Component } from 'react'
import { connect, DispatchProp } from 'react-redux'
import styles from '../SettingsMenu.css'
import { TextInput } from 'components/common/input'
import { IAppState } from 'reducers'
import { getLocalUsername, getLocalColor, getLocalAvatar } from 'reducers/settings'
import { USERNAME_MAX_LEN } from 'constants/settings'
import { setUsername, setColor } from 'actions/settings'
import { t } from 'locale'
import { AvatarRegistry } from '../../../services/avatar'
import { UserAvatar } from '../../lobby/UserAvatar'
import { ExternalLink } from 'components/common/link'
import { Trans } from 'react-i18next'
import { SettingsProps } from '../types'
import { server_updateUser } from '../../../lobby/actions/users'
import { IReactReduxProps } from '../../../types/redux-thunk'
interface IProps extends SettingsProps {}
interface IConnectedProps {
avatar?: string
username: string
color: string
}
type Props = IProps & IConnectedProps & IReactReduxProps
interface State {
dirty?: boolean
}
class ProfileSettings extends Component<Props, State> {
state: State = {}
private usernameInput: HTMLInputElement | null = null
private get username() {
return this.usernameInput && this.usernameInput.value
}
private get selectedAvatar() {
const { avatar } = this.props
return avatar ? AvatarRegistry.getInstance().getByURI(avatar) : null
}
componentWillUnmount() {
if (this.state.dirty) {
this.props.dispatch(
server_updateUser({
name: this.props.username,
color: this.props.color,
avatar: this.props.avatar
})
)
}
}
render(): JSX.Element | null {
const { selectedAvatar } = this
const hasArtist = selectedAvatar ? !!selectedAvatar.artist : false
return (
<section className={styles.section}>
<label>{t('avatar')}</label>
<div className={styles.avatarContainer}>
<div className={styles.avatarList}>
{AvatarRegistry.getInstance()
.getAll()
.map((avatar, idx) => (
<UserAvatar
key={idx}
avatar={avatar.src}
selected={avatar.uri === this.props.avatar}
onClick={() => {
this.props.setSetting('avatar', avatar.uri)
this.setState({ dirty: true })
ga('event', {
ec: 'settings',
ea: 'select_avatar',
el: avatar.pii ? avatar.type : avatar.uri
})
}}
/>
))}
</div>
{hasArtist && (
<div className={styles.small}>
{/* prettier-ignore */}
<Trans i18nKey="avatarCredit" values={{ artist: selectedAvatar!.artist }}>
<span className={styles.blend}>Selected avatar art by</span>
{selectedAvatar!.href ? (
<ExternalLink href={selectedAvatar!.href!} className="link-alt">Unknown</ExternalLink>
) : (
<span className={styles.blend}>Unknown</span>
)}
</Trans>
</div>
)}
</div>
<label htmlFor="profile_username">{t('displayName')}</label>
<TextInput
id="profile_username"
theRef={e => (this.usernameInput = e)}
defaultValue={this.props.username}
maxLength={USERNAME_MAX_LEN}
onChange={this.onChangeUsername}
onBlur={e => {
if (this.usernameInput) {
this.usernameInput.value = this.props.username
}
}}
/>
<label htmlFor="profile_color">{t('chatColor')}</label>
<input
id="profile_color"
type="color"
className={styles.colorSwatch}
defaultValue={this.props.color}
onChange={e => {
this.props.dispatch(setColor(e.target!.value))
this.setState({ dirty: true })
}}
/>
</section>
)
}
// TODO: debounce
private onChangeUsername = () => {
const { username } = this
if (!username) return
if (username !== this.props.username) {
this.props.dispatch(setUsername(username))
this.setState({ dirty: true })
}
}
}
export default connect(
(state: IAppState): IConnectedProps => {
return {
avatar: getLocalAvatar(state),
username: getLocalUsername(state),
color: getLocalColor(state)
}
}
)(ProfileSettings) as React.ComponentClass<IProps>