* Create the UI for adding co-authors. * @param {HTMLElement} root * @param {Element | null} newMergeExperienceContainer
(root, newMergeExperienceContainer)
| 397 | * @param {Element | null} newMergeExperienceContainer |
| 398 | */ |
| 399 | function createCoAuthorsUI(root, newMergeExperienceContainer) { |
| 400 | const banner = document.createElement('div') |
| 401 | banner.setAttribute('aria-live', 'polite') |
| 402 | banner.classList.add('color-fg-subtle') |
| 403 | |
| 404 | let bannerTimeout |
| 405 | /** |
| 406 | * Display a temporary status update about the progress adding co-authors. |
| 407 | * @param {string} msg The message to display |
| 408 | * @param {'subtle' | 'success' | 'danger'} color The status color to use. |
| 409 | */ |
| 410 | const displayStatus = (msg, color = 'subtle') => { |
| 411 | clearTimeout(bannerTimeout) |
| 412 | banner.textContent = msg |
| 413 | banner.className = `color-fg-${color}` |
| 414 | bannerTimeout = setTimeout(() => (banner.textContent = ''), 5000) |
| 415 | } |
| 416 | |
| 417 | const button = document.createElement('button') |
| 418 | button.textContent = 'Add co-authors' |
| 419 | button.classList.add('Button', 'Button--secondary', 'Button--small') |
| 420 | button.type = 'button' |
| 421 | button.addEventListener('click', async () => { |
| 422 | displayStatus('Loading co-authors…') |
| 423 | try { |
| 424 | const { message, count } = await getCoAuthors() |
| 425 | /** @type {HTMLTextAreaElement | null} */ |
| 426 | const textArea = newMergeExperienceContainer?.querySelector('textarea') ?? |
| 427 | root.querySelector('textarea#merge_message_field') |
| 428 | if (!textArea) { |
| 429 | throw new Error('Couldn’t find commit message <textarea>') |
| 430 | } |
| 431 | // Append co-authors to textarea content. |
| 432 | textArea.value = (textArea.value + '\n\n' + message).trim() |
| 433 | // Notify event listeners that the <textarea> content changed. |
| 434 | textArea.dispatchEvent(new Event('change', { bubbles: true, cancelable: true })) |
| 435 | |
| 436 | if (count === 0) { |
| 437 | displayStatus('Found no co-authors to add') |
| 438 | } else { |
| 439 | displayStatus(`Added ${count} co-author${count === 1 ? '' : 's'}`, 'success') |
| 440 | } |
| 441 | } catch (error) { |
| 442 | console.error('Error adding co-authors:', error) |
| 443 | displayStatus('Something went wrong.', 'danger') |
| 444 | } |
| 445 | }) |
| 446 | // Build container |
| 447 | const container = document.createElement('div') |
| 448 | container.classList.add('d-flex', 'flex-items-center', 'gap-2') |
| 449 | if (newMergeExperienceContainer) container.classList.add('mt-3') |
| 450 | container.append(button) |
| 451 | container.append(banner) |
| 452 | return container |
| 453 | } |
| 454 | |
| 455 | /** Get participants for the current PR and generate `Co-authored-by` messages for them. */ |
| 456 | async function getCoAuthors() { |
no test coverage detected