Show clicked-on reaction in ReactionViewer

This commit is contained in:
Ken Powers 2020-02-06 14:57:46 -05:00 committed by GitHub
parent c147e6ce25
commit c9292544aa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 8 deletions

View file

@ -161,6 +161,7 @@ interface State {
isSelected: boolean; isSelected: boolean;
prevSelectedCounter: number; prevSelectedCounter: number;
pickedReaction?: string;
reactionViewerRoot: HTMLDivElement | null; reactionViewerRoot: HTMLDivElement | null;
reactionPickerRoot: HTMLDivElement | null; reactionPickerRoot: HTMLDivElement | null;
@ -1417,7 +1418,10 @@ export class Message extends React.PureComponent<Props, State> {
); );
} }
public toggleReactionViewer = (onlyRemove = false) => { public toggleReactionViewer = (
onlyRemove = false,
pickedReaction?: string
) => {
this.setState(({ reactionViewerRoot }) => { this.setState(({ reactionViewerRoot }) => {
if (reactionViewerRoot) { if (reactionViewerRoot) {
document.body.removeChild(reactionViewerRoot); document.body.removeChild(reactionViewerRoot);
@ -1427,7 +1431,7 @@ export class Message extends React.PureComponent<Props, State> {
true true
); );
return { reactionViewerRoot: null }; return { reactionViewerRoot: null, pickedReaction };
} }
if (!onlyRemove) { if (!onlyRemove) {
@ -1441,10 +1445,11 @@ export class Message extends React.PureComponent<Props, State> {
return { return {
reactionViewerRoot: root, reactionViewerRoot: root,
pickedReaction,
}; };
} }
return { reactionViewerRoot: null }; return { reactionViewerRoot: null, pickedReaction };
}); });
}; };
@ -1537,7 +1542,7 @@ export class Message extends React.PureComponent<Props, State> {
someNotRendered && someNotRendered &&
maybeNotRendered.some(res => res.some(re => Boolean(re.from.isMe))); maybeNotRendered.some(res => res.some(re => Boolean(re.from.isMe)));
const { reactionViewerRoot, containerWidth } = this.state; const { reactionViewerRoot, containerWidth, pickedReaction } = this.state;
// Calculate the width of the reactions container // Calculate the width of the reactions container
const reactionsWidth = toRender.reduce((sum, res, i, arr) => { const reactionsWidth = toRender.reduce((sum, res, i, arr) => {
@ -1598,7 +1603,10 @@ export class Message extends React.PureComponent<Props, State> {
onClick={e => { onClick={e => {
e.stopPropagation(); e.stopPropagation();
e.preventDefault(); e.preventDefault();
this.toggleReactionViewer(); this.toggleReactionViewer(
false,
isMore ? 'all' : re.emoji
);
}} }}
onKeyDown={e => { onKeyDown={e => {
// Prevent enter key from opening stickers/attachments // Prevent enter key from opening stickers/attachments
@ -1653,6 +1661,7 @@ export class Message extends React.PureComponent<Props, State> {
zIndex: 2, zIndex: 2,
}} }}
reactions={reactions} reactions={reactions}
pickedReaction={pickedReaction}
i18n={i18n} i18n={i18n}
onClose={this.toggleReactionViewer} onClose={this.toggleReactionViewer}
/> />

View file

@ -24,6 +24,31 @@
</util.ConversationContext> </util.ConversationContext>
``` ```
#### Picked Reaction
```jsx
<util.ConversationContext theme={util.theme} ios={util.ios} mode={util.mode}>
<ReactionViewer
i18n={util.i18n}
pickedReaction="👍"
reactions={[
{
emoji: '❤️',
from: { id: '+14155552671', name: 'Amelia Briggs', isMe: true },
},
{
emoji: '👍',
from: {
id: '+14155552671',
phoneNumber: '+14155552671',
profileName: 'Joel Ferrari',
},
},
]}
/>
</util.ConversationContext>
```
#### Many Reactions #### Many Reactions
```jsx ```jsx

View file

@ -22,6 +22,7 @@ export type Reaction = {
export type OwnProps = { export type OwnProps = {
reactions: Array<Reaction>; reactions: Array<Reaction>;
pickedReaction?: string;
onClose?: () => unknown; onClose?: () => unknown;
}; };
@ -33,11 +34,11 @@ const emojisOrder = ['❤️', '👍', '👎', '😂', '😮', '😢', '😡'];
export const ReactionViewer = React.forwardRef<HTMLDivElement, Props>( export const ReactionViewer = React.forwardRef<HTMLDivElement, Props>(
// tslint:disable-next-line max-func-body-length // tslint:disable-next-line max-func-body-length
({ i18n, reactions, onClose, ...rest }, ref) => { ({ i18n, reactions, onClose, pickedReaction, ...rest }, ref) => {
const grouped = mapValues(groupBy(reactions, 'emoji'), res => const grouped = mapValues(groupBy(reactions, 'emoji'), res =>
orderBy(res, ['timestamp'], ['desc']) orderBy(res, ['timestamp'], ['desc'])
); );
const [selected, setSelected] = React.useState('all'); const [selected, setSelected] = React.useState(pickedReaction || 'all');
const focusRef = React.useRef<HTMLButtonElement>(null); const focusRef = React.useRef<HTMLButtonElement>(null);
// Handle escape key // Handle escape key
@ -77,7 +78,7 @@ export const ReactionViewer = React.forwardRef<HTMLDivElement, Props>(
}, [reactions]); }, [reactions]);
const allSorted = React.useMemo(() => { const allSorted = React.useMemo(() => {
return sortBy(reactions, 'timestamp'); return orderBy(reactions, ['timestamp'], ['desc']);
}, [reactions]); }, [reactions]);
// If we have previously selected a reaction type that is no longer present // If we have previously selected a reaction type that is no longer present