|
| 1 | +/* |
| 2 | + * @flow strict-local |
| 3 | + * Copyright (C) 2020 MetaBrainz Foundation |
| 4 | + * |
| 5 | + * This file is part of MusicBrainz, the open internet music database, |
| 6 | + * and is licensed under the GPL version 2, or (at your option) any |
| 7 | + * later version: http://www.gnu.org/licenses/gpl-2.0.txt |
| 8 | + */ |
| 9 | + |
| 10 | +import * as React from 'react'; |
| 11 | + |
| 12 | +import { |
| 13 | + EDIT_VOTE_APPROVE, |
| 14 | + EDIT_VOTE_NO, |
| 15 | + EDIT_VOTE_YES, |
| 16 | +} from '../../constants'; |
| 17 | +import {CatalystContext} from '../../context'; |
| 18 | +import EditorLink from '../../static/scripts/common/components/EditorLink'; |
| 19 | +import expand2react from '../../static/scripts/common/i18n/expand2react'; |
| 20 | +import getVoteName from '../../static/scripts/edit/utility/getVoteName'; |
| 21 | +import formatUserDate from '../../utility/formatUserDate'; |
| 22 | + |
| 23 | +import EditorTypeInfo from './EditorTypeInfo'; |
| 24 | + |
| 25 | +type PropsT = { |
| 26 | + +edit: $ReadOnly<{...EditT, +id: number}>, |
| 27 | + +editNote: EditNoteT, |
| 28 | + +index: number, |
| 29 | +}; |
| 30 | + |
| 31 | +function returnNoteAnchor(edit, index) { |
| 32 | + return `note-${edit.id}-${index + 1}`; |
| 33 | +} |
| 34 | + |
| 35 | +function returnVoteClass(vote, isOwner) { |
| 36 | + let className = ''; |
| 37 | + |
| 38 | + if (vote) { |
| 39 | + className = getVoteName(vote.vote); |
| 40 | + } |
| 41 | + |
| 42 | + if (isOwner) { |
| 43 | + if (vote) { |
| 44 | + className += ' '; |
| 45 | + } |
| 46 | + className += 'owner'; |
| 47 | + } |
| 48 | + |
| 49 | + return className; |
| 50 | +} |
| 51 | + |
| 52 | +const EditNote = ({ |
| 53 | + edit, |
| 54 | + editNote, |
| 55 | + index, |
| 56 | +}: PropsT): React.Element<'div'> => { |
| 57 | + const $c = React.useContext(CatalystContext); |
| 58 | + const isModBot = editNote.editor_id === 4; |
| 59 | + const anchor = returnNoteAnchor(edit, index); |
| 60 | + const isOwner = edit.editor_id === editNote.editor_id; |
| 61 | + const lastRelevantVote = edit.votes.find(vote => ( |
| 62 | + vote.editor_id === editNote.editor_id && |
| 63 | + !vote.superseded |
| 64 | + )); |
| 65 | + const showVotingIcon = lastRelevantVote && ( |
| 66 | + lastRelevantVote.vote === EDIT_VOTE_APPROVE || |
| 67 | + lastRelevantVote.vote === EDIT_VOTE_NO || |
| 68 | + lastRelevantVote.vote === EDIT_VOTE_YES |
| 69 | + ); |
| 70 | + |
| 71 | + return ( |
| 72 | + <div className="edit-note" id={anchor}> |
| 73 | + <h3 className={returnVoteClass(lastRelevantVote, isOwner)}> |
| 74 | + <EditorLink editor={editNote.editor} /> |
| 75 | + {showVotingIcon /*:: === true */ |
| 76 | + ? <div className="voting-icon" /> |
| 77 | + : null} |
| 78 | + {' '} |
| 79 | + <EditorTypeInfo editor={editNote.editor} /> |
| 80 | + <a className="date" href={`#${anchor}`}> |
| 81 | + {formatUserDate($c, editNote.post_time)} |
| 82 | + </a> |
| 83 | + </h3> |
| 84 | + <div className={'edit-note-text' + (isModBot ? ' modbot' : '')}> |
| 85 | + {expand2react(editNote.formatted_text)} |
| 86 | + </div> |
| 87 | + </div> |
| 88 | + ); |
| 89 | +}; |
| 90 | + |
| 91 | +export default EditNote; |
0 commit comments