| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame^] | 1 | /** |
| 2 | * Utility functions for rendering commit messages in the timeline |
| 3 | */ |
| 4 | |
| 5 | import { escapeHTML } from "./utils"; |
| 6 | |
| 7 | interface Commit { |
| 8 | hash: string; |
| 9 | subject: string; |
| 10 | body: string; |
| 11 | pushed_branch?: string; |
| 12 | } |
| 13 | |
| 14 | /** |
| 15 | * Create HTML elements to display commits in the timeline |
| 16 | * @param commits List of commit information to display |
| 17 | * @param diffViewerCallback Callback function to show commit diff when requested |
| 18 | * @returns The created HTML container element with commit information |
| 19 | */ |
| 20 | export function createCommitsContainer( |
| 21 | commits: Commit[], |
| 22 | diffViewerCallback: (commitHash: string) => void |
| 23 | ): HTMLElement { |
| 24 | const commitsContainer = document.createElement("div"); |
| 25 | commitsContainer.className = "commits-container"; |
| 26 | |
| 27 | // Create a header for commits |
| 28 | const commitsHeaderRow = document.createElement("div"); |
| 29 | commitsHeaderRow.className = "commits-header"; |
| 30 | commitsHeaderRow.textContent = `${commits.length} new commit${commits.length > 1 ? "s" : ""} detected`; |
| 31 | commitsContainer.appendChild(commitsHeaderRow); |
| 32 | |
| 33 | // Create a row for commit boxes |
| 34 | const commitBoxesRow = document.createElement("div"); |
| 35 | commitBoxesRow.className = "commit-boxes-row"; |
| 36 | |
| 37 | // Add each commit as a box |
| 38 | commits.forEach((commit) => { |
| 39 | // Create the commit box |
| 40 | const commitBox = document.createElement("div"); |
| 41 | commitBox.className = "commit-box"; |
| 42 | |
| 43 | // Show commit hash and subject line as the preview |
| 44 | const commitPreview = document.createElement("div"); |
| 45 | commitPreview.className = "commit-preview"; |
| 46 | |
| 47 | // Include pushed branch information if available |
| 48 | let previewHTML = `<span class="commit-hash">${commit.hash.substring(0, 8)}</span> ${escapeHTML(commit.subject)}`; |
| 49 | if (commit.pushed_branch) { |
| 50 | previewHTML += ` <span class="pushed-branch">→ pushed to ${escapeHTML(commit.pushed_branch)}</span>`; |
| 51 | } |
| 52 | |
| 53 | commitPreview.innerHTML = previewHTML; |
| 54 | commitBox.appendChild(commitPreview); |
| 55 | |
| 56 | // Create expandable view for commit details |
| 57 | const expandedView = document.createElement("div"); |
| 58 | expandedView.className = "commit-details is-hidden"; |
| 59 | expandedView.innerHTML = `<pre>${escapeHTML(commit.body)}</pre>`; |
| 60 | commitBox.appendChild(expandedView); |
| 61 | |
| 62 | // Toggle visibility of expanded view when clicking the preview |
| 63 | commitPreview.addEventListener("click", (event) => { |
| 64 | // If holding Ctrl/Cmd key, show diff for this commit |
| 65 | if (event.ctrlKey || event.metaKey) { |
| 66 | // Call the diff viewer callback with the commit hash |
| 67 | diffViewerCallback(commit.hash); |
| 68 | } else { |
| 69 | // Normal behavior - toggle expanded view |
| 70 | expandedView.classList.toggle("is-hidden"); |
| 71 | } |
| 72 | }); |
| 73 | |
| 74 | // Add a diff button to view commit changes |
| 75 | const diffButton = document.createElement("button"); |
| 76 | diffButton.className = "commit-diff-button"; |
| 77 | diffButton.textContent = "View Changes"; |
| 78 | diffButton.addEventListener("click", (event) => { |
| 79 | event.stopPropagation(); // Prevent triggering the parent click event |
| 80 | diffViewerCallback(commit.hash); |
| 81 | }); |
| 82 | // Add the button directly to the commit box |
| 83 | commitBox.appendChild(diffButton); |
| 84 | |
| 85 | commitBoxesRow.appendChild(commitBox); |
| 86 | }); |
| 87 | |
| 88 | commitsContainer.appendChild(commitBoxesRow); |
| 89 | return commitsContainer; |
| 90 | } |