-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
quick-reactions.tsx
98 lines (83 loc) · 3.62 KB
/
quick-reactions.tsx
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
import delegate, {DelegateEvent} from 'delegate-it';
import React from 'dom-chef';
import * as pageDetect from 'github-url-detection';
import features from '../feature-manager.js';
const emojiSortOrder = ['THUMBS_UP', 'THUMBS_DOWN', 'LAUGH', 'HOORAY', 'CONFUSED', 'HEART', 'ROCKET', 'EYES'];
function optimisticReactionFromPost(event: DelegateEvent<MouseEvent>): void {
const reaction = event.delegateTarget as HTMLButtonElement;
updateReaction(reaction);
}
function optimisticReactionFromMenu(event: DelegateEvent<MouseEvent>): void {
const reactionButton = event.delegateTarget as HTMLButtonElement;
const reactionsMenu = reactionButton.closest('reactions-menu')!;
const details = reactionsMenu.querySelector('details');
details!.open = false; // Close reactions menu immediately
const reactionsContainer = reactionsMenu.nextElementSibling!.querySelector('.js-comment-reactions-options')!;
const reactionButtonValue = reactionButton.getAttribute('value')!;
const existingReaction = reactionsContainer.querySelector<HTMLButtonElement>(`[value="${reactionButtonValue}"]`);
if (existingReaction) {
// The user is updating an existing reaction via the menu, just update the reaction
updateReaction(existingReaction);
} else {
// Reactions have a specific order, so we need to insert the new reaction in the correct position
const reactionIndex = emojiSortOrder.findIndex(item => reactionButtonValue.startsWith(item));
const insertionIndex = [...reactionsContainer.querySelectorAll('button')].findIndex(button => {
const index = Number.parseInt(button.getAttribute('data-button-index-position')!, 10);
return index > reactionIndex;
});
const referenceNode = reactionsContainer.querySelectorAll('button').item(insertionIndex);
const emojiElement = reactionButton.querySelector('g-emoji')!.cloneNode(true);
emojiElement.className = 'social-button-emoji';
reactionsContainer.insertBefore(
<>
<button
name="input[content]"
value={reactionButtonValue}
type="submit"
className="social-reaction-summary-item btn-link d-flex no-underline flex-items-baseline mr-2 user-has-reacted"
>
{emojiElement}
<span className="js-discussion-reaction-group-count">1</span>
</button>
<span/> {/* helps avoid unwanted margin due to adjacent siblings of .social-reaction-summary-item */}
</>,
referenceNode,
);
}
}
function updateReaction(buttonElement: HTMLButtonElement): void {
const countElement = buttonElement.querySelector(
'.js-discussion-reaction-group-count',
)!;
const count = Number.parseInt(countElement.textContent, 10);
const isIncrease = !buttonElement.getAttribute('value')!.endsWith('unreact');
const newCount = isIncrease ? count + 1 : count - 1;
if (newCount === 0) {
buttonElement.setAttribute('hidden', '');
} else if (newCount > count) {
countElement.textContent = String(newCount);
buttonElement.classList.add('user-has-reacted');
buttonElement.classList.remove('color-fg-muted');
} else {
countElement.textContent = String(newCount);
buttonElement.classList.add('color-fg-muted');
}
}
function init(signal: AbortSignal): void {
delegate('reactions-menu + form button[value$=react]', 'click', optimisticReactionFromPost, {signal});
delegate('reactions-menu button[value$=react]', 'click', optimisticReactionFromMenu, {signal});
}
void features.add(import.meta.url, {
include: [
pageDetect.hasComments,
pageDetect.isReleasesOrTags,
pageDetect.isSingleReleaseOrTag,
pageDetect.isDiscussion,
],
init,
});
/*
Test URLs:
https://github.com/brumm/tako/issues/27#issuecomment-617314195
https://github.com/vercel/next.js/discussions/40684#discussioncomment-3689167
*/