2019-01-14 21:49:58 +00:00
|
|
|
import React from 'react';
|
2019-03-09 02:25:28 +00:00
|
|
|
import { AutoSizer, List } from 'react-virtualized';
|
2019-12-17 18:52:36 +00:00
|
|
|
import { debounce, get } from 'lodash';
|
2019-01-14 21:49:58 +00:00
|
|
|
|
|
|
|
import {
|
|
|
|
ConversationListItem,
|
|
|
|
PropsData as ConversationListItemPropsType,
|
|
|
|
} from './ConversationListItem';
|
|
|
|
import {
|
2019-08-09 00:46:49 +00:00
|
|
|
PropsDataType as SearchResultsProps,
|
2019-01-14 21:49:58 +00:00
|
|
|
SearchResults,
|
|
|
|
} from './SearchResults';
|
|
|
|
import { LocalizerType } from '../types/Util';
|
2019-11-19 23:03:00 +00:00
|
|
|
import { cleanId } from './_util';
|
2019-01-14 21:49:58 +00:00
|
|
|
|
2019-08-09 00:46:49 +00:00
|
|
|
export interface PropsType {
|
2019-01-14 21:49:58 +00:00
|
|
|
conversations?: Array<ConversationListItemPropsType>;
|
2019-03-12 00:20:16 +00:00
|
|
|
archivedConversations?: Array<ConversationListItemPropsType>;
|
2019-11-07 21:36:16 +00:00
|
|
|
selectedConversationId?: string;
|
2019-01-14 21:49:58 +00:00
|
|
|
searchResults?: SearchResultsProps;
|
2019-03-12 00:20:16 +00:00
|
|
|
showArchived?: boolean;
|
|
|
|
|
2019-01-14 21:49:58 +00:00
|
|
|
i18n: LocalizerType;
|
|
|
|
|
|
|
|
// Action Creators
|
2019-03-12 00:20:16 +00:00
|
|
|
startNewConversation: (
|
|
|
|
query: string,
|
|
|
|
options: { regionCode: string }
|
|
|
|
) => void;
|
2019-01-14 21:49:58 +00:00
|
|
|
openConversationInternal: (id: string, messageId?: string) => void;
|
2019-03-12 00:20:16 +00:00
|
|
|
showArchivedConversations: () => void;
|
|
|
|
showInbox: () => void;
|
2019-01-14 21:49:58 +00:00
|
|
|
|
|
|
|
// Render Props
|
|
|
|
renderMainHeader: () => JSX.Element;
|
2019-08-09 00:46:49 +00:00
|
|
|
renderMessageSearchResult: (id: string) => JSX.Element;
|
2019-01-14 21:49:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-09 02:25:28 +00:00
|
|
|
// from https://github.com/bvaughn/react-virtualized/blob/fb3484ed5dcc41bffae8eab029126c0fb8f7abc0/source/List/types.js#L5
|
2019-03-12 00:20:16 +00:00
|
|
|
type RowRendererParamsType = {
|
2019-03-09 02:25:28 +00:00
|
|
|
index: number;
|
|
|
|
isScrolling: boolean;
|
|
|
|
isVisible: boolean;
|
|
|
|
key: string;
|
|
|
|
parent: Object;
|
|
|
|
style: Object;
|
|
|
|
};
|
|
|
|
|
2019-08-09 00:46:49 +00:00
|
|
|
export class LeftPane extends React.Component<PropsType> {
|
2019-11-07 21:36:16 +00:00
|
|
|
public listRef = React.createRef<any>();
|
|
|
|
public containerRef = React.createRef<HTMLDivElement>();
|
|
|
|
public setFocusToFirstNeeded = false;
|
|
|
|
public setFocusToLastNeeded = false;
|
|
|
|
|
2019-03-12 00:20:16 +00:00
|
|
|
public renderRow = ({
|
|
|
|
index,
|
|
|
|
key,
|
|
|
|
style,
|
|
|
|
}: RowRendererParamsType): JSX.Element => {
|
|
|
|
const {
|
|
|
|
archivedConversations,
|
|
|
|
conversations,
|
|
|
|
i18n,
|
|
|
|
openConversationInternal,
|
|
|
|
showArchived,
|
|
|
|
} = this.props;
|
|
|
|
if (!conversations || !archivedConversations) {
|
|
|
|
throw new Error(
|
|
|
|
'renderRow: Tried to render without conversations or archivedConversations'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!showArchived && index === conversations.length) {
|
|
|
|
return this.renderArchivedButton({ key, style });
|
|
|
|
}
|
|
|
|
|
|
|
|
const conversation = showArchived
|
|
|
|
? archivedConversations[index]
|
|
|
|
: conversations[index];
|
2019-03-09 02:25:28 +00:00
|
|
|
|
|
|
|
return (
|
2019-03-20 17:42:28 +00:00
|
|
|
<div
|
2019-03-09 02:25:28 +00:00
|
|
|
key={key}
|
2019-03-20 17:42:28 +00:00
|
|
|
className="module-left-pane__conversation-container"
|
2019-03-09 02:25:28 +00:00
|
|
|
style={style}
|
2019-03-20 17:42:28 +00:00
|
|
|
>
|
|
|
|
<ConversationListItem
|
|
|
|
{...conversation}
|
|
|
|
onClick={openConversationInternal}
|
|
|
|
i18n={i18n}
|
|
|
|
/>
|
|
|
|
</div>
|
2019-03-09 02:25:28 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-11-07 21:36:16 +00:00
|
|
|
public renderArchivedButton = ({
|
2019-03-12 00:20:16 +00:00
|
|
|
key,
|
|
|
|
style,
|
|
|
|
}: {
|
|
|
|
key: string;
|
|
|
|
style: Object;
|
2019-11-07 21:36:16 +00:00
|
|
|
}): JSX.Element => {
|
2019-03-12 00:20:16 +00:00
|
|
|
const {
|
|
|
|
archivedConversations,
|
|
|
|
i18n,
|
|
|
|
showArchivedConversations,
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
if (!archivedConversations || !archivedConversations.length) {
|
|
|
|
throw new Error(
|
|
|
|
'renderArchivedButton: Tried to render without archivedConversations'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2019-11-07 21:36:16 +00:00
|
|
|
<button
|
2019-03-12 00:20:16 +00:00
|
|
|
key={key}
|
|
|
|
className="module-left-pane__archived-button"
|
|
|
|
style={style}
|
|
|
|
onClick={showArchivedConversations}
|
|
|
|
>
|
|
|
|
{i18n('archivedConversations')}{' '}
|
|
|
|
<span className="module-left-pane__archived-button__archived-count">
|
|
|
|
{archivedConversations.length}
|
|
|
|
</span>
|
2019-11-07 21:36:16 +00:00
|
|
|
</button>
|
2019-03-12 00:20:16 +00:00
|
|
|
);
|
2019-11-07 21:36:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
public handleKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {
|
2019-12-17 18:52:36 +00:00
|
|
|
const commandKey = get(window, 'platform') === 'darwin' && event.metaKey;
|
|
|
|
const controlKey = get(window, 'platform') !== 'darwin' && event.ctrlKey;
|
|
|
|
const commandOrCtrl = commandKey || controlKey;
|
2019-11-07 21:36:16 +00:00
|
|
|
|
|
|
|
if (commandOrCtrl && !event.shiftKey && event.key === 'ArrowUp') {
|
|
|
|
this.scrollToRow(0);
|
|
|
|
this.setFocusToFirstNeeded = true;
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (commandOrCtrl && !event.shiftKey && event.key === 'ArrowDown') {
|
|
|
|
const length = this.getLength();
|
|
|
|
this.scrollToRow(length - 1);
|
|
|
|
this.setFocusToLastNeeded = true;
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public handleFocus = () => {
|
|
|
|
const { selectedConversationId } = this.props;
|
|
|
|
const { current: container } = this.containerRef;
|
2019-03-12 00:20:16 +00:00
|
|
|
|
2019-11-07 21:36:16 +00:00
|
|
|
if (!container) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (document.activeElement === container) {
|
|
|
|
const scrollingContainer = this.getScrollContainer();
|
|
|
|
if (selectedConversationId && scrollingContainer) {
|
|
|
|
const escapedId = cleanId(selectedConversationId).replace(
|
|
|
|
/["\\]/g,
|
|
|
|
'\\$&'
|
|
|
|
);
|
|
|
|
// tslint:disable-next-line no-unnecessary-type-assertion
|
|
|
|
const target = scrollingContainer.querySelector(
|
|
|
|
`.module-conversation-list-item[data-id="${escapedId}"]`
|
|
|
|
) as any;
|
|
|
|
|
|
|
|
if (target && target.focus) {
|
|
|
|
target.focus();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setFocusToFirst();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public scrollToRow = (row: number) => {
|
|
|
|
if (!this.listRef || !this.listRef.current) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.listRef.current.scrollToRow(row);
|
|
|
|
};
|
|
|
|
|
|
|
|
public getScrollContainer = () => {
|
|
|
|
if (!this.listRef || !this.listRef.current) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const list = this.listRef.current;
|
|
|
|
|
|
|
|
if (!list.Grid || !list.Grid._scrollingContainer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return list.Grid._scrollingContainer as HTMLDivElement;
|
|
|
|
};
|
|
|
|
|
|
|
|
public setFocusToFirst = () => {
|
|
|
|
const scrollContainer = this.getScrollContainer();
|
|
|
|
if (!scrollContainer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// tslint:disable-next-line no-unnecessary-type-assertion
|
|
|
|
const item = scrollContainer.querySelector(
|
|
|
|
'.module-conversation-list-item'
|
|
|
|
) as any;
|
|
|
|
if (item && item.focus) {
|
|
|
|
item.focus();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// tslint:disable-next-line member-ordering
|
|
|
|
public onScroll = debounce(
|
|
|
|
() => {
|
|
|
|
if (this.setFocusToFirstNeeded) {
|
|
|
|
this.setFocusToFirstNeeded = false;
|
|
|
|
this.setFocusToFirst();
|
|
|
|
}
|
|
|
|
if (this.setFocusToLastNeeded) {
|
|
|
|
this.setFocusToLastNeeded = false;
|
|
|
|
|
|
|
|
const scrollContainer = this.getScrollContainer();
|
|
|
|
if (!scrollContainer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// tslint:disable-next-line no-unnecessary-type-assertion
|
|
|
|
const button = scrollContainer.querySelector(
|
|
|
|
'.module-left-pane__archived-button'
|
|
|
|
) as any;
|
|
|
|
if (button && button.focus) {
|
|
|
|
button.focus();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// tslint:disable-next-line no-unnecessary-type-assertion
|
|
|
|
const items = scrollContainer.querySelectorAll(
|
|
|
|
'.module-conversation-list-item'
|
|
|
|
) as any;
|
|
|
|
if (items && items.length > 0) {
|
|
|
|
const last = items[items.length - 1];
|
|
|
|
|
|
|
|
if (last && last.focus) {
|
|
|
|
last.focus();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
100,
|
|
|
|
{ maxWait: 100 }
|
|
|
|
);
|
|
|
|
|
|
|
|
public getLength = () => {
|
|
|
|
const { archivedConversations, conversations, showArchived } = this.props;
|
|
|
|
|
|
|
|
if (!conversations || !archivedConversations) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// That extra 1 element added to the list is the 'archived conversations' button
|
|
|
|
return showArchived
|
|
|
|
? archivedConversations.length
|
|
|
|
: conversations.length + (archivedConversations.length ? 1 : 0);
|
|
|
|
};
|
|
|
|
|
|
|
|
public renderList = (): JSX.Element | Array<JSX.Element | null> => {
|
2019-01-14 21:49:58 +00:00
|
|
|
const {
|
2019-03-12 00:20:16 +00:00
|
|
|
archivedConversations,
|
2019-01-14 21:49:58 +00:00
|
|
|
i18n,
|
2019-03-09 02:25:28 +00:00
|
|
|
conversations,
|
2019-01-14 21:49:58 +00:00
|
|
|
openConversationInternal,
|
2019-08-09 00:46:49 +00:00
|
|
|
renderMessageSearchResult,
|
2019-01-14 21:49:58 +00:00
|
|
|
startNewConversation,
|
|
|
|
searchResults,
|
2019-03-12 00:20:16 +00:00
|
|
|
showArchived,
|
2019-01-14 21:49:58 +00:00
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
if (searchResults) {
|
|
|
|
return (
|
|
|
|
<SearchResults
|
|
|
|
{...searchResults}
|
2019-08-09 00:46:49 +00:00
|
|
|
openConversationInternal={openConversationInternal}
|
2019-01-14 21:49:58 +00:00
|
|
|
startNewConversation={startNewConversation}
|
2019-08-09 00:46:49 +00:00
|
|
|
renderMessageSearchResult={renderMessageSearchResult}
|
2019-01-14 21:49:58 +00:00
|
|
|
i18n={i18n}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-12 00:20:16 +00:00
|
|
|
if (!conversations || !archivedConversations) {
|
|
|
|
throw new Error(
|
|
|
|
'render: must provided conversations and archivedConverstions if no search results are provided'
|
|
|
|
);
|
2019-03-09 02:25:28 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 21:36:16 +00:00
|
|
|
const length = this.getLength();
|
2019-03-12 00:20:16 +00:00
|
|
|
|
2019-03-14 21:41:14 +00:00
|
|
|
const archived = showArchived ? (
|
|
|
|
<div className="module-left-pane__archive-helper-text" key={0}>
|
|
|
|
{i18n('archiveHelperText')}
|
|
|
|
</div>
|
|
|
|
) : null;
|
|
|
|
|
|
|
|
// We ensure that the listKey differs between inbox and archive views, which ensures
|
|
|
|
// that AutoSizer properly detects the new size of its slot in the flexbox. The
|
|
|
|
// archive explainer text at the top of the archive view causes problems otherwise.
|
|
|
|
// It also ensures that we scroll to the top when switching views.
|
|
|
|
const listKey = showArchived ? 1 : 0;
|
|
|
|
|
2019-03-09 02:25:28 +00:00
|
|
|
// Note: conversations is not a known prop for List, but it is required to ensure that
|
|
|
|
// it re-renders when our conversation data changes. Otherwise it would just render
|
|
|
|
// on startup and scroll.
|
2019-03-14 21:41:14 +00:00
|
|
|
const list = (
|
2019-11-07 21:36:16 +00:00
|
|
|
<div
|
|
|
|
className="module-left-pane__list"
|
|
|
|
key={listKey}
|
|
|
|
aria-live="polite"
|
|
|
|
role="group"
|
|
|
|
tabIndex={-1}
|
|
|
|
ref={this.containerRef}
|
|
|
|
onKeyDown={this.handleKeyDown}
|
|
|
|
onFocus={this.handleFocus}
|
|
|
|
>
|
2019-03-09 02:25:28 +00:00
|
|
|
<AutoSizer>
|
|
|
|
{({ height, width }) => (
|
|
|
|
<List
|
2019-11-07 21:36:16 +00:00
|
|
|
ref={this.listRef}
|
|
|
|
onScroll={this.onScroll}
|
2019-03-09 02:25:28 +00:00
|
|
|
className="module-left-pane__virtual-list"
|
|
|
|
conversations={conversations}
|
|
|
|
height={height}
|
2019-03-12 00:20:16 +00:00
|
|
|
rowCount={length}
|
2019-10-04 18:06:17 +00:00
|
|
|
rowHeight={68}
|
2019-11-07 21:36:16 +00:00
|
|
|
tabIndex={-1}
|
2019-03-09 02:25:28 +00:00
|
|
|
rowRenderer={this.renderRow}
|
|
|
|
width={width}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</AutoSizer>
|
2019-01-14 21:49:58 +00:00
|
|
|
</div>
|
|
|
|
);
|
2019-03-14 21:41:14 +00:00
|
|
|
|
|
|
|
return [archived, list];
|
2019-11-07 21:36:16 +00:00
|
|
|
};
|
2019-01-14 21:49:58 +00:00
|
|
|
|
2019-11-07 21:36:16 +00:00
|
|
|
public renderArchivedHeader = (): JSX.Element => {
|
2019-03-12 00:20:16 +00:00
|
|
|
const { i18n, showInbox } = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="module-left-pane__archive-header">
|
2019-11-07 21:36:16 +00:00
|
|
|
<button
|
2019-03-12 00:20:16 +00:00
|
|
|
onClick={showInbox}
|
|
|
|
className="module-left-pane__to-inbox-button"
|
2019-11-07 21:36:16 +00:00
|
|
|
title={i18n('backToInbox')}
|
2019-03-12 00:20:16 +00:00
|
|
|
/>
|
|
|
|
<div className="module-left-pane__archive-header-text">
|
|
|
|
{i18n('archivedConversations')}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
2019-11-07 21:36:16 +00:00
|
|
|
};
|
2019-03-12 00:20:16 +00:00
|
|
|
|
|
|
|
public render(): JSX.Element {
|
|
|
|
const { renderMainHeader, showArchived } = this.props;
|
2019-01-14 21:49:58 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="module-left-pane">
|
2019-03-12 00:20:16 +00:00
|
|
|
<div className="module-left-pane__header">
|
|
|
|
{showArchived ? this.renderArchivedHeader() : renderMainHeader()}
|
|
|
|
</div>
|
2019-01-14 21:49:58 +00:00
|
|
|
{this.renderList()}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|