tarekziade HF Staff commited on
Commit
58c4e76
·
verified ·
1 Parent(s): bd0f095

Add demo inline comments and threaded replies without persistence

Browse files
Files changed (3) hide show
  1. README.md +4 -0
  2. patch_view.html +64 -4
  3. patch_view.js +21 -3
README.md CHANGED
@@ -21,6 +21,10 @@ Use the file and comment filters to explore the changes. Reviews open by default
21
  select **Inline** for code review threads. **Show the line(s)** highlights the code,
22
  including saved context for comments on an earlier revision.
23
 
 
 
 
 
24
  The server uses only Python's standard library and runs on the free CPU tier.
25
  It has no GitHub credentials or access to the production dashboard.
26
 
 
21
  select **Inline** for code review threads. **Show the line(s)** highlights the code,
22
  including saved context for comments on an earlier revision.
23
 
24
+ Click a line number to add an inline comment, use **Reply** in a thread, or select
25
+ **Add comment** under the comment filters. These demo comments stay only in the
26
+ current page's memory, disappear on refresh, and are never posted to GitHub.
27
+
28
  The server uses only Python's standard library and runs on the free CPU tier.
29
  It has no GitHub credentials or access to the production dashboard.
30
 
patch_view.html CHANGED
@@ -7,6 +7,8 @@
7
  :root.light{color-scheme:light;--bg:#fff;--card:#f6f8fa;--fg:#24292f;--muted:#6e7781;--border:#d0d7de;--add:#e6ffec;--del:#ffebe9;--hunk:#ddf4ff;--link:#0969da}
8
  *{box-sizing:border-box}body{margin:0;padding:12px;background:var(--bg);color:var(--fg);font:13px/1.5 Inter,system-ui,sans-serif}
9
  .back-to-top{position:fixed;bottom:16px;left:16px;z-index:10;box-shadow:0 2px 8px #0004}body{padding-bottom:64px}
 
 
10
  a{color:var(--link);text-decoration:none}a:hover{text-decoration:underline}
11
  header{display:flex;gap:12px;align-items:center;flex-wrap:wrap}#source{font-weight:600}button{font:inherit;background:var(--card);color:var(--fg);border:1px solid var(--border);border-radius:4px;padding:4px 10px;cursor:pointer}button:disabled{opacity:.5;cursor:wait}#message{color:var(--muted);padding:14px 0}#message.error{color:#f2495c}
12
  .patch-toolbar{display:flex;justify-content:space-between;align-items:center;gap:12px;margin:12px 0;flex-wrap:wrap;color:var(--muted)}
@@ -50,11 +52,23 @@ header{display:flex;gap:12px;align-items:center;flex-wrap:wrap}#source{font-weig
50
  <label class="filter-check bot-filter"><input type="checkbox" id="hide-commands" checked>Hide command comments</label>
51
  <label class="filter-check bot-filter"><input type="checkbox" id="maintainers-only">Maintainers only</label>
52
  <p class="thread-filter-note">Filters select threads. Replies stay visible for context.</p>
 
53
  </div>
54
  <div id="comments-status" class="comments-status" role="status">Loading comments…</div><div id="comments-list"></div>
55
  </aside>
56
  </main>
57
  <button id="back-to-top" class="back-to-top" aria-label="Back to top">↑ Back to top</button>
 
 
 
 
 
 
 
 
 
 
 
58
  <script src="/patch-view/script.js"></script>
59
  <script>
60
  (() => {
@@ -91,7 +105,48 @@ header{display:flex;gap:12px;align-items:center;flex-wrap:wrap}#source{font-weig
91
  document.getElementById('collapse').textContent = 'Collapse all';
92
  } catch (e) { message.className = 'error'; message.textContent = e.message; }
93
  }
94
- let comments = [], filter = 'review';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  const commentStatus = document.getElementById('comments-status'), commentList = document.getElementById('comments-list');
96
  function node(tag, cls, text) {
97
  const el = document.createElement(tag); el.className = cls; el.textContent = text; return el;
@@ -114,7 +169,7 @@ header{display:flex;gap:12px;align-items:center;flex-wrap:wrap}#source{font-weig
114
  while (root.kind === 'inline' && byId.has(root.reply_to) && !seen.has(root.id)) {
115
  seen.add(root.id); root = byId.get(root.reply_to);
116
  }
117
- const key = c.kind === 'inline' ? 'inline:' + (c.thread_id || root.thread_id || root.reply_to || root.id) : c.kind + ':' + c.id;
118
  if (!groups.has(key)) groups.set(key, []);
119
  groups.get(key).push(c);
120
  }
@@ -155,6 +210,11 @@ header{display:flex;gap:12px;align-items:center;flex-wrap:wrap}#source{font-weig
155
  if (options.context) card.append(node('div', 'comment-context-label', 'Included for thread context'));
156
  if (c.path) card.append(node('div', 'comment-location', c.path + (c.line ? ':' + (c.start_line && c.start_line !== c.line ? c.start_line + '–' : '') + c.line : '') + (c.outdated ? ' · Outdated' : '') + (c.resolved === true ? ' · Resolved' : '')));
157
  card.append(node('div', 'comment-body', c.body));
 
 
 
 
 
158
  if (c.path && c.line) {
159
  const show = node('button', 'comment-link', 'Show the line(s)');
160
  const status = node('div', 'comment-navigation-status', ''); status.setAttribute('role','status');
@@ -175,7 +235,7 @@ header{display:flex;gap:12px;align-items:center;flex-wrap:wrap}#source{font-weig
175
  }
176
  status.textContent = TCIPatch.showLines(container, c) ? (adjusted ? 'File filters adjusted to show this comment.' : '') : 'These lines are not available in the saved diff context.';
177
  };
178
- card.append(show, status);
179
  }
