2016-03-10 03:33:31 +08:00
|
|
|
import React from 'react';
|
|
|
|
import {Actions, FocusedContactsStore} from 'nylas-exports'
|
|
|
|
|
|
|
|
const SPLIT_KEY = "---splitvalue---"
|
|
|
|
|
|
|
|
export default class SidebarParticipantPicker extends React.Component {
|
|
|
|
static displayName = 'SidebarParticipantPicker';
|
|
|
|
|
2016-05-07 07:06:16 +08:00
|
|
|
static containerStyles = {
|
|
|
|
order: 0,
|
|
|
|
flexShrink: 0,
|
|
|
|
};
|
|
|
|
|
2016-03-10 03:33:31 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = this._getStateFromStores();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this._usub = FocusedContactsStore.listen(() => {
|
|
|
|
return this.setState(this._getStateFromStores());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this._usub();
|
|
|
|
}
|
|
|
|
|
|
|
|
_getStateFromStores() {
|
|
|
|
return {
|
|
|
|
sortedContacts: FocusedContactsStore.sortedContacts(),
|
|
|
|
focusedContact: FocusedContactsStore.focusedContact(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-04-06 05:49:03 +08:00
|
|
|
_getKeyForContact(contact) {
|
|
|
|
if (!contact) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
return contact.email + SPLIT_KEY + contact.name
|
2016-03-10 03:33:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_onSelectContact = (event) => {
|
fix(contact-sidebar): Correctly update selected contact
Sometimes, when selecting a contact (with name, email) inside a thread,
the dropdown (`<select>`) did not correctly reflect the selected
contact.
This was because when focusing a contact, the FocusedContactStore
queried that contact from the database just by email, and the contact
retured from the database became the focused one. However, sometimes the
returned contact might have the same email but different email, and
given that the `<select>` component is keyed by both name,email, it
couldn't find the appropriate <option> to render, so it could not update
to reflect the newly selected contact
Now, the FocusedContactStore queries by email and name to prevent this
2016-09-27 07:30:11 +08:00
|
|
|
const {sortedContacts} = this.state
|
2016-03-10 03:33:31 +08:00
|
|
|
const [email, name] = event.target.value.split(SPLIT_KEY);
|
fix(contact-sidebar): Correctly update selected contact
Sometimes, when selecting a contact (with name, email) inside a thread,
the dropdown (`<select>`) did not correctly reflect the selected
contact.
This was because when focusing a contact, the FocusedContactStore
queried that contact from the database just by email, and the contact
retured from the database became the focused one. However, sometimes the
returned contact might have the same email but different email, and
given that the `<select>` component is keyed by both name,email, it
couldn't find the appropriate <option> to render, so it could not update
to reflect the newly selected contact
Now, the FocusedContactStore queries by email and name to prevent this
2016-09-27 07:30:11 +08:00
|
|
|
const contact = sortedContacts.find((c) => c.name === name && c.email === email)
|
2016-03-10 03:33:31 +08:00
|
|
|
return Actions.focusContact(contact);
|
|
|
|
}
|
|
|
|
|
2016-04-06 05:49:03 +08:00
|
|
|
_renderSortedContacts() {
|
|
|
|
return this.state.sortedContacts.map((contact) => {
|
|
|
|
const key = this._getKeyForContact(contact)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<option value={key} key={key}>
|
|
|
|
{contact.displayName({includeAccountLabel: true, forceAccountLabel: true})}
|
|
|
|
</option>
|
|
|
|
)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-03-10 03:33:31 +08:00
|
|
|
render() {
|
2016-04-08 00:35:29 +08:00
|
|
|
const {sortedContacts, focusedContact} = this.state
|
2016-04-06 05:49:03 +08:00
|
|
|
const value = this._getKeyForContact(focusedContact)
|
2016-10-26 05:08:13 +08:00
|
|
|
if (sortedContacts.length === 0 || !value) {
|
2016-04-08 00:35:29 +08:00
|
|
|
return false
|
|
|
|
}
|
2016-03-10 03:33:31 +08:00
|
|
|
return (
|
|
|
|
<div className="sidebar-participant-picker">
|
2016-04-06 05:49:03 +08:00
|
|
|
<select tabIndex={-1} value={value} onChange={this._onSelectContact}>
|
fix(contact-sidebar): Correctly update selected contact
Sometimes, when selecting a contact (with name, email) inside a thread,
the dropdown (`<select>`) did not correctly reflect the selected
contact.
This was because when focusing a contact, the FocusedContactStore
queried that contact from the database just by email, and the contact
retured from the database became the focused one. However, sometimes the
returned contact might have the same email but different email, and
given that the `<select>` component is keyed by both name,email, it
couldn't find the appropriate <option> to render, so it could not update
to reflect the newly selected contact
Now, the FocusedContactStore queries by email and name to prevent this
2016-09-27 07:30:11 +08:00
|
|
|
{this._renderSortedContacts()}
|
2016-03-10 03:33:31 +08:00
|
|
|
</select>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|