File size: 4,426 Bytes
1e92f2d |
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 150 151 152 153 154 155 156 157 158 159 160 |
import clsx from 'clsx';
import { useTranslate } from 'i18n-calypso';
import * as React from 'react';
import { connect } from 'react-redux';
import { getActions } from '../helpers/notes';
import actions from '../state/actions';
import getIsNoteApproved from '../state/selectors/get-is-note-approved';
import getIsNoteRead from '../state/selectors/get-is-note-read';
import NoteBody from './body';
import SummaryInList from './summary-in-list';
import SummaryInSingle from './summary-in-single';
const hasBadge = ( body ) =>
body.some( ( { media } ) => media && media.some( ( { type } ) => 'badge' === type ) );
export const Note = React.forwardRef( ( props, ref ) => {
const {
currentNote,
detailView,
global,
isApproved,
isRead,
note,
selectedNote,
unselectNote,
isShowing,
handleFocus,
} = props;
const translate = useTranslate();
let hasCommentReply = false;
let hasUnapprovedComment = false;
if ( 'comment' === note.type ) {
const noteBody = note.body;
const noteActions = getActions( note );
if ( noteBody.length > 1 && noteActions ) {
/* Check if note has a reply to another comment */
if ( noteBody[ 1 ] && noteBody[ 1 ].nest_level && noteBody[ 1 ].nest_level > 0 ) {
hasCommentReply = true;
}
/* Check if note has unapproved comment */
if ( 'approve-comment' in noteActions && ! isApproved ) {
hasUnapprovedComment = true;
}
}
}
const isSelected = parseInt( selectedNote, 10 ) === parseInt( note.id, 10 );
const classes = clsx( 'wpnc__note', `wpnc__${ note.type }`, {
'comment-reply': hasCommentReply,
read: isRead,
unread: ! isRead,
wpnc__badge: hasBadge( note.body ),
'wpnc__comment-unapproved': hasUnapprovedComment,
wpnc__current: detailView,
'wpnc__selected-note': isSelected,
} );
const noteContainerRef = React.useRef();
const noteBodyRef = React.useRef( null );
const setContainerRef = React.useCallback( ( currentRef ) => {
noteContainerRef.current = currentRef;
if ( typeof ref === 'function' ) {
ref( currentRef );
} else {
ref = currentRef;
}
}, [] );
React.useEffect( () => {
let timerId;
if ( isShowing && isSelected && ! currentNote && noteContainerRef.current ) {
noteContainerRef.current.focus();
// It might not be focused immediately when the panel is opening because of the pointer-events is none.
if ( document.activeElement !== noteContainerRef.current ) {
timerId = window.setTimeout( () => noteContainerRef.current.focus(), 300 );
}
}
return () => {
if ( timerId ) {
window.clearTimeout( timerId );
}
};
}, [ isShowing, isSelected, currentNote ] );
React.useEffect( () => {
if ( detailView && noteBodyRef.current ) {
noteBodyRef.current.focus();
}
}, [ detailView, noteBodyRef ] );
return (
<li
id={ detailView ? 'note-details-' + note.id : 'note-' + note.id }
className={ classes }
ref={ setContainerRef }
tabIndex={ detailView ? -1 : 0 }
role={ detailView ? 'article' : 'listitem' }
aria-controls={ detailView ? null : 'note-details-' + note.id }
aria-selected={ detailView ? null : isSelected }
onFocus={ () => handleFocus( note.id ) }
>
{ ! detailView && (
<SummaryInList
currentNote={ currentNote }
key={ 'note-summary-list' + note.id }
note={ note }
global={ global }
/>
) }
{ detailView && (
<div
className="wpnc__note-body"
role="region"
aria-label={ translate( 'Notification details' ) }
tabIndex={ -1 }
ref={ noteBodyRef }
>
{ note.header && note.header.length > 0 && (
<SummaryInSingle key={ 'note-summary-single-' + note.id } note={ note } />
) }
<NoteBody key={ 'note-body-' + note.id } note={ note } global={ global } />
<button
className="screen-reader-text"
onClick={ () => {
const noteItemElement = document.getElementById( 'note-' + note.id );
if ( noteItemElement ) {
noteItemElement.focus();
}
unselectNote();
} }
>
{ translate( 'Back to notifications' ) }
</button>
</div>
) }
</li>
);
} );
const mapStateToProps = ( state, { note } ) => ( {
isApproved: getIsNoteApproved( state, note ),
isRead: getIsNoteRead( state, note ),
} );
const mapDispatchToProps = {
unselectNote: actions.ui.unselectNote,
};
export default connect( mapStateToProps, mapDispatchToProps, null, { forwardRef: true } )( Note );
|