180
  return card;
181
  }
@@ -188,7 +248,7 @@ header{display:flex;gap:12px;align-items:center;flex-wrap:wrap}#source{font-weig
188
  try {
189
  const r = await fetch('/patch-view/comments?' + new URLSearchParams({pr, repository}), {cache:'no-store'});
190
  if (!r.ok) throw Error(await r.text());
191
- const data = await r.json(); comments = data.comments.slice().sort((a, b) => (b.created_at || '').localeCompare(a.created_at || '') || b.id - a.id);
192
  hideResolved.disabled = !data.resolution_available;
193
  hideResolved.title = data.resolution_available ? '' : 'GitHub review-thread status is unavailable.';
194
  if (hideResolved.disabled) hideResolved.checked = false;
 
7
  :root.light{color-scheme:light;--bg:#fff;--card:#f6f8fa;--fg:#24292f;--muted:#6e7781;--border:#d0d7de;--add:#e6ffec;--del:#ffebe9;--hunk:#ddf4ff;--link:#0969da}
8
  *{box-sizing:border-box}body{margin:0;padding:12px;background:var(--bg);color:var(--fg);font:13px/1.5 Inter,system-ui,sans-serif}
9
  .back-to-top{position:fixed;bottom:16px;left:16px;z-index:10;box-shadow:0 2px 8px #0004}body{padding-bottom:64px}
10
+ .line-comment-button{border:0;background:transparent;padding:0;color:var(--muted);width:100%;border-radius:0;font:inherit;text-align:right}.line-comment-button:hover,.line-comment-button:focus-visible{color:var(--link);background:var(--hunk)}
11
+ .comment-actions{display:flex;gap:8px;flex-wrap:wrap;margin-top:8px}#add-comment{margin-top:10px}.comment-dialog{width:min(520px,calc(100vw - 32px));padding:20px;border:1px solid var(--border);border-radius:8px;background:var(--card);color:var(--fg);box-shadow:0 12px 48px #0008}.comment-dialog::backdrop{background:#0008}.comment-dialog h2{font-size:16px;margin:0 0 12px}.comment-dialog label{display:block;margin:12px 0 6px}.comment-dialog input,.comment-dialog textarea{box-sizing:border-box;width:100%;font:inherit;color:var(--fg);background:var(--bg);border:1px solid var(--border);border-radius:4px;padding:8px}.comment-dialog textarea{min-height:130px;resize:vertical}.comment-dialog-context{font-size:12px;color:var(--muted);overflow-wrap:anywhere}.comment-dialog-actions{display:flex;justify-content:flex-end;gap:8px;margin-top:16px}.comment-dialog-note{font-size:11px;color:var(--muted)}#comment-submit{background:var(--hunk);border-color:var(--link)}#comment-save-error{color:#f2495c;font-size:12px}
12
  a{color:var(--link);text-decoration:none}a:hover{text-decoration:underline}
13
  header{display:flex;gap:12px;align-items:center;flex-wrap:wrap}#source{font-weight:600}button{font:inherit;background:var(--card);color:var(--fg);border:1px solid var(--border);border-radius:4px;padding:4px 10px;cursor:pointer}button:disabled{opacity:.5;cursor:wait}#message{color:var(--muted);padding:14px 0}#message.error{color:#f2495c}
14
  .patch-toolbar{display:flex;justify-content:space-between;align-items:center;gap:12px;margin:12px 0;flex-wrap:wrap;color:var(--muted)}
 
52
  <label class="filter-check bot-filter"><input type="checkbox" id="hide-commands" checked>Hide command comments</label>
53
  <label class="filter-check bot-filter"><input type="checkbox" id="maintainers-only">Maintainers only</label>
54
  <p class="thread-filter-note">Filters select threads. Replies stay visible for context.</p>
55
+ <button id="add-comment" type="button">Add comment</button>
56
  </div>
57
  <div id="comments-status" class="comments-status" role="status">Loading comments…</div><div id="comments-list"></div>
58
  </aside>
59
  </main>
60
  <button id="back-to-top" class="back-to-top" aria-label="Back to top">↑ Back to top</button>
61
+ <dialog id="comment-dialog" class="comment-dialog" aria-labelledby="comment-dialog-title">
62
+ <form id="comment-form">
63
+ <h2 id="comment-dialog-title">Add comment</h2>
64
+ <div id="comment-dialog-context" class="comment-dialog-context"></div>
65
+ <label for="comment-author-input">Display name</label><input id="comment-author-input" maxlength="80" autocomplete="nickname" value="Visitor" required>
66
+ <label for="comment-body-input">Comment</label><textarea id="comment-body-input" maxlength="10000" required placeholder="Write your comment…"></textarea>
67
+ <p id="comment-save-note" class="comment-dialog-note">Demo only. Comments disappear on refresh and are not posted to GitHub.</p>
68
+ <div id="comment-save-error" role="alert"></div>
69
+ <div class="comment-dialog-actions"><button id="comment-cancel" type="button">Cancel</button><button id="comment-submit" type="submit">Add comment</button></div>
70
+ </form>
71
+ </dialog>
72
  <script src="/patch-view/script.js"></script>
73
  <script>
74
  (() => {
 
105
  document.getElementById('collapse').textContent = 'Collapse all';
106
  } catch (e) { message.className = 'error'; message.textContent = e.message; }
107
  }
108
+ let comments = [], demoComments = [], filter = 'review';
109
+ let nextDemoId = -1, composerTarget = null;
110
+ const dialog = document.getElementById('comment-dialog');
111
+ const authorInput = document.getElementById('comment-author-input');
112
+ const bodyInput = document.getElementById('comment-body-input');
113
+ function openComposer(target = {}) {
114
+ composerTarget = target;
115
+ const reply = target.reply;
116
+ document.getElementById('comment-dialog-title').textContent = reply ? 'Reply to ' + reply.author : target.path ? 'Add inline comment' : 'Add comment';
117
+ document.getElementById('comment-dialog-context').textContent = reply ? (reply.path ? reply.path + ':' + (reply.line || reply.original_line || '') + ' · ' : '') + reply.body.slice(0, 220) : target.path ? target.path + ':' + target.line + (target.side === 'LEFT' ? ' · Old version' : ' · New version') : 'Discussion on PR #' + pr;
118
+ document.getElementById('comment-submit').textContent = reply ? 'Add reply' : 'Add comment';
119
+ bodyInput.value = '';
120
+ document.getElementById('comment-save-error').textContent = '';
121
+ dialog.showModal(); bodyInput.focus();
122
+ }
123
+ document.getElementById('add-comment').onclick = () => openComposer();
124
+ document.getElementById('patch-view').addEventListener('patch-comment', e => openComposer(e.detail));
125
+ document.getElementById('comment-cancel').onclick = () => dialog.close();
126
+ document.getElementById('comment-form').onsubmit = e => {
127
+ e.preventDefault();
128
+ const author = authorInput.value.trim(), body = bodyInput.value.trim();
129
+ if (!author || !body) { document.getElementById('comment-save-error').textContent = 'Enter a name and comment.'; return; }
130
+ const target = composerTarget || {}, parent = target.reply;
131
+ const comment = {id:nextDemoId--, author, body, created_at:new Date().toISOString(), kind:parent ? parent.kind : target.path ? 'inline' : 'discussion', prototype:true, is_maintainer:false, is_bot:false};
132
+ if (parent) {
133
+ comment.reply_to = parent.id;
134
+ comment.thread_id = parent.thread_id || (parent.kind === 'inline' ? String(parent.id) : parent.kind + ':' + parent.id);
135
+ for (const key of ['path','line','start_line','original_line','original_start_line','side','diff_hunk','outdated','resolved']) if (key in parent) comment[key] = parent[key];
136
+ } else if (target.path) {
137
+ Object.assign(comment, target, {thread_id:'demo:' + comment.id});
138
+ }
139
+ demoComments.push(comment); comments.push(comment);
140
+ filter = comment.kind;
141
+ document.querySelectorAll('[data-filter]').forEach(b => b.setAttribute('aria-pressed', String(b.dataset.filter === filter)));
142
+ maintainersOnly.checked = false;
143
+ if (comment.resolved) hideResolved.checked = false;
144
+ if (body.toLowerCase().startsWith('run-slow:') || body.toLowerCase().startsWith('@askserge')) hideCommands.checked = false;
145
+ if (author.toLowerCase().endsWith('[bot]') || author.toLowerCase() === 'huggingfacedocbuilderdev') hideBots.checked = false;
146
+ dialog.close(); renderComments();
147
+ const card = commentList.querySelector('[data-comment-id="' + comment.id + '"]');
148
+ if (card) card.scrollIntoView({block:'nearest'});
149
+ };
150
  const commentStatus = document.getElementById('comments-status'), commentList = document.getElementById('comments-list');
151
  function node(tag, cls, text) {
152
  const el = document.createElement(tag); el.className = cls; el.textContent = text; return el;
 
169
  while (root.kind === 'inline' && byId.has(root.reply_to) && !seen.has(root.id)) {
170
  seen.add(root.id); root = byId.get(root.reply_to);
171
  }
172
+ const key = c.kind === 'inline' ? 'inline:' + (c.thread_id || root.thread_id || root.reply_to || root.id) : c.thread_id || c.kind + ':' + c.id;
173
  if (!groups.has(key)) groups.set(key, []);
174
  groups.get(key).push(c);
175
  }
 
210
  if (options.context) card.append(node('div', 'comment-context-label', 'Included for thread context'));
211
  if (c.path) card.append(node('div', 'comment-location', c.path + (c.line ? ':' + (c.start_line && c.start_line !== c.line ? c.start_line + '–' : '') + c.line : '') + (c.outdated ? ' · Outdated' : '') + (c.resolved === true ? ' · Resolved' : '')));
212
  card.append(node('div', 'comment-body', c.body));
213
+ if (c.prototype) card.append(node('div', 'comment-context-label', 'Demo comment · disappears on refresh'));
214
+ const actions = node('div', 'comment-actions', '');
215
+ const reply = node('button', 'comment-link', 'Reply');
216
+ reply.type = 'button'; reply.onclick = () => openComposer({reply:c});
217
+ actions.append(reply); card.append(actions);
218
  if (c.path && c.line) {
219
  const show = node('button', 'comment-link', 'Show the line(s)');
220
  const status = node('div', 'comment-navigation-status', ''); status.setAttribute('role','status');
 
235
  }
236
  status.textContent = TCIPatch.showLines(container, c) ? (adjusted ? 'File filters adjusted to show this comment.' : '') : 'These lines are not available in the saved diff context.';
237
  };
238
+ actions.append(show); card.append(status);
239
  }
240
  return card;
241
  }
 
