Mailspring/internal_packages/message-list/lib/sidebar-participant-picker.jsx
Juan Tejada 6051512276 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-26 16:34:29 -07:00

79 lines
1.9 KiB
JavaScript

import _ from 'underscore'
import React from 'react';
import {Actions, FocusedContactsStore} from 'nylas-exports'
const SPLIT_KEY = "---splitvalue---"
export default class SidebarParticipantPicker extends React.Component {
static displayName = 'SidebarParticipantPicker';
static containerStyles = {
order: 0,
flexShrink: 0,
};
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(),
};
}
_getKeyForContact(contact) {
if (!contact) {
return null
}
return contact.email + SPLIT_KEY + contact.name
}
_onSelectContact = (event) => {
const {sortedContacts} = this.state
const [email, name] = event.target.value.split(SPLIT_KEY);
const contact = sortedContacts.find((c) => c.name === name && c.email === email)
return Actions.focusContact(contact);
}
_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>
)
});
}
render() {
const {sortedContacts, focusedContact} = this.state
const value = this._getKeyForContact(focusedContact)
if (sortedContacts.length === 0) {
return false
}
return (
<div className="sidebar-participant-picker">
<select tabIndex={-1} value={value} onChange={this._onSelectContact}>
{this._renderSortedContacts()}
</select>
</div>
)
}
}