Skip to content

Commit 469ba4b

Browse files
00karthikKarthikmuhsin-k
authored
Bug: Fix issue when adding private notes under js-discussion wrapper (#60)
* fix issue when adding private notes under js-discussion wrapper * Update note error message Co-authored-by: Karthik <[email protected]> Co-authored-by: Muhsin K <[email protected]>
1 parent 6998c6c commit 469ba4b

File tree

3 files changed

+43
-21
lines changed

3 files changed

+43
-21
lines changed

Diff for: server/apis/v1/modules/Note/note.service.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ async function createNote(user, noteDetails) {
3232
if (!userHasAccessToRepo) {
3333
return {
3434
status: 400,
35-
message: 'You cannot add private notes to this repository since you are not a contributor',
35+
message: 'You cannot add private notes to this repository since you are not a collaborator',
3636
};
3737
}
3838

Diff for: src/index.js

+38-18
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ function onInputValueChange(e) {
3535
function initInputArea() {
3636
const textArea = document.getElementById('new_comment_field');
3737
if (textArea) {
38-
textArea.addEventListener('change', e => {
38+
textArea.addEventListener('change', (e) => {
3939
onInputValueChange(e);
4040
});
41-
textArea.addEventListener('input', e => {
41+
textArea.addEventListener('input', (e) => {
4242
onInputValueChange(e);
4343
});
4444
}
@@ -58,7 +58,7 @@ async function deleteNote(noteId) {
5858

5959
// Remove deleted note from dom
6060
const commentBoxes = document.querySelectorAll('.private-note');
61-
commentBoxes.forEach(commentBox => {
61+
commentBoxes.forEach((commentBox) => {
6262
const commentBoxPrivateId = commentBox.getAttribute('private-id');
6363
if (commentBoxPrivateId === noteId) {
6464
commentBox.remove();
@@ -111,13 +111,27 @@ function createPrivateNoteAddButton() {
111111
button.disabled = textArea && !textArea.value;
112112
button.onclick = async () => {
113113
button.disabled = true;
114-
const commentBoxes = document.querySelectorAll(
114+
let commentBoxes = document.querySelectorAll(
115115
'[data-gid]:not([id]):not(.merge-status-list-wrapper).js-timeline-item',
116116
);
117+
let extraClass = '';
118+
let isDiscussionBox = false;
119+
if (commentBoxes.length === 0) {
120+
commentBoxes = document.querySelectorAll('.js-discussion');
121+
if (commentBoxes.length) {
122+
extraClass = 'ml-0 pl-0 ml-md-6 pl-md-3';
123+
isDiscussionBox = true;
124+
}
125+
}
117126
const commentBoxCount = commentBoxes.length;
118127
// Find nearest comment id
119128
let nearestBox = commentBoxes[commentBoxCount - 1];
120-
nearestCommentId = nearestBox.getAttribute('data-gid');
129+
if (isDiscussionBox) {
130+
const box = nearestBox.firstElementChild;
131+
nearestCommentId = box.getAttribute('data-gid');
132+
} else {
133+
nearestCommentId = nearestBox.getAttribute('data-gid');
134+
}
121135

122136
try {
123137
const { issueId, noteType, projectName, repoOwner } = urlAttributes;
@@ -139,7 +153,7 @@ function createPrivateNoteAddButton() {
139153
) {
140154
nearestBox = nearestBox.nextSibling;
141155
}
142-
nearestBox.after(createNoteBox(allNotes[allNotes.length - 1]));
156+
nearestBox.after(createNoteBox(allNotes[allNotes.length - 1], extraClass));
143157
bindDeleteEventToNote(newlyCreatedNote);
144158
bindToggleVisibilityToNote(newlyCreatedNote);
145159

@@ -161,7 +175,7 @@ async function injectContent(apiCall) {
161175
const actionBtns = document.querySelector('#partial-new-comment-form-actions > div');
162176
let commentBtn = {};
163177
if (actionBtns) {
164-
[].forEach.call(actionBtns.children, btn => {
178+
[].forEach.call(actionBtns.children, (btn) => {
165179
if (btn.children.length && btn.children[0] && btn.children[0].innerText === 'Comment') {
166180
commentBtn = btn;
167181
}
@@ -186,7 +200,7 @@ async function injectContent(apiCall) {
186200
const positionMarker = document.getElementById('partial-new-comment-form-actions');
187201
// similar comments hide the gitex comments so opening the collapsible similar comments
188202
const collapsed = document.querySelectorAll('.Details-element.details-reset');
189-
collapsed.forEach(el => {
203+
collapsed.forEach((el) => {
190204
el.setAttribute('open', true);
191205
});
192206
if (positionMarker) {
@@ -209,23 +223,29 @@ async function injectContent(apiCall) {
209223
});
210224
if (allNotes.length) {
211225
// Iterate all the comments and append notes
212-
const commentBoxes = document.querySelectorAll(
226+
let commentBoxes = document.querySelectorAll(
213227
'[data-gid]:not([id]):not(.merge-status-list-wrapper)',
214228
);
215-
216-
commentBoxes.forEach(commentBox => {
229+
let extraClass = '';
230+
if (commentBoxes.length === 0) {
231+
commentBoxes = document.querySelectorAll('.js-discussion');
232+
if (commentBoxes.length) {
233+
extraClass = 'ml-0 pl-0 ml-md-6 pl-md-3';
234+
}
235+
}
236+
commentBoxes.forEach((commentBox) => {
217237
const commentId = commentBox.getAttribute('data-gid');
218238

219-
const findNotesNearestToComment = obj => obj.nearestCommentId === commentId;
239+
const findNotesNearestToComment = (obj) => obj.nearestCommentId === commentId;
220240
const notesNearestToCommentBox = allNotes.filter(findNotesNearestToComment);
221241
const sortedNotes = notesNearestToCommentBox.sort(
222242
(a, b) => new Date(b.createdAt) - new Date(a.createdAt),
223243
);
224-
sortedNotes.forEach(element => {
244+
sortedNotes.forEach((element) => {
225245
const { _id: noteId } = element;
226246
if (!addedNoteIds.includes(noteId)) {
227247
addedNoteIds.push(noteId);
228-
commentBox.after(createNoteBox(element));
248+
commentBox.after(createNoteBox(element, extraClass));
229249
if (commentBox) {
230250
bindDeleteEventToNote(element);
231251
bindToggleVisibilityToNote(element);
@@ -244,7 +264,7 @@ function init() {
244264
const {
245265
location: { href: URL },
246266
} = document;
247-
window.chrome.storage.sync.get(['githubPrivateCommentToken'], result => {
267+
window.chrome.storage.sync.get(['githubPrivateCommentToken'], (result) => {
248268
const authToken = result.githubPrivateCommentToken;
249269

250270
if (!authToken) {
@@ -261,7 +281,7 @@ window.onload = () => {
261281
init();
262282
};
263283

264-
window.addEventListener('message', e => {
284+
window.addEventListener('message', (e) => {
265285
if (e.data && e.data.type === 'githubPrivateCommentToken') {
266286
window.chrome.storage.sync.set({ githubPrivateCommentToken: e.data.value });
267287
}
@@ -283,8 +303,8 @@ window.addEventListener('message', e => {
283303
);
284304
function addSignoutListener() {
285305
const logoutBtns = document.querySelectorAll('form[action="/logout"] [type="submit"]');
286-
const handler = e => {
306+
const handler = (e) => {
287307
chrome.runtime.sendMessage({ logout: true });
288308
};
289-
logoutBtns.forEach(btn => btn.addEventListener('click', handler));
309+
logoutBtns.forEach((btn) => btn.addEventListener('click', handler));
290310
}

Diff for: src/noteBox.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,15 @@ function createAvatar({ userName, githubId, avatarUrl }) {
128128
return avatarWrapper;
129129
}
130130

131-
export default function createNoteBox(noteDetail) {
131+
export default function createNoteBox(noteDetail, extraClass) {
132132
if (!noteDetail.author) {
133133
noteDetail.author = {};
134134
}
135135
const { avatarUrl, githubId, userName } = noteDetail.author;
136136
const noteNode = document.createElement('div');
137-
noteNode.classList = ['js-timeline-item js-timeline-progressive-focus-container private-note'];
137+
noteNode.classList = [
138+
`js-timeline-item js-timeline-progressive-focus-container private-note ${extraClass}`,
139+
];
138140
noteNode.setAttribute('private-id', noteDetail._id);
139141

140142
const noteWrapper = document.createElement('div');

0 commit comments

Comments
 (0)