248
  try {
249
  const r = await fetch('/patch-view/comments?' + new URLSearchParams({pr, repository}), {cache:'no-store'});
250
  if (!r.ok) throw Error(await r.text());
251
+ const data = await r.json(); comments = [...data.comments, ...demoComments].sort((a, b) => (b.created_at || '').localeCompare(a.created_at || '') || b.id - a.id);
252
  hideResolved.disabled = !data.resolution_available;
253
  hideResolved.title = data.resolution_available ? '' : 'GitHub review-thread status is unavailable.';
254
  if (hideResolved.disabled) hideResolved.checked = false;
patch_view.js CHANGED
@@ -51,11 +51,29 @@
51
  return (file.name.slice(dir.length) === '__init__.py' || /^(configuration|modeling|processing|image_processing(?:_pil|_fast)?|video_processing|tokenization(?:_fast)?|feature_extraction)_/.test(file.name.slice(dir.length))) && files.some(f => f.name.startsWith(dir + 'modular_') && !f.name.slice(dir.length).includes('/'));
52
  }
53
 
54
- function createRow(row) {
 
 
 
 
 
 
 
55
  const tr = el('tr', `patch-line ${row.kind}`);
56
  if (row.old != null) tr.dataset.oldLine = row.old;
57
  if (row.next != null) tr.dataset.newLine = row.next;
58
- tr.append(el('td', 'patch-number', row.old), el('td', 'patch-number', row.next), el('td', 'patch-code', row.text));
 
 
 
 
 
 
 
 
 
 
 
59
  return tr;
60
  }
