import './highest-rated-comment.css'; import React from 'dom-chef'; import select from 'select-dom'; import features from '../libs/features'; import * as icons from '../libs/icons'; function getCount(reaction: HTMLElement): number { return Number(/\d+/.exec(reaction.textContent!)![0]); } function init(): false | void { let highest; // `.js-timeline-item` excludes the very first comment for (const like of select.all('.js-timeline-item [aria-label*="reacted with thumbs up"]')) { const count = getCount(like); const dislike = select('[aria-label*="reacted with thumbs down"]', like.parentElement!); if (dislike && getCount(dislike) >= count / 2) { continue; // Controversial comment } if (!highest || count > highest.count) { highest = {like, count}; } } if (!highest || highest.count < 10) { return false; } const event = highest.like.closest('.js-timeline-item')!; const text = select('.comment-body', event)!.textContent!.substring(0, 100); const avatar = select('.timeline-comment-avatar', event)!.cloneNode(true); const {hash} = select('.timestamp', event)!; select('.unminimized-comment', event)!.classList.add('rgh-highest-rated-comment'); select('.unminimized-comment .timeline-comment-header-text', event)!.before( Highest-rated comment ); const position = select.all('.js-comment').indexOf(highest.like.closest('.js-comment') as HTMLElement); if (position >= 4) { event.parentElement!.firstElementChild!.after((
{avatar} {icons.arrowDown()} Highest-rated comment: {text}
)); } } features.add({ id: __featureName__, description: 'Highlights the most useful comment in issues.', screenshot: 'https://user-images.githubusercontent.com/1402241/58757449-5b238880-853f-11e9-9526-e86c41a32f00.png', include: [ features.isIssue ], load: features.onAjaxedPages, init });