| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 1 | // sketch-diff-range-picker.ts |
| 2 | // Component for selecting commit range for diffs |
| 3 | |
| 4 | import { css, html, LitElement } from "lit"; |
| 5 | import { customElement, property, state } from "lit/decorators.js"; |
| philip.zeyliger | 26bc659 | 2025-06-30 20:15:30 -0700 | [diff] [blame] | 6 | import { GitDataService } from "./git-data-service"; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 7 | import { GitLogEntry } from "../types"; |
| 8 | |
| 9 | /** |
| 10 | * Range type for diff views |
| 11 | */ |
| David Crawshaw | 216d2fc | 2025-06-15 18:45:53 +0000 | [diff] [blame] | 12 | export type DiffRange = { type: "range"; from: string; to: string }; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 13 | |
| 14 | /** |
| 15 | * Component for selecting commit range for diffs |
| 16 | */ |
| 17 | @customElement("sketch-diff-range-picker") |
| 18 | export class SketchDiffRangePicker extends LitElement { |
| 19 | @property({ type: Array }) |
| 20 | commits: GitLogEntry[] = []; |
| 21 | |
| 22 | @state() |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 23 | private fromCommit: string = ""; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 24 | |
| 25 | @state() |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 26 | private toCommit: string = ""; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 27 | |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 28 | @state() |
| 29 | private dropdownOpen: boolean = false; |
| 30 | |
| Philip Zeyliger | 38499cc | 2025-06-15 21:17:05 -0700 | [diff] [blame] | 31 | // Removed commitsExpanded state - always expanded now |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 32 | |
| 33 | @state() |
| 34 | private loading: boolean = true; |
| 35 | |
| 36 | @state() |
| 37 | private error: string | null = null; |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 38 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 39 | @property({ attribute: false, type: Object }) |
| 40 | gitService!: GitDataService; |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 41 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 42 | constructor() { |
| 43 | super(); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 44 | console.log("SketchDiffRangePicker initialized"); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static styles = css` |
| 48 | :host { |
| 49 | display: block; |
| 50 | width: 100%; |
| 51 | font-family: var(--font-family, system-ui, sans-serif); |
| 52 | color: var(--text-color, #333); |
| 53 | } |
| 54 | |
| 55 | .range-picker { |
| 56 | display: flex; |
| David Crawshaw | 216d2fc | 2025-06-15 18:45:53 +0000 | [diff] [blame] | 57 | flex-direction: column; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 58 | gap: 12px; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 59 | width: 100%; |
| 60 | box-sizing: border-box; |
| 61 | } |
| 62 | |
| Philip Zeyliger | 38499cc | 2025-06-15 21:17:05 -0700 | [diff] [blame] | 63 | /* Removed commits-header and commits-label styles - no longer needed */ |
| David Crawshaw | 216d2fc | 2025-06-15 18:45:53 +0000 | [diff] [blame] | 64 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 65 | .commit-selectors { |
| 66 | display: flex; |
| 67 | flex-direction: row; |
| 68 | align-items: center; |
| 69 | gap: 12px; |
| 70 | flex: 1; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | .commit-selector { |
| 74 | display: flex; |
| 75 | align-items: center; |
| 76 | gap: 8px; |
| 77 | flex: 1; |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 78 | position: relative; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 81 | /* Custom dropdown styles */ |
| 82 | .custom-select { |
| 83 | position: relative; |
| 84 | width: 100%; |
| 85 | min-width: 300px; |
| 86 | } |
| 87 | |
| 88 | .select-button { |
| 89 | width: 100%; |
| 90 | padding: 8px 32px 8px 12px; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 91 | border: 1px solid var(--border-color, #e0e0e0); |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 92 | border-radius: 4px; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 93 | background-color: var(--background, #fff); |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 94 | cursor: pointer; |
| 95 | text-align: left; |
| 96 | display: flex; |
| 97 | align-items: center; |
| 98 | gap: 8px; |
| 99 | min-height: 36px; |
| 100 | font-family: inherit; |
| 101 | font-size: 14px; |
| 102 | position: relative; |
| 103 | } |
| 104 | |
| 105 | .select-button:hover { |
| 106 | border-color: var(--border-hover, #ccc); |
| 107 | } |
| 108 | |
| 109 | .select-button:focus { |
| 110 | outline: none; |
| 111 | border-color: var(--accent-color, #007acc); |
| 112 | box-shadow: 0 0 0 2px var(--accent-color-light, rgba(0, 122, 204, 0.2)); |
| 113 | } |
| 114 | |
| 115 | .select-button.default-commit { |
| 116 | border-color: var(--accent-color, #007acc); |
| 117 | background-color: var(--accent-color-light, rgba(0, 122, 204, 0.05)); |
| 118 | } |
| 119 | |
| 120 | .dropdown-arrow { |
| 121 | position: absolute; |
| 122 | right: 10px; |
| 123 | top: 50%; |
| 124 | transform: translateY(-50%); |
| 125 | transition: transform 0.2s; |
| 126 | } |
| 127 | |
| 128 | .dropdown-arrow.open { |
| 129 | transform: translateY(-50%) rotate(180deg); |
| 130 | } |
| 131 | |
| 132 | .dropdown-content { |
| 133 | position: absolute; |
| 134 | top: 100%; |
| 135 | left: 0; |
| 136 | right: 0; |
| 137 | background-color: var(--background, #fff); |
| 138 | border: 1px solid var(--border-color, #e0e0e0); |
| 139 | border-radius: 4px; |
| 140 | box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); |
| 141 | z-index: 1000; |
| 142 | max-height: 300px; |
| 143 | overflow-y: auto; |
| 144 | margin-top: 2px; |
| 145 | } |
| 146 | |
| 147 | .dropdown-option { |
| 148 | padding: 10px 12px; |
| 149 | cursor: pointer; |
| 150 | border-bottom: 1px solid var(--border-light, #f0f0f0); |
| 151 | display: flex; |
| 152 | align-items: flex-start; |
| 153 | gap: 8px; |
| 154 | font-size: 14px; |
| 155 | line-height: 1.4; |
| 156 | min-height: auto; |
| 157 | } |
| 158 | |
| 159 | .dropdown-option:last-child { |
| 160 | border-bottom: none; |
| 161 | } |
| 162 | |
| 163 | .dropdown-option:hover { |
| 164 | background-color: var(--background-hover, #f5f5f5); |
| 165 | } |
| 166 | |
| 167 | .dropdown-option.selected { |
| 168 | background-color: var(--accent-color-light, rgba(0, 122, 204, 0.1)); |
| 169 | } |
| 170 | |
| 171 | .dropdown-option.default-commit { |
| 172 | background-color: var(--accent-color-light, rgba(0, 122, 204, 0.05)); |
| 173 | border-left: 3px solid var(--accent-color, #007acc); |
| 174 | padding-left: 9px; |
| 175 | } |
| 176 | |
| 177 | .commit-hash { |
| 178 | font-family: monospace; |
| 179 | color: var(--text-secondary, #666); |
| 180 | font-size: 13px; |
| 181 | } |
| 182 | |
| 183 | .commit-subject { |
| 184 | color: var(--text-primary, #333); |
| 185 | flex: 1; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 186 | overflow: hidden; |
| 187 | text-overflow: ellipsis; |
| 188 | white-space: nowrap; |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 189 | min-width: 200px; /* Ensure commit message gets priority */ |
| 190 | } |
| 191 | |
| 192 | .commit-refs { |
| 193 | display: flex; |
| 194 | gap: 4px; |
| 195 | flex-wrap: wrap; |
| 196 | } |
| 197 | |
| 198 | .commit-refs-container { |
| 199 | display: flex; |
| 200 | gap: 4px; |
| 201 | flex-wrap: wrap; |
| 202 | flex-shrink: 0; |
| 203 | } |
| 204 | |
| 205 | .commit-ref { |
| 206 | background-color: var(--tag-bg, #e1f5fe); |
| 207 | color: var(--tag-text, #0277bd); |
| 208 | padding: 2px 6px; |
| 209 | border-radius: 12px; |
| 210 | font-size: 11px; |
| 211 | font-weight: 500; |
| 212 | } |
| 213 | |
| 214 | .commit-ref.branch { |
| 215 | background-color: var(--branch-bg, #e8f5e8); |
| 216 | color: var(--branch-text, #2e7d32); |
| 217 | } |
| 218 | |
| 219 | .commit-ref.tag { |
| 220 | background-color: var(--tag-bg, #fff3e0); |
| 221 | color: var(--tag-text, #f57c00); |
| 222 | } |
| 223 | |
| 224 | .commit-ref.sketch-base { |
| 225 | background-color: var(--accent-color, #007acc); |
| 226 | color: white; |
| 227 | font-weight: 600; |
| 228 | } |
| 229 | |
| 230 | .truncated-refs { |
| 231 | position: relative; |
| 232 | cursor: help; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | label { |
| 236 | font-weight: 500; |
| 237 | font-size: 14px; |
| 238 | } |
| 239 | |
| 240 | .loading { |
| 241 | font-style: italic; |
| 242 | color: var(--text-muted, #666); |
| 243 | } |
| 244 | |
| 245 | .error { |
| 246 | color: var(--error-color, #dc3545); |
| 247 | font-size: 14px; |
| 248 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 249 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 250 | @media (max-width: 768px) { |
| 251 | .commit-selector { |
| 252 | max-width: 100%; |
| 253 | } |
| 254 | } |
| 255 | `; |
| 256 | |
| 257 | connectedCallback() { |
| 258 | super.connectedCallback(); |
| 259 | // Wait for DOM to be fully loaded to ensure proper initialization order |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 260 | if (document.readyState === "complete") { |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 261 | this.loadCommits(); |
| 262 | } else { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 263 | window.addEventListener("load", () => { |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 264 | setTimeout(() => this.loadCommits(), 0); // Give time for provider initialization |
| 265 | }); |
| 266 | } |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 267 | |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 268 | // Listen for popstate events to handle browser back/forward navigation |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 269 | window.addEventListener("popstate", this.handlePopState.bind(this)); |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | disconnectedCallback() { |
| 273 | super.disconnectedCallback(); |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 274 | window.removeEventListener("popstate", this.handlePopState.bind(this)); |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Handle browser back/forward navigation |
| 279 | */ |
| 280 | private handlePopState() { |
| 281 | // Re-initialize from URL parameters when user navigates |
| 282 | if (this.commits.length > 0) { |
| 283 | const initializedFromUrl = this.initializeFromUrlParams(); |
| 284 | if (initializedFromUrl) { |
| 285 | // Force re-render and dispatch event |
| 286 | this.requestUpdate(); |
| 287 | this.dispatchRangeEvent(); |
| 288 | } |
| 289 | } |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | render() { |
| 293 | return html` |
| 294 | <div class="range-picker"> |
| 295 | ${this.loading |
| 296 | ? html`<div class="loading">Loading commits...</div>` |
| 297 | : this.error |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 298 | ? html`<div class="error">${this.error}</div>` |
| 299 | : this.renderRangePicker()} |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 300 | </div> |
| 301 | `; |
| 302 | } |
| 303 | |
| 304 | renderRangePicker() { |
| 305 | return html` |
| Philip Zeyliger | 38499cc | 2025-06-15 21:17:05 -0700 | [diff] [blame] | 306 | <div class="commit-selectors">${this.renderRangeSelectors()}</div> |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 307 | `; |
| 308 | } |
| 309 | |
| 310 | renderRangeSelectors() { |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 311 | // Always diff against uncommitted changes |
| 312 | this.toCommit = ""; |
| 313 | |
| 314 | const selectedCommit = this.commits.find((c) => c.hash === this.fromCommit); |
| 315 | const isDefaultCommit = |
| 316 | selectedCommit && this.isSketchBaseCommit(selectedCommit); |
| 317 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 318 | return html` |
| 319 | <div class="commit-selector"> |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 320 | <label for="fromCommit">Diff from:</label> |
| 321 | <div class="custom-select" @click=${this.toggleDropdown}> |
| 322 | <button |
| 323 | class="select-button ${isDefaultCommit ? "default-commit" : ""}" |
| 324 | @click=${this.toggleDropdown} |
| 325 | @blur=${this.handleBlur} |
| 326 | > |
| 327 | ${selectedCommit |
| 328 | ? this.renderCommitButton(selectedCommit) |
| 329 | : "Select commit..."} |
| 330 | <svg |
| 331 | class="dropdown-arrow ${this.dropdownOpen ? "open" : ""}" |
| 332 | width="12" |
| 333 | height="12" |
| 334 | viewBox="0 0 12 12" |
| 335 | > |
| 336 | <path d="M6 8l-4-4h8z" fill="currentColor" /> |
| 337 | </svg> |
| 338 | </button> |
| 339 | ${this.dropdownOpen |
| 340 | ? html` |
| 341 | <div class="dropdown-content"> |
| 342 | ${this.commits.map( |
| 343 | (commit) => html` |
| 344 | <div |
| 345 | class="dropdown-option ${commit.hash === this.fromCommit |
| 346 | ? "selected" |
| 347 | : ""} ${this.isSketchBaseCommit(commit) |
| 348 | ? "default-commit" |
| 349 | : ""}" |
| 350 | @click=${() => this.selectCommit(commit.hash)} |
| 351 | > |
| 352 | ${this.renderCommitOption(commit)} |
| 353 | </div> |
| 354 | `, |
| 355 | )} |
| 356 | </div> |
| 357 | ` |
| 358 | : ""} |
| 359 | </div> |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 360 | </div> |
| 361 | `; |
| 362 | } |
| 363 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 364 | /** |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 365 | * Format a commit for display in the dropdown (legacy method, kept for compatibility) |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 366 | */ |
| 367 | formatCommitOption(commit: GitLogEntry): string { |
| 368 | const shortHash = commit.hash.substring(0, 7); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 369 | |
| David Crawshaw | dbca897 | 2025-06-14 23:46:58 +0000 | [diff] [blame] | 370 | // Truncate subject if it's too long |
| 371 | let subject = commit.subject; |
| 372 | if (subject.length > 50) { |
| 373 | subject = subject.substring(0, 47) + "..."; |
| 374 | } |
| 375 | |
| 376 | let label = `${shortHash} ${subject}`; |
| 377 | |
| 378 | // Add refs but keep them concise |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 379 | if (commit.refs && commit.refs.length > 0) { |
| David Crawshaw | dbca897 | 2025-06-14 23:46:58 +0000 | [diff] [blame] | 380 | const refs = commit.refs.map((ref) => { |
| 381 | // Shorten common prefixes |
| 382 | if (ref.startsWith("origin/")) { |
| 383 | return ref.substring(7); |
| 384 | } |
| 385 | if (ref.startsWith("refs/heads/")) { |
| 386 | return ref.substring(11); |
| 387 | } |
| 388 | if (ref.startsWith("refs/remotes/origin/")) { |
| 389 | return ref.substring(20); |
| 390 | } |
| 391 | return ref; |
| 392 | }); |
| 393 | |
| 394 | // Limit to first 2 refs to avoid overcrowding |
| 395 | const displayRefs = refs.slice(0, 2); |
| 396 | if (refs.length > 2) { |
| 397 | displayRefs.push(`+${refs.length - 2} more`); |
| 398 | } |
| 399 | |
| 400 | label += ` (${displayRefs.join(", ")})`; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 401 | } |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 402 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 403 | return label; |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * Load commits from the Git data service |
| 408 | */ |
| 409 | async loadCommits() { |
| 410 | this.loading = true; |
| 411 | this.error = null; |
| 412 | |
| 413 | if (!this.gitService) { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 414 | console.error("GitService was not provided to sketch-diff-range-picker"); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 415 | throw Error(); |
| 416 | } |
| 417 | |
| 418 | try { |
| 419 | // Get the base commit reference |
| 420 | const baseCommitRef = await this.gitService.getBaseCommitRef(); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 421 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 422 | // Load commit history |
| 423 | this.commits = await this.gitService.getCommitHistory(baseCommitRef); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 424 | |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 425 | // Check if we should initialize from URL parameters first |
| 426 | const initializedFromUrl = this.initializeFromUrlParams(); |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 427 | |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 428 | // Set default selections only if not initialized from URL |
| 429 | if (this.commits.length > 0 && !initializedFromUrl) { |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 430 | // For range, default is base to HEAD |
| 431 | // TODO: is sketch-base right in the unsafe context, where it's sketch-base-... |
| 432 | // should this be startswith? |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 433 | const baseCommit = this.commits.find( |
| 434 | (c) => c.refs && c.refs.some((ref) => ref.includes("sketch-base")), |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 435 | ); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 436 | |
| 437 | this.fromCommit = baseCommit |
| 438 | ? baseCommit.hash |
| 439 | : this.commits[this.commits.length - 1].hash; |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 440 | // Default to Uncommitted Changes by setting toCommit to empty string |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 441 | this.toCommit = ""; // Empty string represents uncommitted changes |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 442 | } |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 443 | |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 444 | // Always dispatch range event to ensure diff view is updated |
| 445 | this.dispatchRangeEvent(); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 446 | } catch (error) { |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 447 | console.error("Error loading commits:", error); |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 448 | this.error = `Error loading commits: ${error.message}`; |
| 449 | } finally { |
| 450 | this.loading = false; |
| 451 | } |
| 452 | } |
| 453 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 454 | /** |
| 455 | * Handle From commit change |
| 456 | */ |
| 457 | handleFromChange(event: Event) { |
| 458 | const select = event.target as HTMLSelectElement; |
| 459 | this.fromCommit = select.value; |
| 460 | this.dispatchRangeEvent(); |
| 461 | } |
| 462 | |
| 463 | /** |
| 464 | * Handle To commit change |
| 465 | */ |
| 466 | handleToChange(event: Event) { |
| 467 | const select = event.target as HTMLSelectElement; |
| 468 | this.toCommit = select.value; |
| 469 | this.dispatchRangeEvent(); |
| 470 | } |
| 471 | |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 472 | /** |
| 473 | * Toggle dropdown open/closed |
| 474 | */ |
| 475 | toggleDropdown(event: Event) { |
| 476 | event.stopPropagation(); |
| 477 | this.dropdownOpen = !this.dropdownOpen; |
| 478 | |
| 479 | if (this.dropdownOpen) { |
| 480 | // Close dropdown when clicking outside |
| 481 | setTimeout(() => { |
| 482 | document.addEventListener("click", this.closeDropdown, { once: true }); |
| 483 | }, 0); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | /** |
| 488 | * Close dropdown |
| 489 | */ |
| 490 | closeDropdown = () => { |
| 491 | this.dropdownOpen = false; |
| 492 | }; |
| 493 | |
| 494 | /** |
| 495 | * Handle blur event on select button |
| 496 | */ |
| philip.zeyliger | 26bc659 | 2025-06-30 20:15:30 -0700 | [diff] [blame] | 497 | handleBlur(_event: FocusEvent) { |
| Philip Zeyliger | 364b35a | 2025-06-21 16:39:04 -0700 | [diff] [blame] | 498 | // Small delay to allow click events to process |
| 499 | setTimeout(() => { |
| 500 | if (!this.shadowRoot?.activeElement?.closest(".custom-select")) { |
| 501 | this.dropdownOpen = false; |
| 502 | } |
| 503 | }, 150); |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * Select a commit from dropdown |
| 508 | */ |
| 509 | selectCommit(hash: string) { |
| 510 | this.fromCommit = hash; |
| 511 | this.dropdownOpen = false; |
| 512 | this.dispatchRangeEvent(); |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * Check if a commit is a sketch-base commit |
| 517 | */ |
| 518 | isSketchBaseCommit(commit: GitLogEntry): boolean { |
| 519 | return commit.refs?.some((ref) => ref.includes("sketch-base")) || false; |
| 520 | } |
| 521 | |
| 522 | /** |
| 523 | * Render commit for the dropdown button |
| 524 | */ |
| 525 | renderCommitButton(commit: GitLogEntry) { |
| 526 | const shortHash = commit.hash.substring(0, 7); |
| 527 | let subject = commit.subject; |
| 528 | if (subject.length > 40) { |
| 529 | subject = subject.substring(0, 37) + "..."; |
| 530 | } |
| 531 | |
| 532 | return html` |
| 533 | <span class="commit-hash">${shortHash}</span> |
| 534 | <span class="commit-subject">${subject}</span> |
| 535 | ${this.isSketchBaseCommit(commit) |
| 536 | ? html` <span class="commit-ref sketch-base">base</span> ` |
| 537 | : ""} |
| 538 | `; |
| 539 | } |
| 540 | |
| 541 | /** |
| 542 | * Render commit option in dropdown |
| 543 | */ |
| 544 | renderCommitOption(commit: GitLogEntry) { |
| 545 | const shortHash = commit.hash.substring(0, 7); |
| 546 | let subject = commit.subject; |
| 547 | if (subject.length > 50) { |
| 548 | subject = subject.substring(0, 47) + "..."; |
| 549 | } |
| 550 | |
| 551 | return html` |
| 552 | <span class="commit-hash">${shortHash}</span> |
| 553 | <span class="commit-subject">${subject}</span> |
| 554 | ${commit.refs && commit.refs.length > 0 |
| 555 | ? html` <div class="commit-refs">${this.renderRefs(commit.refs)}</div> ` |
| 556 | : ""} |
| 557 | `; |
| 558 | } |
| 559 | |
| 560 | /** |
| 561 | * Render all refs naturally without truncation |
| 562 | */ |
| 563 | renderRefs(refs: string[]) { |
| 564 | return html` |
| 565 | <div class="commit-refs-container"> |
| 566 | ${refs.map((ref) => { |
| 567 | const shortRef = this.getShortRefName(ref); |
| 568 | const isSketchBase = ref.includes("sketch-base"); |
| 569 | const refClass = isSketchBase |
| 570 | ? "sketch-base" |
| 571 | : ref.includes("tag") |
| 572 | ? "tag" |
| 573 | : "branch"; |
| 574 | return html`<span class="commit-ref ${refClass}">${shortRef}</span>`; |
| 575 | })} |
| 576 | </div> |
| 577 | `; |
| 578 | } |
| 579 | |
| 580 | /** |
| 581 | * Get shortened reference name for compact display |
| 582 | */ |
| 583 | getShortRefName(ref: string): string { |
| 584 | if (ref.startsWith("refs/heads/")) { |
| 585 | return ref.substring(11); |
| 586 | } |
| 587 | if (ref.startsWith("refs/remotes/origin/")) { |
| 588 | return ref.substring(20); |
| 589 | } |
| 590 | if (ref.startsWith("refs/tags/")) { |
| 591 | return ref.substring(10); |
| 592 | } |
| 593 | return ref; |
| 594 | } |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 595 | |
| 596 | /** |
| David Crawshaw | 216d2fc | 2025-06-15 18:45:53 +0000 | [diff] [blame] | 597 | * Get a summary of the current commit range for display |
| Philip Zeyliger | e89b308 | 2025-05-29 03:16:06 +0000 | [diff] [blame] | 598 | */ |
| David Crawshaw | 216d2fc | 2025-06-15 18:45:53 +0000 | [diff] [blame] | 599 | getCommitSummary(): string { |
| 600 | if (!this.fromCommit && !this.toCommit) { |
| Autoformatter | 6255411 | 2025-06-15 19:23:33 +0000 | [diff] [blame] | 601 | return "No commits selected"; |
| David Crawshaw | 216d2fc | 2025-06-15 18:45:53 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| Autoformatter | 6255411 | 2025-06-15 19:23:33 +0000 | [diff] [blame] | 604 | const fromShort = this.fromCommit ? this.fromCommit.substring(0, 7) : ""; |
| 605 | const toShort = this.toCommit |
| 606 | ? this.toCommit.substring(0, 7) |
| 607 | : "Uncommitted"; |
| 608 | |
| David Crawshaw | 216d2fc | 2025-06-15 18:45:53 +0000 | [diff] [blame] | 609 | return `${fromShort}..${toShort}`; |
| Philip Zeyliger | e89b308 | 2025-05-29 03:16:06 +0000 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | /** |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 613 | * Validate that a commit hash exists in the loaded commits |
| 614 | */ |
| 615 | private isValidCommitHash(hash: string): boolean { |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 616 | if (!hash || hash.trim() === "") return true; // Empty is valid (uncommitted changes) |
| 617 | return this.commits.some( |
| 618 | (commit) => commit.hash.startsWith(hash) || commit.hash === hash, |
| 619 | ); |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | /** |
| 623 | * Dispatch range change event and update URL parameters |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 624 | */ |
| 625 | dispatchRangeEvent() { |
| Autoformatter | 6255411 | 2025-06-15 19:23:33 +0000 | [diff] [blame] | 626 | const range: DiffRange = { |
| 627 | type: "range", |
| 628 | from: this.fromCommit, |
| 629 | to: this.toCommit, |
| 630 | }; |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 631 | |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 632 | // Update URL parameters |
| 633 | this.updateUrlParams(range); |
| 634 | |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 635 | const event = new CustomEvent("range-change", { |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 636 | detail: { range }, |
| 637 | bubbles: true, |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 638 | composed: true, |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 639 | }); |
| Autoformatter | 8c46362 | 2025-05-16 21:54:17 +0000 | [diff] [blame] | 640 | |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 641 | this.dispatchEvent(event); |
| 642 | } |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 643 | |
| 644 | /** |
| 645 | * Update URL parameters for from and to commits |
| 646 | */ |
| 647 | private updateUrlParams(range: DiffRange) { |
| 648 | const url = new URL(window.location.href); |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 649 | |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 650 | // Remove existing range parameters |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 651 | url.searchParams.delete("from"); |
| 652 | url.searchParams.delete("to"); |
| 653 | url.searchParams.delete("commit"); |
| 654 | |
| David Crawshaw | 216d2fc | 2025-06-15 18:45:53 +0000 | [diff] [blame] | 655 | // Add from parameter if not empty |
| 656 | if (range.from && range.from.trim() !== "") { |
| 657 | url.searchParams.set("from", range.from); |
| 658 | } |
| 659 | // Add to parameter if not empty (empty string means uncommitted changes) |
| 660 | if (range.to && range.to.trim() !== "") { |
| 661 | url.searchParams.set("to", range.to); |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 662 | } |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 663 | |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 664 | // Update the browser history without reloading the page |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 665 | window.history.replaceState(window.history.state, "", url.toString()); |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | /** |
| 669 | * Initialize from URL parameters if available |
| 670 | */ |
| 671 | private initializeFromUrlParams() { |
| 672 | const url = new URL(window.location.href); |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 673 | const fromParam = url.searchParams.get("from"); |
| 674 | const toParam = url.searchParams.get("to"); |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 675 | |
| David Crawshaw | 216d2fc | 2025-06-15 18:45:53 +0000 | [diff] [blame] | 676 | // If from or to parameters are present, use them |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 677 | if (fromParam || toParam) { |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 678 | if (fromParam) { |
| 679 | this.fromCommit = fromParam; |
| 680 | } |
| 681 | if (toParam) { |
| 682 | this.toCommit = toParam; |
| 683 | } else { |
| 684 | // If no 'to' param, default to uncommitted changes (empty string) |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 685 | this.toCommit = ""; |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 686 | } |
| 687 | return true; // Indicate that we initialized from URL |
| 688 | } |
| Autoformatter | 9abf803 | 2025-06-14 23:24:08 +0000 | [diff] [blame] | 689 | |
| David Crawshaw | 938d2dc | 2025-06-14 22:17:33 +0000 | [diff] [blame] | 690 | return false; // No URL params found |
| 691 | } |
| Philip Zeyliger | 272a90e | 2025-05-16 14:49:51 -0700 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | declare global { |
| 695 | interface HTMLElementTagNameMap { |
| 696 | "sketch-diff-range-picker": SketchDiffRangePicker; |
| 697 | } |
| 698 | } |