|
| 1 | +import {h} from 'dom-chef'; |
| 2 | +import select from 'select-dom'; |
| 3 | +import * as pageDetect from '../libs/page-detect'; |
| 4 | + |
| 5 | +export default function () { |
| 6 | + if (!pageDetect.isIssue() && !pageDetect.isPR()) { |
| 7 | + return; |
| 8 | + } |
| 9 | + |
| 10 | + let uselessCount = 0; |
| 11 | + for (const commentText of select.all('.comment-body > p:only-child')) { |
| 12 | + // Find useless comments |
| 13 | + if (!/^([+-]\d+!*|👍|🙏|👎|👌)+$/.test(commentText.textContent.trim())) { |
| 14 | + continue; |
| 15 | + } |
| 16 | + |
| 17 | + // Ensure that they're not by VIPs (owner, collaborators, etc) |
| 18 | + const comment = commentText.closest('.js-timeline-item'); |
| 19 | + if (select.exists('.timeline-comment-label', comment)) { |
| 20 | + continue; |
| 21 | + } |
| 22 | + |
| 23 | + // If the person is having a conversation, then don't hide it |
| 24 | + const author = select('.author', comment).getAttribute('href'); |
| 25 | + // If the first comment left by the author isn't a useless comment |
| 26 | + // (previously hidden or about to be hidden), then leave this one as well |
| 27 | + const previousComment = select(`.js-timeline-item:not([hidden]) .unminimized-comment .author[href="${author}"]`); |
| 28 | + if (previousComment && previousComment.closest('.js-timeline-item') !== comment) { |
| 29 | + continue; |
| 30 | + } |
| 31 | + |
| 32 | + comment.hidden = true; |
| 33 | + comment.classList.add('rgh-hidden-comment'); |
| 34 | + uselessCount++; |
| 35 | + } |
| 36 | + |
| 37 | + if (uselessCount > 0) { |
| 38 | + select('.discussion-timeline-actions').prepend( |
| 39 | + <p class="rgh-useless-comments-note"> |
| 40 | + {`${uselessCount} useless comment${uselessCount > 1 ? 's were' : ' was'} hidden. `} |
| 41 | + <button class="btn-link text-emphasized" onClick={unhide}>Undo</button> |
| 42 | + </p> |
| 43 | + ); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +function unhide(event) { |
| 48 | + for (const comment of select.all('.rgh-hidden-comment')) { |
| 49 | + comment.hidden = false; |
| 50 | + } |
| 51 | + select('.rgh-hidden-comment').scrollIntoView(); |
| 52 | + event.target.parentElement.remove(); |
| 53 | +} |
0 commit comments