61
 
@@ -121,7 +139,7 @@
121
  for (const row of file.rows) {
122
  if (headerRows.has(row)) continue;
123
  if (options.hideComments && isCommentLine(file, row)) continue;
124
- body.append(createRow(row));
125
  }
126
  table.append(body); scroll.append(table); details.append(summary, scroll); wrapper.append(details);
127
  }
 
51
  return (file.name.slice(dir.length) === '__init__.py' || /^(configuration|modeling|processing|image_processing(?:_pil|_fast)?|video_processing|tokenization(?:_fast)?|feature_extraction)_/.test(file.name.slice(dir.length))) && files.some(f => f.name.startsWith(dir + 'modular_') && !f.name.slice(dir.length).includes('/'));
52
  }
53
 
54
+ function lineContext(file, row) {
55
+ const index = file.rows.indexOf(row);
56
+ const nearby = file.rows.slice(Math.max(0, index - 6), index + 7).filter(r => ['add', 'del', 'context'].includes(r.kind));
57
+ const old = nearby.filter(r => r.old != null), next = nearby.filter(r => r.next != null);
58
+ return `@@ -${old[0]?.old || 0},${old.length} +${next[0]?.next || 0},${next.length} @@\n` + nearby.map(r => r.text).join('\n');
59
+ }
60
+
61
+ function createRow(row, file = null) {
62
  const tr = el('tr', `patch-line ${row.kind}`);
63
  if (row.old != null) tr.dataset.oldLine = row.old;
64
  if (row.next != null) tr.dataset.newLine = row.next;
65
+ for (const [line, side] of [[row.old, 'LEFT'], [row.next, 'RIGHT']]) {
66
+ const cell = el('td', 'patch-number');
67
+ if (line != null && file) {
68
+ const button = el('button', 'line-comment-button', line);
69
+ button.type = 'button'; button.setAttribute('aria-label', `Add comment on ${side === 'LEFT' ? 'old' : 'new'} line ${line}`);
70
+ button.title = 'Add inline comment';
71
+ button.onclick = () => button.dispatchEvent(new CustomEvent('patch-comment', {bubbles:true, detail:{path:file.name, line, side, diff_hunk:lineContext(file, row)}}));
72
+ cell.append(button);
73
+ } else if (line != null) cell.textContent = line;
74
+ tr.append(cell);
75
+ }
76
+ tr.append(el('td', 'patch-code', row.text));
77
  return tr;
78
  }
79
 
 
139
  for (const row of file.rows) {
140
  if (headerRows.has(row)) continue;
141
  if (options.hideComments && isCommentLine(file, row)) continue;
142
+ body.append(createRow(row, file));
143
  }
144
  table.append(body); scroll.append(table); details.append(summary, scroll); wrapper.append(details);
145
  }